From a9d984e2f807fb2e66dca54fc4ac656dac1d69cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Mon, 9 Mar 2020 15:28:02 +0200 Subject: [PATCH] Cleanup processed_headers some more. --- sanic/response.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/sanic/response.py b/sanic/response.py index 7206a41c..78df7afe 100644 --- a/sanic/response.py +++ b/sanic/response.py @@ -36,19 +36,19 @@ class BaseHTTPResponse: Add and remove headers based on status and content_type. """ - status = self.status - headers = [] # TODO: Make a blacklist set of header names and then filter with that - if status in (304, 412): # Not Modified, Precondition Failed + if self.status in (304, 412): # Not Modified, Precondition Failed self.headers = remove_entity_headers(self.headers) - if has_message_body(status): - if self.content_type and not "content-type" in self.headers: - headers += (b"content-type", self.content_type.encode()), + if has_message_body(self.status): + self.headers.setdefault("content-type", self.content_type) # Encode headers into bytes - for name, value in self.headers.items(): - name = name.encode("ascii").lower() - headers += (name, f"{value}".encode("utf-8")), - return headers + return ( + ( + name.encode("ascii"), + f"{value}".encode("utf-8", errors="surrogateescape") + ) + for name, value in self.headers.items() + ) async def send(self, data=None, end_stream=None): """Send any pending response headers and the given data as body.