]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Convert http request bodies to utf8 earlier so content-length is correct
authorBen Darnell <ben@bendarnell.com>
Sun, 15 May 2011 04:29:58 +0000 (21:29 -0700)
committerBen Darnell <ben@bendarnell.com>
Sun, 15 May 2011 04:29:58 +0000 (21:29 -0700)
tornado/httpclient.py
tornado/simple_httpclient.py
tornado/test/simple_httpclient_test.py

index e7a30304eaa5fafeaa500879f43ca2ef51bdd877..2caaf21a3048b036f26c0e9c7a0e8a1e174a2560 100644 (file)
@@ -170,7 +170,7 @@ class HTTPRequest(object):
         self.url = utf8(url)
         self.method = method
         self.headers = headers
-        self.body = body
+        self.body = utf8(body)
         self.auth_username = utf8(auth_username)
         self.auth_password = utf8(auth_password)
         self.connect_timeout = connect_timeout
index 19645e324f3d0f28a18819d43057d84849c8ae5f..bc3c21203b5b052c20a5e9c93a2a694e63074d2f 100644 (file)
@@ -230,7 +230,7 @@ class _HTTPConnection(object):
             request_lines.append(line)
         self.stream.write(b("\r\n").join(request_lines) + b("\r\n\r\n"))
         if has_body:
-            self.stream.write(utf8(self.request.body))
+            self.stream.write(self.request.body)
         self.stream.read_until(b("\r\n\r\n"), self._on_headers)
 
     @contextlib.contextmanager
index 1cff9e5df4bb7bd78ae22881c14a6e387f37193b..ce71b6e99b4682bf217c07911264b7ce7197c9ff 100644 (file)
@@ -9,6 +9,7 @@ import logging
 import socket
 
 from contextlib import closing
+from tornado.escape import utf8
 from tornado.ioloop import IOLoop
 from tornado.simple_httpclient import SimpleAsyncHTTPClient, _DEFAULT_CA_CERTS
 from tornado.testing import AsyncHTTPTestCase, LogTrapTestCase, get_unused_port
@@ -60,6 +61,10 @@ class CountdownHandler(RequestHandler):
         else:
             self.write("Zero")
 
+class EchoPostHandler(RequestHandler):
+    def post(self):
+        self.write(self.request.body)
+
 class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase):
     def get_app(self):
         # callable objects to finish pending /trigger requests
@@ -73,6 +78,7 @@ class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase):
             url("/trigger", TriggerHandler, dict(queue=self.triggers,
                                                  wake_callback=self.stop)),
             url("/countdown/([0-9]+)", CountdownHandler, name="countdown"),
+            url("/echopost", EchoPostHandler),
             ], gzip=True)
 
     def setUp(self):
@@ -226,3 +232,17 @@ class SimpleHTTPClientTestCase(AsyncHTTPTestCase, LogTrapTestCase):
         self.assertEqual(b("Basic ") + base64.b64encode(b("me:secret")),
                          response.body)
 
+    def test_body_encoding(self):
+        # unicode string in body gets converted to utf8
+        response = self.fetch("/echopost", method="POST", body=u"\xe9")
+        self.assertEqual(response.body, utf8(u"\xe9"))
+
+        # byte strings pass through directly
+        response = self.fetch("/echopost", method="POST", body="\xe9")
+        self.assertEqual(response.body, "\xe9")
+
+        # Mixing unicode in headers and byte string bodies shouldn't
+        # break anything
+        response = self.fetch("/echopost", method="POST", body="\xe9",
+                              user_agent=u"foo")
+        self.assertEqual(response.body, "\xe9")