<s> import base64 <EOL> import sys <EOL> if sys . version_info < ( <NUM_LIT:3> , ) : <EOL> from httplib import ( <EOL> HTTP_PORT , <EOL> HTTPS_PORT , <EOL> ) <EOL> from urlparse import urlparse <EOL> from urllib2 import quote as url_quote <EOL> else : <EOL> from http . client import ( <EOL> HTTP_PORT , <EOL> HTTPS_PORT , <EOL> ) <EOL> from urllib . parse import urlparse <EOL> from urllib . parse import quote as url_quote <EOL> from . import HTTPError , HTTPResponse <EOL> from . requestsclient import _RequestsConnection <EOL> DEBUG_REQUESTS = False <EOL> DEBUG_RESPONSES = False <EOL> class _HTTPClient ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , service_instance , cert_file = None , protocol = '<STR_LIT>' , <EOL> request_session = None , timeout = None , user_agent = '<STR_LIT>' ) : <EOL> '''<STR_LIT>''' <EOL> self . service_instance = service_instance <EOL> self . cert_file = cert_file <EOL> self . protocol = protocol <EOL> self . proxy_host = None <EOL> self . proxy_port = None <EOL> self . proxy_user = None <EOL> self . proxy_password = None <EOL> self . request_session = request_session <EOL> self . timeout = timeout <EOL> self . user_agent = user_agent <EOL> def set_proxy ( self , host , port , user , password ) : <EOL> '''<STR_LIT>''' <EOL> self . proxy_host = host <EOL> self . proxy_port = port <EOL> self . proxy_user = user <EOL> self . proxy_password = password <EOL> def get_uri ( self , request ) : <EOL> '''<STR_LIT>''' <EOL> protocol = request . protocol_override if request . protocol_override else self . protocol <EOL> protocol = protocol . lower ( ) <EOL> port = HTTP_PORT if protocol == '<STR_LIT:http>' else HTTPS_PORT <EOL> return protocol + '<STR_LIT>' + request . host + '<STR_LIT::>' + str ( port ) + request . path <EOL> def get_connection ( self , request ) : <EOL> '''<STR_LIT>''' <EOL> protocol = request . protocol_override if request . protocol_override else self . protocol <EOL> protocol = protocol . lower ( ) <EOL> target_host = request . host <EOL> target_port = HTTP_PORT if protocol == '<STR_LIT:http>' else HTTPS_PORT <EOL> connection = _RequestsConnection ( <EOL> target_host , protocol , self . request_session , self . timeout ) <EOL> proxy_host = self . proxy_host <EOL> proxy_port = self . proxy_port <EOL> if self . proxy_host : <EOL> headers = None <EOL> if self . proxy_user and self . proxy_password : <EOL> auth = base64 . encodestring ( <EOL> "<STR_LIT>" . format ( self . proxy_user , self . proxy_password ) . encode ( ) ) . rstrip ( ) <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' . format ( auth . decode ( ) ) } <EOL> connection . set_tunnel ( proxy_host , int ( proxy_port ) , headers ) <EOL> return connection <EOL> def send_request_headers ( self , connection , request_headers ) : <EOL> if self . proxy_host and self . request_session is None : <EOL> for i in connection . _buffer : <EOL> if i . startswith ( b"<STR_LIT>" ) : <EOL> connection . _buffer . remove ( i ) <EOL> connection . putheader ( <EOL> '<STR_LIT>' , "<STR_LIT>" . format ( connection . _tunnel_host , <EOL> connection . _tunnel_port ) ) <EOL> for name , value in request_headers : <EOL> if value : <EOL> connection . putheader ( name , value ) <EOL> connection . putheader ( '<STR_LIT>' , self . user_agent ) <EOL> connection . endheaders ( ) <EOL> def send_request_body ( self , connection , request_body ) : <EOL> if request_body : <EOL> assert isinstance ( request_body , bytes ) <EOL> connection . send ( request_body ) <EOL> else : <EOL> connection . send ( None ) <EOL> def _update_request_uri_query ( self , request ) : <EOL> '''<STR_LIT>''' <EOL> if '<STR_LIT:?>' in request . path : <EOL> request . path , _ , query_string = request . path . partition ( '<STR_LIT:?>' ) <EOL> if query_string : <EOL> query_params = query_string . split ( '<STR_LIT:&>' ) <EOL> for query in query_params : <EOL> if '<STR_LIT:=>' in query : <EOL> name , _ , value = query . partition ( '<STR_LIT:=>' ) <EOL> request . query . append ( ( name , value ) ) <EOL> request . path = url_quote ( request . path , '<STR_LIT>' ) <EOL> if request . query : <EOL> request . path += '<STR_LIT:?>' <EOL> for name , value in request . query : <EOL> if value is not None : <EOL> request . path += name + '<STR_LIT:=>' + url_quote ( value , '<STR_LIT>' ) + '<STR_LIT:&>' <EOL> request . path = request . path [ : - <NUM_LIT:1> ] <EOL> return request . path , request . query <EOL> def perform_request ( self , request ) : <EOL> '''<STR_LIT>''' <EOL> connection = self . get_connection ( request ) <EOL> try : <EOL> connection . putrequest ( request . method , request . path ) <EOL> self . send_request_headers ( connection , request . headers ) <EOL> self . send_request_body ( connection , request . body ) <EOL> if DEBUG_REQUESTS and request . body : <EOL> print ( '<STR_LIT>' ) <EOL> try : <EOL> print ( request . body ) <EOL> except : <EOL> pass <EOL> resp = connection . getresponse ( ) <EOL> status = int ( resp . status ) <EOL> message = resp . reason <EOL> respheaders = resp . getheaders ( ) <EOL> for i , value in enumerate ( respheaders ) : <EOL> respheaders [ i ] = ( value [ <NUM_LIT:0> ] . lower ( ) , value [ <NUM_LIT:1> ] ) <EOL> respbody = None <EOL> if resp . length is None : <EOL> respbody = resp . read ( ) <EOL> elif resp . length > <NUM_LIT:0> : <EOL> respbody = resp . read ( resp . length ) <EOL> if DEBUG_RESPONSES and respbody : <EOL> print ( '<STR_LIT>' ) <EOL> try : <EOL> print ( respbody ) <EOL> except : <EOL> pass <EOL> response = HTTPResponse ( <EOL> status , resp . reason , respheaders , respbody ) <EOL> if status == <NUM_LIT> : <EOL> new_url = urlparse ( dict ( headers ) [ '<STR_LIT:location>' ] ) <EOL> request . host = new_url . hostname <EOL> request . path = new_url . path <EOL> request . path , request . query = self . _update_request_uri_query ( request ) <EOL> return self . perform_request ( request ) <EOL> if status >= <NUM_LIT> : <EOL> raise HTTPError ( status , message , respheaders , respbody ) <EOL> return response <EOL> finally : <EOL> connection . close ( ) </s>
