Merge pull request #215 from cr0hn/patch-1

Improvement: avoid to encoding in each HTTP Response
This commit is contained in:
Eli Uriegas 2016-12-24 18:41:01 -08:00 committed by GitHub
commit 67a50becb0

View File

@ -1,9 +1,11 @@
from aiofiles import open as open_async from aiofiles import open as open_async
from .cookies import CookieJar
from mimetypes import guess_type from mimetypes import guess_type
from os import path from os import path
from ujson import dumps as json_dumps from ujson import dumps as json_dumps
from .cookies import CookieJar
COMMON_STATUS_CODES = { COMMON_STATUS_CODES = {
200: b'OK', 200: b'OK',
400: b'Bad Request', 400: b'Bad Request',
@ -79,7 +81,12 @@ class HTTPResponse:
self.content_type = content_type self.content_type = content_type
if body is not None: if body is not None:
try:
# Try to encode it regularly
self.body = body.encode('utf-8') self.body = body.encode('utf-8')
except AttributeError:
# Convert it to a str if you can't
self.body = str(body).encode('utf-8')
else: else:
self.body = body_bytes self.body = body_bytes