Fix api exception with unicode tenant name.

There are a lot request debug logging in Trove, when some values of
headers are encoded in utf8, UnicodeEncodeError will be raised by
webob.Request. Override how webob.Request is represented will fix.

Closes-Bug: #1720121

Change-Id: I91683b8dd24262b0f643e8d2bc7886a7c03be40a
Signed-off-by: Zhao Chao <zhaochao1984@gmail.com>
This commit is contained in:
Zhao Chao
2018-01-22 22:14:32 +08:00
parent 7232a2b857
commit 08ea56b21c
5 changed files with 80 additions and 1 deletions

View File

@@ -26,6 +26,7 @@ import jinja2
from oslo_concurrency import processutils
from oslo_log import log as logging
from oslo_service import loopingcall
from oslo_utils.encodeutils import safe_encode
from oslo_utils import importutils
from oslo_utils import strutils
from passlib import pwd
@@ -383,3 +384,28 @@ def to_mb(bytes):
size = bytes / 1024.0 ** 2
# Make sure we don't return 0.0 if the size is greater than 0
return max(round(size, 2), 0.01)
def req_to_text(req):
"""
We do a lot request logging for debug, but if the value of one
requst header is encoded in utf-8, an UnicodeEncodeError will
be raised. So we should carefully encode request headers.
To be consitent with webob, main procedures are copied from
webob.Request.as_bytes.
"""
url = req.url
host = req.host_url
assert url.startswith(host)
url = url[len(host):]
parts = [safe_encode('%s %s %s' % (req.method, url, req.http_version))]
for k, v in sorted(req.headers.items()):
header = safe_encode('%s: %s' % (k, v))
parts.append(header)
if req.body:
parts.extend([b'', safe_encode(req.body)])
return b'\r\n'.join(parts).decode(req.charset)