Cleanup processed_headers some more.

This commit is contained in:
L. Kärkkäinen 2020-03-09 15:28:02 +02:00
parent 32ee5399d9
commit a9d984e2f8

View File

@ -36,19 +36,19 @@ class BaseHTTPResponse:
Add and remove headers based on status and content_type. 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 # 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) self.headers = remove_entity_headers(self.headers)
if has_message_body(status): if has_message_body(self.status):
if self.content_type and not "content-type" in self.headers: self.headers.setdefault("content-type", self.content_type)
headers += (b"content-type", self.content_type.encode()),
# Encode headers into bytes # Encode headers into bytes
for name, value in self.headers.items(): return (
name = name.encode("ascii").lower() (
headers += (name, f"{value}".encode("utf-8")), name.encode("ascii"),
return headers f"{value}".encode("utf-8", errors="surrogateescape")
)
for name, value in self.headers.items()
)
async def send(self, data=None, end_stream=None): async def send(self, data=None, end_stream=None):
"""Send any pending response headers and the given data as body. """Send any pending response headers and the given data as body.