From: Ben Darnell Date: Sat, 19 Apr 2014 20:04:53 +0000 (-0400) Subject: Don't log errors about content-length mismatch when the connection closes. X-Git-Tag: v4.0.0b1~91^2~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=18cfb371d45cbb7abba5fc094d4331f31b9a0f42;p=thirdparty%2Ftornado.git Don't log errors about content-length mismatch when the connection closes. --- diff --git a/tornado/http1connection.py b/tornado/http1connection.py index b64425223..5c50fad00 100644 --- a/tornado/http1connection.py +++ b/tornado/http1connection.py @@ -298,7 +298,8 @@ class HTTP1Connection(object): def finish(self): """Finishes the request.""" if (self._expected_content_remaining is not None and - self._expected_content_remaining != 0): + self._expected_content_remaining != 0 and + not self.stream.closed()): self.stream.close() raise httputil.HTTPOutputException( "Tried to write %d bytes less than Content-Length" % diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index e05ae0697..92b768f60 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -1962,6 +1962,7 @@ class IncorrectContentLengthTest(SimpleHandlerTestCase): self.finish("hello") except Exception as e: test.server_error = e + raise return [('/high', TooHigh), ('/low', TooLow)] @@ -1989,3 +1990,19 @@ class IncorrectContentLengthTest(SimpleHandlerTestCase): self.assertEqual(response.code, 599) self.assertEqual(str(self.server_error), "Tried to write more data than Content-Length") + + +class ClientCloseTest(SimpleHandlerTestCase): + class Handler(RequestHandler): + def get(self): + # Simulate a connection closed by the client during + # request processing. The client will see an error, but the + # server should respond gracefully (without logging errors + # because we were unable to write out as many bytes as + # Content-Length said we would) + self.request.connection.stream.close() + self.write('hello') + + def test_client_close(self): + response = self.fetch('/') + self.assertEqual(response.code, 599)