From 23421084a515144299b09adab9c0f0aee9635b3f Mon Sep 17 00:00:00 2001 From: Ivan Kolodyazhny Date: Tue, 12 Apr 2016 12:01:39 +0300 Subject: [PATCH] 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 --- cinder/api/openstack/wsgi.py | 9 +++------ cinder/utils.py | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/cinder/api/openstack/wsgi.py b/cinder/api/openstack/wsgi.py index 5e966756ba1..ed93b6caef6 100644 --- a/cinder/api/openstack/wsgi.py +++ b/cinder/api/openstack/wsgi.py @@ -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)): diff --git a/cinder/utils.py b/cinder/utils.py index e4bc2978bc8..a501b8295c9 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -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')