From: felloak Date: Tue, 23 Dec 2014 08:28:36 +0000 (+0800) Subject: Ensure that HTTPServerRequest.body is a byte string X-Git-Tag: v4.1.0b1~44^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=429aae47dd3d7d78fc7f66fd8f627f0af4ff795b;p=thirdparty%2Ftornado.git 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. --- 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"):