Module

_h2_handler

HTTP/2 connection handler — manages multiplexed streams over a single connection.

Extracted fromworker.pyto keep protocol-specific connection handling separate from the core Worker lifecycle. The worker delegates to handle_h2_connection() when ALPN negotiation selects h2.

Each function is a standalone coroutine that receives the minimal state it needs (app, config, logger) — no Worker reference required.

Connection flow:

TLS handshake → ALPN selects "h2" → H2Connection state machine
→ per-stream ASGI tasks → multiplexed responses → GOAWAY

Functions

_get_header_from_tuple 2 bytes | None
Get a header value by lowercase name from a headers tuple.
def _get_header_from_tuple(headers: tuple[tuple[bytes, bytes], ...], name: bytes) -> bytes | None
Parameters
Name Type Description
headers tuple[tuple[bytes, bytes], ...]
name bytes
Returns
bytes | None
handle_h2_connection 8 None
Handle a full HTTP/2 connection with multiplexed streams. Runs one ASGI task p…
async
async def handle_h2_connection(app: ASGIApp, config: ServerConfig, logger: logging.Logger, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, client: tuple[str, int], server: tuple[str, int], client_str: str) -> None

Handle a full HTTP/2 connection with multiplexed streams.

Runs one ASGI task per stream. The main loop reads data from the network, feeds it toH2Connection, and dispatches per-stream events to the appropriate ASGI task.

Parameters
Name Type Description
app ASGIApp

The ASGI application callable.

config ServerConfig

Immutable server configuration.

logger logging.Logger

Logger scoped to the worker.

reader asyncio.StreamReader

Asyncio stream reader for the connection.

writer asyncio.StreamWriter

Asyncio stream writer for the connection.

client tuple[str, int]

(host, port)of the remote peer.

server tuple[str, int]

(host, port)of the local endpoint.

client_str str

Formatted"host:port"string for logging.

handle_h2_websocket_stream 11 None
Handle a WebSocket-over-HTTP/2 stream (RFC 8441). The Extended CONNECT bootstr…
async
async def handle_h2_websocket_stream(app: ASGIApp, config: ServerConfig, logger: logging.Logger, h2_conn: object, stream_id: int, request: RequestReceived, data_queue: asyncio.Queue[dict], writer: asyncio.StreamWriter, client: tuple[str, int], server: tuple[str, int], client_str: str) -> None

Handle a WebSocket-over-HTTP/2 stream (RFC 8441).

The Extended CONNECT bootstraps a WebSocket session within an H2 stream. Data frames on this stream carry WebSocket frames (via wsproto), and the ASGI app sees a standardwebsocketscope.

Parameters
Name Type Description
app ASGIApp

The ASGI application callable.

config ServerConfig

Immutable server configuration.

logger logging.Logger

Logger scoped to the worker.

h2_conn object

TheH2Connectionmanaging this HTTP/2 connection.

stream_id int

The H2 stream identifier for this WebSocket session.

request RequestReceived

The parsed request event (Extended CONNECT).

data_queue asyncio.Queue[dict]

Queue fed by the H2 connection's event loop.

writer asyncio.StreamWriter

Asyncio stream writer for the connection.

client tuple[str, int]

(host, port)of the remote peer.

server tuple[str, int]

(host, port)of the local endpoint.

client_str str

Formatted"host:port"string for logging.