Merge "Fix "Codec can't encode characters""

This commit is contained in:
Jenkins
2016-05-23 14:31:04 +00:00
committed by Gerrit Code Review
2 changed files with 17 additions and 20 deletions

View File

@@ -42,6 +42,20 @@ USER_AGENT = 'python-glanceclient'
CHUNKSIZE = 1024 * 64 # 64kB 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): class _BaseHTTPClient(object):
@staticmethod @staticmethod
@@ -194,20 +208,6 @@ class HTTPClient(_BaseHTTPClient):
LOG.debug('\n'.join([encodeutils.safe_decode(x, errors='ignore') LOG.debug('\n'.join([encodeutils.safe_decode(x, errors='ignore')
for x in dump])) 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): def _request(self, method, url, **kwargs):
"""Send an http request with the specified characteristics. """Send an http request with the specified characteristics.
@@ -233,7 +233,7 @@ class HTTPClient(_BaseHTTPClient):
# Note(flaper87): Before letting headers / url fly, # Note(flaper87): Before letting headers / url fly,
# they should be encoded otherwise httplib will # they should be encoded otherwise httplib will
# complain. # complain.
headers = self.encode_headers(headers) headers = encode_headers(headers)
if self.endpoint.endswith("/") or url.startswith("/"): if self.endpoint.endswith("/") or url.startswith("/"):
conn_url = "%s%s" % (self.endpoint, url) conn_url = "%s%s" % (self.endpoint, url)
@@ -309,7 +309,7 @@ class SessionClient(adapter.Adapter, _BaseHTTPClient):
super(SessionClient, self).__init__(session, **kwargs) super(SessionClient, self).__init__(session, **kwargs)
def request(self, url, method, **kwargs): def request(self, url, method, **kwargs):
headers = kwargs.pop('headers', {}) headers = encode_headers(kwargs.pop('headers', {}))
kwargs['raise_exc'] = False kwargs['raise_exc'] = False
data = self._set_common_request_kwargs(headers, kwargs) data = self._set_common_request_kwargs(headers, kwargs)

View File

@@ -207,12 +207,9 @@ class TestClient(testtools.TestCase):
self.assertEqual(self.client.last_request_id, 'req-aaa') self.assertEqual(self.client.last_request_id, 'req-aaa')
def test_headers_encoding(self): 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' value = u'ni\xf1o'
headers = {"test": value, "none-val": None} 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.assertEqual(b"ni\xc3\xb1o", encoded[b"test"])
self.assertNotIn("none-val", encoded) self.assertNotIn("none-val", encoded)