Use utils.convert_str to convert HTTP header values

HTTP header values must be a string, Originally fixed with:
 Change the logic of how we encode the response headers to be
  a utf-8 string for pre- and post python3.
 https://review.openstack.org/#/c/286821
 Closes-Bug: #1550951

That code was changed:
 Use utils.convert_str to convert HTTP header values
 This patch removes duplicate code between wsgi.py and utils.py modules.
 https://review.openstack.org/#/c/304462

and then fixed:
 Use to_utf8() instead of safe_encode() in convert_str()
 safe_encode() convert string using locale [1]. To avoid possible confuses
 we should use to_utf8() method instead.
 [1] https://wiki.openstack.org/wiki/Python3#safe_encode
 https://review.openstack.org/#/c/313521

Change-Id: Ic3eefc2bc28ff202324e7859102b289aac7974d4
This commit is contained in:
Ivan Kolodyazhny 2016-04-12 12:01:39 +03:00 committed by scottda
parent 55a668dea7
commit 23421084a5
2 changed files with 4 additions and 7 deletions

View File

@ -1165,12 +1165,9 @@ class Resource(wsgi.Application):
if hasattr(response, 'headers'):
for hdr, val in response.headers.items():
# Headers must be utf-8 strings
try:
# python 2.x
response.headers[hdr] = val.encode('utf-8')
except Exception:
# python 3.x
response.headers[hdr] = six.text_type(val)
val = utils.convert_str(val)
response.headers[hdr] = val
if (not request.api_version_request.is_null() and
not self._is_legacy_endpoint(request)):

View File

@ -836,7 +836,7 @@ def convert_str(text):
* convert to Unicode on Python 3: decode bytes from UTF-8
"""
if six.PY2:
return encodeutils.safe_encode(text)
return encodeutils.to_utf8(text)
else:
if isinstance(text, bytes):
return text.decode('utf-8')