python: Encode headers in UTF-8 if they are given in unicode

This commit is contained in:
Tatsuhiro Tsujikawa 2014-02-25 21:21:35 +09:00
parent 4dcb68d128
commit add07c4303
1 changed files with 11 additions and 7 deletions

View File

@ -828,6 +828,7 @@ class BaseRequestHandler:
if request_headers is None: if request_headers is None:
request_headers = [] request_headers = []
request_headers = _encode_headers(request_headers)
request_headers.append((b':scheme', promised_handler.scheme)) request_headers.append((b':scheme', promised_handler.scheme))
request_headers.append((b':method', promised_handler.method)) request_headers.append((b':method', promised_handler.method))
request_headers.append((b':authority', promised_handler.host)) request_headers.append((b':authority', promised_handler.host))
@ -839,16 +840,14 @@ class BaseRequestHandler:
def _set_response_prop(self, status, headers, body): def _set_response_prop(self, status, headers, body):
self.status = status self.status = status
self.response_headers = headers
if self.response_headers is None: if headers is None:
self.response_headers = [] headers = []
self.response_headers = _encode_headers(headers)
self.response_headers.append((b':status', str(status).encode('utf-8'))) self.response_headers.append((b':status', str(status).encode('utf-8')))
if body: self.response_body = body
self.response_body = io.BufferedReader(body)
else:
self.response_body = None
def _wrap_body(self, body): def _wrap_body(self, body):
if body is None: if body is None:
@ -863,6 +862,11 @@ class BaseRequestHandler:
raise Exception(('body must be None or instance of str or bytes ' raise Exception(('body must be None or instance of str or bytes '
'or io.BytesIO')) 'or io.BytesIO'))
def _encode_headers(headers):
return [(k if isinstance(k, bytes) else k.encode('utf-8'),
v if isinstance(v, bytes) else v.encode('utf-8')) \
for k, v in headers]
class _HTTP2Session(asyncio.Protocol): class _HTTP2Session(asyncio.Protocol):
def __init__(self, RequestHandlerClass): def __init__(self, RequestHandlerClass):