Skip to content
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

Don't set connection close headers when they are already set on the Response #157

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions h11/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,9 @@ def _clean_up_response_headers_for_sending(self, response: Response) -> Response
# Make sure Connection: close is set
connection = set(get_comma_header(headers, b"connection"))
connection.discard(b"keep-alive")
connection.add(b"close")
headers = set_comma_header(headers, b"connection", sorted(connection))
if b"close" not in connection:
connection.add(b"close")
headers = set_comma_header(headers, b"connection", sorted(connection))

return Response(
headers=headers,
Expand Down
7 changes: 5 additions & 2 deletions h11/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
ConnectionClosed,
Data,
EndOfMessage,
Event,
InformationalResponse,
Request,
Response,
Expand All @@ -17,7 +16,6 @@
CLOSED,
DONE,
ERROR,
IDLE,
MIGHT_SWITCH_PROTOCOL,
MUST_CLOSE,
SEND_BODY,
Expand Down Expand Up @@ -1120,3 +1118,8 @@ def test_special_exceptions_for_lost_connection_in_message_body() -> None:
with pytest.raises(RemoteProtocolError) as excinfo:
c.next_event()
assert "incomplete chunked read" in str(excinfo.value)

def test_ensure_connection_close_remains_untouched() -> None:
c = Connection(SERVER)
data = c.send(Response(status_code=200, headers=[(b"connection", b"close")])) # type: ignore[arg-type]
assert data == b"HTTP/1.1 200 \r\n" b"connection: close\r\n\r\n"
Loading