Skip to content
Open
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
7 changes: 4 additions & 3 deletions h11/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,10 @@ def _clean_up_response_headers_for_sending(self, response: Response) -> Response
if not self._cstate.keep_alive or need_close:
# 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 or b"keep-alive" in connection:
connection.discard(b"keep-alive")
connection.add(b"close")
headers = set_comma_header(headers, b"connection", sorted(connection))

return Response(
headers=headers,
Expand Down
17 changes: 17 additions & 0 deletions h11/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,23 @@ def test_automagic_connection_close_handling() -> None:
assert conn.states == {CLIENT: MUST_CLOSE, SERVER: MUST_CLOSE}


def test_explicit_connection_close_preserves_header_case() -> None:
c = Connection(SERVER)
c.receive_data(
b"GET / HTTP/1.1\r\n"
b"Host: example.com\r\n"
b"Connection: close\r\n"
b"\r\n"
)
assert isinstance(c.next_event(), Request)
assert isinstance(c.next_event(), EndOfMessage)

assert (
c.send(Response(status_code=204, headers=[("connection", "close")]))
== b"HTTP/1.1 204 \r\nconnection: close\r\n\r\n"
)


def test_100_continue() -> None:
def setup() -> ConnectionPair:
p = ConnectionPair()
Expand Down
2 changes: 2 additions & 0 deletions newsfragments/156.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Preserve an explicitly provided ``Connection`` header's original casing when
it already requests ``close``.