From 800007251bfac10b3310efb80a38a79089617ee9 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sat, 24 Dec 2022 10:41:50 +0100 Subject: [PATCH 1/2] Don't set connection close headers when they are already set on the Response --- h11/_connection.py | 5 +++-- h11/tests/test_connection.py | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/h11/_connection.py b/h11/_connection.py index a812298..2616efa 100644 --- a/h11/_connection.py +++ b/h11/_connection.py @@ -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, diff --git a/h11/tests/test_connection.py b/h11/tests/test_connection.py index f45cb97..56f2c32 100644 --- a/h11/tests/test_connection.py +++ b/h11/tests/test_connection.py @@ -7,7 +7,6 @@ ConnectionClosed, Data, EndOfMessage, - Event, InformationalResponse, Request, Response, @@ -17,7 +16,6 @@ CLOSED, DONE, ERROR, - IDLE, MIGHT_SWITCH_PROTOCOL, MUST_CLOSE, SEND_BODY, @@ -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" From 78f878b31939d6b4a2932ee1f57b753aef006107 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sat, 2 Mar 2024 11:16:20 +0100 Subject: [PATCH 2/2] Empty commit