Acceleration¶
palfrey.acceleration
¶
Acceleration shim pattern: optional Rust extension with pure Python fallbacks.
This module implements a graceful degradation pattern for performance-critical functions. The module attempts to import pre-compiled Rust accelerators from palfrey_rust and uses them if available; otherwise, it provides pure Python implementations ensuring the server runs without compiled dependencies.
Accelerated Functions (with Python fallback): - parse_request_head: Parse HTTP/1.1 request line and headers to (method, target, http_version, headers) tuple. Used by HTTP/1.1 parsing pipeline. - parse_header_items: Parse CLI header arguments ('name:value' format) into tuples. - split_csv_values: Split comma-separated strings into normalized value lists. - unmask_websocket_payload: Unmask WebSocket frame payloads (XOR with 4-byte mask). Used for every client-to-server WebSocket frame per RFC 6455.
The module provides a HAS_RUST_EXTENSION flag indicating whether Rust libraries are available, allowing diagnostics and logging. Each function gracefully falls back to Python if the Rust version fails at import time or at call time.
Import Error Handling
- If palfrey_rust is not found (no compiled binary), use Python implementations.
- If individual Rust functions fail at runtime, caught exceptions preserve function availability while logging the failure for diagnostic purposes.
Key Functions
- All public functions (parse_request_head, parse_header_items, etc.) abstract the backend selection; call sites never need to know which implementation runs.
HeaderParseError
¶
Bases: ValueError
Exception raised when a header entry fails to conform to the expected 'name:value' format.
This error typically occurs during the manual parsing of header sequences when the required colon separator is missing from an input string.
Source code in palfrey/acceleration.py
parse_header_items(headers)
¶
Parses a sequence of raw header strings into a list of key-value tuples.
Each string in the input sequence is expected to be in the format 'name:value'. The function trims whitespace from the name and leading whitespace from the value.
| PARAMETER | DESCRIPTION |
|---|---|
headers
|
A sequence of header strings, such as those provided via command-line interfaces.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[str, str]]
|
list[tuple[str, str]]: A list of tuples where the first element is the stripped header name and the second is the left-stripped header value. |
| RAISES | DESCRIPTION |
|---|---|
HeaderParseError
|
If a header string does not contain the mandatory colon separator. |
Source code in palfrey/acceleration.py
split_csv_values(value)
¶
Splits a comma-separated string into a list of individual, normalized values.
This function removes surrounding whitespace from each segment and filters out any resulting empty strings to ensure a clean list of values.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The raw comma-separated string to be processed.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
list[str]: A list of non-empty, trimmed strings extracted from the input. |
Source code in palfrey/acceleration.py
parse_request_head(data)
¶
Parses raw HTTP request head bytes into structured components.
Processes the initial request line and subsequent headers. The input data should ideally contain the full header block ending with the standard CRLFCRLF delimiter.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
The raw byte sequence representing the HTTP request head.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, str, str, list[tuple[str, str]]]
|
tuple[str, str, str, list[tuple[str, str]]]: A four-element tuple containing (method, target, version, headers), where headers is a list of name-value pairs. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the request line is missing, malformed, or if any header line is not correctly formatted. |
Source code in palfrey/acceleration.py
unmask_websocket_payload(payload, masking_key)
¶
Applies the WebSocket XOR masking algorithm to a payload.
WebSockets require client-to-server frames to be masked using a 4-byte key. This function reverses that mask to retrieve the original data (or applies it).
| PARAMETER | DESCRIPTION |
|---|---|
payload
|
The masked (or unmasked) payload data buffer.
TYPE:
|
masking_key
|
A 4-byte byte string used as the XOR masking key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bytes
|
The resulting transformed byte string.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the provided masking_key is not exactly 4 bytes in length. |