Skip to content

Commit d73b6cd

Browse files
committed
Merge pull request #17 from w3c/jgraham/connection_close
Always close the connection at the end of a request.
2 parents cfc6192 + 7d74185 commit d73b6cd

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

docs/pipes.rst

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ code 404.
1414
There are several built-in pipe functions, and it is possible to add
1515
more using the `@pipe` decorator on a function, if required.
1616

17+
.. note::
18+
Because of the way pipes compose, using some pipe functions prevents the
19+
content-length of the response from being known in advance. In these cases
20+
the server will close the connection to indicate the end of the response,
21+
preventing the use of HTTP 1.1 keepalive.
22+
1723
Built-In Pipes
1824
--------------
1925

@@ -109,6 +115,9 @@ Would send the first 20 bytes of example.txt.
109115
trickle
110116
~~~~~~~
111117

118+
.. note::
119+
Using this function will force a connection close.
120+
112121
Used to send the body of a response in chunks with delays. Takes a
113122
single argument that is a microsyntax consisting of colon-separated
114123
commands. There are three types of commands:

wptserve/response.py

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def __init__(self, handler, request):
6868
self.add_required_headers = True
6969
self.send_body_for_head_request = False
7070
self.explicit_flush = False
71+
self.close_connection = False
7172

7273
self.writer = ResponseWriter(handler, self)
7374

@@ -401,6 +402,8 @@ def end_headers(self):
401402
self.write_default_headers()
402403

403404
self.write("\r\n")
405+
if not "content-length" in self._headers_seen:
406+
self._response.close_connection = True
404407
if not self._response.explicit_flush:
405408
self.flush()
406409
self._headers_complete = True

wptserve/server.py

+6
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ def handle_one_request(self):
218218
request.request_path, request.headers.get('Referer'), request.raw_input.length))
219219
if not response.writer.content_written:
220220
response.write()
221+
# If we want to remove this in the future, a solution is needed for
222+
# scripts that produce a non-string iterable of content, since these
223+
# can't set a Content-Length header. A notable example of this kind of
224+
# problem is with the trickle pipe i.e. foo.js?pipe=trickle(d1)
225+
if response.close_connection:
226+
self.close_connection = 1
221227

222228
except socket.timeout, e:
223229
self.log_error("Request timed out: %r", e)

0 commit comments

Comments
 (0)