<s> import collections <EOL> import functools <EOL> import httplib <EOL> import os <EOL> import urllib <EOL> import six . moves . urllib . parse as urlparse <EOL> try : <EOL> from eventlet . green import socket <EOL> from eventlet . green import ssl <EOL> except ImportError : <EOL> import socket <EOL> import ssl <EOL> from heat_cfnclient . common import auth <EOL> from heat_cfnclient . common import exception <EOL> from heat_cfnclient . common import utils <EOL> from heat_cfnclient . openstack . common . gettextutils import _ <EOL> CHUNKSIZE = <NUM_LIT> <EOL> def handle_unauthorized ( func ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( func ) <EOL> def wrapped ( self , * args , ** kwargs ) : <EOL> try : <EOL> return func ( self , * args , ** kwargs ) <EOL> except exception . NotAuthorized : <EOL> self . _authenticate ( force_reauth = True ) <EOL> return func ( self , * args , ** kwargs ) <EOL> return wrapped <EOL> def handle_redirects ( func ) : <EOL> """<STR_LIT>""" <EOL> MAX_REDIRECTS = <NUM_LIT:5> <EOL> @ functools . wraps ( func ) <EOL> def wrapped ( self , method , url , body , headers ) : <EOL> for _dum in xrange ( MAX_REDIRECTS ) : <EOL> try : <EOL> return func ( self , method , url , body , headers ) <EOL> except exception . RedirectException as redirect : <EOL> if redirect . url is None : <EOL> raise exception . InvalidRedirect ( ) <EOL> url = redirect . url <EOL> raise exception . MaxRedirectsExceeded ( redirects = MAX_REDIRECTS ) <EOL> return wrapped <EOL> class ImageBodyIterator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source ) : <EOL> """<STR_LIT>""" <EOL> self . source = source <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> chunk = self . source . read ( CHUNKSIZE ) <EOL> if chunk : <EOL> yield chunk <EOL> else : <EOL> break <EOL> class HTTPSClientAuthConnection ( httplib . HTTPSConnection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port , key_file , cert_file , <EOL> ca_file , timeout = None , insecure = False ) : <EOL> httplib . HTTPSConnection . __init__ ( self , host , port , key_file = key_file , <EOL> cert_file = cert_file ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> self . ca_file = ca_file <EOL> self . timeout = timeout <EOL> self . insecure = insecure <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> sock = socket . create_connection ( ( self . host , self . port ) , self . timeout ) <EOL> if self . _tunnel_host : <EOL> self . sock = sock <EOL> self . _tunnel ( ) <EOL> if self . insecure is True : <EOL> self . sock = ssl . wrap_socket ( sock , self . key_file , self . cert_file , <EOL> cert_reqs = ssl . CERT_NONE ) <EOL> else : <EOL> self . sock = ssl . wrap_socket ( sock , self . key_file , self . cert_file , <EOL> ca_certs = self . ca_file , <EOL> cert_reqs = ssl . CERT_REQUIRED ) <EOL> class BaseClient ( object ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_PORT = <NUM_LIT> <EOL> DEFAULT_DOC_ROOT = None <EOL> DEFAULT_CA_FILE_PATH = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> OK_RESPONSE_CODES = ( <EOL> httplib . OK , <EOL> httplib . CREATED , <EOL> httplib . ACCEPTED , <EOL> httplib . NO_CONTENT , <EOL> ) <EOL> REDIRECT_RESPONSE_CODES = ( <EOL> httplib . MOVED_PERMANENTLY , <EOL> httplib . FOUND , <EOL> httplib . SEE_OTHER , <EOL> httplib . USE_PROXY , <EOL> httplib . TEMPORARY_REDIRECT , <EOL> ) <EOL> def __init__ ( self , host = None , port = None , use_ssl = False , auth_tok = None , <EOL> creds = None , doc_root = None , key_file = None , <EOL> cert_file = None , ca_file = None , insecure = False , <EOL> configure_via_auth = True , service_type = None ) : <EOL> """<STR_LIT>""" <EOL> self . host = host <EOL> self . port = port or self . DEFAULT_PORT <EOL> self . use_ssl = use_ssl <EOL> self . auth_tok = auth_tok <EOL> self . creds = creds or { } <EOL> self . connection = None <EOL> self . configure_via_auth = configure_via_auth <EOL> self . service_type = service_type <EOL> self . doc_root = ( doc_root if doc_root is not None <EOL> else self . DEFAULT_DOC_ROOT ) <EOL> self . auth_plugin = self . make_auth_plugin ( self . creds ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> self . ca_file = ca_file <EOL> self . insecure = insecure <EOL> self . connect_kwargs = self . get_connect_kwargs ( ) <EOL> def get_connect_kwargs ( self ) : <EOL> connect_kwargs = { } <EOL> if self . use_ssl : <EOL> if self . key_file is None : <EOL> self . key_file = os . environ . get ( '<STR_LIT>' ) <EOL> if self . cert_file is None : <EOL> self . cert_file = os . environ . get ( '<STR_LIT>' ) <EOL> if self . ca_file is None : <EOL> self . ca_file = os . environ . get ( '<STR_LIT>' ) <EOL> if self . cert_file is not None and self . key_file is None : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exception . ClientConnectionError ( msg ) <EOL> if self . key_file is not None and self . cert_file is None : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exception . ClientConnectionError ( msg ) <EOL> if ( self . key_file is not None and <EOL> not os . path . exists ( self . key_file ) ) : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % self . key_file <EOL> raise exception . ClientConnectionError ( msg ) <EOL> connect_kwargs [ '<STR_LIT>' ] = self . key_file <EOL> if ( self . cert_file is not None and <EOL> not os . path . exists ( self . cert_file ) ) : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % self . cert_file <EOL> raise exception . ClientConnectionError ( msg ) <EOL> connect_kwargs [ '<STR_LIT>' ] = self . cert_file <EOL> if ( self . ca_file is not None and <EOL> not os . path . exists ( self . ca_file ) ) : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % self . ca_file <EOL> raise exception . ClientConnectionError ( msg ) <EOL> if self . ca_file is None : <EOL> for ca in self . DEFAULT_CA_FILE_PATH . split ( "<STR_LIT::>" ) : <EOL> if os . path . exists ( ca ) : <EOL> self . ca_file = ca <EOL> break <EOL> connect_kwargs [ '<STR_LIT>' ] = self . ca_file <EOL> connect_kwargs [ '<STR_LIT>' ] = self . insecure <EOL> return connect_kwargs <EOL> def set_auth_token ( self , auth_tok ) : <EOL> """<STR_LIT>""" <EOL> self . auth_tok = auth_tok <EOL> def configure_from_url ( self , url ) : <EOL> """<STR_LIT>""" <EOL> parsed = urlparse . urlparse ( url ) <EOL> self . use_ssl = parsed . scheme == '<STR_LIT>' <EOL> if self . host is None : <EOL> self . host = parsed . hostname <EOL> std_port = <NUM_LIT> if self . use_ssl else self . DEFAULT_PORT <EOL> self . port = parsed . port or std_port <EOL> self . doc_root = parsed . path <EOL> self . connect_kwargs = self . get_connect_kwargs ( ) <EOL> def make_auth_plugin ( self , creds ) : <EOL> """<STR_LIT>""" <EOL> strategy = creds . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> plugin = auth . get_plugin_from_strategy ( strategy , <EOL> creds , self . service_type ) <EOL> return plugin <EOL> def get_connection_type ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . use_ssl : <EOL> return HTTPSClientAuthConnection <EOL> else : <EOL> return httplib . HTTPConnection <EOL> def _authenticate ( self , force_reauth = False ) : <EOL> """<STR_LIT>""" <EOL> auth_plugin = self . auth_plugin <EOL> if not auth_plugin . is_authenticated or force_reauth : <EOL> auth_plugin . authenticate ( ) <EOL> self . auth_tok = auth_plugin . auth_token <EOL> management_url = auth_plugin . management_url <EOL> if management_url and self . configure_via_auth : <EOL> self . configure_from_url ( management_url ) <EOL> @ handle_unauthorized <EOL> def do_request ( self , method , action , body = None , headers = None , <EOL> params = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . auth_tok : <EOL> self . _authenticate ( ) <EOL> url = self . _construct_url ( action , params ) <EOL> return self . _do_request ( method = method , url = url , body = body , <EOL> headers = headers ) <EOL> def _construct_url ( self , action , params = None ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT:/>' . join ( [ self . doc_root or '<STR_LIT>' , action . lstrip ( '<STR_LIT:/>' ) ] ) <EOL> scheme = "<STR_LIT>" if self . use_ssl else "<STR_LIT:http>" <EOL> netloc = "<STR_LIT>" % ( self . host , self . port ) <EOL> if isinstance ( params , dict ) : <EOL> for ( key , value ) in params . items ( ) : <EOL> if value is None : <EOL> del params [ key ] <EOL> query = urllib . urlencode ( params ) <EOL> else : <EOL> query = None <EOL> return urlparse . ParseResult ( scheme , netloc , path , '<STR_LIT>' , query , '<STR_LIT>' ) <EOL> @ handle_redirects <EOL> def _do_request ( self , method , url , body , headers ) : <EOL> """<STR_LIT>""" <EOL> if url . query : <EOL> path = url . path + "<STR_LIT:?>" + url . query <EOL> else : <EOL> path = url . path <EOL> try : <EOL> connection_type = self . get_connection_type ( ) <EOL> headers = headers or { } <EOL> if '<STR_LIT>' not in headers and self . auth_tok : <EOL> headers [ '<STR_LIT>' ] = self . auth_tok <EOL> c = connection_type ( url . hostname , url . port , ** self . connect_kwargs ) <EOL> def _pushing ( method ) : <EOL> return method . lower ( ) in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _simple ( body ) : <EOL> return body is None or isinstance ( body , basestring ) <EOL> def _filelike ( body ) : <EOL> return hasattr ( body , '<STR_LIT>' ) <EOL> def _sendbody ( connection , iter ) : <EOL> connection . endheaders ( ) <EOL> for sent in iter : <EOL> pass <EOL> def _chunkbody ( connection , iter ) : <EOL> connection . putheader ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> connection . endheaders ( ) <EOL> for chunk in iter : <EOL> connection . send ( '<STR_LIT>' % ( len ( chunk ) , chunk ) ) <EOL> connection . send ( '<STR_LIT>' ) <EOL> if not _pushing ( method ) or _simple ( body ) : <EOL> c . request ( method , path , body , headers ) <EOL> elif _filelike ( body ) or self . _iterable ( body ) : <EOL> c . putrequest ( method , path ) <EOL> for header , value in headers . items ( ) : <EOL> c . putheader ( header , value ) <EOL> iter = self . image_iterator ( c , headers , body ) <EOL> _chunkbody ( c , iter ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % body . __class__ ) <EOL> res = c . getresponse ( ) <EOL> def _retry ( res ) : <EOL> return res . getheader ( '<STR_LIT>' ) <EOL> status_code = self . get_status_code ( res ) <EOL> if status_code in self . OK_RESPONSE_CODES : <EOL> return res <EOL> elif status_code in self . REDIRECT_RESPONSE_CODES : <EOL> raise exception . RedirectException ( res . getheader ( '<STR_LIT>' ) ) <EOL> elif status_code == httplib . UNAUTHORIZED : <EOL> raise exception . NotAuthorized ( ) <EOL> elif status_code == httplib . FORBIDDEN : <EOL> raise exception . NotAuthorized ( ) <EOL> elif status_code == httplib . NOT_FOUND : <EOL> raise exception . NotFound ( res . read ( ) ) <EOL> elif status_code == httplib . CONFLICT : <EOL> raise exception . Duplicate ( res . read ( ) ) <EOL> elif status_code == httplib . BAD_REQUEST : <EOL> raise exception . Invalid ( reason = res . read ( ) ) <EOL> elif status_code == httplib . MULTIPLE_CHOICES : <EOL> raise exception . MultipleChoices ( body = res . read ( ) ) <EOL> elif status_code == httplib . REQUEST_ENTITY_TOO_LARGE : <EOL> raise exception . LimitExceeded ( retry = _retry ( res ) , <EOL> body = res . read ( ) ) <EOL> elif status_code == httplib . INTERNAL_SERVER_ERROR : <EOL> raise Exception ( "<STR_LIT>" % res . read ( ) ) <EOL> elif status_code == httplib . SERVICE_UNAVAILABLE : <EOL> raise exception . ServiceUnavailable ( retry = _retry ( res ) ) <EOL> elif status_code == httplib . REQUEST_URI_TOO_LONG : <EOL> raise exception . RequestUriTooLong ( body = res . read ( ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % res . read ( ) ) <EOL> except ( socket . error , IOError ) as e : <EOL> raise exception . ClientConnectionError ( e ) <EOL> def _iterable ( self , body ) : <EOL> return isinstance ( body , collections . Iterable ) <EOL> def image_iterator ( self , connection , headers , body ) : <EOL> if self . _iterable ( body ) : <EOL> return utils . chunkreadable ( body ) <EOL> else : <EOL> return ImageBodyIterator ( body ) <EOL> def get_status_code ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( response , '<STR_LIT>' ) : <EOL> return response . status_int <EOL> else : <EOL> return response . status <EOL> def _extract_params ( self , actual_params , allowed_params ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> for param in actual_params : <EOL> if param in allowed_params : <EOL> result [ param ] = actual_params [ param ] <EOL> elif '<STR_LIT>' in param : <EOL> result [ param ] = actual_params [ param ] <EOL> return result </s>
