Skip to content

Ensure the headers are not modified in HTTPResponse #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions adafruit_httpserver/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments

response = f"{http_version} {status.code} {status.text}\r\n"

headers = headers or {}
# Make a copy of the headers so that we don't modify the incoming dict
response_headers = {} if headers is None else headers.copy()

headers.setdefault("Content-Type", content_type)
headers.setdefault("Content-Length", content_length or len(body))
headers.setdefault("Connection", "close")
response_headers.setdefault("Content-Type", content_type)
response_headers.setdefault("Content-Length", content_length or len(body))
response_headers.setdefault("Connection", "close")

for header, value in headers.items():
for header, value in response_headers.items():
response += f"{header}: {value}\r\n"

response += f"\r\n{body}"
Expand Down