]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Don't assume IP-style socket addresses in error messages.
authorBen Darnell <ben@bendarnell.com>
Sun, 22 Sep 2013 16:50:19 +0000 (12:50 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 22 Sep 2013 16:50:19 +0000 (12:50 -0400)
Closes #900.

tornado/httpserver.py
tornado/test/httpserver_test.py

index d005545e83eabe79bf0f102a2a3dde5bf9cdece8..cacb514e478e33736b068de32e7b75ae0415c074 100644 (file)
@@ -326,8 +326,8 @@ class HTTPConnection(object):
 
             self.request_callback(self._request)
         except _BadRequestException as e:
-            gen_log.info("Malformed HTTP request from %s: %s",
-                         self.address[0], e)
+            gen_log.info("Malformed HTTP request from %r: %s",
+                         self.address, e)
             self.close()
             return
 
index d903854039e1fa38b1e144569eac3e1792627945..97085c52c0e627a0bccfcdbc3569f6942cd1cffd 100644 (file)
@@ -497,31 +497,41 @@ class UnixSocketTest(AsyncTestCase):
     def setUp(self):
         super(UnixSocketTest, self).setUp()
         self.tmpdir = tempfile.mkdtemp()
+        self.sockfile = os.path.join(self.tmpdir, "test.sock")
+        sock = netutil.bind_unix_socket(self.sockfile)
+        app = Application([("/hello", HelloWorldRequestHandler)])
+        self.server = HTTPServer(app, io_loop=self.io_loop)
+        self.server.add_socket(sock)
+        self.stream = IOStream(socket.socket(socket.AF_UNIX), io_loop=self.io_loop)
+        self.stream.connect(self.sockfile, self.stop)
+        self.wait()
 
     def tearDown(self):
+        self.stream.close()
+        self.server.stop()
         shutil.rmtree(self.tmpdir)
         super(UnixSocketTest, self).tearDown()
 
     def test_unix_socket(self):
-        sockfile = os.path.join(self.tmpdir, "test.sock")
-        sock = netutil.bind_unix_socket(sockfile)
-        app = Application([("/hello", HelloWorldRequestHandler)])
-        server = HTTPServer(app, io_loop=self.io_loop)
-        server.add_socket(sock)
-        stream = IOStream(socket.socket(socket.AF_UNIX), io_loop=self.io_loop)
-        stream.connect(sockfile, self.stop)
-        self.wait()
-        stream.write(b"GET /hello HTTP/1.0\r\n\r\n")
-        stream.read_until(b"\r\n", self.stop)
+        self.stream.write(b"GET /hello HTTP/1.0\r\n\r\n")
+        self.stream.read_until(b"\r\n", self.stop)
         response = self.wait()
         self.assertEqual(response, b"HTTP/1.0 200 OK\r\n")
-        stream.read_until(b"\r\n\r\n", self.stop)
+        self.stream.read_until(b"\r\n\r\n", self.stop)
         headers = HTTPHeaders.parse(self.wait().decode('latin1'))
-        stream.read_bytes(int(headers["Content-Length"]), self.stop)
+        self.stream.read_bytes(int(headers["Content-Length"]), self.stop)
         body = self.wait()
         self.assertEqual(body, b"Hello world")
-        stream.close()
-        server.stop()
+
+    def test_unix_socket_bad_request(self):
+        # Unix sockets don't have remote addresses so they just return an
+        # empty string.
+        with ExpectLog(gen_log, "Malformed HTTP request from ''"):
+            self.stream.write(b"garbage\r\n\r\n")
+            self.stream.read_until_close(self.stop)
+            response = self.wait()
+        self.assertEqual(response, b"")
+
 
 
 class KeepAliveTest(AsyncHTTPTestCase):