summaryrefslogtreecommitdiff
path: root/swiftclient
diff options
context:
space:
mode:
authorTimur Alperovich <timur@timuralp.com>2018-04-17 14:36:57 -0700
committerTimur Alperovich <timur@timuralp.com>2018-07-23 14:38:40 -0700
commitf4a2b16c2cc65410765abdff7a45532305a4548f (patch)
tree918fef8f12ec8cfdebddb378753c1d4aa5ec6047 /swiftclient
parent23d29eda8d03785c22d67930e21a0ba4098ba23c (diff)
downloadpython-swiftclient-f4a2b16c2cc65410765abdff7a45532305a4548f.tar.gz
Properly handle unicode headers.
Fix unicode handling in Python 3 and Python 2. There are currently two failure modes. In python 2, swiftclient fails to log in debug mode if the account name has a non-ASCII character. This is because the account name will appear in the storage URL, which we attempt to pass to the logger as a byte string (whereas it should be a unicode string). This patch changes the behavior to convert the path strings into unicode by calling the parse_header_string() function. The second failure mode is with Python 3, where http_lib returns headers that are latin-1 encoded, but swiftclient expects UTF-8. The patch automatically converts headers from latin-1 (iso-8859-1) to UTF-8, so that we can properly handle non-ASCII headers in responses. Change-Id: Ifa7f3d5af71bde8127129f1f8603772d80d063c1
Diffstat (limited to 'swiftclient')
-rw-r--r--swiftclient/client.py29
1 files changed, 24 insertions, 5 deletions
diff --git a/swiftclient/client.py b/swiftclient/client.py
index 8cbdf45..a518d32 100644
--- a/swiftclient/client.py
+++ b/swiftclient/client.py
@@ -151,7 +151,7 @@ def http_log(args, kwargs, resp, body):
elif element in ('GET', 'POST', 'PUT'):
string_parts.append(' -X %s' % element)
else:
- string_parts.append(' %s' % element)
+ string_parts.append(' %s' % parse_header_string(element))
if 'headers' in kwargs:
headers = scrub_headers(kwargs['headers'])
for element in headers:
@@ -455,11 +455,23 @@ class HTTPConnection(object):
self.resp.status = self.resp.status_code
old_getheader = self.resp.raw.getheader
+ def _decode_header(string):
+ if string is None or six.PY2:
+ return string
+ return string.encode('iso-8859-1').decode('utf-8')
+
+ def _encode_header(string):
+ if string is None or six.PY2:
+ return string
+ return string.encode('utf-8').decode('iso-8859-1')
+
def getheaders():
- return self.resp.headers.items()
+ return [(_decode_header(k), _decode_header(v))
+ for k, v in self.resp.headers.items()]
def getheader(k, v=None):
- return old_getheader(k.lower(), v)
+ return _decode_header(old_getheader(
+ _encode_header(k.lower()), _encode_header(v)))
def releasing_read(*args, **kwargs):
chunk = self.resp.raw.read(*args, **kwargs)
@@ -513,8 +525,11 @@ def get_auth_1_0(url, user, key, snet, **kwargs):
netloc = parsed[1]
parsed[1] = 'snet-' + netloc
url = urlunparse(parsed)
- return url, resp.getheader('x-storage-token',
- resp.getheader('x-auth-token'))
+
+ auth_token = resp.getheader('x-auth-token')
+ if auth_token is not None:
+ auth_token = parse_header_string(auth_token)
+ return url, resp.getheader('x-storage-token', auth_token)
def get_keystoneclient_2_0(auth_url, user, key, os_options, **kwargs):
@@ -694,10 +709,14 @@ def get_auth(auth_url, user, key, **kwargs):
raise ClientException('Unknown auth_version %s specified and no '
'session found.' % auth_version)
+ if token is not None:
+ token = parse_header_string(token)
# Override storage url, if necessary
if os_options.get('object_storage_url'):
return os_options['object_storage_url'], token
else:
+ if storage_url is not None:
+ return parse_header_string(storage_url), token
return storage_url, token