Skip to content
Snippets Groups Projects
Commit cf5c76017e11 authored by kiilerix's avatar kiilerix
Browse files

serve: use chunked encoding in hgweb responses

'hg serve' used to close connections when sending a response with unknown
length ... such as a bundle or archive.

Now chunked encoding will be used for responses with unknown length, and the
connection do thus not have to be closed to indicate the end of the response.

Chunked encoding is only used if the length is unknown, if the connection
wouldn't be closed for other reasons, AND if it is a HTTP 1.1 request.

This will not benefit other users of hgweb ... but it can serve as an example
that it can be done.
parent a9fd11ffa13f
No related branches found
No related tags found
No related merge requests found
......@@ -133,7 +133,8 @@
self.saved_headers = []
self.sent_headers = False
self.length = None
self._chunked = None
for chunk in self.server.application(env, self._start_response):
self._write(chunk)
if not self.sent_headers:
self.send_headers()
......@@ -136,7 +137,8 @@
for chunk in self.server.application(env, self._start_response):
self._write(chunk)
if not self.sent_headers:
self.send_headers()
self._done()
def send_headers(self):
if not self.saved_status:
......@@ -145,7 +147,8 @@
saved_status = self.saved_status.split(None, 1)
saved_status[0] = int(saved_status[0])
self.send_response(*saved_status)
should_close = True
self.length = None
self._chunked = False
for h in self.saved_headers:
self.send_header(*h)
if h[0].lower() == 'content-length':
......@@ -149,5 +152,4 @@
for h in self.saved_headers:
self.send_header(*h)
if h[0].lower() == 'content-length':
should_close = False
self.length = int(h[1])
......@@ -153,8 +155,11 @@
self.length = int(h[1])
# The value of the Connection header is a list of case-insensitive
# tokens separated by commas and optional whitespace.
if should_close:
self.send_header('Connection', 'close')
if self.length is None:
self._chunked = (not self.close_connection and
self.request_version == "HTTP/1.1")
if self._chunked:
self.send_header('Transfer-Encoding', 'chunked')
else:
self.send_header('Connection', 'close')
self.end_headers()
self.sent_headers = True
......@@ -177,6 +182,8 @@
raise AssertionError("Content-length header sent, but more "
"bytes than specified are being written.")
self.length = self.length - len(data)
elif self._chunked and data:
data = '%x\r\n%s\r\n' % (len(data), data)
self.wfile.write(data)
self.wfile.flush()
......@@ -180,6 +187,11 @@
self.wfile.write(data)
self.wfile.flush()
def _done(self):
if self._chunked:
self.wfile.write('0\r\n\r\n')
self.wfile.flush()
class _httprequesthandleropenssl(_httprequesthandler):
"""HTTPS handler based on pyOpenSSL"""
......
......@@ -124,7 +124,6 @@
adding manifests
adding file changes
added 1 changesets with 4 changes to 4 files
warning: localhost certificate with fingerprint 91:4f:1a:ff:87:24:9c:09:b6:85:9b:88:b1:90:6d:30:75:64:91:ca not verified (check hostfingerprints or web.cacerts config setting)
updating to branch default
4 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ hg verify -R copy-pull
......@@ -152,7 +151,6 @@
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files
warning: localhost certificate with fingerprint 91:4f:1a:ff:87:24:9c:09:b6:85:9b:88:b1:90:6d:30:75:64:91:ca not verified (check hostfingerprints or web.cacerts config setting)
changegroup hook: HG_NODE=5fed3813f7f5e1824344fdc9cf8f63bb662c292d HG_SOURCE=pull HG_URL=https://localhost:$HGPORT/
(run 'hg update' to get a working copy)
$ cd ..
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment