<s> """<STR_LIT>""" <EOL> import calendar <EOL> import cStringIO <EOL> import email <EOL> import functools <EOL> import logging <EOL> import time <EOL> import wsgiref . headers <EOL> from google . appengine . tools . devappserver2 import blob_download <EOL> from google . appengine . tools . devappserver2 import constants <EOL> def _ignore_request_headers_rewriter ( environ ) : <EOL> """<STR_LIT>""" <EOL> for h in constants . IGNORED_REQUEST_HEADERS : <EOL> h = '<STR_LIT>' + h . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) . upper ( ) <EOL> try : <EOL> del environ [ h ] <EOL> except KeyError : <EOL> pass <EOL> _REQUEST_REWRITER_CHAIN = [ <EOL> _ignore_request_headers_rewriter , <EOL> ] <EOL> class RewriterState ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , environ , status , headers , body ) : <EOL> """<STR_LIT>""" <EOL> self . environ = environ <EOL> self . status = status <EOL> self . headers = wsgiref . headers . Headers ( headers ) <EOL> self . body = body <EOL> self . allow_large_response = False <EOL> @ property <EOL> def status_code ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . status . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> ALLOWED_HEADER_NAME_CHARACTERS = ( frozenset ( [ chr ( c ) for c in range ( <NUM_LIT> , <NUM_LIT> ) ] ) - <EOL> frozenset ( [ '<STR_LIT::>' ] ) ) <EOL> ALLOWED_HEADER_VALUE_CHARACTERS = frozenset ( [ chr ( c ) for c in range ( <NUM_LIT:32> , <NUM_LIT> ) ] ) <EOL> def _ignore_response_headers_rewriter ( ignored_response_headers , state ) : <EOL> """<STR_LIT>""" <EOL> for name , value in state . headers . items ( ) : <EOL> if name . lower ( ) in ignored_response_headers : <EOL> del state . headers [ name ] <EOL> try : <EOL> if isinstance ( name , unicode ) : <EOL> name = name . encode ( '<STR_LIT:ascii>' ) <EOL> if isinstance ( value , unicode ) : <EOL> value = value . encode ( '<STR_LIT:ascii>' ) <EOL> except UnicodeEncodeError : <EOL> del state . headers [ name ] <EOL> if ( set ( name ) - ALLOWED_HEADER_NAME_CHARACTERS or <EOL> set ( value ) - ALLOWED_HEADER_VALUE_CHARACTERS ) : <EOL> del state . headers [ name ] <EOL> def _default_content_type_rewriter ( state ) : <EOL> """<STR_LIT>""" <EOL> if not '<STR_LIT:Content-Type>' in state . headers : <EOL> state . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> def _cache_rewriter ( state ) : <EOL> """<STR_LIT>""" <EOL> if state . status_code in constants . NO_BODY_RESPONSE_STATUSES : <EOL> return <EOL> if not '<STR_LIT>' in state . headers : <EOL> state . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if not '<STR_LIT>' in state . headers : <EOL> state . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if '<STR_LIT>' in state . headers : <EOL> current_date = time . time ( ) <EOL> expires = state . headers . get ( '<STR_LIT>' ) <EOL> reset_expires = True <EOL> if expires : <EOL> expires_time = email . Utils . parsedate ( expires ) <EOL> if expires_time : <EOL> reset_expires = calendar . timegm ( expires_time ) >= current_date <EOL> if reset_expires : <EOL> state . headers [ '<STR_LIT>' ] = time . strftime ( '<STR_LIT>' , <EOL> time . gmtime ( current_date ) ) <EOL> cache_directives = [ ] <EOL> for header in state . headers . get_all ( '<STR_LIT>' ) : <EOL> cache_directives . extend ( v . strip ( ) for v in header . split ( '<STR_LIT:U+002C>' ) ) <EOL> cache_directives = [ d for d in cache_directives if d != '<STR_LIT>' ] <EOL> if not constants . NON_PUBLIC_CACHE_CONTROLS . intersection ( cache_directives ) : <EOL> cache_directives . append ( '<STR_LIT>' ) <EOL> state . headers [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( cache_directives ) <EOL> def _content_length_rewriter ( state ) : <EOL> """<STR_LIT>""" <EOL> state . body = list ( state . body ) <EOL> length = sum ( len ( block ) for block in state . body ) <EOL> if state . status_code in constants . NO_BODY_RESPONSE_STATUSES : <EOL> state . body = [ ] <EOL> del state . headers [ '<STR_LIT>' ] <EOL> elif state . environ . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> if length : <EOL> logging . warning ( '<STR_LIT>' ) <EOL> state . body = [ ] <EOL> else : <EOL> if ( not state . allow_large_response and <EOL> length > constants . MAX_RUNTIME_RESPONSE_SIZE ) : <EOL> logging . error ( '<STR_LIT>' , <EOL> length , constants . MAX_RUNTIME_RESPONSE_SIZE ) <EOL> new_response = ( '<STR_LIT>' % <EOL> ( length , constants . MAX_RUNTIME_RESPONSE_SIZE ) ) <EOL> state . status = '<STR_LIT>' <EOL> state . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> state . headers [ '<STR_LIT>' ] = str ( len ( new_response ) ) <EOL> state . body = [ new_response ] <EOL> else : <EOL> state . headers [ '<STR_LIT>' ] = str ( length ) <EOL> _FRONTEND_RESPONSE_REWRITER_CHAIN = [ <EOL> blob_download . blobstore_download_rewriter , <EOL> functools . partial ( _ignore_response_headers_rewriter , <EOL> constants . FRONTEND_IGNORED_RESPONSE_HEADERS ) , <EOL> _default_content_type_rewriter , <EOL> _cache_rewriter , <EOL> _content_length_rewriter , <EOL> ] <EOL> _RUNTIME_RESPONSE_REWRITER_CHAIN = [ <EOL> _content_length_rewriter , <EOL> functools . partial ( _ignore_response_headers_rewriter , <EOL> constants . RUNTIME_IGNORED_RESPONSE_HEADERS ) , <EOL> ] <EOL> def _rewriter_middleware ( request_rewriter_chain , response_rewriter_chain , <EOL> application , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> response_dict = { '<STR_LIT>' : False } <EOL> write_body = cStringIO . StringIO ( ) <EOL> def wrapped_start_response ( status , response_headers , exc_info = None ) : <EOL> if exc_info and response_dict [ '<STR_LIT>' ] : <EOL> raise exc_info [ <NUM_LIT:0> ] , exc_info [ <NUM_LIT:1> ] , exc_info [ <NUM_LIT:2> ] <EOL> response_dict [ '<STR_LIT:status>' ] = status <EOL> response_dict [ '<STR_LIT>' ] = response_headers <EOL> return write_body . write <EOL> for rewriter in request_rewriter_chain : <EOL> rewriter ( environ ) <EOL> response_body = iter ( application ( environ , wrapped_start_response ) ) <EOL> first = write_body . getvalue ( ) <EOL> while not first : <EOL> try : <EOL> first = response_body . next ( ) <EOL> except StopIteration : <EOL> break <EOL> response_dict [ '<STR_LIT>' ] = True <EOL> try : <EOL> status = response_dict [ '<STR_LIT:status>' ] <EOL> response_headers = response_dict [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> def reconstructed_body ( ) : <EOL> yield first <EOL> for string in response_body : <EOL> yield string <EOL> body = reconstructed_body ( ) <EOL> state = RewriterState ( environ , status , response_headers , body ) <EOL> for rewriter in response_rewriter_chain : <EOL> rewriter ( state ) <EOL> start_response ( state . status , state . headers . items ( ) ) <EOL> return state . body <EOL> def frontend_rewriter_middleware ( application ) : <EOL> """<STR_LIT>""" <EOL> return functools . partial ( _rewriter_middleware , <EOL> _REQUEST_REWRITER_CHAIN , <EOL> _FRONTEND_RESPONSE_REWRITER_CHAIN , <EOL> application ) <EOL> def runtime_rewriter_middleware ( application ) : <EOL> """<STR_LIT>""" <EOL> return functools . partial ( _rewriter_middleware , <EOL> _REQUEST_REWRITER_CHAIN , <EOL> _RUNTIME_RESPONSE_REWRITER_CHAIN , <EOL> application ) </s>
