]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
http1connection: add "Connection: close" header if appropriate
authorPierce Lopez <pierce.lopez@gmail.com>
Mon, 5 Jun 2017 04:48:45 +0000 (00:48 -0400)
committerPierce Lopez <pierce.lopez@gmail.com>
Mon, 5 Jun 2017 17:22:01 +0000 (13:22 -0400)
When HTTP1Connection acts as the server, it closes the connection
after writing the response if the client includes the
"Connection: close" header in the request, or if the `no_keep_alive`
option is set to True in the constructor.

According to https://tools.ietf.org/html/rfc7230#section-6.6

> The server SHOULD send a "close" connection option in its final
response on that connection.

It was possible for an Application to set the Connection header
appropriately. But it is very helpful for tornado to take care of
this automatically, particularly if "close" was specified in a
request header.

tornado/http1connection.py
tornado/test/httpserver_test.py

index c6d3e336fbcf7ee1586e0dcea5fca29bf7041c14..b7a62b8c0c36d633eb221166434dc835c99dec17 100644 (file)
@@ -357,6 +357,10 @@ class HTTP1Connection(httputil.HTTPConnection):
                 # Applications are discouraged from touching Transfer-Encoding,
                 # but if they do, leave it alone.
                 'Transfer-Encoding' not in headers)
+            # If connection to a 1.1 client will be closed, inform client
+            if (self._request_start_line.version == 'HTTP/1.1' and
+                self._disconnect_on_finish):
+                headers['Connection'] = 'close'
             # If a 1.0 client asked for keep-alive, add the header.
             if (self._request_start_line.version == 'HTTP/1.0' and
                 (self._request_headers.get('Connection', '').lower() ==
@@ -418,7 +422,7 @@ class HTTP1Connection(httputil.HTTPConnection):
     def write(self, chunk, callback=None):
         """Implements `.HTTPConnection.write`.
 
-        For backwards compatibility is is allowed but deprecated to
+        For backwards compatibility it is allowed but deprecated to
         skip `write_headers` and instead call `write()` with a
         pre-encoded header block.
         """
index f5f91a9df2767a421726e79ba9bc646e954ef725..d8342f748d1d9a0e1b29f842d12913fefcc65aad 100644 (file)
@@ -719,6 +719,7 @@ class KeepAliveTest(AsyncHTTPTestCase):
         self.stream.read_until_close(callback=self.stop)
         data = self.wait()
         self.assertTrue(not data)
+        self.assertEqual(self.headers['Connection'], 'close')
         self.close()
 
     # keepalive is supported for http 1.0 too, but it's opt-in