<s> """<STR_LIT>""" <EOL> import cStringIO <EOL> import os <EOL> import sys <EOL> import traceback <EOL> import urllib <EOL> import urlparse <EOL> import google <EOL> from google . appengine . api import api_base_pb <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . api import appinfo <EOL> from google . appengine . api . logservice import log_service_pb <EOL> from google . appengine . api . logservice import logservice <EOL> from google . appengine . ext . remote_api import remote_api_stub <EOL> from google . appengine . runtime import background <EOL> from google . appengine . runtime import request_environment <EOL> from google . appengine . runtime import runtime <EOL> from google . appengine . runtime import shutdown <EOL> from google . appengine . tools . devappserver2 import environ_utils <EOL> from google . appengine . tools . devappserver2 import http_runtime_constants <EOL> from google . appengine . tools . devappserver2 . python import request_state <EOL> httplib_responses = { <EOL> <NUM_LIT:100> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT:200> : '<STR_LIT:OK>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> <NUM_LIT> : '<STR_LIT>' , <EOL> } <EOL> class RequestHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> _PYTHON_LIB_DIR = os . path . dirname ( os . path . dirname ( google . __file__ ) ) <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> if appinfo . MODULE_SEPARATOR not in config . version_id : <EOL> module_id = appinfo . DEFAULT_MODULE <EOL> version_id = config . version_id <EOL> else : <EOL> module_id , version_id = config . version_id . split ( appinfo . MODULE_SEPARATOR ) <EOL> self . environ_template = { <EOL> '<STR_LIT>' : config . app_id , <EOL> '<STR_LIT>' : module_id , <EOL> '<STR_LIT>' : version_id , <EOL> '<STR_LIT>' : config . datacenter . encode ( '<STR_LIT:ascii>' ) , <EOL> '<STR_LIT>' : config . instance_id . encode ( '<STR_LIT:ascii>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : config . auth_domain . encode ( '<STR_LIT:ascii>' ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : http_runtime_constants . SERVER_SOFTWARE , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : config . threadsafe , <EOL> } <EOL> self . _command_globals = { } <EOL> self . environ_template . update ( ( env . key , env . value ) for env in config . environ ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> remote_api_stub . RemoteStub . _SetRequestId ( <EOL> environ [ http_runtime_constants . REQUEST_ID_ENVIRON ] ) <EOL> request_type = environ . pop ( http_runtime_constants . REQUEST_TYPE_HEADER , None ) <EOL> request_state . start_request ( <EOL> environ [ http_runtime_constants . REQUEST_ID_ENVIRON ] ) <EOL> try : <EOL> if request_type == '<STR_LIT>' : <EOL> response = self . handle_background_request ( environ ) <EOL> elif request_type == '<STR_LIT>' : <EOL> response = self . handle_shutdown_request ( environ ) <EOL> elif request_type == '<STR_LIT>' : <EOL> response = self . handle_interactive_request ( environ ) <EOL> else : <EOL> response = self . handle_normal_request ( environ ) <EOL> finally : <EOL> request_state . end_request ( <EOL> environ [ http_runtime_constants . REQUEST_ID_ENVIRON ] ) <EOL> error = response . get ( '<STR_LIT:error>' , <NUM_LIT:0> ) <EOL> self . _flush_logs ( response . get ( '<STR_LIT>' , [ ] ) ) <EOL> if error == <NUM_LIT:0> : <EOL> response_code = response . get ( '<STR_LIT>' , <NUM_LIT:200> ) <EOL> status = '<STR_LIT>' % ( response_code , httplib_responses . get ( <EOL> response_code , '<STR_LIT>' ) ) <EOL> start_response ( status , response . get ( '<STR_LIT>' , [ ] ) ) <EOL> return [ response . get ( '<STR_LIT:body>' , '<STR_LIT>' ) ] <EOL> elif error == <NUM_LIT:2> : <EOL> start_response ( '<STR_LIT>' , [ ] ) <EOL> return [ ] <EOL> else : <EOL> start_response ( '<STR_LIT>' , <EOL> [ ( http_runtime_constants . ERROR_CODE_HEADER , str ( error ) ) ] ) <EOL> return [ ] <EOL> def handle_normal_request ( self , environ ) : <EOL> user_environ = self . get_user_environ ( environ ) <EOL> script = environ . pop ( http_runtime_constants . SCRIPT_HEADER ) <EOL> body = environ [ '<STR_LIT>' ] . read ( int ( environ . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) ) <EOL> url = '<STR_LIT>' % ( user_environ [ '<STR_LIT>' ] , <EOL> user_environ [ '<STR_LIT>' ] , <EOL> urllib . quote ( environ [ '<STR_LIT>' ] ) , <EOL> environ [ '<STR_LIT>' ] ) <EOL> return runtime . HandleRequest ( user_environ , script , url , body , <EOL> self . config . application_root , <EOL> self . _PYTHON_LIB_DIR ) <EOL> def handle_background_request ( self , environ ) : <EOL> return background . Handle ( self . get_user_environ ( environ ) ) <EOL> def handle_shutdown_request ( self , environ ) : <EOL> response , exc = shutdown . Handle ( self . get_user_environ ( environ ) ) <EOL> if exc : <EOL> for request in request_state . get_request_states ( ) : <EOL> if ( request . request_id != <EOL> environ [ http_runtime_constants . REQUEST_ID_ENVIRON ] ) : <EOL> request . inject_exception ( exc [ <NUM_LIT:1> ] ) <EOL> return response <EOL> def handle_interactive_request ( self , environ ) : <EOL> code = environ [ '<STR_LIT>' ] . read ( ) . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) <EOL> user_environ = self . get_user_environ ( environ ) <EOL> if '<STR_LIT>' in user_environ : <EOL> del user_environ [ '<STR_LIT>' ] <EOL> user_environ [ '<STR_LIT>' ] = '<STR_LIT:GET>' <EOL> url = '<STR_LIT>' % ( user_environ [ '<STR_LIT>' ] , <EOL> user_environ [ '<STR_LIT>' ] , <EOL> urllib . quote ( environ [ '<STR_LIT>' ] ) , <EOL> environ [ '<STR_LIT>' ] ) <EOL> results_io = cStringIO . StringIO ( ) <EOL> old_sys_stdout = sys . stdout <EOL> try : <EOL> error = logservice . LogsBuffer ( ) <EOL> request_environment . current_request . Init ( error , user_environ ) <EOL> url = urlparse . urlsplit ( url ) <EOL> environ . update ( runtime . CgiDictFromParsedUrl ( url ) ) <EOL> sys . stdout = results_io <EOL> try : <EOL> try : <EOL> __import__ ( '<STR_LIT>' , self . _command_globals ) <EOL> except ImportError as e : <EOL> if '<STR_LIT>' not in e . message : <EOL> raise <EOL> compiled_code = compile ( code , '<STR_LIT>' , '<STR_LIT>' ) <EOL> exec ( compiled_code , self . _command_globals ) <EOL> except : <EOL> traceback . print_exc ( file = results_io ) <EOL> return { '<STR_LIT:error>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:200> , <EOL> '<STR_LIT>' : [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) ] , <EOL> '<STR_LIT:body>' : results_io . getvalue ( ) , <EOL> '<STR_LIT>' : error . parse_logs ( ) } <EOL> finally : <EOL> request_environment . current_request . Clear ( ) <EOL> sys . stdout = old_sys_stdout <EOL> def get_user_environ ( self , environ ) : <EOL> """<STR_LIT>""" <EOL> user_environ = self . environ_template . copy ( ) <EOL> environ_utils . propagate_environs ( environ , user_environ ) <EOL> user_environ [ '<STR_LIT>' ] = environ . get ( '<STR_LIT>' , '<STR_LIT:GET>' ) <EOL> content_type = environ . get ( '<STR_LIT>' ) <EOL> if content_type : <EOL> user_environ [ '<STR_LIT>' ] = content_type <EOL> content_length = environ . get ( '<STR_LIT>' ) <EOL> if content_length : <EOL> user_environ [ '<STR_LIT>' ] = content_length <EOL> return user_environ <EOL> def _flush_logs ( self , logs ) : <EOL> """<STR_LIT>""" <EOL> logs_group = log_service_pb . UserAppLogGroup ( ) <EOL> for timestamp_usec , level , message , source_location in logs : <EOL> log_line = logs_group . add_log_line ( ) <EOL> log_line . set_timestamp_usec ( timestamp_usec ) <EOL> log_line . set_level ( level ) <EOL> if source_location : <EOL> log_line . mutable_source_location ( ) . set_file ( source_location [ <NUM_LIT:0> ] ) <EOL> log_line . mutable_source_location ( ) . set_line ( source_location [ <NUM_LIT:1> ] ) <EOL> log_line . mutable_source_location ( ) . set_function_name ( source_location [ <NUM_LIT:2> ] ) <EOL> log_line . set_message ( message ) <EOL> request = log_service_pb . FlushRequest ( ) <EOL> request . set_logs ( logs_group . Encode ( ) ) <EOL> response = api_base_pb . VoidProto ( ) <EOL> apiproxy_stub_map . MakeSyncCall ( '<STR_LIT>' , '<STR_LIT>' , request , response ) </s>
