From c61664dd3c594247e7fdc56ba442d2b8a670ae8a Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sat, 2 Jan 2016 18:41:02 -0800 Subject: [PATCH] Response chunk support --- daphne/http_protocol.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 05c9775..d89141a 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -29,6 +29,7 @@ class WebRequest(http.Request): # Tell factory we're that channel's client self.last_keepalive = time.time() self.factory.reply_protocols[self.reply_channel] = self + self._got_response_start = False def process(self): # Get upgrade header @@ -104,16 +105,24 @@ class WebRequest(http.Request): """ Writes a received HTTP response back out to the transport. """ - # Write code - self.setResponseCode(message['status']) - # Write headers - for header, value in message.get("headers", {}): - self.setHeader(header.encode("utf8"), value.encode("utf8")) + if "status" in message: + if self._got_response_start: + raise ValueError("Got multiple Response messages!") + self._got_response_start = True + # 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 if "content" in message: http.Request.write(self, message['content'].encode("utf8")) - self.finish() - logging.debug("HTTP %s response for %s", message['status'], self.reply_channel) + # End if there's no more content + 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):