Skip to main content

flowscope/http/
types.rs

1use bytes::Bytes;
2
3/// Parsed HTTP/1.x request — start line + headers + body.
4#[derive(Debug, Clone)]
5pub struct HttpRequest {
6    pub method: String,
7    pub path: String,
8    pub version: HttpVersion,
9    /// Header (name, value) pairs in order. Names are ASCII;
10    /// values are bytes (RFC 7230 §3.2.4 allows any byte).
11    pub headers: Vec<(String, Vec<u8>)>,
12    /// Body bytes. Empty if no body or transfer-encoding only signals
13    /// EOF semantics with nothing transferred yet.
14    pub body: Bytes,
15}
16
17/// Parsed HTTP/1.x response.
18#[derive(Debug, Clone)]
19pub struct HttpResponse {
20    pub status: u16,
21    pub reason: String,
22    pub version: HttpVersion,
23    pub headers: Vec<(String, Vec<u8>)>,
24    pub body: Bytes,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum HttpVersion {
29    Http1_0,
30    Http1_1,
31}
32
33/// User implements this to receive parsed HTTP messages.
34pub trait HttpHandler: Send + Sync + 'static {
35    fn on_request(&self, _req: &HttpRequest) {}
36    fn on_response(&self, _resp: &HttpResponse) {}
37}
38
39/// Configuration knobs for the HTTP parser.
40#[derive(Debug, Clone)]
41pub struct HttpConfig {
42    /// Cap on the buffered bytes per direction. Once exceeded the
43    /// reassembler drops the per-flow buffer to recover memory; the
44    /// flow continues at TCP level but HTTP for that direction is
45    /// considered desynced.
46    pub max_buffer: usize,
47    /// Cap on number of headers per message. Default: 64.
48    pub max_headers: usize,
49}
50
51impl Default for HttpConfig {
52    fn default() -> Self {
53        Self {
54            max_buffer: 1024 * 1024, // 1 MiB
55            max_headers: 64,
56        }
57    }
58}