Skip to content
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 @@ -648,8 +648,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
6 changes: 6 additions & 0 deletions h11/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,3 +1118,9 @@ 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")]))
assert data == b"HTTP/1.1 200 \r\n" b"connection: close\r\n\r\n"