HTTP/2¶
palfrey.protocols.http2
¶
HTTP/2 protocol implementation with stream multiplexing and flow control via h2 library.
This module handles HTTP/2 connection management, request/response multiplexing over virtual streams, header compression via HPACK, flow control, and server push mechanics. The module uses the h2 library (pure Python hyperframe/hpack implementation) to manage the binary framing layer, stream state machines, and priority windows. A per-stream state object accumulates pseudo-headers and body chunks before constructing an HTTPRequest.
Key Design Decisions: - Streams are multiplexed concurrently, each with independent request/response cycles. - Connection-specific headers (e.g., Connection, Transfer-Encoding) are stripped per HTTP/2 spec since the protocol handles framing and keep-alive at the connection level. - Each stream is mapped to an ASGI scope; multiple requests on one connection are independent from the server's application perspective but share TCP buffering. - Flow control windows are respected to avoid overwhelming the client; the h2 library tracks remote and local window sizes automatically.
Key Classes
- _HTTP2StreamState: Accumulates method, target, headers, and body chunks per stream.
Key Functions
- serve_http2_connection: Main event loop reading frames, routing to streams, managing flow control, and dispatching ASGI app calls.
- _decode_request_headers: Extracts pseudo-headers and normalizes to HTTPRequest.
- _to_text: Decodes header bytes to text with latin-1 semantics.
serve_http2_connection(*, reader, writer, request_handler)
async
¶
Manages an HTTP/2 connection lifecycle.
Reads frames from the reader, updates connection and stream state machines, and dispatches completed requests to the provided handler.
| PARAMETER | DESCRIPTION |
|---|---|
reader
|
The source for incoming frames.
TYPE:
|
writer
|
The destination for outgoing frames.
TYPE:
|
request_handler
|
A coroutine that processes HTTPRequest and returns an HTTPResponse.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the 'h2' library is not available in the environment. |
Source code in palfrey/protocols/http2.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |