Module

_ws_handler

WebSocket connection handler — HTTP/1.1 upgrade lifecycle.

Extracted fromworker.pyto keep protocol-specific connection handling separate from the core Worker lifecycle. The worker delegates to handle_websocket()when it detects an HTTP/1.1 WebSocket upgrade.

Connection flow:

H1 request → detect ``Connection: Upgrade`` + ``Upgrade: websocket``
→ build ASGI ``websocket`` scope → 101 Switching Protocols
→ wsproto frame loop → app(scope, receive, send)

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_websocket 9 None
Handle a WebSocket connection after HTTP/1.1 upgrade detection. **Lifecycle:**…
async
async def handle_websocket(app: ASGIApp, config: ServerConfig, logger: logging.Logger, request: RequestReceived, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, client: tuple[str, int], server: tuple[str, int], client_str: str) -> None

Handle a WebSocket connection after HTTP/1.1 upgrade detection.

Lifecycle:

  1. Build ASGIwebsocketscope
    1. Pushwebsocket.connectto the receive queue
    2. Run the ASGI app — it sendswebsocket.accept(or close)
    3. Read WebSocket frames and feed to receive queue
    4. App sendswebsocket.send / websocket.close
    5. Clean up when either side disconnects
Parameters
Name Type Description
app ASGIApp

The ASGI application callable.

config ServerConfig

Immutable server configuration.

logger logging.Logger

Logger scoped to the worker.

request RequestReceived

The parsed HTTP request that triggered the upgrade.

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.