From 954df42e5c4492c908b2a3bdc6f01c1678dc1efa Mon Sep 17 00:00:00 2001 From: Delta Regeer Date: Sun, 4 Feb 2024 13:47:23 -0700 Subject: [PATCH] Only use self.response_headers after normalization --- src/waitress/task.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/waitress/task.py b/src/waitress/task.py index 93baebe2..1b69c64f 100644 --- a/src/waitress/task.py +++ b/src/waitress/task.py @@ -186,8 +186,7 @@ def set_close_on_finish(self) -> None: if not self.wrote_header: connection_close_header = None for (headername, headerval) in self.response_headers: - headername = "-".join([x.capitalize() for x in headername.split("-")]) - if headername == "Connection": + if headername.capitalize() == "Connection": connection_close_header = headerval.lower() if connection_close_header is None: self.response_headers.append(("Connection", "close")) @@ -220,20 +219,23 @@ def build_response_header(self): # replace with properly capitalized version response_headers.append((headername, headerval)) + # Overwrite the response headers we have with normalized ones + self.response_headers = response_headers + if ( content_length_header is None and self.content_length is not None and self.has_body ): content_length_header = str(self.content_length) - response_headers.append(("Content-Length", content_length_header)) + self.response_headers.append(("Content-Length", content_length_header)) if version == "1.0": if connection == "keep-alive": if not content_length_header: self.set_close_on_finish() else: - response_headers.append(("Connection", "Keep-Alive")) + self.response_headers.append(("Connection", "Keep-Alive")) else: self.set_close_on_finish() @@ -246,7 +248,7 @@ def build_response_header(self): # for any response with a status code of 1xx, 204 or 304. if self.has_body: - response_headers.append(("Transfer-Encoding", "chunked")) + self.response_headers.append(("Transfer-Encoding", "chunked")) self.chunked_response = True if not self.close_on_finish: @@ -262,14 +264,12 @@ def build_response_header(self): if not server_header: if ident: - response_headers.append(("Server", ident)) + self.response_headers.append(("Server", ident)) else: - response_headers.append(("Via", ident or "waitress")) + self.response_headers.append(("Via", ident or "waitress")) if not date_header: - response_headers.append(("Date", build_http_date(self.start_time))) - - self.response_headers = response_headers + self.response_headers.append(("Date", build_http_date(self.start_time))) first_line = f"HTTP/{self.version} {self.status}" # NB: sorting headers needs to preserve same-named-header order