<s> """<STR_LIT>""" <EOL> import base64 <EOL> import contextlib <EOL> import httplib <EOL> import logging <EOL> import os <EOL> import socket <EOL> import subprocess <EOL> import threading <EOL> import urllib <EOL> import wsgiref . headers <EOL> from google . appengine . tools . devappserver2 import http_runtime_constants <EOL> from google . appengine . tools . devappserver2 import instance <EOL> from google . appengine . tools . devappserver2 import login <EOL> from google . appengine . tools . devappserver2 import safe_subprocess <EOL> from google . appengine . tools . devappserver2 import shutdown <EOL> from google . appengine . tools . devappserver2 import util <EOL> class HttpRuntimeProxy ( instance . RuntimeProxy ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , args , runtime_config_getter , server_configuration , <EOL> env = None ) : <EOL> """<STR_LIT>""" <EOL> super ( HttpRuntimeProxy , self ) . __init__ ( ) <EOL> self . _host = '<STR_LIT:localhost>' <EOL> self . _port = None <EOL> self . _process = None <EOL> self . _process_lock = threading . Lock ( ) <EOL> self . _runtime_config_getter = runtime_config_getter <EOL> self . _args = args <EOL> self . _server_configuration = server_configuration <EOL> self . _env = env <EOL> def _get_error_file ( self ) : <EOL> for error_handler in self . _server_configuration . error_handlers or [ ] : <EOL> if not error_handler . error_code or error_handler . error_code == '<STR_LIT:default>' : <EOL> return os . path . join ( self . _server_configuration . application_root , <EOL> error_handler . file ) <EOL> else : <EOL> return None <EOL> def handle ( self , environ , start_response , url_map , match , request_id , <EOL> request_type ) : <EOL> """<STR_LIT>""" <EOL> environ [ http_runtime_constants . SCRIPT_HEADER ] = match . expand ( url_map . script ) <EOL> if request_type == instance . BACKGROUND_REQUEST : <EOL> environ [ http_runtime_constants . REQUEST_TYPE_HEADER ] = '<STR_LIT>' <EOL> elif request_type == instance . SHUTDOWN_REQUEST : <EOL> environ [ http_runtime_constants . REQUEST_TYPE_HEADER ] = '<STR_LIT>' <EOL> elif request_type == instance . INTERACTIVE_REQUEST : <EOL> environ [ http_runtime_constants . REQUEST_TYPE_HEADER ] = '<STR_LIT>' <EOL> for name in http_runtime_constants . ENVIRONS_TO_PROPAGATE : <EOL> if http_runtime_constants . INTERNAL_ENVIRON_PREFIX + name not in environ : <EOL> value = environ . get ( name , None ) <EOL> if value is not None : <EOL> environ [ <EOL> http_runtime_constants . INTERNAL_ENVIRON_PREFIX + name ] = value <EOL> headers = util . get_headers_from_environ ( environ ) <EOL> if environ . get ( '<STR_LIT>' ) : <EOL> url = '<STR_LIT>' % ( urllib . quote ( environ [ '<STR_LIT>' ] ) , <EOL> environ [ '<STR_LIT>' ] ) <EOL> else : <EOL> url = urllib . quote ( environ [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in environ : <EOL> headers [ '<STR_LIT>' ] = environ [ '<STR_LIT>' ] <EOL> data = environ [ '<STR_LIT>' ] . read ( int ( environ [ '<STR_LIT>' ] ) ) <EOL> else : <EOL> data = '<STR_LIT>' <EOL> cookies = environ . get ( '<STR_LIT>' ) <EOL> user_email , admin , user_id = login . get_user_info ( cookies ) <EOL> if user_email : <EOL> nickname , organization = user_email . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> else : <EOL> nickname = '<STR_LIT>' <EOL> organization = '<STR_LIT>' <EOL> headers [ http_runtime_constants . REQUEST_ID_HEADER ] = request_id <EOL> headers [ http_runtime_constants . INTERNAL_HEADER_PREFIX + '<STR_LIT>' ] = ( <EOL> user_id ) <EOL> headers [ http_runtime_constants . INTERNAL_HEADER_PREFIX + '<STR_LIT>' ] = ( <EOL> user_email ) <EOL> headers [ <EOL> http_runtime_constants . INTERNAL_HEADER_PREFIX + '<STR_LIT>' ] = ( <EOL> str ( int ( admin ) ) ) <EOL> headers [ <EOL> http_runtime_constants . INTERNAL_HEADER_PREFIX + '<STR_LIT>' ] = ( <EOL> nickname ) <EOL> headers [ <EOL> http_runtime_constants . INTERNAL_HEADER_PREFIX + '<STR_LIT>' ] = ( <EOL> organization ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> connection = httplib . HTTPConnection ( self . _host , self . _port ) <EOL> with contextlib . closing ( connection ) : <EOL> try : <EOL> connection . connect ( ) <EOL> connection . request ( environ . get ( '<STR_LIT>' , '<STR_LIT:GET>' ) , <EOL> url , <EOL> data , <EOL> dict ( headers . items ( ) ) ) <EOL> response = connection . getresponse ( ) <EOL> headers = [ ] <EOL> for name in response . msg : <EOL> for value in response . msg . getheaders ( name ) : <EOL> headers . append ( ( name , value ) ) <EOL> response_headers = wsgiref . headers . Headers ( headers ) <EOL> error_file = self . _get_error_file ( ) <EOL> if ( error_file and <EOL> http_runtime_constants . ERROR_CODE_HEADER in response_headers ) : <EOL> try : <EOL> with open ( error_file ) as f : <EOL> content = f . read ( ) <EOL> except IOError : <EOL> content = '<STR_LIT>' <EOL> logging . exception ( '<STR_LIT>' , error_file ) <EOL> start_response ( '<STR_LIT>' , <EOL> [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , str ( len ( content ) ) ) ] ) <EOL> yield content <EOL> return <EOL> del response_headers [ http_runtime_constants . ERROR_CODE_HEADER ] <EOL> start_response ( '<STR_LIT>' % ( response . status , response . reason ) , <EOL> response_headers . items ( ) ) <EOL> block = response . read ( <NUM_LIT> ) <EOL> while block : <EOL> yield block <EOL> block = response . read ( <NUM_LIT> ) <EOL> except Exception : <EOL> with self . _process_lock : <EOL> if self . _process and self . _process . poll ( ) is not None : <EOL> message = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> self . _port ) ) <EOL> logging . error ( message ) <EOL> start_response ( '<STR_LIT>' , <EOL> [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , str ( len ( message ) ) ) ] ) <EOL> shutdown . async_quit ( ) <EOL> yield message <EOL> else : <EOL> raise <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> runtime_config = self . _runtime_config_getter ( ) <EOL> serialized_config = base64 . b64encode ( runtime_config . SerializeToString ( ) ) <EOL> with self . _process_lock : <EOL> assert not self . _process , '<STR_LIT>' <EOL> self . _process = safe_subprocess . start_process ( <EOL> self . _args , <EOL> serialized_config , <EOL> stdout = subprocess . PIPE , <EOL> env = self . _env , <EOL> cwd = self . _server_configuration . application_root ) <EOL> line = self . _process . stdout . readline ( ) <EOL> try : <EOL> self . _port = int ( line ) <EOL> except ValueError : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> line ) <EOL> shutdown . async_quit ( ) <EOL> else : <EOL> self . _check_serving ( ) <EOL> def _can_connect ( self ) : <EOL> connection = httplib . HTTPConnection ( self . _host , self . _port ) <EOL> with contextlib . closing ( connection ) : <EOL> try : <EOL> connection . connect ( ) <EOL> except socket . error : <EOL> return False <EOL> else : <EOL> return True <EOL> def _check_serving ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _can_connect ( ) : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . _port ) <EOL> shutdown . async_quit ( ) <EOL> def quit ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . _process_lock : <EOL> assert self . _process , '<STR_LIT>' <EOL> try : <EOL> self . _process . kill ( ) <EOL> except OSError : <EOL> pass <EOL> self . _process = None </s>
