From bfbef8ea969114cc64468fd1b5765366280475f1 Mon Sep 17 00:00:00 2001 From: Maik Hoepfel Date: Wed, 22 Mar 2017 12:45:50 +0800 Subject: [PATCH] Require status key in first message to response channel Previous to this commit, it was possible to not pass in a 'status' key. This would result in any passed in headers being ignored as well. Instead of relying on user data ('status' being present or not), this commit now enforces that the first message to a response channel is indead a HTTP Response-style message, and hence contains status. It will complain loudly if that isn't the case. --- daphne/http_protocol.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 2de1f85..2f82250 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -227,11 +227,12 @@ class WebRequest(http.Request): """ Writes a received HTTP response back out to the transport. """ - if "status" in message: - if self._got_response_start: - raise ValueError("Got multiple Response messages for %s!" % self.reply_channel) + if not self._got_response_start: self._got_response_start = True - # Write code + if 'status' not in message: + raise ValueError("Specifying a status code is required for a Response message.") + + # Set HTTP status code self.setResponseCode(message['status']) # Write headers for header, value in message.get("headers", {}): @@ -240,6 +241,9 @@ class WebRequest(http.Request): header = header.encode("latin1") self.responseHeaders.addRawHeader(header, value) logger.debug("HTTP %s response started for %s", message['status'], self.reply_channel) + else: + if 'status' in message: + raise ValueError("Got multiple Response messages for %s!" % self.reply_channel) # Write out body http.Request.write(self, message.get('content', b''))