1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! Passive HTTP/1.x observer.
//!
//! Bridges [`httparse`](https://crates.io/crates/httparse)'s
//! zero-copy parser into `flowscope`'s
//! [`SessionParser`](crate::SessionParser) abstraction. Receives
//! bytes from the per-flow TCP stream, emits parsed
//! [`HttpRequest`] / [`HttpResponse`] events as
//! [`HttpMessage`] variants.
//!
//! # Quick start
//!
//! Register an [`HttpParser`] on the typed
//! [`Driver`](crate::driver::Driver) and drain its slot:
//!
//! ```
//! use flowscope::driver::Driver;
//! use flowscope::extract::FiveTuple;
//! use flowscope::http::HttpParser;
//!
//! let mut builder = Driver::builder(FiveTuple::bidirectional());
//! let mut http = builder.session_on_ports(HttpParser::default(), [80, 8080]);
//! let _driver = builder.build();
//! # let _ = &mut http;
//! ```
//!
//! Each drained [`SlotMessage`](crate::driver::SlotMessage) carries
//! the parsed [`HttpMessage`] directly:
//!
//! ```no_run
//! use flowscope::driver::{Driver, Event, SlotMessage};
//! use flowscope::extract::{FiveTuple, FiveTupleKey};
//! use flowscope::http::{HttpMessage, HttpParser};
//! use flowscope::PacketView;
//!
//! let mut builder = Driver::builder(FiveTuple::bidirectional());
//! let mut http = builder.session_on_ports(HttpParser::default(), [80, 8080]);
//! let mut driver = builder.build();
//!
//! let mut events: Vec<Event<FiveTupleKey>> = Vec::new();
//! let mut msgs: Vec<SlotMessage<HttpMessage, FiveTupleKey>> = Vec::new();
//! # fn views() -> impl IntoIterator<Item = PacketView<'static>> { std::iter::empty() }
//! for view in views() {
//! events.clear();
//! msgs.clear();
//! driver.track_into(view, &mut events);
//! http.drain(&mut msgs);
//! for m in &msgs {
//! match &m.message {
//! HttpMessage::Request(r) => { let _ = r; /* handle request */ }
//! HttpMessage::Response(r) => { let _ = r; /* handle response */ }
//! _ => {}
//! }
//! }
//! }
//! ```
//!
//! # Scope
//!
//! - HTTP/1.0 and HTTP/1.1.
//! - Request line + headers + body via `Content-Length`, chunked
//! `Transfer-Encoding` (decoded, with trailers), or connection
//! close.
//! - Method-aware response framing: `HEAD` / `1xx` / `204` / `304`
//! responses carry no body (RFC 9112 §6.3 rules 1–2).
//! - Pipelined requests on one connection.
//! - HTTP/2 / HTTP/3: out of scope here (see the `http2` feature).
//!
//! # Two front-ends, one engine
//!
//! Internally this module is a single streaming state machine that
//! emits a head as soon as the header block parses, then body spans
//! it never retains, then trailers, then an end marker. Two public
//! shapes sit on top of it — pick by what you are building:
//!
//! | | [`HttpParser`] | [`HttpProxyParser`] |
//! |---|---|---|
//! | For | passive observation | an inline proxy |
//! | Output | one [`HttpRequest`] / [`HttpResponse`] per message | [`HttpEvent`]s as they happen |
//! | Body | buffered, capped by `max_buffer` | never retained |
//! | Routing key | after the whole message | at the head, before the body |
//! | Framing violation | dropped quietly, flow survives | typed [`HttpPoison`], connection stops |
//! | Driven by | flowscope (`SessionParser`) | you (`push` / `next_event`) |
//!
//! [`HttpProxySession`] bridges the two worlds: the streaming event
//! stream as a `SessionParser`, so it can ride the typed `Driver` and
//! the pcap helpers when flowscope owns the bytes.
//!
//! Because framing lives in one place, chunked decoding, body
//! delimitation, method-aware response framing, and the RFC 9112
//! §6.3 smuggling rules behave identically whichever you use — only
//! the [`SmugglingPolicy`] differs, and that is deliberate: a proxy
//! refuses an ambiguous message, an observer keeps observing.
//!
//! # Inline proxying
//!
//! ```no_run
//! use bytes::Bytes;
//! use flowscope::FlowSide;
//! use flowscope::http::{HttpEvent, HttpProxyParser};
//!
//! # fn read_from_client() -> Bytes { Bytes::new() }
//! # fn forward_upstream(_: &[u8]) {}
//! # fn close_with_status(_: u16) {}
//! let mut proxy = HttpProxyParser::new();
//! let mut pending = read_from_client();
//!
//! while !pending.is_empty() {
//! // A short count means "drain events first" — that is the
//! // backpressure signal, and slicing is a refcount bump.
//! let accepted = proxy.push(FlowSide::Initiator, &pending);
//! pending = pending.slice(accepted..);
//!
//! while let Some(ev) = proxy.next_event() {
//! match ev {
//! // Pick a backend before a single body byte is read.
//! HttpEvent::RequestHead(head) => {
//! let authority = head.authority();
//! forward_upstream(&head.raw);
//! }
//! // Relay the wire bytes; nothing is retained here.
//! HttpEvent::Body { raw, .. } => forward_upstream(&raw),
//! HttpEvent::SwitchProtocols { .. } => break, // splice from here
//! _ => {}
//! }
//! }
//!
//! // Framing is in doubt: stop, do not forward the remainder.
//! if let Some(reason) = proxy.poison() {
//! eprintln!("refusing connection: {reason}");
//! close_with_status(400);
//! break;
//! }
//! }
//! ```
//!
//! See `examples/01-l7-logging/http_streaming_proxy.rs` for a
//! runnable version, including the check that forwarded bytes are
//! byte-identical to what arrived.
//!
//! # Convenience accessors
//!
//! Every header accessor below skips the boilerplate of "iterate
//! the `headers: Vec<(String, Vec<u8>)>`, case-insensitive
//! compare, then `from_utf8`". They cover the headers every
//! HTTP-monitor example reached for.
//!
//! ## [`HttpRequest`]
//!
//! | Method | Returns | Equivalent |
//! |--------|---------|------------|
//! | [`host`](HttpRequest::host) | `Option<&str>` | first `Host` header |
//! | [`user_agent`](HttpRequest::user_agent) | `Option<&str>` | first `User-Agent` |
//! | [`cookie`](HttpRequest::cookie) | `Option<&str>` | first `Cookie` |
//! | [`content_type`](HttpRequest::content_type) | `Option<&str>` | `Content-Type` (**new in 0.10**) |
//! | [`content_length`](HttpRequest::content_length) | `Option<u64>` | parsed `Content-Length` (**new in 0.10**) |
//! | [`referer`](HttpRequest::referer) | `Option<&str>` | `Referer` (**new in 0.10**) |
//! | [`accept`](HttpRequest::accept) | `Option<&str>` | `Accept` (**new in 0.10**) |
//! | [`header`](HttpRequest::header) | `Option<&[u8]>` | first matching header (case-insensitive) |
//! | [`headers_all`](HttpRequest::headers_all) | `impl Iterator<Item = &[u8]>` | every matching header |
//!
//! ## [`HttpResponse`]
//!
//! | Method | Returns | Equivalent |
//! |--------|---------|------------|
//! | [`status_class`](HttpResponse::status_class) | `Option<u8>` | `status / 100` (**new in 0.10**) |
//! | [`is_success`](HttpResponse::is_success) | `bool` | `2xx` (**new in 0.10**) |
//! | [`is_redirect`](HttpResponse::is_redirect) | `bool` | `3xx` (**new in 0.10**) |
//! | [`is_client_error`](HttpResponse::is_client_error) | `bool` | `4xx` (**new in 0.10**) |
//! | [`is_server_error`](HttpResponse::is_server_error) | `bool` | `5xx` (**new in 0.10**) |
//! | [`content_type`](HttpResponse::content_type) | `Option<&str>` | `Content-Type` |
//! | [`content_length`](HttpResponse::content_length) | `Option<u64>` | parsed `Content-Length` |
//! | [`set_cookie`](HttpResponse::set_cookie) | `impl Iterator<Item = &str>` | every `Set-Cookie` header |
//! | [`header`](HttpResponse::header) | `Option<&[u8]>` | first matching header |
//! | [`headers_all`](HttpResponse::headers_all) | `impl Iterator<Item = &[u8]>` | every matching header |
// JA4H is FoxIO License 1.1 (patent pending) — opt-in via the
// `ja4plus` feature alongside JA4S. See LICENSE-FoxIO-1.1 +
// NOTICE.
pub use ;
pub use ;
pub use ;
pub use ;
pub use HttpPoison;
pub use ;
pub use ;
pub use ;
/// Slug returned by [`HttpParser`]'s `parser_kind()`. Use at
/// match sites in place of a string literal so typos fail to
/// resolve instead of silently miss. Prefer the typed
/// [`ParserKind::Http1`](crate::ParserKind::Http1).
///
/// Stability: locked from 0.8 forward.
pub const PARSER_KIND: &str = "http/1";