<s> """<STR_LIT>""" <EOL> import base64 <EOL> import httparchive <EOL> import json <EOL> import logging <EOL> import os <EOL> COMMON_URL_PREFIX = '<STR_LIT>' <EOL> COMMAND_URL_PREFIX = COMMON_URL_PREFIX + '<STR_LIT>' <EOL> GENERATOR_URL_PREFIX = COMMON_URL_PREFIX + '<STR_LIT>' <EOL> POST_IMAGE_URL_PREFIX = COMMON_URL_PREFIX + '<STR_LIT>' <EOL> IMAGE_DATA_PREFIX = '<STR_LIT>' <EOL> def SimpleResponse ( status ) : <EOL> """<STR_LIT>""" <EOL> return httparchive . create_response ( status ) <EOL> def JsonResponse ( data ) : <EOL> """<STR_LIT>""" <EOL> status = <NUM_LIT:200> <EOL> reason = '<STR_LIT:OK>' <EOL> headers = [ ( '<STR_LIT>' , '<STR_LIT:application/json>' ) ] <EOL> body = json . dumps ( data ) <EOL> return httparchive . create_response ( status , reason , headers , body ) <EOL> class CustomHandlers ( object ) : <EOL> def __init__ ( self , options , http_archive ) : <EOL> """<STR_LIT>""" <EOL> self . server_manager = None <EOL> self . options = options <EOL> self . http_archive = http_archive <EOL> self . handlers = [ <EOL> ( GENERATOR_URL_PREFIX , self . get_generator_url_response_code ) ] <EOL> if options . screenshot_dir : <EOL> if not os . path . exists ( options . screenshot_dir ) : <EOL> try : <EOL> os . makedirs ( options . screenshot_dir ) <EOL> except IOError : <EOL> logging . error ( '<STR_LIT>' , <EOL> options . screenshot_dir ) <EOL> options . screenshot_dir = None <EOL> if options . screenshot_dir : <EOL> self . screenshot_dir = options . screenshot_dir <EOL> self . handlers . append ( <EOL> ( POST_IMAGE_URL_PREFIX , self . handle_possible_post_image ) ) <EOL> def handle ( self , request ) : <EOL> """<STR_LIT>""" <EOL> for prefix , handler in self . handlers : <EOL> if request . full_path . startswith ( prefix ) : <EOL> return handler ( request , request . full_path [ len ( prefix ) : ] ) <EOL> return None <EOL> def get_generator_url_response_code ( self , request , url_suffix ) : <EOL> """<STR_LIT>""" <EOL> del request <EOL> try : <EOL> response_code = int ( url_suffix ) <EOL> return SimpleResponse ( response_code ) <EOL> except ValueError : <EOL> return None <EOL> def handle_possible_post_image ( self , request , url_suffix ) : <EOL> """<STR_LIT>""" <EOL> basename = url_suffix <EOL> if not basename : <EOL> return None <EOL> data = request . request_body <EOL> if not data . startswith ( IMAGE_DATA_PREFIX ) : <EOL> logging . error ( '<STR_LIT>' , basename ) <EOL> return SimpleResponse ( <NUM_LIT> ) <EOL> data = data [ len ( IMAGE_DATA_PREFIX ) : ] <EOL> png = base64 . b64decode ( data ) <EOL> filename = os . path . join ( self . screenshot_dir , <EOL> '<STR_LIT>' % ( request . host , basename ) ) <EOL> if not os . access ( self . screenshot_dir , os . W_OK ) : <EOL> logging . error ( '<STR_LIT>' , filename ) <EOL> return SimpleResponse ( <NUM_LIT> ) <EOL> with file ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( png ) <EOL> return SimpleResponse ( <NUM_LIT:200> ) <EOL> def add_server_manager_handler ( self , server_manager ) : <EOL> """<STR_LIT>""" <EOL> self . server_manager = server_manager <EOL> self . handlers . append ( <EOL> ( COMMAND_URL_PREFIX , self . handle_server_manager_command ) ) <EOL> def handle_server_manager_command ( self , request , url_suffix ) : <EOL> """<STR_LIT>""" <EOL> command = url_suffix <EOL> if command == '<STR_LIT>' : <EOL> self . server_manager . SetRecordMode ( ) <EOL> return SimpleResponse ( <NUM_LIT:200> ) <EOL> elif command == '<STR_LIT>' : <EOL> self . server_manager . SetReplayMode ( ) <EOL> return SimpleResponse ( <NUM_LIT:200> ) <EOL> elif command == '<STR_LIT:status>' : <EOL> status = { } <EOL> is_record_mode = self . server_manager . IsRecordMode ( ) <EOL> status [ '<STR_LIT>' ] = is_record_mode <EOL> status [ '<STR_LIT>' ] = json . loads ( str ( self . options ) ) <EOL> archive_stats = self . http_archive . stats ( ) <EOL> if archive_stats : <EOL> status [ '<STR_LIT>' ] = json . loads ( archive_stats ) <EOL> return JsonResponse ( status ) <EOL> elif command == '<STR_LIT>' : <EOL> self . server_manager . should_exit = True <EOL> return SimpleResponse ( <NUM_LIT:200> ) <EOL> elif command == '<STR_LIT>' : <EOL> logging . info ( '<STR_LIT>' , str ( request . request_body ) [ : <NUM_LIT> ] ) <EOL> return SimpleResponse ( <NUM_LIT:200> ) <EOL> return None </s>
