<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import types <EOL> import os <EOL> import httplib <EOL> import atom . url <EOL> import atom . http_interface <EOL> import socket <EOL> import base64 <EOL> class ProxyError ( atom . http_interface . Error ) : <EOL> pass <EOL> DEFAULT_CONTENT_TYPE = '<STR_LIT>' <EOL> class HttpClient ( atom . http_interface . GenericHttpClient ) : <EOL> def __init__ ( self , headers = None ) : <EOL> self . debug = False <EOL> self . headers = headers or { } <EOL> def request ( self , operation , url , data = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( url , atom . url . Url ) : <EOL> if isinstance ( url , types . StringTypes ) : <EOL> url = atom . url . parse_url ( url ) <EOL> else : <EOL> raise atom . http_interface . UnparsableUrlObject ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> all_headers = self . headers . copy ( ) <EOL> if headers : <EOL> all_headers . update ( headers ) <EOL> connection = self . _prepare_connection ( url , all_headers ) <EOL> if self . debug : <EOL> connection . debuglevel = <NUM_LIT:1> <EOL> connection . putrequest ( operation , self . _get_access_url ( url ) , <EOL> skip_host = True ) <EOL> connection . putheader ( '<STR_LIT>' , url . host ) <EOL> if ( url . protocol == '<STR_LIT>' and int ( url . port or <NUM_LIT> ) == <NUM_LIT> and <EOL> hasattr ( connection , '<STR_LIT>' ) and <EOL> isinstance ( connection . _buffer , list ) ) : <EOL> header_line = '<STR_LIT>' % url . host <EOL> replacement_header_line = '<STR_LIT>' % url . host <EOL> try : <EOL> connection . _buffer [ connection . _buffer . index ( header_line ) ] = ( <EOL> replacement_header_line ) <EOL> except ValueError : <EOL> pass <EOL> if data and '<STR_LIT>' not in all_headers : <EOL> if isinstance ( data , types . StringTypes ) : <EOL> all_headers [ '<STR_LIT>' ] = len ( data ) <EOL> else : <EOL> raise atom . http_interface . ContentLengthRequired ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if '<STR_LIT:Content-Type>' not in all_headers : <EOL> all_headers [ '<STR_LIT:Content-Type>' ] = DEFAULT_CONTENT_TYPE <EOL> for header_name in all_headers : <EOL> connection . putheader ( header_name , all_headers [ header_name ] ) <EOL> connection . endheaders ( ) <EOL> if data : <EOL> if isinstance ( data , list ) : <EOL> for data_part in data : <EOL> _send_data_part ( data_part , connection ) <EOL> else : <EOL> _send_data_part ( data , connection ) <EOL> return connection . getresponse ( ) <EOL> def _prepare_connection ( self , url , headers ) : <EOL> if not isinstance ( url , atom . url . Url ) : <EOL> if isinstance ( url , types . StringTypes ) : <EOL> url = atom . url . parse_url ( url ) <EOL> else : <EOL> raise atom . http_interface . UnparsableUrlObject ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if url . protocol == '<STR_LIT>' : <EOL> if not url . port : <EOL> return httplib . HTTPSConnection ( url . host ) <EOL> return httplib . HTTPSConnection ( url . host , int ( url . port ) ) <EOL> else : <EOL> if not url . port : <EOL> return httplib . HTTPConnection ( url . host ) <EOL> return httplib . HTTPConnection ( url . host , int ( url . port ) ) <EOL> def _get_access_url ( self , url ) : <EOL> return url . to_string ( ) <EOL> class ProxiedHttpClient ( HttpClient ) : <EOL> """<STR_LIT>""" <EOL> def _prepare_connection ( self , url , headers ) : <EOL> proxy_auth = _get_proxy_auth ( ) <EOL> if url . protocol == '<STR_LIT>' : <EOL> proxy = os . environ . get ( '<STR_LIT>' ) <EOL> if proxy : <EOL> if proxy_auth : <EOL> proxy_auth = '<STR_LIT>' % proxy_auth <EOL> port = url . port <EOL> if not port : <EOL> port = '<STR_LIT>' <EOL> proxy_connect = '<STR_LIT>' % ( url . host , port ) <EOL> if headers and '<STR_LIT>' in headers : <EOL> user_agent = '<STR_LIT>' % ( headers [ '<STR_LIT>' ] ) <EOL> else : <EOL> user_agent = '<STR_LIT>' <EOL> proxy_pieces = '<STR_LIT>' % ( proxy_connect , proxy_auth , user_agent ) <EOL> proxy_url = atom . url . parse_url ( proxy ) <EOL> if not proxy_url . port : <EOL> proxy_url . port = '<STR_LIT>' <EOL> p_sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> p_sock . connect ( ( proxy_url . host , int ( proxy_url . port ) ) ) <EOL> p_sock . sendall ( proxy_pieces ) <EOL> response = '<STR_LIT>' <EOL> while response . find ( "<STR_LIT>" ) == - <NUM_LIT:1> : <EOL> response += p_sock . recv ( <NUM_LIT> ) <EOL> p_status = response . split ( ) [ <NUM_LIT:1> ] <EOL> if p_status != str ( <NUM_LIT:200> ) : <EOL> raise ProxyError ( '<STR_LIT>' % str ( p_status ) ) <EOL> ssl = socket . ssl ( p_sock , None , None ) <EOL> fake_sock = httplib . FakeSocket ( p_sock , ssl ) <EOL> connection = httplib . HTTPConnection ( proxy_url . host ) <EOL> connection . sock = fake_sock <EOL> return connection <EOL> else : <EOL> return HttpClient . _prepare_connection ( self , url , headers ) <EOL> else : <EOL> proxy = os . environ . get ( '<STR_LIT>' ) <EOL> if proxy : <EOL> proxy_url = atom . url . parse_url ( proxy ) <EOL> if not proxy_url . port : <EOL> proxy_url . port = '<STR_LIT>' <EOL> if proxy_auth : <EOL> headers [ '<STR_LIT>' ] = proxy_auth . strip ( ) <EOL> return httplib . HTTPConnection ( proxy_url . host , int ( proxy_url . port ) ) <EOL> else : <EOL> return HttpClient . _prepare_connection ( self , url , headers ) <EOL> def _get_access_url ( self , url ) : <EOL> return url . to_string ( ) <EOL> def _get_proxy_auth ( ) : <EOL> proxy_username = os . environ . get ( '<STR_LIT>' ) <EOL> if not proxy_username : <EOL> proxy_username = os . environ . get ( '<STR_LIT>' ) <EOL> proxy_password = os . environ . get ( '<STR_LIT>' ) <EOL> if not proxy_password : <EOL> proxy_password = os . environ . get ( '<STR_LIT>' ) <EOL> if proxy_username : <EOL> user_auth = base64 . encodestring ( '<STR_LIT>' % ( proxy_username , <EOL> proxy_password ) ) <EOL> return '<STR_LIT>' % ( user_auth . strip ( ) ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def _send_data_part ( data , connection ) : <EOL> if isinstance ( data , types . StringTypes ) : <EOL> connection . send ( data ) <EOL> return <EOL> elif hasattr ( data , '<STR_LIT>' ) : <EOL> while <NUM_LIT:1> : <EOL> binarydata = data . read ( <NUM_LIT> ) <EOL> if binarydata == '<STR_LIT>' : break <EOL> connection . send ( binarydata ) <EOL> return <EOL> else : <EOL> connection . send ( str ( data ) ) <EOL> return </s>
