Skip to content
This repository was archived by the owner on Nov 29, 2023. It is now read-only.

Commit cdc06d9

Browse files
committed
Simplify http(s) connection instantiation
The endpoint parsing and connection instantiation code was too complicated and easily broken. This assigns human-readable names to instance variables and breaks up the parsing into more understandable chunks. Fixes bug 1060316. Change-Id: I5c5236f90d88b9e797cf0a476aabe8cc7cfa1cc9
1 parent 11e6aad commit cdc06d9

1 file changed

Lines changed: 29 additions & 19 deletions

File tree

glanceclient/common/http.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,46 @@ class HTTPClient(object):
5050

5151
def __init__(self, endpoint, **kwargs):
5252
self.endpoint = endpoint
53+
endpoint_parts = self.parse_endpoint(self.endpoint)
54+
self.endpoint_scheme = endpoint_parts.scheme
55+
self.endpoint_hostname = endpoint_parts.hostname
56+
self.endpoint_port = endpoint_parts.port
57+
self.endpoint_path = endpoint_parts.path
58+
59+
self.connection_class = self.get_connection_class(self.endpoint_scheme)
60+
self.connection_kwargs = self.get_connection_kwargs(
61+
self.endpoint_scheme, **kwargs)
62+
5363
self.auth_token = kwargs.get('token')
54-
self.connection_params = self.get_connection_params(endpoint, **kwargs)
5564

5665
@staticmethod
57-
def get_connection_params(endpoint, **kwargs):
58-
parts = urlparse.urlparse(endpoint)
66+
def parse_endpoint(endpoint):
67+
return urlparse.urlparse(endpoint)
5968

60-
_args = (parts.hostname, parts.port, parts.path)
69+
@staticmethod
70+
def get_connection_class(scheme):
71+
if scheme == 'https':
72+
return VerifiedHTTPSConnection
73+
else:
74+
return httplib.HTTPConnection
75+
76+
@staticmethod
77+
def get_connection_kwargs(scheme, **kwargs):
6178
_kwargs = {'timeout': float(kwargs.get('timeout', 600))}
6279

63-
if parts.scheme == 'https':
64-
_class = VerifiedHTTPSConnection
80+
if scheme == 'https':
6581
_kwargs['ca_file'] = kwargs.get('ca_file', None)
6682
_kwargs['cert_file'] = kwargs.get('cert_file', None)
6783
_kwargs['key_file'] = kwargs.get('key_file', None)
6884
_kwargs['insecure'] = kwargs.get('insecure', False)
69-
elif parts.scheme == 'http':
70-
_class = httplib.HTTPConnection
71-
else:
72-
msg = 'Unsupported scheme: %s' % parts.scheme
73-
raise exc.InvalidEndpoint(msg)
7485

75-
return (_class, _args, _kwargs)
86+
return _kwargs
7687

7788
def get_connection(self):
78-
_class = self.connection_params[0]
89+
_class = self.connection_class
7990
try:
80-
return _class(*self.connection_params[1],
81-
**self.connection_params[2])
91+
return _class(self.endpoint_hostname, self.endpoint_port,
92+
**self.connection_kwargs)
8293
except httplib.InvalidURL:
8394
raise exc.InvalidEndpoint()
8495

@@ -95,11 +106,11 @@ def log_curl_request(self, method, url, kwargs):
95106
('ca_file', '--cacert %s'),
96107
]
97108
for (key, fmt) in conn_params_fmt:
98-
value = self.connection_params[2].get(key)
109+
value = self.connection_kwargs.get(key)
99110
if value:
100111
curl.append(fmt % value)
101112

102-
if self.connection_params[2].get('insecure'):
113+
if self.connection_kwargs.get('insecure'):
103114
curl.append('-k')
104115

105116
if 'body' in kwargs:
@@ -134,8 +145,7 @@ def _http_request(self, url, method, **kwargs):
134145
conn = self.get_connection()
135146

136147
try:
137-
conn_params = self.connection_params[1][2]
138-
conn_url = os.path.normpath('%s/%s' % (conn_params, url))
148+
conn_url = os.path.normpath('%s/%s' % (self.endpoint_path, url))
139149
conn.request(method, conn_url, **kwargs)
140150
resp = conn.getresponse()
141151
except socket.gaierror as e:

0 commit comments

Comments
 (0)