Skip to content

Commit

Permalink
Merge pull request #2074 from ploxiln/http1_connection_close
Browse files Browse the repository at this point in the history
http1connection: add "Connection: close" header if appropriate
  • Loading branch information
bdarnell committed Jun 10, 2017
2 parents cb8d421 + 6d6d44e commit 28e631a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion tornado/http1connection.py
Expand Up @@ -359,6 +359,10 @@ def write_headers(self, start_line, headers, chunk=None, callback=None):
# 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() ==
Expand Down Expand Up @@ -420,7 +424,7 @@ def _format_chunk(self, chunk):
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.
"""
Expand Down
1 change: 0 additions & 1 deletion tornado/httpserver.py
Expand Up @@ -154,7 +154,6 @@ def initialize(self, request_callback, no_keep_alive=False,
max_body_size=None, max_buffer_size=None,
trusted_downstream=None):
self.request_callback = request_callback
self.no_keep_alive = no_keep_alive
self.xheaders = xheaders
self.protocol = protocol
self.conn_params = HTTP1ConnectionParameters(
Expand Down
1 change: 1 addition & 0 deletions tornado/test/httpserver_test.py
Expand Up @@ -728,6 +728,7 @@ def test_request_close(self):
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
Expand Down

0 comments on commit 28e631a

Please sign in to comment.