Skip to content

Commit 39e4245

Browse files
committed
Preserve explicit Connection header casing
1 parent 62c5068 commit 39e4245

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

h11/_connection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,10 @@ def _clean_up_response_headers_for_sending(self, response: Response) -> Response
647647
if not self._cstate.keep_alive or need_close:
648648
# Make sure Connection: close is set
649649
connection = set(get_comma_header(headers, b"connection"))
650-
connection.discard(b"keep-alive")
651-
connection.add(b"close")
652-
headers = set_comma_header(headers, b"connection", sorted(connection))
650+
if b"close" not in connection or b"keep-alive" in connection:
651+
connection.discard(b"keep-alive")
652+
connection.add(b"close")
653+
headers = set_comma_header(headers, b"connection", sorted(connection))
653654

654655
return Response(
655656
headers=headers,

h11/tests/test_connection.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,23 @@ def test_automagic_connection_close_handling() -> None:
365365
assert conn.states == {CLIENT: MUST_CLOSE, SERVER: MUST_CLOSE}
366366

367367

368+
def test_explicit_connection_close_preserves_header_case() -> None:
369+
c = Connection(SERVER)
370+
c.receive_data(
371+
b"GET / HTTP/1.1\r\n"
372+
b"Host: example.com\r\n"
373+
b"Connection: close\r\n"
374+
b"\r\n"
375+
)
376+
assert isinstance(c.next_event(), Request)
377+
assert isinstance(c.next_event(), EndOfMessage)
378+
379+
assert (
380+
c.send(Response(status_code=204, headers=[("connection", "close")]))
381+
== b"HTTP/1.1 204 \r\nconnection: close\r\n\r\n"
382+
)
383+
384+
368385
def test_100_continue() -> None:
369386
def setup() -> ConnectionPair:
370387
p = ConnectionPair()

newsfragments/156.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Preserve an explicitly provided ``Connection`` header's original casing when
2+
it already requests ``close``.

0 commit comments

Comments
 (0)