Skip to content

Commit

Permalink
Don't set connection close headers when they are already set on the R…
Browse files Browse the repository at this point in the history
…esponse
  • Loading branch information
Kludex committed Dec 24, 2022
1 parent cdccbef commit 0532b97
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 3 additions & 2 deletions h11/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,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"

0 comments on commit 0532b97

Please sign in to comment.