<s> import logging <EOL> import json <EOL> import base64 <EOL> from urllib import urlencode <EOL> from requestlogger import ApacheFormatter <EOL> from StringIO import StringIO <EOL> def create_wsgi_request ( event_info , server_name = '<STR_LIT>' , script_name = None , <EOL> trailing_slash = True ) : <EOL> """<STR_LIT>""" <EOL> method = event_info [ '<STR_LIT>' ] <EOL> params = event_info [ '<STR_LIT>' ] <EOL> query = event_info [ '<STR_LIT>' ] <EOL> headers = event_info [ '<STR_LIT>' ] <EOL> for header in headers . keys ( ) : <EOL> canonical = header . title ( ) <EOL> if canonical != header : <EOL> headers [ canonical ] = headers . pop ( header ) <EOL> content_type = headers . get ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> content_type = content_type . lower ( ) . split ( '<STR_LIT:;>' ) [ <NUM_LIT:0> ] <EOL> if content_type == '<STR_LIT:application/json>' : <EOL> body = str ( json . dumps ( event_info [ '<STR_LIT:body>' ] ) ) <EOL> else : <EOL> body = str ( event_info [ '<STR_LIT:body>' ] ) <EOL> path = "<STR_LIT:/>" <EOL> for key in sorted ( params . keys ( ) ) : <EOL> path = path + params [ key ] + "<STR_LIT:/>" <EOL> if not trailing_slash and params . keys ( ) : <EOL> path = path [ : - <NUM_LIT:1> ] <EOL> query_string = urlencode ( query ) <EOL> x_forwarded_for = headers . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if '<STR_LIT:U+002C>' in x_forwarded_for : <EOL> remote_addr = x_forwarded_for . split ( '<STR_LIT:U+002CU+0020>' ) [ <NUM_LIT:0> ] <EOL> else : <EOL> remote_addr = '<STR_LIT:127.0.0.1>' <EOL> environ = { <EOL> '<STR_LIT>' : path , <EOL> '<STR_LIT>' : query_string , <EOL> '<STR_LIT>' : remote_addr , <EOL> '<STR_LIT>' : method , <EOL> '<STR_LIT>' : str ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : str ( server_name ) , <EOL> '<STR_LIT>' : str ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : str ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : str ( '<STR_LIT:http>' ) , <EOL> '<STR_LIT>' : body , <EOL> '<STR_LIT>' : str ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> if method in [ "<STR_LIT:POST>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if '<STR_LIT:Content-Type>' in event_info [ "<STR_LIT>" ] : <EOL> environ [ '<STR_LIT>' ] = event_info [ "<STR_LIT>" ] [ '<STR_LIT:Content-Type>' ] <EOL> if '<STR_LIT>' in event_info [ "<STR_LIT>" ] [ '<STR_LIT:Content-Type>' ] : <EOL> body = base64 . b64decode ( body ) <EOL> environ [ '<STR_LIT>' ] = StringIO ( body ) <EOL> environ [ '<STR_LIT>' ] = str ( len ( body ) ) <EOL> for header in event_info [ "<STR_LIT>" ] : <EOL> wsgi_name = "<STR_LIT>" + header . upper ( ) . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> environ [ wsgi_name ] = str ( event_info [ "<STR_LIT>" ] [ header ] ) <EOL> if script_name : <EOL> environ [ '<STR_LIT>' ] = script_name <EOL> path_info = environ [ '<STR_LIT>' ] <EOL> if script_name in path_info : <EOL> environ [ '<STR_LIT>' ] . replace ( script_name , '<STR_LIT>' ) <EOL> return environ <EOL> def common_log ( environ , response , response_time = None ) : <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( ) <EOL> logger . setLevel ( logging . INFO ) <EOL> if response_time : <EOL> formatter = ApacheFormatter ( with_response_time = True ) <EOL> log_entry = formatter ( response . status_code , environ , <EOL> len ( response . content ) , rt_ms = response_time ) <EOL> else : <EOL> formatter = ApacheFormatter ( with_response_time = False ) <EOL> log_entry = formatter ( response . status_code , environ , <EOL> len ( response . content ) ) <EOL> logger . info ( log_entry ) <EOL> return log_entry </s>
