Fix broken chunked response generation.

* Transfer-Encoding header was not being set.
* Whether a connection should close should depend on whether
  we are able to continue accepting requests.
This commit is contained in:
Paul J. Davis 2011-01-14 19:18:50 -05:00
parent bcafd6eb22
commit f942b3a8b2
2 changed files with 18 additions and 13 deletions

View File

@ -114,15 +114,21 @@ class Response(object):
self.version = SERVER_SOFTWARE self.version = SERVER_SOFTWARE
self.status = None self.status = None
self.chunked = False self.chunked = False
self.should_close = req.should_close() self.must_close = False
self.headers = [] self.headers = []
self.headers_sent = False self.headers_sent = False
self.clength = None self.clength = None
#self.te = None
self.sent = 0 self.sent = 0
def force_close(self): def force_close(self):
self.should_close = True self.must_close = True
def should_close(self):
if self.must_close or self.req.should_close():
return True
if self.clength is not None or self.chunked:
return False
return True
def start_response(self, status, headers, exc_info=None): def start_response(self, status, headers, exc_info=None):
if exc_info: if exc_info:
@ -146,8 +152,6 @@ class Response(object):
if lname == "content-length": if lname == "content-length":
self.clength = int(value) self.clength = int(value)
elif util.is_hoppish(name): elif util.is_hoppish(name):
#if lname == "transfer-encoding":
# self.te = value.lower().strip()
if lname == "connection": if lname == "connection":
# handle websocket # handle websocket
if value.lower().strip() != "upgrade": if value.lower().strip() != "upgrade":
@ -159,10 +163,9 @@ class Response(object):
def is_chunked(self): def is_chunked(self):
# maybe we should do this test if users expect this header # Only use chunked responses when the client is
# to force chunked encoding # speaking HTTP/1.1 or newer and there was
#if self.te == "chunked" and self.req.version > (1, 0): # no Content-Length header set.
# return True
if self.clength: if self.clength:
return False return False
elif self.req.version <= (1,0): elif self.req.version <= (1,0):
@ -171,16 +174,18 @@ class Response(object):
def default_headers(self): def default_headers(self):
connection = "keep-alive" connection = "keep-alive"
if self.should_close: if self.should_close():
connection = "close" connection = "close"
headers = [
return [
"HTTP/%s.%s %s\r\n" % (self.req.version[0], "HTTP/%s.%s %s\r\n" % (self.req.version[0],
self.req.version[1], self.status), self.req.version[1], self.status),
"Server: %s\r\n" % self.version, "Server: %s\r\n" % self.version,
"Date: %s\r\n" % util.http_date(), "Date: %s\r\n" % util.http_date(),
"Connection: %s\r\n" % connection "Connection: %s\r\n" % connection
] ]
if self.chunked:
headers.append("Transfer-Encoding: chunked\r\n")
return headers
def send_headers(self): def send_headers(self):
if self.headers_sent: if self.headers_sent:

View File

@ -68,7 +68,7 @@ class AsyncWorker(base.Worker):
resp.close() resp.close()
if hasattr(respiter, "close"): if hasattr(respiter, "close"):
respiter.close() respiter.close()
if req.should_close(): if resp.should_close():
raise StopIteration() raise StopIteration()
except StopIteration: except StopIteration:
raise raise