Response chunk support

This commit is contained in:
Andrew Godwin 2016-01-02 18:41:02 -08:00
parent 0f5fe8b274
commit c61664dd3c

View File

@ -29,6 +29,7 @@ class WebRequest(http.Request):
# Tell factory we're that channel's client # Tell factory we're that channel's client
self.last_keepalive = time.time() self.last_keepalive = time.time()
self.factory.reply_protocols[self.reply_channel] = self self.factory.reply_protocols[self.reply_channel] = self
self._got_response_start = False
def process(self): def process(self):
# Get upgrade header # Get upgrade header
@ -104,16 +105,24 @@ class WebRequest(http.Request):
""" """
Writes a received HTTP response back out to the transport. Writes a received HTTP response back out to the transport.
""" """
# Write code if "status" in message:
self.setResponseCode(message['status']) if self._got_response_start:
# Write headers raise ValueError("Got multiple Response messages!")
for header, value in message.get("headers", {}): self._got_response_start = True
self.setHeader(header.encode("utf8"), value.encode("utf8")) # Write code
self.setResponseCode(message['status'])
# Write headers
for header, value in message.get("headers", {}):
self.setHeader(header.encode("utf8"), value.encode("utf8"))
# Write out body # Write out body
if "content" in message: if "content" in message:
http.Request.write(self, message['content'].encode("utf8")) http.Request.write(self, message['content'].encode("utf8"))
self.finish() # End if there's no more content
logging.debug("HTTP %s response for %s", message['status'], self.reply_channel) if not message.get("more_content", False):
self.finish()
logging.debug("HTTP %s response for %s", message['status'], self.reply_channel)
else:
logging.debug("HTTP %s response chunk for %s", message['status'], self.reply_channel)
class HTTPProtocol(http.HTTPChannel): class HTTPProtocol(http.HTTPChannel):