From 429aae47dd3d7d78fc7f66fd8f627f0af4ff795b Mon Sep 17 00:00:00 2001 From: felloak Date: Tue, 23 Dec 2014 16:28:36 +0800 Subject: [PATCH] Ensure that HTTPServerRequest.body is a byte string HTTPServerRequest.body should be bytes but it is an empty string that occurs in Python3 when pass no body to HTTPServerRequest. --- tornado/httputil.py | 2 +- tornado/test/httputil_test.py | 4 ++++ tornado/wsgi.py | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tornado/httputil.py b/tornado/httputil.py index a3745569c..88389fedf 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -331,7 +331,7 @@ class HTTPServerRequest(object): self.uri = uri self.version = version self.headers = headers or HTTPHeaders() - self.body = body or "" + self.body = body or b"" # set remote IP and protocol context = getattr(connection, 'context', None) diff --git a/tornado/test/httputil_test.py b/tornado/test/httputil_test.py index 5ca5cf9f3..adbd17114 100644 --- a/tornado/test/httputil_test.py +++ b/tornado/test/httputil_test.py @@ -264,6 +264,10 @@ class HTTPServerRequestTest(unittest.TestCase): # more required parameters slip in. HTTPServerRequest(uri='/') + def test_body_is_a_byte_string(self): + requets = HTTPServerRequest(uri='/') + self.assertIsInstance(requets.body, bytes) + class ParseRequestStartLineTest(unittest.TestCase): METHOD = "GET" diff --git a/tornado/wsgi.py b/tornado/wsgi.py index f3aa66503..e7e07fbc9 100644 --- a/tornado/wsgi.py +++ b/tornado/wsgi.py @@ -207,7 +207,7 @@ class WSGIAdapter(object): body = environ["wsgi.input"].read( int(headers["Content-Length"])) else: - body = "" + body = b"" protocol = environ["wsgi.url_scheme"] remote_ip = environ.get("REMOTE_ADDR", "") if environ.get("HTTP_HOST"): -- 2.47.2