<s> """<STR_LIT>""" <EOL> import io <EOL> import urlparse <EOL> import socket <EOL> from http_parser . http import HttpStream , NoMoreData , ParserError <EOL> from http_parser . parser import HttpParser <EOL> from tproxy . util import parse_address <EOL> def get_host ( addr , is_ssl = False ) : <EOL> """<STR_LIT>""" <EOL> host = addr [ <NUM_LIT:0> ] <EOL> if addr [ <NUM_LIT:1> ] != ( is_ssl and <NUM_LIT> or <NUM_LIT> ) : <EOL> host = "<STR_LIT>" % ( host , addr [ <NUM_LIT:1> ] ) <EOL> return host <EOL> def write_chunk ( to , data ) : <EOL> """<STR_LIT>""" <EOL> chunk = "<STR_LIT>" . join ( ( "<STR_LIT>" % len ( data ) , data , "<STR_LIT:\r\n>" ) ) <EOL> to . writeall ( chunk ) <EOL> def write ( to , data ) : <EOL> to . writeall ( data ) <EOL> def send_body ( to , body , chunked = False ) : <EOL> if chunked : <EOL> _write = write_chunk <EOL> else : <EOL> _write = write <EOL> while True : <EOL> data = body . read ( io . DEFAULT_BUFFER_SIZE ) <EOL> if not data : <EOL> break <EOL> _write ( to , data ) <EOL> if chunked : <EOL> _write ( to , "<STR_LIT>" ) <EOL> def rewrite_request ( req ) : <EOL> try : <EOL> while True : <EOL> parser = HttpStream ( req ) <EOL> headers = parser . headers ( ) <EOL> parsed_url = urlparse . urlparse ( parser . url ( ) ) <EOL> is_ssl = parsed_url . scheme == "<STR_LIT>" <EOL> host = get_host ( parse_address ( parsed_url . netloc , <NUM_LIT> ) , <EOL> is_ssl = is_ssl ) <EOL> headers [ '<STR_LIT>' ] = host <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if '<STR_LIT>' in headers : <EOL> del headers [ '<STR_LIT>' ] <EOL> location = urlparse . urlunparse ( ( '<STR_LIT>' , '<STR_LIT>' , parsed_url . path , <EOL> parsed_url . params , parsed_url . query , parsed_url . fragment ) ) <EOL> httpver = "<STR_LIT>" % "<STR_LIT:.>" . join ( map ( str , <EOL> parser . version ( ) ) ) <EOL> new_headers = [ "<STR_LIT>" % ( parser . method ( ) , location , <EOL> httpver ) ] <EOL> new_headers . extend ( [ "<STR_LIT>" % ( hname , hvalue ) for hname , hvalue in headers . items ( ) ] ) <EOL> req . writeall ( bytes ( "<STR_LIT>" . join ( new_headers ) + "<STR_LIT:\r\n>" ) ) <EOL> body = parser . body_file ( ) <EOL> send_body ( req , body , parser . is_chunked ( ) ) <EOL> except ( socket . error , NoMoreData , ParserError ) : <EOL> pass <EOL> def proxy ( data ) : <EOL> recved = len ( data ) <EOL> idx = data . find ( "<STR_LIT:\r\n>" ) <EOL> if idx <= <NUM_LIT:0> : <EOL> return <EOL> line , rest = data [ : idx ] , data [ idx : ] <EOL> if line . startswith ( "<STR_LIT>" ) : <EOL> parts = line . split ( None ) <EOL> netloc = parts [ <NUM_LIT:1> ] <EOL> remote = parse_address ( netloc , <NUM_LIT> ) <EOL> reply_msg = "<STR_LIT>" % parts [ <NUM_LIT:2> ] <EOL> return { "<STR_LIT>" : remote , <EOL> "<STR_LIT>" : reply_msg , <EOL> "<STR_LIT:data>" : "<STR_LIT>" } <EOL> parser = HttpParser ( ) <EOL> parsed = parser . execute ( data , recved ) <EOL> if parsed != recved : <EOL> return { '<STR_LIT>' : '<STR_LIT>' } <EOL> if not parser . get_url ( ) : <EOL> return <EOL> parsed_url = urlparse . urlparse ( parser . get_url ( ) ) <EOL> is_ssl = parsed_url . scheme == "<STR_LIT>" <EOL> remote = parse_address ( parsed_url . netloc , <NUM_LIT> ) <EOL> return { "<STR_LIT>" : remote , <EOL> "<STR_LIT>" : is_ssl } </s>
