<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> from google . appengine . tools import os_compat <EOL> import __builtin__ <EOL> import BaseHTTPServer <EOL> import Cookie <EOL> import base64 <EOL> import cStringIO <EOL> import cgi <EOL> import cgitb <EOL> import email . Utils <EOL> import errno <EOL> import hashlib <EOL> import heapq <EOL> import httplib <EOL> import imp <EOL> import inspect <EOL> import logging <EOL> import mimetools <EOL> import mimetypes <EOL> import os <EOL> import pdb <EOL> import select <EOL> import shutil <EOL> import simplejson <EOL> import tempfile <EOL> import yaml <EOL> import re <EOL> import sre_compile <EOL> import sre_constants <EOL> import sre_parse <EOL> import socket <EOL> import sys <EOL> import time <EOL> import types <EOL> import urlparse <EOL> import urllib <EOL> import zlib <EOL> import google <EOL> from google . pyglib import gexcept <EOL> try : <EOL> from google . third_party . apphosting . python . webapp2 import v2_3 as tmp <EOL> sys . path . append ( os . path . dirname ( tmp . __file__ ) ) <EOL> del tmp <EOL> except ImportError : <EOL> pass <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . api import appinfo <EOL> from google . appengine . api import appinfo_includes <EOL> from google . appengine . api import app_logging <EOL> from google . appengine . api import blobstore <EOL> from google . appengine . api import croninfo <EOL> from google . appengine . api import datastore_admin <EOL> from google . appengine . api import lib_config <EOL> from google . appengine . api import mail <EOL> from google . appengine . api import urlfetch_stub <EOL> from google . appengine . api import user_service_stub <EOL> from google . appengine . api import yaml_errors <EOL> from google . appengine . api . app_identity import app_identity_stub <EOL> from google . appengine . api . capabilities import capability_stub <EOL> from google . appengine . api . conversion import conversion_stub <EOL> from google . appengine . api . channel import channel_service_stub <EOL> from google . appengine . api . files import file_service_stub <EOL> from google . appengine . api . logservice import logservice <EOL> from google . appengine . api . logservice import logservice_stub <EOL> from google . appengine . api . search import simple_search_stub <EOL> from google . appengine . api . prospective_search import prospective_search_stub <EOL> from google . appengine . api import rdbms_mysqldb <EOL> from google . appengine . api . system import system_stub <EOL> from google . appengine . datastore import datastore_stub_util <EOL> from google . appengine import dist <EOL> from google . appengine . api . taskqueue import taskqueue_rabbitmq <EOL> from google . appengine . api import mail_stub <EOL> from google . appengine . api . blobstore import blobstore_stub <EOL> from google . appengine . api import datastore_distributed <EOL> from google . appengine . api . blobstore import datastore_blob_storage <EOL> from google . appengine . api . memcache import memcache_distributed <EOL> from google . appengine . api . xmpp import xmpp_service_real <EOL> from google . appengine . api . appscale import babel <EOL> from google . appengine . api . appscale import ec2 <EOL> from google . appengine . api . appscale import mapreduce <EOL> from google . appengine . api . appscale import neptune <EOL> try : <EOL> from google . appengine . runtime import request_environment <EOL> from google . appengine . runtime import runtime <EOL> except : <EOL> request_environment = None <EOL> runtime = None <EOL> from google . appengine . tools import dev_appserver_blobstore <EOL> from google . appengine . tools import dev_appserver_channel <EOL> from google . appengine . tools import dev_appserver_blobimage <EOL> from google . appengine . tools import dev_appserver_import_hook <EOL> from google . appengine . tools import dev_appserver_index <EOL> from google . appengine . tools import dev_appserver_login <EOL> from google . appengine . tools import dev_appserver_oauth <EOL> from google . appengine . tools import dev_appserver_multiprocess as multiprocess <EOL> from google . appengine . tools import dev_appserver_upload <EOL> from google . storage . speckle . python . api import rdbms <EOL> CouldNotFindModuleError = dev_appserver_import_hook . CouldNotFindModuleError <EOL> FakeAccess = dev_appserver_import_hook . FakeAccess <EOL> FakeFile = dev_appserver_import_hook . FakeFile <EOL> FakeReadlink = dev_appserver_import_hook . FakeReadlink <EOL> FakeSetLocale = dev_appserver_import_hook . FakeSetLocale <EOL> FakeUnlink = dev_appserver_import_hook . FakeUnlink <EOL> GetSubmoduleName = dev_appserver_import_hook . GetSubmoduleName <EOL> HardenedModulesHook = dev_appserver_import_hook . HardenedModulesHook <EOL> SDK_ROOT = dev_appserver_import_hook . SDK_ROOT <EOL> PYTHON_LIB_VAR = '<STR_LIT>' <EOL> DEVEL_CONSOLE_PATH = PYTHON_LIB_VAR + '<STR_LIT>' <EOL> REMOTE_API_PATH = ( PYTHON_LIB_VAR + <EOL> '<STR_LIT>' ) <EOL> FILE_MISSING_EXCEPTIONS = frozenset ( [ errno . ENOENT , errno . ENOTDIR ] ) <EOL> MAX_URL_LENGTH = <NUM_LIT> <EOL> DEFAULT_ENV = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> DEFAULT_SELECT_DELAY = <NUM_LIT> <EOL> for ext , mime_type in mail . EXTENSION_MIME_MAP . iteritems ( ) : <EOL> mimetypes . add_type ( mime_type , '<STR_LIT:.>' + ext ) <EOL> MAX_RUNTIME_RESPONSE_SIZE = <NUM_LIT:32> << <NUM_LIT:20> <EOL> MAX_REQUEST_SIZE = <NUM_LIT:32> * <NUM_LIT> * <NUM_LIT> <EOL> COPY_BLOCK_SIZE = <NUM_LIT:1> << <NUM_LIT:20> <EOL> API_VERSION = '<STR_LIT:1>' <EOL> VERSION_FILE = '<STR_LIT>' <EOL> DEVEL_PAYLOAD_HEADER = '<STR_LIT>' <EOL> DEVEL_PAYLOAD_RAW_HEADER = '<STR_LIT>' <EOL> DEVEL_FAKE_IS_ADMIN_HEADER = '<STR_LIT>' <EOL> DEVEL_FAKE_IS_ADMIN_RAW_HEADER = '<STR_LIT>' <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class InvalidAppConfigError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class AppConfigNotFoundError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class CompileError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text ) : <EOL> self . text = text <EOL> def MonkeyPatchPdb ( pdb ) : <EOL> """<STR_LIT>""" <EOL> def NewSetTrace ( ) : <EOL> """<STR_LIT>""" <EOL> p = pdb . Pdb ( stdin = sys . __stdin__ , stdout = sys . __stdout__ ) <EOL> p . set_trace ( sys . _getframe ( ) . f_back ) <EOL> pdb . set_trace = NewSetTrace <EOL> def SplitURL ( relative_url ) : <EOL> """<STR_LIT>""" <EOL> ( unused_scheme , unused_netloc , path , query , <EOL> unused_fragment ) = urlparse . urlsplit ( relative_url ) <EOL> return path , query <EOL> def GetFullURL ( server_name , server_port , relative_url ) : <EOL> """<STR_LIT>""" <EOL> if str ( server_port ) != '<STR_LIT>' : <EOL> netloc = '<STR_LIT>' % ( server_name , server_port ) <EOL> else : <EOL> netloc = server_name <EOL> return '<STR_LIT>' % ( netloc , relative_url ) <EOL> def CopyStreamPart ( source , destination , content_size ) : <EOL> """<STR_LIT>""" <EOL> bytes_copied = <NUM_LIT:0> <EOL> bytes_left = content_size <EOL> while bytes_left > <NUM_LIT:0> : <EOL> bytes = source . read ( min ( bytes_left , COPY_BLOCK_SIZE ) ) <EOL> bytes_read = len ( bytes ) <EOL> if bytes_read == <NUM_LIT:0> : <EOL> break <EOL> destination . write ( bytes ) <EOL> bytes_copied += bytes_read <EOL> bytes_left -= bytes_read <EOL> return bytes_copied <EOL> def AppIdWithDefaultPartition ( app_id , default_partition ) : <EOL> """<STR_LIT>""" <EOL> if not default_partition : <EOL> return app_id <EOL> if '<STR_LIT>' in app_id : <EOL> return app_id <EOL> return default_partition + '<STR_LIT>' + app_id <EOL> class AppServerRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> ATTRIBUTES = [ '<STR_LIT>' , <EOL> '<STR_LIT:path>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def __init__ ( self , <EOL> relative_url , <EOL> path , <EOL> headers , <EOL> infile , <EOL> secret_hash , <EOL> force_admin = False ) : <EOL> """<STR_LIT>""" <EOL> self . relative_url = relative_url <EOL> self . path = path <EOL> self . headers = headers <EOL> self . infile = infile <EOL> self . force_admin = force_admin <EOL> if DEVEL_PAYLOAD_RAW_HEADER in self . headers : <EOL> if self . headers [ DEVEL_PAYLOAD_RAW_HEADER ] == secret_hash : <EOL> self . force_admin = True <EOL> if ( DEVEL_FAKE_IS_ADMIN_RAW_HEADER in self . headers ) : <EOL> if self . headers [ DEVEL_FAKE_IS_ADMIN_RAW_HEADER ] == secret_hash : <EOL> self . force_admin = True <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if type ( self ) == type ( other ) : <EOL> for attribute in self . ATTRIBUTES : <EOL> if getattr ( self , attribute ) != getattr ( other , attribute ) : <EOL> return False <EOL> return True <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> for attribute in self . ATTRIBUTES : <EOL> results . append ( '<STR_LIT>' % ( attribute , getattr ( self , attribute ) ) ) <EOL> return '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( results ) <EOL> class URLDispatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def Dispatch ( self , <EOL> request , <EOL> outfile , <EOL> base_env_dict = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def EndRedirect ( self , dispatched_output , original_output ) : <EOL> """<STR_LIT>""" <EOL> original_output . write ( dispatched_output . read ( ) ) <EOL> class URLMatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _url_patterns = [ ] <EOL> def AddURL ( self , regex , dispatcher , path , requires_login , admin_only , <EOL> auth_fail_action ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( dispatcher , URLDispatcher ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if regex . startswith ( '<STR_LIT>' ) or regex . endswith ( '<STR_LIT:$>' ) : <EOL> raise InvalidAppConfigError ( '<STR_LIT>' ) <EOL> adjusted_regex = '<STR_LIT>' % regex <EOL> try : <EOL> url_re = re . compile ( adjusted_regex ) <EOL> except re . error , e : <EOL> raise InvalidAppConfigError ( '<STR_LIT>' % e ) <EOL> match_tuple = ( url_re , dispatcher , path , requires_login , admin_only , <EOL> auth_fail_action ) <EOL> self . _url_patterns . append ( match_tuple ) <EOL> def Match ( self , <EOL> relative_url , <EOL> split_url = SplitURL ) : <EOL> """<STR_LIT>""" <EOL> adjusted_url , unused_query_string = split_url ( relative_url ) <EOL> for url_tuple in self . _url_patterns : <EOL> url_re , dispatcher , path , requires_login , admin_only , auth_fail_action = url_tuple <EOL> the_match = url_re . match ( adjusted_url ) <EOL> if the_match : <EOL> adjusted_path = the_match . expand ( path ) <EOL> return ( dispatcher , adjusted_path , requires_login , admin_only , <EOL> auth_fail_action ) <EOL> return None , None , None , None , None <EOL> def GetDispatchers ( self ) : <EOL> """<STR_LIT>""" <EOL> return set ( [ url_tuple [ <NUM_LIT:1> ] for url_tuple in self . _url_patterns ] ) <EOL> class MatcherDispatcher ( URLDispatcher ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> config , <EOL> login_url , <EOL> url_matchers , <EOL> get_user_info = dev_appserver_login . GetUserInfo , <EOL> login_redirect = dev_appserver_login . LoginRedirect ) : <EOL> """<STR_LIT>""" <EOL> self . _config = config <EOL> self . _login_url = login_url <EOL> self . _url_matchers = tuple ( url_matchers ) <EOL> self . _get_user_info = get_user_info <EOL> self . _login_redirect = login_redirect <EOL> def Dispatch ( self , <EOL> request , <EOL> outfile , <EOL> base_env_dict = None ) : <EOL> """<STR_LIT>""" <EOL> cookies = '<STR_LIT:U+002CU+0020>' . join ( request . headers . getheaders ( '<STR_LIT>' ) ) <EOL> email_addr , user_id , admin , valid_cookie = self . _get_user_info ( cookies ) <EOL> for matcher in self . _url_matchers : <EOL> dispatcher , matched_path , requires_login , admin_only , auth_fail_action = matcher . Match ( request . relative_url ) <EOL> if dispatcher is None : <EOL> continue <EOL> logging . debug ( '<STR_LIT>' , <EOL> request . relative_url , dispatcher , matched_path ) <EOL> if ( ( requires_login or admin_only ) and <EOL> not email_addr and <EOL> not request . force_admin ) : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> if auth_fail_action == appinfo . AUTH_FAIL_ACTION_REDIRECT : <EOL> self . _login_redirect ( self . _login_url , <EOL> base_env_dict [ '<STR_LIT>' ] , <EOL> base_env_dict [ '<STR_LIT>' ] , <EOL> request . relative_url , <EOL> outfile ) <EOL> elif auth_fail_action == appinfo . AUTH_FAIL_ACTION_UNAUTHORIZED : <EOL> outfile . write ( '<STR_LIT>' <EOL> '<STR_LIT:\r\n>' <EOL> '<STR_LIT>' <EOL> % ( httplib . UNAUTHORIZED ) ) <EOL> elif admin_only and not admin and not request . force_admin : <EOL> outfile . write ( '<STR_LIT>' <EOL> '<STR_LIT:\r\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( httplib . FORBIDDEN , email_addr ) ) <EOL> else : <EOL> request . path = matched_path <EOL> forward_request = dispatcher . Dispatch ( request , <EOL> outfile , <EOL> base_env_dict = base_env_dict ) <EOL> if forward_request : <EOL> logging . info ( '<STR_LIT>' , <EOL> forward_request . relative_url ) <EOL> new_outfile = cStringIO . StringIO ( ) <EOL> self . Dispatch ( forward_request , <EOL> new_outfile , <EOL> dict ( base_env_dict ) ) <EOL> new_outfile . seek ( <NUM_LIT:0> ) <EOL> dispatcher . EndRedirect ( new_outfile , outfile ) <EOL> return <EOL> outfile . write ( '<STR_LIT>' <EOL> '<STR_LIT:\r\n>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( httplib . NOT_FOUND , request . relative_url ) ) <EOL> _IGNORE_REQUEST_HEADERS = frozenset ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> _request_id = <NUM_LIT:0> <EOL> def _generate_request_id_hash ( ) : <EOL> """<STR_LIT>""" <EOL> return hashlib . sha1 ( str ( _request_id ) ) . hexdigest ( ) [ : <NUM_LIT:8> ] . upper ( ) <EOL> def GetGoogleSqlOAuth2RefreshToken ( oauth_file_path ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( oauth_file_path ) : <EOL> return None <EOL> try : <EOL> with open ( oauth_file_path ) as oauth_file : <EOL> token = simplejson . load ( oauth_file ) <EOL> return token [ '<STR_LIT>' ] <EOL> except ( IOError , KeyError , simplejson . decoder . JSONDecodeError ) : <EOL> logging . exception ( <EOL> '<STR_LIT>' , oauth_file_path ) <EOL> return None <EOL> def SetupEnvironment ( cgi_path , <EOL> relative_url , <EOL> headers , <EOL> infile , <EOL> split_url = SplitURL , <EOL> get_user_info = dev_appserver_login . GetUserInfo ) : <EOL> """<STR_LIT>""" <EOL> env = DEFAULT_ENV . copy ( ) <EOL> script_name , query_string = split_url ( relative_url ) <EOL> env [ '<STR_LIT>' ] = script_name <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> env [ '<STR_LIT>' ] = query_string <EOL> env [ '<STR_LIT>' ] = urllib . unquote ( script_name ) <EOL> env [ '<STR_LIT>' ] = cgi_path <EOL> env [ '<STR_LIT>' ] = headers . getheader ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> env [ '<STR_LIT>' ] = headers . getheader ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> cookies = '<STR_LIT:U+002CU+0020>' . join ( headers . getheaders ( '<STR_LIT>' ) ) <EOL> email_addr , user_id , admin , valid_cookie = get_user_info ( cookies ) <EOL> if valid_cookie : <EOL> env [ '<STR_LIT>' ] = email_addr <EOL> env [ '<STR_LIT>' ] = user_id <EOL> env [ '<STR_LIT>' ] = user_id <EOL> else : <EOL> env [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> env [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> env [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> env [ '<STR_LIT>' ] = '<STR_LIT:0>' <EOL> if admin and valid_cookie : <EOL> env [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> if env [ '<STR_LIT>' ] == '<STR_LIT:*>' : <EOL> auth_domain = '<STR_LIT>' <EOL> parts = email_addr . split ( '<STR_LIT:@>' ) <EOL> if len ( parts ) == <NUM_LIT:2> and parts [ <NUM_LIT:1> ] : <EOL> auth_domain = parts [ <NUM_LIT:1> ] <EOL> env [ '<STR_LIT>' ] = auth_domain <EOL> global _request_id <EOL> env [ '<STR_LIT>' ] = _generate_request_id_hash ( ) <EOL> _request_id += <NUM_LIT:1> <EOL> for key in headers : <EOL> if key in _IGNORE_REQUEST_HEADERS : <EOL> continue <EOL> adjusted_name = key . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) . upper ( ) <EOL> env [ '<STR_LIT>' + adjusted_name ] = '<STR_LIT:U+002CU+0020>' . join ( headers . getheaders ( key ) ) <EOL> if DEVEL_PAYLOAD_HEADER in env : <EOL> del env [ DEVEL_PAYLOAD_HEADER ] <EOL> new_data = base64 . standard_b64decode ( infile . getvalue ( ) ) <EOL> infile . seek ( <NUM_LIT:0> ) <EOL> infile . truncate ( ) <EOL> infile . write ( new_data ) <EOL> infile . seek ( <NUM_LIT:0> ) <EOL> env [ '<STR_LIT>' ] = str ( len ( new_data ) ) <EOL> if DEVEL_FAKE_IS_ADMIN_HEADER in env : <EOL> del env [ DEVEL_FAKE_IS_ADMIN_HEADER ] <EOL> token = GetGoogleSqlOAuth2RefreshToken ( rdbms . OAUTH_CREDENTIALS_PATH ) <EOL> if token : <EOL> env [ '<STR_LIT>' ] = token <EOL> return env <EOL> def NotImplementedFake ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> class NotImplementedFakeClass ( object ) : <EOL> """<STR_LIT>""" <EOL> __init__ = NotImplementedFake <EOL> def IsEncodingsModule ( module_name ) : <EOL> """<STR_LIT>""" <EOL> if ( module_name in ( '<STR_LIT>' , '<STR_LIT>' ) or <EOL> module_name . startswith ( '<STR_LIT>' ) ) : <EOL> return True <EOL> return False <EOL> def ClearAllButEncodingsModules ( module_dict ) : <EOL> """<STR_LIT>""" <EOL> for module_name in module_dict . keys ( ) : <EOL> if not IsEncodingsModule ( module_name ) : <EOL> del module_dict [ module_name ] <EOL> def UpdateNotSharedModulesInParents ( module_dict ) : <EOL> """<STR_LIT>""" <EOL> for prefix in NOT_SHARED_MODULE_PREFIXES : <EOL> parent_name , _ , submodule_name = prefix . rpartition ( '<STR_LIT:.>' ) <EOL> parent = module_dict . get ( parent_name ) <EOL> if parent : <EOL> if prefix in module_dict : <EOL> setattr ( parent , submodule_name , module_dict [ prefix ] ) <EOL> elif hasattr ( parent , submodule_name ) : <EOL> delattr ( parent , submodule_name ) <EOL> SHARED_MODULE_PREFIXES = set ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:email>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> NOT_SHARED_MODULE_PREFIXES = set ( [ <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def ModuleNameHasPrefix ( module_name , prefix_set ) : <EOL> """<STR_LIT>""" <EOL> for prefix in prefix_set : <EOL> if prefix == module_name : <EOL> return True <EOL> if module_name . startswith ( prefix + '<STR_LIT:.>' ) : <EOL> return True <EOL> return False <EOL> def SetupSharedModules ( module_dict ) : <EOL> """<STR_LIT>""" <EOL> output_dict = { } <EOL> for module_name , module in module_dict . iteritems ( ) : <EOL> if module is None : <EOL> continue <EOL> if IsEncodingsModule ( module_name ) : <EOL> output_dict [ module_name ] = module <EOL> continue <EOL> shared_prefix = ModuleNameHasPrefix ( module_name , SHARED_MODULE_PREFIXES ) <EOL> banned_prefix = ModuleNameHasPrefix ( module_name , NOT_SHARED_MODULE_PREFIXES ) <EOL> if shared_prefix and not banned_prefix : <EOL> output_dict [ module_name ] = module <EOL> return output_dict <EOL> def ModuleHasValidMainFunction ( module ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( module , '<STR_LIT>' ) and type ( module . main ) is types . FunctionType : <EOL> arg_names , var_args , var_kwargs , default_values = inspect . getargspec ( <EOL> module . main ) <EOL> if len ( arg_names ) == <NUM_LIT:0> : <EOL> return True <EOL> if default_values is not None and len ( arg_names ) == len ( default_values ) : <EOL> return True <EOL> return False <EOL> def CheckScriptExists ( cgi_path , handler_path ) : <EOL> """<STR_LIT>""" <EOL> if handler_path . startswith ( PYTHON_LIB_VAR + '<STR_LIT:/>' ) : <EOL> return <EOL> if ( not os . path . isdir ( cgi_path ) and <EOL> not os . path . isfile ( cgi_path ) and <EOL> os . path . isfile ( cgi_path + '<STR_LIT>' ) ) : <EOL> raise CouldNotFindModuleError ( <EOL> '<STR_LIT>' % <EOL> handler_path ) <EOL> def GetScriptModuleName ( handler_path ) : <EOL> """<STR_LIT>""" <EOL> if handler_path . startswith ( PYTHON_LIB_VAR + '<STR_LIT:/>' ) : <EOL> handler_path = handler_path [ len ( PYTHON_LIB_VAR ) : ] <EOL> handler_path = os . path . normpath ( handler_path ) <EOL> extension_index = handler_path . rfind ( '<STR_LIT>' ) <EOL> if extension_index != - <NUM_LIT:1> : <EOL> handler_path = handler_path [ : extension_index ] <EOL> module_fullname = handler_path . replace ( os . sep , '<STR_LIT:.>' ) <EOL> module_fullname = module_fullname . strip ( '<STR_LIT:.>' ) <EOL> module_fullname = re . sub ( '<STR_LIT>' , '<STR_LIT:.>' , module_fullname ) <EOL> if module_fullname . endswith ( '<STR_LIT>' ) : <EOL> module_fullname = module_fullname [ : - len ( '<STR_LIT>' ) ] <EOL> return module_fullname <EOL> def FindMissingInitFiles ( cgi_path , module_fullname , isfile = os . path . isfile ) : <EOL> """<STR_LIT>""" <EOL> missing_init_files = [ ] <EOL> if cgi_path . endswith ( '<STR_LIT>' ) : <EOL> module_base = os . path . dirname ( cgi_path ) <EOL> else : <EOL> module_base = cgi_path <EOL> depth_count = module_fullname . count ( '<STR_LIT:.>' ) <EOL> if cgi_path . endswith ( '<STR_LIT>' ) or not cgi_path . endswith ( '<STR_LIT>' ) : <EOL> depth_count += <NUM_LIT:1> <EOL> for index in xrange ( depth_count ) : <EOL> current_init_file = os . path . abspath ( <EOL> os . path . join ( module_base , '<STR_LIT>' ) ) <EOL> if not isfile ( current_init_file ) : <EOL> missing_init_files . append ( current_init_file ) <EOL> module_base = os . path . abspath ( os . path . join ( module_base , os . pardir ) ) <EOL> return missing_init_files <EOL> def LoadTargetModule ( handler_path , <EOL> cgi_path , <EOL> import_hook , <EOL> module_dict = sys . modules ) : <EOL> """<STR_LIT>""" <EOL> CheckScriptExists ( cgi_path , handler_path ) <EOL> module_fullname = GetScriptModuleName ( handler_path ) <EOL> script_module = module_dict . get ( module_fullname ) <EOL> module_code = None <EOL> if script_module is not None and ModuleHasValidMainFunction ( script_module ) : <EOL> logging . debug ( '<STR_LIT>' , module_fullname ) <EOL> else : <EOL> if script_module is None : <EOL> script_module = imp . new_module ( module_fullname ) <EOL> script_module . __loader__ = import_hook <EOL> try : <EOL> module_code = import_hook . get_code ( module_fullname ) <EOL> full_path , search_path , submodule = ( <EOL> import_hook . GetModuleInfo ( module_fullname ) ) <EOL> script_module . __file__ = full_path <EOL> if search_path is not None : <EOL> script_module . __path__ = search_path <EOL> except UnicodeDecodeError , e : <EOL> error = ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( e , handler_path ) ) <EOL> raise SyntaxError ( error ) <EOL> except : <EOL> exc_type , exc_value , exc_tb = sys . exc_info ( ) <EOL> import_error_message = str ( exc_type ) <EOL> if exc_value : <EOL> import_error_message += '<STR_LIT>' + str ( exc_value ) <EOL> logging . exception ( '<STR_LIT>' , <EOL> module_fullname , import_error_message ) <EOL> missing_inits = FindMissingInitFiles ( cgi_path , module_fullname ) <EOL> if missing_inits : <EOL> logging . warning ( '<STR_LIT>' , <EOL> '<STR_LIT:U+002CU+0020>' . join ( missing_inits ) ) <EOL> else : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> independent_load_successful = True <EOL> if not os . path . isfile ( cgi_path ) : <EOL> independent_load_successful = False <EOL> else : <EOL> try : <EOL> source_file = open ( cgi_path ) <EOL> try : <EOL> module_code = compile ( source_file . read ( ) , cgi_path , '<STR_LIT>' ) <EOL> script_module . __file__ = cgi_path <EOL> finally : <EOL> source_file . close ( ) <EOL> except OSError : <EOL> independent_load_successful = False <EOL> if not independent_load_successful : <EOL> raise exc_type , exc_value , exc_tb <EOL> module_dict [ module_fullname ] = script_module <EOL> return module_fullname , script_module , module_code <EOL> def _WriteErrorToOutput ( status , message , outfile ) : <EOL> """<STR_LIT>""" <EOL> logging . error ( message ) <EOL> outfile . write ( '<STR_LIT>' % ( status , message ) ) <EOL> def GetRequestSize ( request , env_dict , outfile ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in request . headers : <EOL> request_size = int ( request . headers [ '<STR_LIT>' ] ) <EOL> elif env_dict and env_dict . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT:POST>' : <EOL> _WriteErrorToOutput ( '<STR_LIT>' % httplib . LENGTH_REQUIRED , <EOL> '<STR_LIT>' , <EOL> outfile ) <EOL> return None <EOL> else : <EOL> request_size = <NUM_LIT:0> <EOL> if request_size <= MAX_REQUEST_SIZE : <EOL> return request_size <EOL> else : <EOL> msg = ( '<STR_LIT>' <EOL> % ( request_size , MAX_REQUEST_SIZE ) ) <EOL> _WriteErrorToOutput ( <EOL> '<STR_LIT>' % httplib . REQUEST_ENTITY_TOO_LARGE , <EOL> msg , outfile ) <EOL> return None <EOL> def ExecuteOrImportScript ( config , handler_path , cgi_path , import_hook ) : <EOL> """<STR_LIT>""" <EOL> module_fullname , script_module , module_code = LoadTargetModule ( <EOL> handler_path , cgi_path , import_hook ) <EOL> script_module . __name__ = '<STR_LIT:__main__>' <EOL> sys . modules [ '<STR_LIT:__main__>' ] = script_module <EOL> try : <EOL> import pdb <EOL> MonkeyPatchPdb ( pdb ) <EOL> if module_code : <EOL> exec module_code in script_module . __dict__ <EOL> else : <EOL> script_module . main ( ) <EOL> sys . stdout . flush ( ) <EOL> sys . stdout . seek ( <NUM_LIT:0> ) <EOL> try : <EOL> headers = mimetools . Message ( sys . stdout ) <EOL> finally : <EOL> sys . stdout . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> status_header = headers . get ( '<STR_LIT:status>' ) <EOL> error_response = False <EOL> if status_header : <EOL> try : <EOL> status_code = int ( status_header . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> error_response = status_code >= <NUM_LIT> <EOL> except ValueError : <EOL> error_response = True <EOL> if not error_response : <EOL> try : <EOL> parent_package = import_hook . GetParentPackage ( module_fullname ) <EOL> except Exception : <EOL> parent_package = None <EOL> if parent_package is not None : <EOL> submodule = GetSubmoduleName ( module_fullname ) <EOL> setattr ( parent_package , submodule , script_module ) <EOL> return error_response <EOL> finally : <EOL> script_module . __name__ = module_fullname <EOL> def ExecutePy27Handler ( config , handler_path , cgi_path , import_hook ) : <EOL> """<STR_LIT>""" <EOL> if request_environment is None or runtime is None : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> import os <EOL> save_environ = os . environ <EOL> save_getenv = os . getenv <EOL> env = dict ( save_environ ) <EOL> if env . get ( '<STR_LIT>' ) : <EOL> env [ '<STR_LIT>' ] = True <EOL> url = '<STR_LIT>' % ( env . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> env . get ( '<STR_LIT>' , '<STR_LIT:/>' ) ) <EOL> qs = env . get ( '<STR_LIT>' ) <EOL> if qs : <EOL> url += '<STR_LIT:?>' + qs <EOL> post_data = sys . stdin . read ( ) <EOL> if post_data and '<STR_LIT>' in env : <EOL> env [ '<STR_LIT>' ] = env [ '<STR_LIT>' ] <EOL> if cgi_path . endswith ( handler_path ) : <EOL> application_root = cgi_path [ : - len ( handler_path ) ] <EOL> if application_root . endswith ( '<STR_LIT:/>' ) and application_root != '<STR_LIT:/>' : <EOL> application_root = application_root [ : - <NUM_LIT:1> ] <EOL> else : <EOL> application_root = '<STR_LIT>' <EOL> try : <EOL> import pdb <EOL> MonkeyPatchPdb ( pdb ) <EOL> os . environ = request_environment . RequestLocalEnviron ( <EOL> request_environment . current_request ) <EOL> os . getenv = os . environ . get <EOL> response = runtime . HandleRequest ( env , handler_path , url , <EOL> post_data , application_root , SDK_ROOT , <EOL> import_hook ) <EOL> finally : <EOL> os . environ = save_environ <EOL> os . getenv = save_getenv <EOL> error = response . get ( '<STR_LIT:error>' ) <EOL> if error : <EOL> status = <NUM_LIT> <EOL> else : <EOL> status = <NUM_LIT:200> <EOL> status = response . get ( '<STR_LIT>' , status ) <EOL> sys . stdout . write ( '<STR_LIT>' % status ) <EOL> for key , value in response . get ( '<STR_LIT>' , ( ) ) : <EOL> key = '<STR_LIT:->' . join ( key . split ( ) ) <EOL> value = value . replace ( '<STR_LIT:\r>' , '<STR_LIT:U+0020>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) <EOL> sys . stdout . write ( '<STR_LIT>' % ( key , value ) ) <EOL> sys . stdout . write ( '<STR_LIT:\r\n>' ) <EOL> body = response . get ( '<STR_LIT:body>' ) <EOL> if body : <EOL> sys . stdout . write ( body ) <EOL> logs = response . get ( '<STR_LIT>' ) <EOL> if logs : <EOL> for timestamp_usec , severity , message in logs : <EOL> logging . log ( severity * <NUM_LIT:10> + <NUM_LIT:10> , '<STR_LIT>' , <EOL> time . ctime ( timestamp_usec * <NUM_LIT> ) , message ) <EOL> return error <EOL> class LoggingStream ( object ) : <EOL> """<STR_LIT>""" <EOL> def write ( self , message ) : <EOL> logging . getLogger ( ) . _log ( logging . ERROR , message , ( ) ) <EOL> def writelines ( self , lines ) : <EOL> for line in lines : <EOL> logging . getLogger ( ) . _log ( logging . ERROR , line , ( ) ) <EOL> def __getattr__ ( self , key ) : <EOL> return getattr ( sys . __stderr__ , key ) <EOL> def ExecuteCGI ( config , <EOL> root_path , <EOL> handler_path , <EOL> cgi_path , <EOL> env , <EOL> infile , <EOL> outfile , <EOL> module_dict , <EOL> exec_script = ExecuteOrImportScript , <EOL> exec_py27_handler = ExecutePy27Handler ) : <EOL> """<STR_LIT>""" <EOL> if handler_path == '<STR_LIT>' : <EOL> from google . appengine . ext . go import execute_go_cgi <EOL> return execute_go_cgi ( root_path , handler_path , cgi_path , <EOL> env , infile , outfile ) <EOL> old_module_dict = sys . modules . copy ( ) <EOL> old_builtin = __builtin__ . __dict__ . copy ( ) <EOL> old_argv = sys . argv <EOL> old_stdin = sys . stdin <EOL> old_stdout = sys . stdout <EOL> old_stderr = sys . stderr <EOL> old_env = os . environ . copy ( ) <EOL> old_cwd = os . getcwd ( ) <EOL> old_file_type = types . FileType <EOL> old_path = sys . path [ : ] <EOL> reset_modules = False <EOL> app_log_handler = None <EOL> try : <EOL> ClearAllButEncodingsModules ( sys . modules ) <EOL> sys . modules . update ( module_dict ) <EOL> UpdateNotSharedModulesInParents ( module_dict ) <EOL> sys . argv = [ cgi_path ] <EOL> sys . stdin = cStringIO . StringIO ( infile . getvalue ( ) ) <EOL> sys . stdout = outfile <EOL> sys . stderr = LoggingStream ( ) <EOL> logservice . _global_buffer = logservice . LogsBuffer ( ) <EOL> app_log_handler = app_logging . AppLogsHandler ( <EOL> logservice . logs_buffer ( ) . stream ( ) ) <EOL> logging . getLogger ( ) . addHandler ( app_log_handler ) <EOL> os . environ [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> os . environ [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> os . environ [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> os . environ . update ( env ) <EOL> cgi_dir = os . path . normpath ( os . path . dirname ( cgi_path ) ) <EOL> root_path = os . path . normpath ( os . path . abspath ( root_path ) ) <EOL> if ( cgi_dir . startswith ( root_path + os . sep ) and <EOL> not ( config and config . runtime == '<STR_LIT>' ) ) : <EOL> os . chdir ( cgi_dir ) <EOL> else : <EOL> os . chdir ( root_path ) <EOL> dist . fix_paths ( root_path , SDK_ROOT ) <EOL> hook = HardenedModulesHook ( config , sys . modules , root_path ) <EOL> sys . meta_path = [ finder for finder in sys . meta_path <EOL> if not isinstance ( finder , HardenedModulesHook ) ] <EOL> sys . meta_path . insert ( <NUM_LIT:0> , hook ) <EOL> if hasattr ( sys , '<STR_LIT>' ) : <EOL> sys . path_importer_cache . clear ( ) <EOL> __builtin__ . file = FakeFile <EOL> __builtin__ . open = FakeFile <EOL> types . FileType = FakeFile <EOL> if not ( config and config . runtime == '<STR_LIT>' ) : <EOL> __builtin__ . buffer = NotImplementedFakeClass <EOL> sys . modules [ '<STR_LIT>' ] = __builtin__ <EOL> logging . debug ( '<STR_LIT>' , repr ( env ) ) <EOL> try : <EOL> if handler_path and config and config . runtime == '<STR_LIT>' : <EOL> reset_modules = exec_py27_handler ( config , handler_path , cgi_path , hook ) <EOL> else : <EOL> reset_modules = exec_script ( config , handler_path , cgi_path , hook ) <EOL> except SystemExit , e : <EOL> logging . debug ( '<STR_LIT>' , e ) <EOL> except : <EOL> reset_modules = True <EOL> raise <EOL> finally : <EOL> sys . path_importer_cache . clear ( ) <EOL> _ClearTemplateCache ( sys . modules ) <EOL> module_dict . update ( sys . modules ) <EOL> ClearAllButEncodingsModules ( sys . modules ) <EOL> sys . modules . update ( old_module_dict ) <EOL> UpdateNotSharedModulesInParents ( old_module_dict ) <EOL> __builtin__ . __dict__ . update ( old_builtin ) <EOL> sys . argv = old_argv <EOL> sys . stdin = old_stdin <EOL> sys . stdout = old_stdout <EOL> logservice_stub . _flush_logs_buffer ( ) <EOL> sys . stderr = old_stderr <EOL> logging . getLogger ( ) . removeHandler ( app_log_handler ) <EOL> sys . path [ : ] = old_path <EOL> os . environ . clear ( ) <EOL> os . environ . update ( old_env ) <EOL> os . chdir ( old_cwd ) <EOL> types . FileType = old_file_type <EOL> class CGIDispatcher ( URLDispatcher ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> config , <EOL> module_dict , <EOL> root_path , <EOL> path_adjuster , <EOL> setup_env = SetupEnvironment , <EOL> exec_cgi = ExecuteCGI ) : <EOL> """<STR_LIT>""" <EOL> self . _config = config <EOL> self . _module_dict = module_dict <EOL> self . _root_path = root_path <EOL> self . _path_adjuster = path_adjuster <EOL> self . _setup_env = setup_env <EOL> self . _exec_cgi = exec_cgi <EOL> def Dispatch ( self , <EOL> request , <EOL> outfile , <EOL> base_env_dict = None ) : <EOL> """<STR_LIT>""" <EOL> request_size = GetRequestSize ( request , base_env_dict , outfile ) <EOL> if request_size is None : <EOL> return <EOL> memory_file = cStringIO . StringIO ( ) <EOL> CopyStreamPart ( request . infile , memory_file , request_size ) <EOL> memory_file . seek ( <NUM_LIT:0> ) <EOL> before_level = logging . root . level <EOL> try : <EOL> env = { } <EOL> if base_env_dict : <EOL> env . update ( base_env_dict ) <EOL> cgi_path = self . _path_adjuster . AdjustPath ( request . path ) <EOL> env . update ( self . _setup_env ( cgi_path , <EOL> request . relative_url , <EOL> request . headers , <EOL> memory_file ) ) <EOL> self . _exec_cgi ( self . _config , <EOL> self . _root_path , <EOL> request . path , <EOL> cgi_path , <EOL> env , <EOL> memory_file , <EOL> outfile , <EOL> self . _module_dict ) <EOL> finally : <EOL> logging . root . level = before_level <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> class LocalCGIDispatcher ( CGIDispatcher ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config , module_dict , path_adjuster , cgi_func ) : <EOL> """<STR_LIT>""" <EOL> self . _cgi_func = cgi_func <EOL> def curried_exec_script ( * args , ** kwargs ) : <EOL> cgi_func ( ) <EOL> return False <EOL> def curried_exec_cgi ( * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = curried_exec_script <EOL> return ExecuteCGI ( * args , ** kwargs ) <EOL> CGIDispatcher . __init__ ( self , <EOL> config , <EOL> module_dict , <EOL> '<STR_LIT>' , <EOL> path_adjuster , <EOL> exec_cgi = curried_exec_cgi ) <EOL> def Dispatch ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _module_dict . update ( sys . modules ) <EOL> CGIDispatcher . Dispatch ( self , * args , ** kwargs ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % self . _cgi_func <EOL> class PathAdjuster ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , root_path ) : <EOL> """<STR_LIT>""" <EOL> self . _root_path = os . path . abspath ( root_path ) <EOL> def AdjustPath ( self , path ) : <EOL> """<STR_LIT>""" <EOL> if path . startswith ( PYTHON_LIB_VAR ) : <EOL> path = os . path . join ( SDK_ROOT , path [ len ( PYTHON_LIB_VAR ) + <NUM_LIT:1> : ] ) <EOL> else : <EOL> path = os . path . join ( self . _root_path , path ) <EOL> return path <EOL> class StaticFileConfigMatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> url_map_list , <EOL> path_adjuster , <EOL> default_expiration ) : <EOL> """<STR_LIT>""" <EOL> if default_expiration is not None : <EOL> self . _default_expiration = appinfo . ParseExpiration ( default_expiration ) <EOL> else : <EOL> self . _default_expiration = None <EOL> self . _patterns = [ ] <EOL> if url_map_list : <EOL> for entry in url_map_list : <EOL> handler_type = entry . GetHandlerType ( ) <EOL> if handler_type not in ( appinfo . STATIC_FILES , appinfo . STATIC_DIR ) : <EOL> continue <EOL> if handler_type == appinfo . STATIC_FILES : <EOL> regex = entry . upload + '<STR_LIT:$>' <EOL> else : <EOL> path = entry . static_dir <EOL> if path [ - <NUM_LIT:1> ] == '<STR_LIT:/>' : <EOL> path = path [ : - <NUM_LIT:1> ] <EOL> regex = re . escape ( path + os . path . sep ) + r'<STR_LIT>' <EOL> try : <EOL> path_re = re . compile ( regex ) <EOL> except re . error , e : <EOL> raise InvalidAppConfigError ( '<STR_LIT>' % <EOL> ( regex , e ) ) <EOL> if self . _default_expiration is None : <EOL> expiration = <NUM_LIT:0> <EOL> elif entry . expiration is None : <EOL> expiration = self . _default_expiration <EOL> else : <EOL> expiration = appinfo . ParseExpiration ( entry . expiration ) <EOL> self . _patterns . append ( ( path_re , entry . mime_type , expiration ) ) <EOL> def IsStaticFile ( self , path ) : <EOL> """<STR_LIT>""" <EOL> for ( path_re , _ , _ ) in self . _patterns : <EOL> if path_re . match ( path ) : <EOL> return True <EOL> return False <EOL> def GetMimeType ( self , path ) : <EOL> """<STR_LIT>""" <EOL> for ( path_re , mimetype , unused_expiration ) in self . _patterns : <EOL> if mimetype is not None : <EOL> the_match = path_re . match ( path ) <EOL> if the_match : <EOL> return mimetype <EOL> unused_filename , extension = os . path . splitext ( path ) <EOL> return mimetypes . types_map . get ( extension , '<STR_LIT>' ) <EOL> def GetExpiration ( self , path ) : <EOL> """<STR_LIT>""" <EOL> for ( path_re , unused_mimetype , expiration ) in self . _patterns : <EOL> the_match = path_re . match ( path ) <EOL> if the_match : <EOL> return expiration <EOL> return self . _default_expiration or <NUM_LIT:0> <EOL> def ReadDataFile ( data_path , openfile = file ) : <EOL> """<STR_LIT>""" <EOL> status = httplib . INTERNAL_SERVER_ERROR <EOL> data = "<STR_LIT>" <EOL> try : <EOL> data_file = openfile ( data_path , '<STR_LIT:rb>' ) <EOL> try : <EOL> data = data_file . read ( ) <EOL> finally : <EOL> data_file . close ( ) <EOL> status = httplib . OK <EOL> except ( OSError , IOError ) , e : <EOL> logging . error ( '<STR_LIT>' , data_path , e ) <EOL> if e . errno in FILE_MISSING_EXCEPTIONS : <EOL> status = httplib . NOT_FOUND <EOL> else : <EOL> status = httplib . FORBIDDEN <EOL> return status , data <EOL> class FileDispatcher ( URLDispatcher ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> config , <EOL> path_adjuster , <EOL> static_file_config_matcher , <EOL> read_data_file = ReadDataFile ) : <EOL> """<STR_LIT>""" <EOL> self . _config = config <EOL> self . _path_adjuster = path_adjuster <EOL> self . _static_file_config_matcher = static_file_config_matcher <EOL> self . _read_data_file = read_data_file <EOL> def Dispatch ( self , <EOL> request , <EOL> outfile , <EOL> base_env_dict = None ) : <EOL> """<STR_LIT>""" <EOL> full_path = self . _path_adjuster . AdjustPath ( request . path ) <EOL> status , data = self . _read_data_file ( full_path ) <EOL> content_type = self . _static_file_config_matcher . GetMimeType ( request . path ) <EOL> static_file = self . _static_file_config_matcher . IsStaticFile ( request . path ) <EOL> expiration = self . _static_file_config_matcher . GetExpiration ( request . path ) <EOL> current_etag = self . CreateEtag ( data ) <EOL> if_match_etag = request . headers . get ( '<STR_LIT>' , None ) <EOL> if_none_match_etag = request . headers . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) <EOL> if if_match_etag and not self . _CheckETagMatches ( if_match_etag . split ( '<STR_LIT:U+002C>' ) , <EOL> current_etag , <EOL> False ) : <EOL> outfile . write ( '<STR_LIT>' % httplib . PRECONDITION_FAILED ) <EOL> outfile . write ( '<STR_LIT>' % current_etag ) <EOL> outfile . write ( '<STR_LIT:\r\n>' ) <EOL> elif self . _CheckETagMatches ( if_none_match_etag , current_etag , True ) : <EOL> outfile . write ( '<STR_LIT>' % httplib . NOT_MODIFIED ) <EOL> outfile . write ( '<STR_LIT>' % current_etag ) <EOL> outfile . write ( '<STR_LIT:\r\n>' ) <EOL> else : <EOL> outfile . write ( '<STR_LIT>' % status ) <EOL> outfile . write ( '<STR_LIT>' % content_type ) <EOL> if expiration : <EOL> outfile . write ( '<STR_LIT>' <EOL> % email . Utils . formatdate ( time . time ( ) + expiration , <EOL> usegmt = True ) ) <EOL> outfile . write ( '<STR_LIT>' % expiration ) <EOL> if static_file : <EOL> outfile . write ( '<STR_LIT>' % current_etag ) <EOL> outfile . write ( '<STR_LIT:\r\n>' ) <EOL> outfile . write ( data ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> @ staticmethod <EOL> def CreateEtag ( data ) : <EOL> """<STR_LIT>""" <EOL> data_crc = zlib . crc32 ( data ) <EOL> return base64 . b64encode ( str ( data_crc ) ) <EOL> @ staticmethod <EOL> def _CheckETagMatches ( supplied_etags , current_etag , allow_weak_match ) : <EOL> """<STR_LIT>""" <EOL> for tag in supplied_etags : <EOL> if allow_weak_match and tag . startswith ( '<STR_LIT>' ) : <EOL> tag = tag [ <NUM_LIT:2> : ] <EOL> tag_data = tag . strip ( '<STR_LIT:">' ) <EOL> if tag_data == '<STR_LIT:*>' or tag_data == current_etag : <EOL> return True <EOL> return False <EOL> _IGNORE_RESPONSE_HEADERS = frozenset ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:date>' , blobstore . BLOB_KEY_HEADER <EOL> ] ) <EOL> class AppServerResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:body>' , <EOL> '<STR_LIT>' ] <EOL> def __init__ ( self , response_file = None , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> self . status_code = <NUM_LIT:200> <EOL> self . status_message = '<STR_LIT>' <EOL> self . large_response = False <EOL> if response_file : <EOL> self . SetResponse ( response_file ) <EOL> else : <EOL> self . headers = mimetools . Message ( cStringIO . StringIO ( ) ) <EOL> self . body = None <EOL> for name , value in kwds . iteritems ( ) : <EOL> setattr ( self , name , value ) <EOL> def SetResponse ( self , response_file ) : <EOL> """<STR_LIT>""" <EOL> self . headers = mimetools . Message ( response_file ) <EOL> self . body = response_file <EOL> @ property <EOL> def header_data ( self ) : <EOL> """<STR_LIT>""" <EOL> header_list = [ ] <EOL> for header in self . headers . headers : <EOL> header = header . rstrip ( '<STR_LIT>' ) <EOL> header_list . append ( header ) <EOL> return '<STR_LIT:\r\n>' . join ( header_list ) + '<STR_LIT:\r\n>' <EOL> def IgnoreHeadersRewriter ( response ) : <EOL> """<STR_LIT>""" <EOL> for h in _IGNORE_RESPONSE_HEADERS : <EOL> if h in response . headers : <EOL> del response . headers [ h ] <EOL> def ParseStatusRewriter ( response ) : <EOL> """<STR_LIT>""" <EOL> location_value = response . headers . getheader ( '<STR_LIT:location>' ) <EOL> status_value = response . headers . getheader ( '<STR_LIT:status>' ) <EOL> if status_value : <EOL> response_status = status_value <EOL> del response . headers [ '<STR_LIT:status>' ] <EOL> elif location_value : <EOL> response_status = '<STR_LIT>' % httplib . FOUND <EOL> else : <EOL> return response <EOL> status_parts = response_status . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> response . status_code , response . status_message = ( status_parts + [ '<STR_LIT>' ] ) [ : <NUM_LIT:2> ] <EOL> try : <EOL> response . status_code = int ( response . status_code ) <EOL> except ValueError : <EOL> response . status_code = <NUM_LIT> <EOL> response . body = cStringIO . StringIO ( <EOL> '<STR_LIT>' ) <EOL> def CacheRewriter ( response ) : <EOL> """<STR_LIT>""" <EOL> if response . status_code != httplib . NOT_MODIFIED : <EOL> if not '<STR_LIT>' in response . headers : <EOL> response . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if not '<STR_LIT>' in response . headers : <EOL> response . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def ContentLengthRewriter ( response ) : <EOL> """<STR_LIT>""" <EOL> if response . status_code != httplib . NOT_MODIFIED : <EOL> current_position = response . body . tell ( ) <EOL> response . body . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> response . headers [ '<STR_LIT>' ] = str ( response . body . tell ( ) - <EOL> current_position ) <EOL> response . body . seek ( current_position ) <EOL> elif '<STR_LIT>' in response . headers : <EOL> del response . headers [ '<STR_LIT>' ] <EOL> def CreateResponseRewritersChain ( ) : <EOL> """<STR_LIT>""" <EOL> rewriters = [ dev_appserver_blobstore . DownloadRewriter , <EOL> IgnoreHeadersRewriter , <EOL> ParseStatusRewriter , <EOL> CacheRewriter , <EOL> ContentLengthRewriter , <EOL> ] <EOL> return rewriters <EOL> def RewriteResponse ( response_file , <EOL> response_rewriters = None , <EOL> request_headers = None ) : <EOL> """<STR_LIT>""" <EOL> if response_rewriters is None : <EOL> response_rewriters = CreateResponseRewritersChain ( ) <EOL> response = AppServerResponse ( response_file ) <EOL> for response_rewriter in response_rewriters : <EOL> if response_rewriter . func_code . co_argcount == <NUM_LIT:1> : <EOL> response_rewriter ( response ) <EOL> else : <EOL> response_rewriter ( response , request_headers ) <EOL> return response <EOL> class ModuleManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , modules ) : <EOL> """<STR_LIT>""" <EOL> self . _modules = modules <EOL> self . _default_modules = self . _modules . copy ( ) <EOL> self . _save_path_hooks = sys . path_hooks [ : ] <EOL> self . _modification_times = { } <EOL> @ staticmethod <EOL> def GetModuleFile ( module , is_file = os . path . isfile ) : <EOL> """<STR_LIT>""" <EOL> module_file = getattr ( module , '<STR_LIT>' , None ) <EOL> if module_file is None : <EOL> return None <EOL> source_file = module_file [ : module_file . rfind ( '<STR_LIT>' ) + <NUM_LIT:2> ] <EOL> if is_file ( source_file ) : <EOL> return source_file <EOL> return module . __file__ <EOL> def AreModuleFilesModified ( self ) : <EOL> """<STR_LIT>""" <EOL> for name , ( mtime , fname ) in self . _modification_times . iteritems ( ) : <EOL> if name not in self . _modules : <EOL> continue <EOL> module = self . _modules [ name ] <EOL> if not os . path . isfile ( fname ) : <EOL> return True <EOL> if mtime != os . path . getmtime ( fname ) : <EOL> return True <EOL> return False <EOL> def UpdateModuleFileModificationTimes ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _modification_times . clear ( ) <EOL> for name , module in self . _modules . items ( ) : <EOL> if not isinstance ( module , types . ModuleType ) : <EOL> continue <EOL> module_file = self . GetModuleFile ( module ) <EOL> if not module_file : <EOL> continue <EOL> try : <EOL> self . _modification_times [ name ] = ( os . path . getmtime ( module_file ) , <EOL> module_file ) <EOL> except OSError , e : <EOL> if e . errno not in FILE_MISSING_EXCEPTIONS : <EOL> raise e <EOL> def ResetModules ( self ) : <EOL> """<STR_LIT>""" <EOL> lib_config . _default_registry . reset ( ) <EOL> self . _modules . clear ( ) <EOL> self . _modules . update ( self . _default_modules ) <EOL> sys . path_hooks [ : ] = self . _save_path_hooks <EOL> sys . meta_path = [ ] <EOL> apiproxy_stub_map . apiproxy . GetPreCallHooks ( ) . Clear ( ) <EOL> apiproxy_stub_map . apiproxy . GetPostCallHooks ( ) . Clear ( ) <EOL> def GetVersionObject ( isfile = os . path . isfile , open_fn = open ) : <EOL> """<STR_LIT>""" <EOL> version_filename = os . path . join ( os . path . dirname ( google . appengine . __file__ ) , <EOL> VERSION_FILE ) <EOL> if not isfile ( version_filename ) : <EOL> logging . error ( '<STR_LIT>' , version_filename ) <EOL> return None <EOL> version_fh = open_fn ( version_filename , '<STR_LIT:r>' ) <EOL> try : <EOL> version = yaml . safe_load ( version_fh ) <EOL> finally : <EOL> version_fh . close ( ) <EOL> return version <EOL> def _ClearTemplateCache ( module_dict = sys . modules ) : <EOL> """<STR_LIT>""" <EOL> template_module = module_dict . get ( '<STR_LIT>' ) <EOL> if template_module is not None : <EOL> template_module . template_cache . clear ( ) <EOL> def CreateRequestHandler ( root_path , <EOL> login_url , <EOL> require_indexes = False , <EOL> static_caching = True , <EOL> default_partition = None , <EOL> persist_logs = False , <EOL> secret_hash = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> application_module_dict = SetupSharedModules ( sys . modules ) <EOL> if require_indexes : <EOL> index_yaml_updater = None <EOL> else : <EOL> index_yaml_updater = dev_appserver_index . IndexYamlUpdater ( root_path ) <EOL> application_config_cache = AppConfigCache ( ) <EOL> class DevAppServerRequestHandler ( BaseHTTPServer . BaseHTTPRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> server_version = '<STR_LIT>' <EOL> module_dict = application_module_dict <EOL> module_manager = ModuleManager ( application_module_dict ) <EOL> config_cache = application_config_cache <EOL> rewriter_chain = CreateResponseRewritersChain ( ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . _log_record_writer = logservice_stub . RequestLogWriter ( persist_logs ) <EOL> BaseHTTPServer . BaseHTTPRequestHandler . __init__ ( self , * args , ** kwargs ) <EOL> def version_string ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . server_version <EOL> def do_GET ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _HasNoBody ( '<STR_LIT:GET>' ) : <EOL> self . _HandleRequest ( ) <EOL> def do_POST ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _HandleRequest ( ) <EOL> def do_PUT ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _HandleRequest ( ) <EOL> def do_HEAD ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _HasNoBody ( '<STR_LIT>' ) : <EOL> self . _HandleRequest ( ) <EOL> def do_OPTIONS ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _HandleRequest ( ) <EOL> def do_DELETE ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _HandleRequest ( ) <EOL> def do_TRACE ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _HasNoBody ( '<STR_LIT>' ) : <EOL> self . _HandleRequest ( ) <EOL> def _HasNoBody ( self , method ) : <EOL> """<STR_LIT>""" <EOL> content_length = int ( self . headers . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> if content_length : <EOL> body = self . rfile . read ( content_length ) <EOL> logging . warning ( '<STR_LIT>' , method , body ) <EOL> self . send_response ( httplib . BAD_REQUEST ) <EOL> return False <EOL> return True <EOL> def _Dispatch ( self , dispatcher , socket_infile , outfile , env_dict ) : <EOL> """<STR_LIT>""" <EOL> request_descriptor , request_file_name = tempfile . mkstemp ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> try : <EOL> request_file = os . fdopen ( request_descriptor , '<STR_LIT:wb>' ) <EOL> try : <EOL> CopyStreamPart ( self . rfile , <EOL> request_file , <EOL> int ( self . headers . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) ) <EOL> finally : <EOL> request_file . close ( ) <EOL> request_file = open ( request_file_name , '<STR_LIT:rb>' ) <EOL> try : <EOL> app_server_request = AppServerRequest ( self . path , <EOL> None , <EOL> self . headers , <EOL> request_file , <EOL> secret_hash ) <EOL> dispatcher . Dispatch ( app_server_request , <EOL> outfile , <EOL> base_env_dict = env_dict ) <EOL> finally : <EOL> request_file . close ( ) <EOL> finally : <EOL> try : <EOL> os . remove ( request_file_name ) <EOL> except OSError , err : <EOL> if err . errno != errno . ENOENT : <EOL> raise <EOL> def _HandleRequest ( self ) : <EOL> """<STR_LIT>""" <EOL> host_name = self . headers . get ( '<STR_LIT:host>' ) or self . server . server_name <EOL> server_name = host_name . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> env_dict = { <EOL> '<STR_LIT>' : self . command , <EOL> '<STR_LIT>' : self . headers . get ( "<STR_LIT>" , self . client_address [ <NUM_LIT:0> ] ) , <EOL> '<STR_LIT>' : self . server_version , <EOL> '<STR_LIT>' : server_name , <EOL> '<STR_LIT>' : self . protocol_version , <EOL> '<STR_LIT>' : str ( self . server . server_port ) , <EOL> } <EOL> full_url = GetFullURL ( server_name , self . server . server_port , self . path ) <EOL> if len ( full_url ) > MAX_URL_LENGTH : <EOL> msg = '<STR_LIT>' % full_url <EOL> logging . error ( msg ) <EOL> self . send_response ( httplib . REQUEST_URI_TOO_LONG , msg ) <EOL> return <EOL> tbhandler = cgitb . Hook ( file = self . wfile ) . handle <EOL> try : <EOL> config , explicit_matcher , from_cache = LoadAppConfig ( <EOL> root_path , self . module_dict , cache = self . config_cache , <EOL> static_caching = static_caching , default_partition = default_partition ) <EOL> if not from_cache or self . module_manager . AreModuleFilesModified ( ) : <EOL> self . module_manager . ResetModules ( ) <EOL> implicit_matcher = CreateImplicitMatcher ( config , <EOL> self . module_dict , <EOL> root_path , <EOL> login_url ) <EOL> if config . api_version != API_VERSION : <EOL> logging . error ( <EOL> "<STR_LIT>" , <EOL> config . api_version , API_VERSION ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> ( exclude , service_match ) = ReservedPathFilter ( <EOL> config . inbound_services ) . ExcludePath ( self . path ) <EOL> if exclude : <EOL> logging . warning ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . path , service_match ) ) <EOL> self . send_response ( httplib . NOT_FOUND ) <EOL> return <EOL> if config . runtime == '<STR_LIT>' : <EOL> from google . appengine . ext import go <EOL> go . APP_CONFIG = config <EOL> version = GetVersionObject ( ) <EOL> env_dict [ '<STR_LIT>' ] = version [ '<STR_LIT>' ] <EOL> env_dict [ '<STR_LIT>' ] = config . version + "<STR_LIT>" <EOL> env_dict [ '<STR_LIT>' ] = config . application <EOL> env_dict [ '<STR_LIT>' ] = config . runtime <EOL> if config . runtime == '<STR_LIT>' and config . threadsafe : <EOL> env_dict [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> global _request_id <EOL> request_id_hash = _generate_request_id_hash ( ) <EOL> env_dict [ '<STR_LIT>' ] = request_id_hash <EOL> os . environ [ '<STR_LIT>' ] = request_id_hash <EOL> multiprocess . GlobalProcess ( ) . UpdateEnv ( env_dict ) <EOL> cookies = '<STR_LIT:U+002CU+0020>' . join ( self . headers . getheaders ( '<STR_LIT>' ) ) <EOL> email_addr , user_id , admin , valid_cookie = dev_appserver_login . GetUserInfo ( cookies ) <EOL> self . _log_record_writer . write_request_info ( <EOL> ip = env_dict [ '<STR_LIT>' ] , <EOL> app_id = env_dict [ '<STR_LIT>' ] , <EOL> version_id = env_dict [ '<STR_LIT>' ] , <EOL> nickname = email_addr . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] , <EOL> user_agent = self . headers . get ( '<STR_LIT>' ) , <EOL> host = host_name ) <EOL> dispatcher = MatcherDispatcher ( config , login_url , <EOL> [ implicit_matcher , explicit_matcher ] ) <EOL> dev_appserver_index . SetupIndexes ( config . application , root_path ) <EOL> if multiprocess . GlobalProcess ( ) . HandleRequest ( self ) : <EOL> return <EOL> outfile = cStringIO . StringIO ( ) <EOL> try : <EOL> self . _Dispatch ( dispatcher , self . rfile , outfile , env_dict ) <EOL> finally : <EOL> self . module_manager . UpdateModuleFileModificationTimes ( ) <EOL> outfile . flush ( ) <EOL> outfile . seek ( <NUM_LIT:0> ) <EOL> response = RewriteResponse ( outfile , self . rewriter_chain , self . headers ) <EOL> if not response . large_response : <EOL> position = response . body . tell ( ) <EOL> response . body . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> end = response . body . tell ( ) <EOL> response . body . seek ( position ) <EOL> runtime_response_size = end - position <EOL> if runtime_response_size > MAX_RUNTIME_RESPONSE_SIZE : <EOL> logging . error ( '<STR_LIT>' , <EOL> runtime_response_size , MAX_RUNTIME_RESPONSE_SIZE ) <EOL> response . status_code = <NUM_LIT> <EOL> response . status_message = '<STR_LIT>' <EOL> if '<STR_LIT>' in response . headers : <EOL> del response . headers [ '<STR_LIT>' ] <EOL> new_response = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( runtime_response_size , <EOL> MAX_RUNTIME_RESPONSE_SIZE ) ) <EOL> response . headers [ '<STR_LIT>' ] = str ( len ( new_response ) ) <EOL> response . body = cStringIO . StringIO ( new_response ) <EOL> multiprocess . GlobalProcess ( ) . RequestComplete ( self , response ) <EOL> except yaml_errors . EventListenerError , e : <EOL> title = '<STR_LIT>' <EOL> msg = '<STR_LIT>' % ( title , str ( e ) ) <EOL> logging . error ( msg ) <EOL> self . send_response ( httplib . INTERNAL_SERVER_ERROR , title ) <EOL> self . wfile . write ( '<STR_LIT>' ) <EOL> self . wfile . write ( '<STR_LIT>' % cgi . escape ( msg ) ) <EOL> except KeyboardInterrupt , e : <EOL> logging . info ( '<STR_LIT>' ) <EOL> self . server . stop_serving_forever ( ) <EOL> except CompileError , e : <EOL> msg = '<STR_LIT>' + e . text + '<STR_LIT:\n>' <EOL> logging . error ( msg ) <EOL> self . send_response ( httplib . INTERNAL_SERVER_ERROR , '<STR_LIT>' ) <EOL> self . wfile . write ( '<STR_LIT>' ) <EOL> self . wfile . write ( msg ) <EOL> except : <EOL> msg = '<STR_LIT>' <EOL> logging . exception ( msg ) <EOL> self . send_response ( httplib . INTERNAL_SERVER_ERROR , msg ) <EOL> tbhandler ( ) <EOL> else : <EOL> try : <EOL> self . send_response ( response . status_code , response . status_message ) <EOL> self . wfile . write ( response . header_data ) <EOL> self . wfile . write ( '<STR_LIT:\r\n>' ) <EOL> if self . command != '<STR_LIT>' : <EOL> shutil . copyfileobj ( response . body , self . wfile , COPY_BLOCK_SIZE ) <EOL> elif response . body : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> except ( IOError , OSError ) , e : <EOL> if e . errno not in [ errno . EPIPE , os_compat . WSAECONNABORTED ] : <EOL> raise e <EOL> except socket . error , e : <EOL> logging . error ( "<STR_LIT>" % str ( e ) ) <EOL> self . server . stop_serving_forever ( ) <EOL> def log_error ( self , format , * args ) : <EOL> """<STR_LIT>""" <EOL> logging . error ( format , * args ) <EOL> def log_message ( self , format , * args ) : <EOL> """<STR_LIT>""" <EOL> logging . info ( format , * args ) <EOL> def log_request ( self , code = '<STR_LIT:->' , size = '<STR_LIT:->' ) : <EOL> """<STR_LIT>""" <EOL> BaseHTTPServer . BaseHTTPRequestHandler . log_request ( self , code , size ) <EOL> if code == '<STR_LIT:->' : <EOL> code = <NUM_LIT:0> <EOL> if size == '<STR_LIT:->' : <EOL> size = <NUM_LIT:0> <EOL> self . _log_record_writer . write ( self . command , self . path , code , size , <EOL> self . request_version ) <EOL> return DevAppServerRequestHandler <EOL> def ReadAppConfig ( appinfo_path , parse_app_config = appinfo_includes . Parse ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> appinfo_file = file ( appinfo_path , '<STR_LIT:r>' ) <EOL> except IOError , unused_e : <EOL> raise InvalidAppConfigError ( <EOL> '<STR_LIT>' % appinfo_path ) <EOL> try : <EOL> return parse_app_config ( appinfo_file ) <EOL> finally : <EOL> appinfo_file . close ( ) <EOL> def CreateURLMatcherFromMaps ( config , <EOL> root_path , <EOL> url_map_list , <EOL> module_dict , <EOL> default_expiration , <EOL> create_url_matcher = URLMatcher , <EOL> create_cgi_dispatcher = CGIDispatcher , <EOL> create_file_dispatcher = FileDispatcher , <EOL> create_path_adjuster = PathAdjuster , <EOL> normpath = os . path . normpath ) : <EOL> """<STR_LIT>""" <EOL> if config and config . handlers and not url_map_list : <EOL> url_map_list = config . handlers <EOL> url_matcher = create_url_matcher ( ) <EOL> path_adjuster = create_path_adjuster ( root_path ) <EOL> cgi_dispatcher = create_cgi_dispatcher ( config , module_dict , <EOL> root_path , path_adjuster ) <EOL> static_file_config_matcher = StaticFileConfigMatcher ( url_map_list , <EOL> path_adjuster , <EOL> default_expiration ) <EOL> file_dispatcher = create_file_dispatcher ( config , path_adjuster , <EOL> static_file_config_matcher ) <EOL> FakeFile . SetStaticFileConfigMatcher ( static_file_config_matcher ) <EOL> for url_map in url_map_list : <EOL> admin_only = url_map . login == appinfo . LOGIN_ADMIN <EOL> requires_login = url_map . login == appinfo . LOGIN_REQUIRED or admin_only <EOL> auth_fail_action = url_map . auth_fail_action <EOL> handler_type = url_map . GetHandlerType ( ) <EOL> if handler_type == appinfo . HANDLER_SCRIPT : <EOL> dispatcher = cgi_dispatcher <EOL> elif handler_type in ( appinfo . STATIC_FILES , appinfo . STATIC_DIR ) : <EOL> dispatcher = file_dispatcher <EOL> else : <EOL> raise InvalidAppConfigError ( '<STR_LIT>' % handler_type ) <EOL> regex = url_map . url <EOL> path = url_map . GetHandler ( ) <EOL> if handler_type == appinfo . STATIC_DIR : <EOL> if regex [ - <NUM_LIT:1> ] == r'<STR_LIT:/>' : <EOL> regex = regex [ : - <NUM_LIT:1> ] <EOL> if path [ - <NUM_LIT:1> ] == os . path . sep : <EOL> path = path [ : - <NUM_LIT:1> ] <EOL> regex = '<STR_LIT:/>' . join ( ( re . escape ( regex ) , '<STR_LIT>' ) ) <EOL> if os . path . sep == '<STR_LIT:\\>' : <EOL> backref = r'<STR_LIT>' <EOL> else : <EOL> backref = r'<STR_LIT>' <EOL> path = ( normpath ( path ) . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) + <EOL> os . path . sep + backref ) <EOL> url_matcher . AddURL ( regex , <EOL> dispatcher , <EOL> path , <EOL> requires_login , admin_only , auth_fail_action ) <EOL> return url_matcher <EOL> class AppConfigCache ( object ) : <EOL> """<STR_LIT>""" <EOL> path = None <EOL> mtime = None <EOL> config = None <EOL> matcher = None <EOL> def LoadAppConfig ( root_path , <EOL> module_dict , <EOL> cache = None , <EOL> static_caching = True , <EOL> read_app_config = ReadAppConfig , <EOL> create_matcher = CreateURLMatcherFromMaps , <EOL> default_partition = None ) : <EOL> """<STR_LIT>""" <EOL> for appinfo_path in [ os . path . join ( root_path , '<STR_LIT>' ) , <EOL> os . path . join ( root_path , '<STR_LIT>' ) ] : <EOL> if os . path . isfile ( appinfo_path ) : <EOL> if cache is not None : <EOL> mtime = os . path . getmtime ( appinfo_path ) <EOL> if cache . path == appinfo_path and cache . mtime == mtime : <EOL> return ( cache . config , cache . matcher , True ) <EOL> cache . config = cache . matcher = cache . path = None <EOL> cache . mtime = mtime <EOL> try : <EOL> config = read_app_config ( appinfo_path , appinfo_includes . Parse ) <EOL> multiprocess . GlobalProcess ( ) . NewAppInfo ( config ) <EOL> if static_caching : <EOL> if config . default_expiration : <EOL> default_expiration = config . default_expiration <EOL> else : <EOL> default_expiration = '<STR_LIT:0>' <EOL> else : <EOL> default_expiration = None <EOL> matcher = create_matcher ( config , <EOL> root_path , <EOL> config . handlers , <EOL> module_dict , <EOL> default_expiration ) <EOL> FakeFile . SetSkippedFiles ( config . skip_files ) <EOL> if cache is not None : <EOL> cache . path = appinfo_path <EOL> cache . config = config <EOL> cache . matcher = matcher <EOL> return ( config , matcher , False ) <EOL> except gexcept . AbstractMethod : <EOL> pass <EOL> raise AppConfigNotFoundError <EOL> class ReservedPathFilter ( ) : <EOL> """<STR_LIT>""" <EOL> reserved_paths = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def __init__ ( self , inbound_services ) : <EOL> self . inbound_services = inbound_services <EOL> def ExcludePath ( self , path ) : <EOL> """<STR_LIT>""" <EOL> skip = False <EOL> for reserved_path in self . reserved_paths . keys ( ) : <EOL> if path . startswith ( reserved_path ) : <EOL> if ( not self . inbound_services or <EOL> self . reserved_paths [ reserved_path ] not in self . inbound_services ) : <EOL> return ( True , self . reserved_paths [ reserved_path ] ) <EOL> return ( False , None ) <EOL> def CreateInboundServiceFilter ( inbound_services ) : <EOL> return ReservedPathFilter ( inbound_services ) <EOL> def ReadCronConfig ( croninfo_path , parse_cron_config = croninfo . LoadSingleCron ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> croninfo_file = file ( croninfo_path , '<STR_LIT:r>' ) <EOL> except IOError , e : <EOL> raise InvalidAppConfigError ( <EOL> '<STR_LIT>' <EOL> % ( croninfo_path , e ) ) <EOL> try : <EOL> return parse_cron_config ( croninfo_file ) <EOL> finally : <EOL> croninfo_file . close ( ) <EOL> def SetupStubs ( app_id , ** config ) : <EOL> """<STR_LIT>""" <EOL> root_path = config . get ( '<STR_LIT>' , None ) <EOL> login_url = config [ '<STR_LIT>' ] <EOL> blobstore_path = config [ '<STR_LIT>' ] <EOL> datastore_path = config [ '<STR_LIT>' ] <EOL> clear_datastore = config [ '<STR_LIT>' ] <EOL> prospective_search_path = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> clear_prospective_search = config . get ( '<STR_LIT>' , False ) <EOL> use_sqlite = config . get ( '<STR_LIT>' , False ) <EOL> high_replication = config . get ( '<STR_LIT>' , False ) <EOL> require_indexes = config . get ( '<STR_LIT>' , False ) <EOL> mysql_host = config . get ( '<STR_LIT>' , None ) <EOL> mysql_port = config . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> mysql_user = config . get ( '<STR_LIT>' , None ) <EOL> mysql_password = config . get ( '<STR_LIT>' , None ) <EOL> mysql_socket = config . get ( '<STR_LIT>' , None ) <EOL> smtp_host = config . get ( '<STR_LIT>' , None ) <EOL> smtp_port = config . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> smtp_user = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> smtp_password = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> enable_sendmail = config . get ( '<STR_LIT>' , False ) <EOL> show_mail_body = config . get ( '<STR_LIT>' , False ) <EOL> remove = config . get ( '<STR_LIT>' , os . remove ) <EOL> disable_task_running = config . get ( '<STR_LIT>' , False ) <EOL> task_retry_seconds = config . get ( '<STR_LIT>' , <NUM_LIT:30> ) <EOL> persist_logs = config . get ( '<STR_LIT>' , False ) <EOL> trusted = config . get ( '<STR_LIT>' , False ) <EOL> serve_port = int ( config . get ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> serve_address = config . get ( '<STR_LIT>' , '<STR_LIT:localhost>' ) <EOL> xmpp_path = config [ '<STR_LIT>' ] <EOL> uaserver_path = config [ '<STR_LIT>' ] <EOL> login_server = config [ '<STR_LIT>' ] <EOL> cookie_secret = config [ '<STR_LIT>' ] <EOL> os . environ [ '<STR_LIT>' ] = app_id <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if clear_prospective_search and prospective_search_path : <EOL> if os . path . lexists ( prospective_search_path ) : <EOL> logging . info ( '<STR_LIT>' , prospective_search_path ) <EOL> try : <EOL> remove ( prospective_search_path ) <EOL> except OSError , e : <EOL> logging . warning ( '<STR_LIT>' , e ) <EOL> if clear_datastore : <EOL> path = datastore_path <EOL> if os . path . lexists ( path ) : <EOL> logging . info ( '<STR_LIT>' , path ) <EOL> try : <EOL> remove ( path ) <EOL> except OSError , e : <EOL> logging . warning ( '<STR_LIT>' , e ) <EOL> if not multiprocess . GlobalProcess ( ) . MaybeConfigureRemoteDataApis ( ) : <EOL> """<STR_LIT>""" <EOL> apiproxy_stub_map . apiproxy = apiproxy_stub_map . APIProxyStubMap ( ) <EOL> datastore = datastore_distributed . DatastoreDistributed ( <EOL> app_id , datastore_path , require_indexes = require_indexes , <EOL> trusted = trusted ) <EOL> if high_replication : <EOL> datastore . SetConsistencyPolicy ( <EOL> datastore_stub_util . TimeBasedHRConsistencyPolicy ( ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , datastore ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> memcache_distributed . MemcacheService ( ) ) <EOL> hash_secret = hashlib . sha1 ( app_id + '<STR_LIT:/>' + cookie_secret ) . hexdigest ( ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> taskqueue_rabbitmq . TaskQueueServiceStub ( <EOL> root_path = root_path , <EOL> auto_task_running = ( not disable_task_running ) , <EOL> task_retry_seconds = task_retry_seconds , <EOL> default_http_server = '<STR_LIT>' % ( serve_address , serve_port ) , app_id = app_id , hash_secret = hash_secret ) ) <EOL> if mysql_user : <EOL> from google . appengine import api <EOL> sys . modules [ '<STR_LIT>' ] = rdbms_mysqldb <EOL> api . rdbms = rdbms_mysqldb <EOL> rdbms_mysqldb . SetConnectKwargs ( host = mysql_host , port = mysql_port , <EOL> user = mysql_user , passwd = mysql_password , <EOL> unix_socket = mysql_socket ) <EOL> rdbms_mysqldb . connect ( database = '<STR_LIT>' ) <EOL> fixed_login_url = '<STR_LIT>' % ( login_url , <EOL> dev_appserver_login . CONTINUE_PARAM ) <EOL> fixed_logout_url = '<STR_LIT>' % login_server <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT:user>' , <EOL> user_service_stub . UserServiceStub ( login_url = fixed_login_url , <EOL> logout_url = fixed_logout_url ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> urlfetch_stub . URLFetchServiceStub ( ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> mail_stub . MailServiceStub ( smtp_host , <EOL> smtp_port , <EOL> smtp_user , <EOL> smtp_password , <EOL> enable_sendmail = enable_sendmail , <EOL> show_mail_body = show_mail_body ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> capability_stub . CapabilityServiceStub ( ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> xmpp_service_real . XmppService ( domain = xmpp_path , uaserver = uaserver_path ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> channel_service_stub . ChannelServiceStub ( ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> prospective_search_stub . ProspectiveSearchStub ( <EOL> prospective_search_path , <EOL> apiproxy_stub_map . apiproxy . GetStub ( '<STR_LIT>' ) ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> app_identity_stub . AppIdentityServiceStub ( ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> simple_search_stub . SearchServiceStub ( ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> conversion_stub . ConversionServiceStub ( ) ) <EOL> try : <EOL> from google . appengine . api . images import images_stub <EOL> host_prefix = '<STR_LIT>' % ( serve_address , serve_port ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> images_stub . ImagesServiceStub ( host_prefix = host_prefix ) ) <EOL> except ImportError , e : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' , e ) <EOL> from google . appengine . api . images import images_not_implemented_stub <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> images_not_implemented_stub . ImagesNotImplementedServiceStub ( ) ) <EOL> blob_storage = datastore_blob_storage . DatastoreBlobStorage ( blobstore_path , app_id ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> blobstore_stub . BlobstoreServiceStub ( blob_storage ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT:file>' , <EOL> file_service_stub . FileServiceStub ( blob_storage ) ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( <EOL> '<STR_LIT>' , <EOL> logservice_stub . LogServiceStub ( persist_logs ) ) <EOL> system_service_stub = system_stub . SystemServiceStub ( ) <EOL> multiprocess . GlobalProcess ( ) . UpdateSystemStub ( system_service_stub ) <EOL> apiproxy_stub_map . apiproxy . RegisterStub ( '<STR_LIT>' , system_service_stub ) <EOL> def CreateImplicitMatcher ( <EOL> config , <EOL> module_dict , <EOL> root_path , <EOL> login_url , <EOL> create_path_adjuster = PathAdjuster , <EOL> create_local_dispatcher = LocalCGIDispatcher , <EOL> create_cgi_dispatcher = CGIDispatcher , <EOL> get_blob_storage = dev_appserver_blobstore . GetBlobStorage ) : <EOL> """<STR_LIT>""" <EOL> url_matcher = URLMatcher ( ) <EOL> path_adjuster = create_path_adjuster ( root_path ) <EOL> if multiprocess . GlobalProcess ( ) . IsApiServer ( ) : <EOL> remote_api_dispatcher = create_cgi_dispatcher ( <EOL> config , module_dict , root_path , path_adjuster ) <EOL> url_matcher . AddURL ( multiprocess . PATH_DEV_API_SERVER , <EOL> remote_api_dispatcher , <EOL> REMOTE_API_PATH , <EOL> False , <EOL> False , <EOL> appinfo . AUTH_FAIL_ACTION_REDIRECT ) <EOL> login_dispatcher = create_local_dispatcher ( config , sys . modules , path_adjuster , <EOL> dev_appserver_login . main ) <EOL> url_matcher . AddURL ( login_url , <EOL> login_dispatcher , <EOL> '<STR_LIT>' , <EOL> False , <EOL> False , <EOL> appinfo . AUTH_FAIL_ACTION_REDIRECT ) <EOL> admin_dispatcher = create_cgi_dispatcher ( config , module_dict , root_path , <EOL> path_adjuster ) <EOL> url_matcher . AddURL ( '<STR_LIT>' , <EOL> admin_dispatcher , <EOL> DEVEL_CONSOLE_PATH , <EOL> True , <EOL> True , <EOL> appinfo . AUTH_FAIL_ACTION_REDIRECT ) <EOL> upload_dispatcher = dev_appserver_blobstore . CreateUploadDispatcher ( <EOL> get_blob_storage ) <EOL> url_matcher . AddURL ( dev_appserver_blobstore . UPLOAD_URL_PATTERN , <EOL> upload_dispatcher , <EOL> '<STR_LIT>' , <EOL> True , <EOL> True , <EOL> appinfo . AUTH_FAIL_ACTION_UNAUTHORIZED ) <EOL> blobimage_dispatcher = dev_appserver_blobimage . CreateBlobImageDispatcher ( <EOL> apiproxy_stub_map . apiproxy . GetStub ( '<STR_LIT>' ) ) <EOL> url_matcher . AddURL ( dev_appserver_blobimage . BLOBIMAGE_URL_PATTERN , <EOL> blobimage_dispatcher , <EOL> '<STR_LIT>' , <EOL> False , <EOL> False , <EOL> appinfo . AUTH_FAIL_ACTION_UNAUTHORIZED ) <EOL> oauth_dispatcher = dev_appserver_oauth . CreateOAuthDispatcher ( ) <EOL> url_matcher . AddURL ( dev_appserver_oauth . OAUTH_URL_PATTERN , <EOL> oauth_dispatcher , <EOL> '<STR_LIT>' , <EOL> False , <EOL> False , <EOL> appinfo . AUTH_FAIL_ACTION_UNAUTHORIZED ) <EOL> channel_dispatcher = dev_appserver_channel . CreateChannelDispatcher ( <EOL> apiproxy_stub_map . apiproxy . GetStub ( '<STR_LIT>' ) ) <EOL> url_matcher . AddURL ( dev_appserver_channel . CHANNEL_JSAPI_PATTERN , <EOL> channel_dispatcher , <EOL> '<STR_LIT>' , <EOL> False , <EOL> False , <EOL> appinfo . AUTH_FAIL_ACTION_UNAUTHORIZED ) <EOL> return url_matcher <EOL> def CreateServer ( root_path , <EOL> login_url , <EOL> port , <EOL> template_dir = None , <EOL> serve_address = '<STR_LIT>' , <EOL> require_indexes = False , <EOL> allow_skipped_files = False , <EOL> static_caching = True , <EOL> python_path_list = sys . path , <EOL> sdk_dir = SDK_ROOT , <EOL> default_partition = None , <EOL> persist_logs = False , <EOL> secret_hash = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> absolute_root_path = os . path . realpath ( root_path ) <EOL> FakeFile . SetAllowedPaths ( absolute_root_path , <EOL> [ sdk_dir ] ) <EOL> FakeFile . SetAllowSkippedFiles ( allow_skipped_files ) <EOL> handler_class = CreateRequestHandler ( absolute_root_path , <EOL> login_url , <EOL> require_indexes , <EOL> static_caching , <EOL> default_partition , <EOL> persist_logs , <EOL> secret_hash ) <EOL> if absolute_root_path not in python_path_list : <EOL> python_path_list . insert ( <NUM_LIT:0> , absolute_root_path ) <EOL> if multiprocess . Enabled ( ) : <EOL> server = HttpServerWithMultiProcess ( ( serve_address , port ) , handler_class ) <EOL> else : <EOL> server = HTTPServerWithScheduler ( ( serve_address , port ) , handler_class ) <EOL> queue_stub = apiproxy_stub_map . apiproxy . GetStub ( '<STR_LIT>' ) <EOL> if queue_stub and hasattr ( queue_stub , '<STR_LIT>' ) : <EOL> queue_stub . StartBackgroundExecution ( ) <EOL> channel_stub = apiproxy_stub_map . apiproxy . GetStub ( '<STR_LIT>' ) <EOL> if channel_stub : <EOL> channel_stub . _add_event = server . AddEvent <EOL> channel_stub . _update_event = server . UpdateEvent <EOL> return server <EOL> class HTTPServerWithScheduler ( BaseHTTPServer . HTTPServer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server_address , request_handler_class ) : <EOL> """<STR_LIT>""" <EOL> BaseHTTPServer . HTTPServer . __init__ ( self , server_address , <EOL> request_handler_class ) <EOL> self . _events = [ ] <EOL> self . _stopped = False <EOL> def handle_request ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> self . _handle_request_noblock ( ) <EOL> else : <EOL> BaseHTTPServer . HTTPServer . handle_request ( self ) <EOL> def get_request ( self , time_func = time . time , select_func = select . select ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> if self . _events : <EOL> current_time = time_func ( ) <EOL> next_eta = self . _events [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> delay = next_eta - current_time <EOL> else : <EOL> delay = DEFAULT_SELECT_DELAY <EOL> readable , _ , _ = select_func ( [ self . socket ] , [ ] , [ ] , max ( delay , <NUM_LIT:0> ) ) <EOL> if readable : <EOL> return self . socket . accept ( ) <EOL> current_time = time_func ( ) <EOL> if self . _events and current_time >= self . _events [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] : <EOL> runnable = heapq . heappop ( self . _events ) [ <NUM_LIT:1> ] <EOL> request_tuple = runnable ( ) <EOL> if request_tuple : <EOL> return request_tuple <EOL> def serve_forever ( self ) : <EOL> """<STR_LIT>""" <EOL> while not self . _stopped : <EOL> self . handle_request ( ) <EOL> self . server_close ( ) <EOL> def stop_serving_forever ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _stopped = True <EOL> def AddEvent ( self , eta , runnable , service = None , event_id = None ) : <EOL> """<STR_LIT>""" <EOL> heapq . heappush ( self . _events , ( eta , runnable , service , event_id ) ) <EOL> def UpdateEvent ( self , service , event_id , eta ) : <EOL> """<STR_LIT>""" <EOL> for id in xrange ( len ( self . _events ) ) : <EOL> item = self . _events [ id ] <EOL> if item [ <NUM_LIT:2> ] == service and item [ <NUM_LIT:3> ] == event_id : <EOL> item = ( eta , item [ <NUM_LIT:1> ] , item [ <NUM_LIT:2> ] , item [ <NUM_LIT:3> ] ) <EOL> del ( self . _events [ id ] ) <EOL> heapq . heappush ( self . _events , item ) <EOL> break <EOL> class HttpServerWithMultiProcess ( HTTPServerWithScheduler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server_address , request_handler_class ) : <EOL> """<STR_LIT>""" <EOL> HTTPServerWithScheduler . __init__ ( self , server_address , <EOL> request_handler_class ) <EOL> multiprocess . GlobalProcess ( ) . SetHttpServer ( self ) <EOL> def process_request ( self , request , client_address ) : <EOL> """<STR_LIT>""" <EOL> multiprocess . GlobalProcess ( ) . ProcessRequest ( request , client_address ) </s>
