From 9b490340e52c46c75d5f668de5fecc76e1e47f4a Mon Sep 17 00:00:00 2001 From: Jack Kuan Date: Thu, 18 Dec 2014 10:15:13 -1000 Subject: [PATCH 1/2] Fix the problem with chunked encoding with an empty body. When the response body is empty, rocket won't send the final zero-length terminating chunk for chunked encoding. I think this causes the browser to wait for the connection close in order to tell the end of the response. --- gluon/rocket.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gluon/rocket.py b/gluon/rocket.py index 373c46b3..eb7c5c57 100644 --- a/gluon/rocket.py +++ b/gluon/rocket.py @@ -1850,12 +1850,13 @@ class WSGIWorker(Worker): if data: self.write(data, sections) + if not self.headers_sent: + # Send headers if the body was empty + self.send_headers('', sections) + if self.chunked: # If chunked, send our final chunk length self.conn.sendall(b('0\r\n\r\n')) - elif not self.headers_sent: - # Send headers if the body was empty - self.send_headers('', sections) # Don't capture exceptions here. The Worker class handles # them appropriately. From 39af574e7f7edfbcbb52678ce19ecc72f7c6dfe0 Mon Sep 17 00:00:00 2001 From: Jack Kuan Date: Fri, 19 Dec 2014 23:44:55 -1000 Subject: [PATCH 2/2] Avoid sending the terminating chunk in case it's a HEAD request. --- gluon/rocket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/rocket.py b/gluon/rocket.py index eb7c5c57..97f317bd 100644 --- a/gluon/rocket.py +++ b/gluon/rocket.py @@ -1854,7 +1854,7 @@ class WSGIWorker(Worker): # Send headers if the body was empty self.send_headers('', sections) - if self.chunked: + if self.chunked and self.request_method != 'HEAD': # If chunked, send our final chunk length self.conn.sendall(b('0\r\n\r\n'))