From 7d407ae02c8993d7ef9a58438e7bf77a824a8fcf Mon Sep 17 00:00:00 2001 From: Gyanu Date: Sat, 29 Aug 2026 10:39:27 +0530 Subject: [PATCH 1/2] Type Headers as Sequence, not list Callers shouldn't have to copy a tuple or h11 headers object into a list. --- CHANGELOG.rst | 2 ++ src/wsproto/typing.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fe46003..8258108 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,8 @@ Release History 1.4.0 (unreleased) ------------------ +- Accept any sequence of ``(name, value)`` header pairs, matching ``h11``. + - Require ``event_hint`` when constructing ``RemoteProtocolError``. This is an API-breaking change. diff --git a/src/wsproto/typing.py b/src/wsproto/typing.py index 0063d21..d090091 100644 --- a/src/wsproto/typing.py +++ b/src/wsproto/typing.py @@ -1,3 +1,3 @@ -from __future__ import annotations +from typing import Sequence -Headers = list[tuple[bytes, bytes]] +Headers = Sequence[tuple[bytes, bytes]] From 3330d6d7b058444ed04920dce853cdbf7d6b32c5 Mon Sep 17 00:00:00 2001 From: Gyanu Date: Sat, 29 Aug 2026 18:22:23 +0530 Subject: [PATCH 2/2] Type incoming headers as Sequence, keep handshake lists as lists. --- src/wsproto/handshake.py | 12 +++++++----- src/wsproto/typing.py | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/wsproto/handshake.py b/src/wsproto/handshake.py index ab390b8..c2c25f8 100644 --- a/src/wsproto/handshake.py +++ b/src/wsproto/handshake.py @@ -89,7 +89,9 @@ def initiate_upgrade_connection( raise LocalProtocolError( msg, ) - upgrade_request = h11.Request(method=b"GET", target=path, headers=headers) + upgrade_request = h11.Request( + method=b"GET", target=path, headers=list(headers), + ) h11_client = h11.Connection(h11.CLIENT) self.receive_data(h11_client.send(upgrade_request)) @@ -204,7 +206,7 @@ def _process_connection_request( subprotocols: list[str] = [] upgrade = b"" version = None - headers: Headers = [] + headers: list[tuple[bytes, bytes]] = [] for name, value in event.headers: name = name.lower() if name == b"connection": @@ -299,7 +301,7 @@ def _accept(self, event: AcceptConnection) -> bytes: response = h11.InformationalResponse( status_code=101, - headers=headers + event.extra_headers, + headers=headers + list(event.extra_headers), reason=b"Switching Protocols", ) self._connection = Connection( @@ -381,7 +383,7 @@ def _initiate_connection(self, request: Request) -> bytes: upgrade = h11.Request( method=b"GET", target=request.target.encode("ascii"), - headers=headers + request.extra_headers, + headers=headers + list(request.extra_headers), ) return self._h11_connection.send(upgrade) or b"" @@ -397,7 +399,7 @@ def _establish_client_connection( accepts: list[str] = [] subprotocol = None upgrade = b"" - headers: Headers = [] + headers: list[tuple[bytes, bytes]] = [] for name, value in event.headers: name = name.lower() if name == b"connection": diff --git a/src/wsproto/typing.py b/src/wsproto/typing.py index d090091..1674b92 100644 --- a/src/wsproto/typing.py +++ b/src/wsproto/typing.py @@ -1,3 +1,3 @@ -from typing import Sequence +from collections.abc import Sequence Headers = Sequence[tuple[bytes, bytes]]