Skip to content

Commit

Permalink
Only use self.response_headers after normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalresistor committed Feb 4, 2024
1 parent 69f0115 commit dd25fab
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/waitress/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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()

Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -483,8 +483,9 @@ def start_response(status, headers, exc_info=None):
if self.request.command != "HEAD":
self.logger.warning(
"application returned too few bytes (%s) "
"for specified Content-Length (%s) via app_iter"
, self.content_bytes_written, cl
"for specified Content-Length (%s) via app_iter",
self.content_bytes_written,
cl,
)
finally:
if can_close_app_iter and hasattr(app_iter, "close"):
Expand Down

0 comments on commit dd25fab

Please sign in to comment.