diff --git a/h11/_connection.py b/h11/_connection.py index e37d82a..d479bb8 100644 --- a/h11/_connection.py +++ b/h11/_connection.py @@ -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, diff --git a/h11/tests/test_connection.py b/h11/tests/test_connection.py index 01260dc..dde6765 100644 --- a/h11/tests/test_connection.py +++ b/h11/tests/test_connection.py @@ -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"