WebSocket¶
palfrey.protocols.websocket
¶
WebSocket protocol implementation with dual backend support (wsproto/websockets).
This module handles WebSocket upgrade negotiation from HTTP, frame parsing/encoding, and full-duplex message exchange. The module supports two backends: wsproto (pure Python, ASGI-native) and websockets (C extension fallback for speed).
Backend selection is automatic: the module tries to import the Rust-accelerated websockets library first, falls back to wsproto if unavailable. Each backend provides frame masking/unmasking, frame opcode handling (text, binary, close, ping, pong), and payload reassembly from fragmented frames. Backpressure is managed via write buffers and read timeout logic to prevent unbounded accumulation.
Key Design Decisions: - WebSocket upgrade detection leverages existing HTTP request headers (Upgrade, Connection, Sec-WebSocket-Key) parsed by the HTTP module. - Frame payload unmasking uses accelerated palfrey_rust.unmask_websocket_payload when available (Rust extension) or falls back to pure Python bitwise operations. - The module enforces RFC 6455 semantics: client frames must be masked, server frames must not; close frames carry status codes and optional reasons. - Backpressure is signaled via asyncio.Event when write buffer exceeds thresholds, preventing unlimited memory growth under slow client conditions.
Key Classes
- WebSocketFrame: Decoded frame structure (fin, opcode, payload).
Key Functions
- handle_websocket: Main coroutine managing upgrade, frame I/O, and app delegation.
- _read_frame, _write_frame: Low-level frame encoding/decoding.
- _header_value, _header_map: Helper functions for HTTP header lookup.
WebSocketFrame
dataclass
¶
Decoded WebSocket frame structure containing control flags and payload data.
| ATTRIBUTE | DESCRIPTION |
|---|---|
fin |
Indicates if this is the final fragment in a message.
TYPE:
|
opcode |
Defines the interpretation of the payload data (e.g., 0x1 for text, 0x8 for close).
TYPE:
|
payload |
The raw data content of the frame.
TYPE:
|
Source code in palfrey/protocols/websocket.py
build_websocket_scope(*, target, headers, client, server, root_path, is_tls, protocol_header=None, app_state=None, asgi_version='3.0')
¶
Construct an ASGI scope dictionary for a WebSocket connection.
This scope includes connection metadata such as path, headers, and negotiated subprotocols required by the ASGI 3.0 specification.
| PARAMETER | DESCRIPTION |
|---|---|
target
|
The raw request target string.
TYPE:
|
headers
|
Initial HTTP upgrade headers.
TYPE:
|
client
|
IP and port of the connecting client.
TYPE:
|
server
|
IP and port of the server.
TYPE:
|
root_path
|
The ASGI root path.
TYPE:
|
is_tls
|
True if the connection is encrypted.
TYPE:
|
protocol_header
|
Optional pre-extracted protocol header.
TYPE:
|
app_state
|
Lifespan state to shallow-copy into the per-connection scope.
TYPE:
|
asgi_version
|
ASGI callable version reported in the scope.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Scope
|
A dictionary representing the connection scope.
TYPE:
|
Source code in palfrey/protocols/websocket.py
build_handshake_response(headers, *, subprotocol, extra_headers=None)
¶
Extract the client key and build a full Switching Protocols handshake response.
| PARAMETER | DESCRIPTION |
|---|---|
headers
|
Incoming HTTP request headers.
TYPE:
|
subprotocol
|
The negotiated subprotocol to include in headers.
TYPE:
|
extra_headers
|
Additional server headers.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bytes
|
Serialized HTTP 101 response bytes.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the 'Sec-WebSocket-Key' header is missing from the request. |
Source code in palfrey/protocols/websocket.py
handle_websocket(app, config, *, reader, writer, headers, target, client, server, is_tls, app_state=None, asgi_version='3.0')
async
¶
Handle the ASGI WebSocket flow for a connection, dispatching to the configured backend.
Backend dispatching follows Uvicorn semantics: - 'wsproto' -> Use the wsproto engine. - 'websockets-sansio' -> Use the websockets sans-io implementation. - 'websockets' -> Use the full websockets library backend. - 'none' -> Use the core manual framing backend.
Source code in palfrey/protocols/websocket.py
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 | |