<s> import io <EOL> import os <EOL> import posixpath <EOL> import re <EOL> from tproxy . util import parse_address <EOL> absolute_http_url_re = re . compile ( r"<STR_LIT>" , re . I ) <EOL> def normalize ( prefix , link ) : <EOL> """<STR_LIT>""" <EOL> if link . startswith ( "<STR_LIT:#>" ) : <EOL> return link <EOL> if not link . startswith ( '<STR_LIT:/>' ) : <EOL> link = "<STR_LIT>" % link <EOL> path = posixpath . normpath ( "<STR_LIT>" % ( prefix , link ) ) <EOL> return path <EOL> def headers_lines ( parser , headers ) : <EOL> """<STR_LIT>""" <EOL> httpver = "<STR_LIT>" % "<STR_LIT:.>" . join ( map ( str , parser . version ( ) ) ) <EOL> new_headers = [ "<STR_LIT>" % ( httpver , parser . status ( ) ) ] <EOL> new_headers . extend ( [ "<STR_LIT>" % ( hname , hvalue ) for hname , hvalue in headers . items ( ) ] ) <EOL> return new_headers <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 base_uri ( host , is_ssl = False ) : <EOL> """<STR_LIT>""" <EOL> if is_ssl : <EOL> scheme = "<STR_LIT>" <EOL> else : <EOL> scheme = "<STR_LIT:http>" <EOL> return "<STR_LIT>" % ( scheme , 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>" ) </s>
