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.
This commit is contained in:
Maik Hoepfel 2017-03-22 12:45:50 +08:00
parent 129fe71f83
commit bfbef8ea96

View File

@ -227,11 +227,12 @@ class WebRequest(http.Request):
""" """
Writes a received HTTP response back out to the transport. Writes a received HTTP response back out to the transport.
""" """
if "status" in message: if not self._got_response_start:
if self._got_response_start:
raise ValueError("Got multiple Response messages for %s!" % self.reply_channel)
self._got_response_start = True 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']) self.setResponseCode(message['status'])
# Write headers # Write headers
for header, value in message.get("headers", {}): for header, value in message.get("headers", {}):
@ -240,6 +241,9 @@ class WebRequest(http.Request):
header = header.encode("latin1") header = header.encode("latin1")
self.responseHeaders.addRawHeader(header, value) self.responseHeaders.addRawHeader(header, value)
logger.debug("HTTP %s response started for %s", message['status'], self.reply_channel) 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 # Write out body
http.Request.write(self, message.get('content', b'')) http.Request.write(self, message.get('content', b''))