summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarja Shakhray <dshakhray@mirantis.com>2016-05-13 15:03:41 +0300
committerMike Fedosin <mfedosin@mirantis.com>2016-05-23 14:37:35 +0000
commit4a5567109174a05d434fe336c056ca09eda4b976 (patch)
tree43faea9e843b634c30eec6a95486d24268b07a62
parent02b2f48f9abeb9cc1d31727946d80954d7635849 (diff)
downloadpython-glanceclient-4a5567109174a05d434fe336c056ca09eda4b976.tar.gz
Fix "Codec can't encode characters"
Headers were encoded in HTTPClient, but when glance client started to use SessionClient this functionality was lost. This commit replaces static method "encode_headers" from HTTPClient and makes it a common function, that SessionClient can use when converting image meta to headers. Change-Id: If9f8020220d2a0431b4241b38b9c83c09c0d75cb Closes-bug: #1574587 (cherry picked from commit 9329ef0bc40375cd9b115415e10baf89789f56f0)
-rw-r--r--glanceclient/common/http.py32
-rw-r--r--glanceclient/tests/unit/test_http.py5
2 files changed, 17 insertions, 20 deletions
diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py
index 1157381..aec50fe 100644
--- a/glanceclient/common/http.py
+++ b/glanceclient/common/http.py
@@ -42,6 +42,20 @@ USER_AGENT = 'python-glanceclient'
CHUNKSIZE = 1024 * 64 # 64kB
+def encode_headers(headers):
+ """Encodes headers.
+
+ Note: This should be used right before
+ sending anything out.
+
+ :param headers: Headers to encode
+ :returns: Dictionary with encoded headers'
+ names and values
+ """
+ return dict((encodeutils.safe_encode(h), encodeutils.safe_encode(v))
+ for h, v in six.iteritems(headers) if v is not None)
+
+
class _BaseHTTPClient(object):
@staticmethod
@@ -197,20 +211,6 @@ class HTTPClient(_BaseHTTPClient):
LOG.debug('\n'.join([encodeutils.safe_decode(x, errors='ignore')
for x in dump]))
- @staticmethod
- def encode_headers(headers):
- """Encodes headers.
-
- Note: This should be used right before
- sending anything out.
-
- :param headers: Headers to encode
- :returns: Dictionary with encoded headers'
- names and values
- """
- return dict((encodeutils.safe_encode(h), encodeutils.safe_encode(v))
- for h, v in six.iteritems(headers) if v is not None)
-
def _request(self, method, url, **kwargs):
"""Send an http request with the specified characteristics.
@@ -232,7 +232,7 @@ class HTTPClient(_BaseHTTPClient):
# Note(flaper87): Before letting headers / url fly,
# they should be encoded otherwise httplib will
# complain.
- headers = self.encode_headers(headers)
+ headers = encode_headers(headers)
if self.endpoint.endswith("/") or url.startswith("/"):
conn_url = "%s%s" % (self.endpoint, url)
@@ -306,7 +306,7 @@ class SessionClient(adapter.Adapter, _BaseHTTPClient):
super(SessionClient, self).__init__(session, **kwargs)
def request(self, url, method, **kwargs):
- headers = kwargs.pop('headers', {})
+ headers = encode_headers(kwargs.pop('headers', {}))
kwargs['raise_exc'] = False
data = self._set_common_request_kwargs(headers, kwargs)
diff --git a/glanceclient/tests/unit/test_http.py b/glanceclient/tests/unit/test_http.py
index c18660e..4bc7a39 100644
--- a/glanceclient/tests/unit/test_http.py
+++ b/glanceclient/tests/unit/test_http.py
@@ -201,12 +201,9 @@ class TestClient(testtools.TestCase):
self.assertEqual(text, resp.text)
def test_headers_encoding(self):
- if not hasattr(self.client, 'encode_headers'):
- self.skipTest('Cannot do header encoding check on SessionClient')
-
value = u'ni\xf1o'
headers = {"test": value, "none-val": None}
- encoded = self.client.encode_headers(headers)
+ encoded = http.encode_headers(headers)
self.assertEqual(b"ni\xc3\xb1o", encoded[b"test"])
self.assertNotIn("none-val", encoded)