Skip to main content

Crate h2session

Crate h2session 

Source
Expand description

Stateful HTTP/2 frame parser with HPACK support for passive traffic monitoring.

This crate decodes HTTP/2 frames and HPACK-compressed headers from raw byte streams, maintaining per-connection decoder state so that dynamic table entries are preserved across successive feed() calls.

§Key types

  • H2SessionCache — thread-safe cache of many connections keyed by an arbitrary K. Best when you have many connections and want automatic state management.
  • H2ConnectionState — state for a single HTTP/2 connection. Use feed() to push data incrementally and try_pop() to retrieve completed messages.

§Examples

§Multi-connection cache

use h2session::{H2SessionCache, ParsedH2Message};

let cache = H2SessionCache::<u64>::new();

// Parse a buffer for connection 42
let completed = cache.parse(42, &raw_bytes).unwrap();
for (stream_id, msg) in &completed {
    if msg.is_request() {
        println!(
            "{} {}",
            msg.method.as_deref().unwrap_or("?"),
            msg.path.as_deref().unwrap_or("/")
        );
    }
}

§Single-connection incremental parsing

use h2session::{H2ConnectionState, TimestampNs};

let mut state = H2ConnectionState::new();

// Feed data as it arrives
state.feed(&chunk, TimestampNs(0)).unwrap();

// Pop completed messages
while let Some((stream_id, msg)) = state.try_pop() {
    println!("stream {stream_id}: request={}", msg.is_request());
}

§Feature flags

  • tracing — emit tracing::warn! events for non-fatal parse issues (stale stream eviction, etc.)

Structs§

H2ConnectionState
Connection-level HTTP/2 state
H2Limits
Configurable limits for HTTP/2 header decoding and stream management.
H2SessionCache
HTTP/2 session cache with generic connection keys.
HttpRequest
HTTP request parsed from any HTTP version
HttpResponse
HTTP response parsed from any HTTP version
ParseError
Parse error with optional stream context (public API)
ParsedH2Message
A fully parsed HTTP/2 message extracted from a completed stream.
StreamId
Newtype for HTTP/2 stream identifiers (RFC 7540 §5.1.1: 31-bit unsigned integer).
TimestampNs
Newtype for nanosecond-precision timestamps (monotonic clock).

Enums§

ParseErrorKind
Classification of parse errors (public API)

Constants§

CONNECTION_PREFACE
HTTP/2 connection preface: “PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n”

Functions§

is_http2_preface
Check if buffer starts with HTTP/2 connection preface
looks_like_http2_frame
Heuristic check if buffer looks like an HTTP/2 frame header. Checks for valid frame type and reasonable length.