summaryrefslogtreecommitdiff
path: root/swiftclient
diff options
context:
space:
mode:
authorTim Burke <tim.burke@gmail.com>2017-08-25 12:13:12 -0700
committerTim Burke <tim.burke@gmail.com>2017-08-25 12:26:03 -0700
commitae5fd46e8772e50a0ab2ac89216188b1cdc83d8e (patch)
treea0144b7d309508b1eda4da7ce1cd5de06366696d /swiftclient
parentf323bb4824d98e74fdd5e357526ce678d0f7d45a (diff)
downloadpython-swiftclient-ae5fd46e8772e50a0ab2ac89216188b1cdc83d8e.tar.gz
Stop mutating header dicts
Change-Id: Ia1638c216eff9db6fbe416bc0570c27cfdcfe730
Diffstat (limited to 'swiftclient')
-rw-r--r--swiftclient/client.py65
1 files changed, 34 insertions, 31 deletions
diff --git a/swiftclient/client.py b/swiftclient/client.py
index 80bc4a3..95e89b8 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -302,7 +302,7 @@ class _RetryBody(_ObjectBody):
self.obj = obj
self.query_string = query_string
self.response_dict = response_dict
- self.headers = headers if headers is not None else {}
+ self.headers = dict(headers) if headers is not None else {}
self.bytes_read = 0
def read(self, length=None):
@@ -834,13 +834,15 @@ def post_account(url, token, headers, http_conn=None, response_dict=None,
path = parsed.path
if query_string:
path += '?' + query_string
- headers['X-Auth-Token'] = token
+ req_headers = {'X-Auth-Token': token}
if service_token:
- headers['X-Service-Token'] = service_token
- conn.request(method, path, data, headers)
+ req_headers['X-Service-Token'] = service_token
+ if headers:
+ req_headers.update(headers)
+ conn.request(method, path, data, req_headers)
resp = conn.getresponse()
body = resp.read()
- http_log((url, method,), {'headers': headers}, resp, body)
+ http_log((url, method,), {'headers': req_headers}, resp, body)
store_response(resp, response_dict)
@@ -882,12 +884,6 @@ def get_container(url, token, container, marker=None, limit=None,
"""
if not http_conn:
http_conn = http_connection(url)
- if headers:
- headers = dict(headers)
- else:
- headers = {}
- headers['X-Auth-Token'] = token
- headers['Accept-Encoding'] = 'gzip'
if full_listing:
rv = get_container(url, token, container, marker, limit, prefix,
delimiter, end_marker, path, http_conn,
@@ -922,17 +918,20 @@ def get_container(url, token, container, marker=None, limit=None,
qs += '&path=%s' % quote(path)
if query_string:
qs += '&%s' % query_string.lstrip('?')
+ req_headers = {'X-Auth-Token': token, 'Accept-Encoding': 'gzip'}
if service_token:
- headers['X-Service-Token'] = service_token
+ req_headers['X-Service-Token'] = service_token
+ if headers:
+ req_headers.update(headers)
method = 'GET'
- conn.request(method, '%s?%s' % (cont_path, qs), '', headers)
+ conn.request(method, '%s?%s' % (cont_path, qs), '', req_headers)
resp = conn.getresponse()
body = resp.read()
http_log(('%(url)s%(cont_path)s?%(qs)s' %
{'url': url.replace(parsed.path, ''),
'cont_path': cont_path,
'qs': qs}, method,),
- {'headers': headers}, resp, body)
+ {'headers': req_headers}, resp, body)
if resp.status < 200 or resp.status >= 300:
raise ClientException.from_response(resp, 'Container GET failed', body)
@@ -1005,23 +1004,23 @@ def put_container(url, token, container, headers=None, http_conn=None,
parsed, conn = http_connection(url)
path = '%s/%s' % (parsed.path, quote(container))
method = 'PUT'
- if not headers:
- headers = {}
- headers['X-Auth-Token'] = token
+ req_headers = {'X-Auth-Token': token}
if service_token:
- headers['X-Service-Token'] = service_token
- if 'content-length' not in (k.lower() for k in headers):
- headers['Content-Length'] = '0'
+ req_headers['X-Service-Token'] = service_token
+ if headers:
+ req_headers.update(headers)
+ if 'content-length' not in (k.lower() for k in req_headers):
+ req_headers['Content-Length'] = '0'
if query_string:
path += '?' + query_string.lstrip('?')
- conn.request(method, path, '', headers)
+ conn.request(method, path, '', req_headers)
resp = conn.getresponse()
body = resp.read()
store_response(resp, response_dict)
http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,),
- {'headers': headers}, resp, body)
+ {'headers': req_headers}, resp, body)
if resp.status < 200 or resp.status >= 300:
raise ClientException.from_response(resp, 'Container PUT failed', body)
@@ -1048,16 +1047,18 @@ def post_container(url, token, container, headers, http_conn=None,
parsed, conn = http_connection(url)
path = '%s/%s' % (parsed.path, quote(container))
method = 'POST'
- headers['X-Auth-Token'] = token
+ req_headers = {'X-Auth-Token': token}
if service_token:
- headers['X-Service-Token'] = service_token
+ req_headers['X-Service-Token'] = service_token
+ if headers:
+ req_headers.update(headers)
if 'content-length' not in (k.lower() for k in headers):
- headers['Content-Length'] = '0'
- conn.request(method, path, '', headers)
+ req_headers['Content-Length'] = '0'
+ conn.request(method, path, '', req_headers)
resp = conn.getresponse()
body = resp.read()
http_log(('%s%s' % (url.replace(parsed.path, ''), path), method,),
- {'headers': headers}, resp, body)
+ {'headers': req_headers}, resp, body)
store_response(resp, response_dict)
@@ -1352,14 +1353,16 @@ def post_object(url, token, container, name, headers, http_conn=None,
else:
parsed, conn = http_connection(url)
path = '%s/%s/%s' % (parsed.path, quote(container), quote(name))
- headers['X-Auth-Token'] = token
+ req_headers = {'X-Auth-Token': token}
if service_token:
- headers['X-Service-Token'] = service_token
- conn.request('POST', path, '', headers)
+ req_headers['X-Service-Token'] = service_token
+ if headers:
+ req_headers.update(headers)
+ conn.request('POST', path, '', req_headers)
resp = conn.getresponse()
body = resp.read()
http_log(('%s%s' % (url.replace(parsed.path, ''), path), 'POST',),
- {'headers': headers}, resp, body)
+ {'headers': req_headers}, resp, body)
store_response(resp, response_dict)