<s> __author__ = '<STR_LIT>' <EOL> import os <EOL> import StringIO <EOL> import urlparse <EOL> import urllib <EOL> import httplib <EOL> ssl = None <EOL> try : <EOL> import ssl <EOL> except ImportError : <EOL> pass <EOL> class Error ( Exception ) : <EOL> pass <EOL> class UnknownSize ( Error ) : <EOL> pass <EOL> class ProxyError ( Error ) : <EOL> pass <EOL> MIME_BOUNDARY = '<STR_LIT>' <EOL> def get_headers ( http_response ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( http_response , '<STR_LIT>' ) : <EOL> return http_response . getheaders ( ) <EOL> else : <EOL> headers = [ ] <EOL> for header in ( <EOL> '<STR_LIT:location>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:date>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) : <EOL> value = http_response . getheader ( header , None ) <EOL> if value is not None : <EOL> headers . append ( ( header , value ) ) <EOL> return headers <EOL> class HttpRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> method = None <EOL> uri = None <EOL> def __init__ ( self , uri = None , method = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> self . headers = headers or { } <EOL> self . _body_parts = [ ] <EOL> if method is not None : <EOL> self . method = method <EOL> if isinstance ( uri , ( str , unicode ) ) : <EOL> uri = Uri . parse_uri ( uri ) <EOL> self . uri = uri or Uri ( ) <EOL> def add_body_part ( self , data , mime_type , size = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( data , str ) : <EOL> size = len ( data ) <EOL> if size is None : <EOL> raise UnknownSize ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in self . headers : <EOL> content_length = int ( self . headers [ '<STR_LIT>' ] ) <EOL> else : <EOL> content_length = <NUM_LIT:0> <EOL> if len ( self . _body_parts ) == <NUM_LIT:0> : <EOL> self . headers [ '<STR_LIT:Content-Type>' ] = mime_type <EOL> content_length = size <EOL> self . _body_parts . append ( data ) <EOL> elif len ( self . _body_parts ) == <NUM_LIT:1> : <EOL> self . _body_parts . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> boundary_string = '<STR_LIT>' % ( MIME_BOUNDARY , ) <EOL> content_length += len ( boundary_string ) + size <EOL> self . _body_parts . insert ( <NUM_LIT:1> , boundary_string ) <EOL> content_length += len ( '<STR_LIT>' ) <EOL> original_type_string = '<STR_LIT>' % ( <EOL> self . headers [ '<STR_LIT:Content-Type>' ] , ) <EOL> self . _body_parts . insert ( <NUM_LIT:2> , original_type_string ) <EOL> content_length += len ( original_type_string ) <EOL> boundary_string = '<STR_LIT>' % ( MIME_BOUNDARY , ) <EOL> self . _body_parts . append ( boundary_string ) <EOL> content_length += len ( boundary_string ) <EOL> self . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' % ( <EOL> MIME_BOUNDARY , ) <EOL> self . headers [ '<STR_LIT>' ] = '<STR_LIT:1.0>' <EOL> type_string = '<STR_LIT>' % ( mime_type ) <EOL> self . _body_parts . append ( type_string ) <EOL> content_length += len ( type_string ) <EOL> self . _body_parts . append ( data ) <EOL> ending_boundary_string = '<STR_LIT>' % ( MIME_BOUNDARY , ) <EOL> self . _body_parts . append ( ending_boundary_string ) <EOL> content_length += len ( ending_boundary_string ) <EOL> else : <EOL> boundary_string = '<STR_LIT>' % ( MIME_BOUNDARY , ) <EOL> self . _body_parts . insert ( - <NUM_LIT:1> , boundary_string ) <EOL> content_length += len ( boundary_string ) + size <EOL> type_string = '<STR_LIT>' % ( mime_type ) <EOL> self . _body_parts . insert ( - <NUM_LIT:1> , type_string ) <EOL> content_length += len ( type_string ) <EOL> self . _body_parts . insert ( - <NUM_LIT:1> , data ) <EOL> self . headers [ '<STR_LIT>' ] = str ( content_length ) <EOL> AddBodyPart = add_body_part <EOL> def add_form_inputs ( self , form_data , <EOL> mime_type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> body = urllib . urlencode ( form_data ) <EOL> self . add_body_part ( body , mime_type ) <EOL> AddFormInputs = add_form_inputs <EOL> def _copy ( self ) : <EOL> """<STR_LIT>""" <EOL> copied_uri = Uri ( self . uri . scheme , self . uri . host , self . uri . port , <EOL> self . uri . path , self . uri . query . copy ( ) ) <EOL> new_request = HttpRequest ( uri = copied_uri , method = self . method , <EOL> headers = self . headers . copy ( ) ) <EOL> new_request . _body_parts = self . _body_parts [ : ] <EOL> return new_request <EOL> def _dump ( self ) : <EOL> """<STR_LIT>""" <EOL> output = '<STR_LIT>' % ( <EOL> self . method , str ( self . uri ) ) <EOL> for header , value in self . headers . iteritems ( ) : <EOL> output += '<STR_LIT>' % ( header , value ) <EOL> output += '<STR_LIT>' <EOL> i = <NUM_LIT:0> <EOL> for part in self . _body_parts : <EOL> if isinstance ( part , ( str , unicode ) ) : <EOL> output += '<STR_LIT>' % ( i , part ) <EOL> else : <EOL> output += '<STR_LIT>' % i <EOL> i += <NUM_LIT:1> <EOL> return output <EOL> def _apply_defaults ( http_request ) : <EOL> if http_request . uri . scheme is None : <EOL> if http_request . uri . port == <NUM_LIT> : <EOL> http_request . uri . scheme = '<STR_LIT>' <EOL> else : <EOL> http_request . uri . scheme = '<STR_LIT:http>' <EOL> class Uri ( object ) : <EOL> """<STR_LIT>""" <EOL> scheme = None <EOL> host = None <EOL> port = None <EOL> path = None <EOL> def __init__ ( self , scheme = None , host = None , port = None , path = None , query = None ) : <EOL> """<STR_LIT>""" <EOL> self . query = query or { } <EOL> if scheme is not None : <EOL> self . scheme = scheme <EOL> if host is not None : <EOL> self . host = host <EOL> if port is not None : <EOL> self . port = port <EOL> if path : <EOL> self . path = path <EOL> def _get_query_string ( self ) : <EOL> param_pairs = [ ] <EOL> for key , value in self . query . iteritems ( ) : <EOL> quoted_key = urllib . quote_plus ( str ( key ) ) <EOL> if value is None : <EOL> param_pairs . append ( quoted_key ) <EOL> else : <EOL> quoted_value = urllib . quote_plus ( str ( value ) ) <EOL> param_pairs . append ( '<STR_LIT>' % ( quoted_key , quoted_value ) ) <EOL> return '<STR_LIT:&>' . join ( param_pairs ) <EOL> def _get_relative_path ( self ) : <EOL> """<STR_LIT>""" <EOL> param_string = self . _get_query_string ( ) <EOL> if self . path is None : <EOL> path = '<STR_LIT:/>' <EOL> else : <EOL> path = self . path <EOL> if param_string : <EOL> return '<STR_LIT:?>' . join ( [ path , param_string ] ) <EOL> else : <EOL> return path <EOL> def _to_string ( self ) : <EOL> if self . scheme is None and self . port == <NUM_LIT> : <EOL> scheme = '<STR_LIT>' <EOL> elif self . scheme is None : <EOL> scheme = '<STR_LIT:http>' <EOL> else : <EOL> scheme = self . scheme <EOL> if self . path is None : <EOL> path = '<STR_LIT:/>' <EOL> else : <EOL> path = self . path <EOL> if self . port is None : <EOL> return '<STR_LIT>' % ( scheme , self . host , self . _get_relative_path ( ) ) <EOL> else : <EOL> return '<STR_LIT>' % ( scheme , self . host , str ( self . port ) , <EOL> self . _get_relative_path ( ) ) <EOL> def __str__ ( self ) : <EOL> return self . _to_string ( ) <EOL> def modify_request ( self , http_request = None ) : <EOL> """<STR_LIT>""" <EOL> if http_request is None : <EOL> http_request = HttpRequest ( ) <EOL> if http_request . uri is None : <EOL> http_request . uri = Uri ( ) <EOL> if self . scheme : <EOL> http_request . uri . scheme = self . scheme <EOL> if self . port : <EOL> http_request . uri . port = self . port <EOL> if self . host : <EOL> http_request . uri . host = self . host <EOL> if self . path : <EOL> http_request . uri . path = self . path <EOL> if self . query : <EOL> http_request . uri . query = self . query . copy ( ) <EOL> return http_request <EOL> ModifyRequest = modify_request <EOL> def parse_uri ( uri_string ) : <EOL> """<STR_LIT>""" <EOL> parts = urlparse . urlparse ( uri_string ) <EOL> uri = Uri ( ) <EOL> if parts [ <NUM_LIT:0> ] : <EOL> uri . scheme = parts [ <NUM_LIT:0> ] <EOL> if parts [ <NUM_LIT:1> ] : <EOL> host_parts = parts [ <NUM_LIT:1> ] . split ( '<STR_LIT::>' ) <EOL> if host_parts [ <NUM_LIT:0> ] : <EOL> uri . host = host_parts [ <NUM_LIT:0> ] <EOL> if len ( host_parts ) > <NUM_LIT:1> : <EOL> uri . port = int ( host_parts [ <NUM_LIT:1> ] ) <EOL> if parts [ <NUM_LIT:2> ] : <EOL> uri . path = parts [ <NUM_LIT:2> ] <EOL> if parts [ <NUM_LIT:4> ] : <EOL> param_pairs = parts [ <NUM_LIT:4> ] . split ( '<STR_LIT:&>' ) <EOL> for pair in param_pairs : <EOL> pair_parts = pair . split ( '<STR_LIT:=>' ) <EOL> if len ( pair_parts ) > <NUM_LIT:1> : <EOL> uri . query [ urllib . unquote_plus ( pair_parts [ <NUM_LIT:0> ] ) ] = ( <EOL> urllib . unquote_plus ( pair_parts [ <NUM_LIT:1> ] ) ) <EOL> elif len ( pair_parts ) == <NUM_LIT:1> : <EOL> uri . query [ urllib . unquote_plus ( pair_parts [ <NUM_LIT:0> ] ) ] = None <EOL> return uri <EOL> parse_uri = staticmethod ( parse_uri ) <EOL> ParseUri = parse_uri <EOL> parse_uri = Uri . parse_uri <EOL> ParseUri = Uri . parse_uri <EOL> class HttpResponse ( object ) : <EOL> status = None <EOL> reason = None <EOL> _body = None <EOL> def __init__ ( self , status = None , reason = None , headers = None , body = None ) : <EOL> self . _headers = headers or { } <EOL> if status is not None : <EOL> self . status = status <EOL> if reason is not None : <EOL> self . reason = reason <EOL> if body is not None : <EOL> if hasattr ( body , '<STR_LIT>' ) : <EOL> self . _body = body <EOL> else : <EOL> self . _body = StringIO . StringIO ( body ) <EOL> def getheader ( self , name , default = None ) : <EOL> if name in self . _headers : <EOL> return self . _headers [ name ] <EOL> else : <EOL> return default <EOL> def getheaders ( self ) : <EOL> return self . _headers <EOL> def read ( self , amt = None ) : <EOL> if self . _body is None : <EOL> return None <EOL> if not amt : <EOL> return self . _body . read ( ) <EOL> else : <EOL> return self . _body . read ( amt ) <EOL> def _dump_response ( http_response ) : <EOL> """<STR_LIT>""" <EOL> output = '<STR_LIT>' % ( <EOL> http_response . status , http_response . reason ) <EOL> headers = get_headers ( http_response ) <EOL> if isinstance ( headers , dict ) : <EOL> for header , value in headers . iteritems ( ) : <EOL> output += '<STR_LIT>' % ( header , value ) <EOL> else : <EOL> for pair in headers : <EOL> output += '<STR_LIT>' % ( pair [ <NUM_LIT:0> ] , pair [ <NUM_LIT:1> ] ) <EOL> return output <EOL> class HttpClient ( object ) : <EOL> """<STR_LIT>""" <EOL> debug = None <EOL> def request ( self , http_request ) : <EOL> return self . _http_request ( http_request . method , http_request . uri , <EOL> http_request . headers , http_request . _body_parts ) <EOL> Request = request <EOL> def _get_connection ( self , uri , headers = None ) : <EOL> """<STR_LIT>""" <EOL> connection = None <EOL> if uri . scheme == '<STR_LIT>' : <EOL> if not uri . port : <EOL> connection = httplib . HTTPSConnection ( uri . host ) <EOL> else : <EOL> connection = httplib . HTTPSConnection ( uri . host , int ( uri . port ) ) <EOL> else : <EOL> if not uri . port : <EOL> connection = httplib . HTTPConnection ( uri . host ) <EOL> else : <EOL> connection = httplib . HTTPConnection ( uri . host , int ( uri . port ) ) <EOL> return connection <EOL> def _http_request ( self , method , uri , headers = None , body_parts = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( uri , ( str , unicode ) ) : <EOL> uri = Uri . parse_uri ( uri ) <EOL> connection = self . _get_connection ( uri , headers = headers ) <EOL> if self . debug : <EOL> connection . debuglevel = <NUM_LIT:1> <EOL> if connection . host != uri . host : <EOL> connection . putrequest ( method , str ( uri ) ) <EOL> else : <EOL> connection . putrequest ( method , uri . _get_relative_path ( ) ) <EOL> if ( uri . scheme == '<STR_LIT>' and int ( uri . port or <NUM_LIT> ) == <NUM_LIT> and <EOL> hasattr ( connection , '<STR_LIT>' ) and <EOL> isinstance ( connection . _buffer , list ) ) : <EOL> header_line = '<STR_LIT>' % uri . host <EOL> replacement_header_line = '<STR_LIT>' % uri . host <EOL> try : <EOL> connection . _buffer [ connection . _buffer . index ( header_line ) ] = ( <EOL> replacement_header_line ) <EOL> except ValueError : <EOL> pass <EOL> for header_name , value in headers . iteritems ( ) : <EOL> connection . putheader ( header_name , value ) <EOL> connection . endheaders ( ) <EOL> if body_parts and filter ( lambda x : x != '<STR_LIT>' , body_parts ) : <EOL> for part in body_parts : <EOL> _send_data_part ( part , connection ) <EOL> return connection . getresponse ( ) <EOL> def _send_data_part ( data , connection ) : <EOL> if isinstance ( data , ( str , unicode ) ) : <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 <EOL> class ProxiedHttpClient ( HttpClient ) : <EOL> def _get_connection ( self , uri , headers = None ) : <EOL> proxy = None <EOL> if uri . scheme == '<STR_LIT>' : <EOL> proxy = os . environ . get ( '<STR_LIT>' ) <EOL> elif uri . scheme == '<STR_LIT:http>' : <EOL> proxy = os . environ . get ( '<STR_LIT>' ) <EOL> if not proxy : <EOL> return HttpClient . _get_connection ( self , uri , headers = headers ) <EOL> proxy_auth = _get_proxy_auth ( ) <EOL> if uri . scheme == '<STR_LIT>' : <EOL> import socket <EOL> if proxy_auth : <EOL> proxy_auth = '<STR_LIT>' % proxy_auth <EOL> port = uri . port <EOL> if not port : <EOL> port = <NUM_LIT> <EOL> proxy_connect = '<STR_LIT>' % ( uri . host , port ) <EOL> user_agent = '<STR_LIT>' <EOL> if headers and '<STR_LIT>' in headers : <EOL> user_agent = '<STR_LIT>' % ( headers [ '<STR_LIT>' ] ) <EOL> proxy_pieces = '<STR_LIT>' % ( proxy_connect , proxy_auth , user_agent ) <EOL> proxy_uri = Uri . parse_uri ( proxy ) <EOL> if not proxy_uri . port : <EOL> proxy_uri . port = '<STR_LIT>' <EOL> p_sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> p_sock . connect ( ( proxy_uri . host , int ( proxy_uri . 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> sslobj = None <EOL> if ssl is not None : <EOL> sslobj = ssl . wrap_socket ( p_sock , None , None ) <EOL> else : <EOL> sock_ssl = socket . ssl ( p_sock , None , Nonesock_ ) <EOL> sslobj = httplib . FakeSocket ( p_sock , sock_ssl ) <EOL> connection = httplib . HTTPConnection ( proxy_uri . host ) <EOL> connection . sock = sslobj <EOL> return connection <EOL> elif uri . scheme == '<STR_LIT:http>' : <EOL> proxy_uri = Uri . parse_uri ( proxy ) <EOL> if not proxy_uri . port : <EOL> proxy_uri . port = '<STR_LIT>' <EOL> if proxy_auth : <EOL> headers [ '<STR_LIT>' ] = proxy_auth . strip ( ) <EOL> return httplib . HTTPConnection ( proxy_uri . host , int ( proxy_uri . port ) ) <EOL> return None <EOL> def _get_proxy_auth ( ) : <EOL> import base64 <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 . b64encode ( '<STR_LIT>' % ( proxy_username , <EOL> proxy_password ) ) <EOL> return '<STR_LIT>' % ( user_auth . strip ( ) ) <EOL> else : <EOL> return '<STR_LIT>' </s>
