simoom 0.1.0

simoom: a small, fast, secure embeddable HTTP/1.1, HTTP/2 and HTTP/3 server.
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! HTTP/2 through the facade (h2c prior knowledge and `Upgrade: h2c`) with the simoom-h2 client.

#![cfg(feature = "http2")]

mod common;

use common::*;
use simoom::{blocking, Limits, Outcome, OwnedRequest, Reply, Request, Response};
use simoom_h2::{Config, Connection, Event};
use std::collections::HashMap;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::time::{Duration, Instant};

#[derive(Default, Debug, Clone)]
pub struct H2Resp {
    pub status: u16,
    pub headers: Vec<(Vec<u8>, Vec<u8>)>,
    pub body: Vec<u8>,
    pub done: bool,
    pub reset: bool,
}

impl H2Resp {
    fn header(&self, n: &str) -> Option<&[u8]> {
        self.headers.iter().find(|(k, _)| k == n.as_bytes()).map(|(_, v)| &v[..])
    }
}

pub struct H2Client {
    pub s: TcpStream,
    pub conn: Connection,
    inbuf: Vec<u8>,
    pub resps: HashMap<u32, H2Resp>,
    start: Instant,
    pub goaway: bool,
    pub eof: bool,
}

impl H2Client {
    pub fn with(s: TcpStream, conn: Connection, leftover: Vec<u8>) -> H2Client {
        s.set_read_timeout(Some(Duration::from_millis(50))).unwrap();
        let mut c = H2Client { s, conn, inbuf: leftover, resps: HashMap::new(), start: Instant::now(), goaway: false, eof: false };
        c.flush();
        c
    }

    pub fn prior(h: &simoom::ServerHandle) -> H2Client {
        H2Client::with(connect(h), Connection::client(Config::default()), Vec::new())
    }

    fn now(&self) -> u64 {
        self.start.elapsed().as_millis() as u64
    }

    pub fn flush(&mut self) {
        let out = self.conn.output().to_vec();
        if !out.is_empty() {
            let _ = self.s.write_all(&out);
            self.conn.advance_output(out.len());
        }
    }

    /// One read and processing step.
    pub fn step(&mut self) {
        let mut tmp = vec![0u8; 65536];
        match self.s.read(&mut tmp) {
            Ok(0) => self.eof = true,
            Ok(n) => self.inbuf.extend_from_slice(&tmp[..n]),
            Err(e) if e.kind() == std::io::ErrorKind::WouldBlock || e.kind() == std::io::ErrorKind::TimedOut => {}
            Err(_) => self.eof = true,
        }
        let now = self.now();
        let resps = &mut self.resps;
        let mut release = Vec::new();
        let mut goaway = false;
        let n = self.conn.recv(&self.inbuf, now, &mut |ev| match ev {
            Event::Headers { stream, headers, end_stream } => {
                let r = resps.entry(stream).or_default();
                if let Some(st) = headers.status() {
                    if st >= 200 {
                        r.status = st;
                        r.headers = headers.iter().map(|(a, b)| (a.to_vec(), b.to_vec())).collect();
                    }
                }
                r.done |= end_stream;
            }
            Event::Data { stream, data, end_stream } => {
                let r = resps.entry(stream).or_default();
                r.body.extend_from_slice(data);
                r.done |= end_stream;
                release.push((stream, data.len()));
            }
            Event::Trailers { stream, .. } => resps.entry(stream).or_default().done = true,
            Event::Reset { stream, .. } => {
                let r = resps.entry(stream).or_default();
                r.reset = true;
                r.done = true;
            }
            Event::GoAway { .. } | Event::Closed { .. } => goaway = true,
            _ => {}
        });
        self.goaway |= goaway;
        self.inbuf.drain(..n);
        for (s, n) in release {
            self.conn.release(s, n);
        }
        self.flush();
    }

    pub fn request(&mut self, method: &str, path: &str, body: &[u8]) -> u32 {
        let id = self.conn.send_request(method.as_bytes(), b"http", b"t", path.as_bytes(), &[], body.is_empty()).unwrap();
        let mut sent = 0;
        while !body.is_empty() && sent < body.len() {
            let n = self.conn.send_data(id, &body[sent..], true).unwrap();
            sent += n;
            self.flush();
            if sent < body.len() {
                self.step();
            }
        }
        self.flush();
        id
    }

    pub fn wait(&mut self, id: u32) -> H2Resp {
        let end = Instant::now() + Duration::from_secs(15);
        while !self.resps.get(&id).is_some_and(|r| r.done) {
            assert!(Instant::now() < end, "timeout waiting for stream {id}");
            assert!(!self.eof, "connection closed waiting for stream {id}");
            self.step();
        }
        self.resps.remove(&id).unwrap()
    }
}

fn echo(req: &mut Request<'_>, reply: Reply<'_>) -> Outcome {
    let s = format!("{} {} {:?} {}", String::from_utf8_lossy(req.method()), req.path_str(), req.version(), String::from_utf8_lossy(req.body()));
    reply.header("content-type", "text/plain").header("x-host", req.host().unwrap_or(b"")).body(s)
}

#[test]
fn prior_knowledge_get_and_post() {
    let h = serve(echo);
    let mut c = H2Client::prior(&h);
    let id = c.request("GET", "/a?q=1", b"");
    let r = c.wait(id);
    assert_eq!(r.status, 200);
    assert_eq!(r.body, b"GET /a H2 ");
    assert_eq!(r.header("x-host"), Some(&b"t"[..]));
    assert!(r.header("date").is_some());
    assert_eq!(r.header("content-length"), Some(&b"10"[..]));
    let id = c.request("POST", "/b", b"payload");
    assert_eq!(c.wait(id).body, b"POST /b H2 payload");
    assert!(h.stats().h2_connections >= 1);
}

#[test]
fn concurrent_streams_complete_out_of_order() {
    let h = serve(|req: &mut Request<'_>, reply: Reply<'_>| -> Outcome {
        let n: u64 = req.path_str().trim_start_matches('/').parse().unwrap_or(0);
        let responder = reply.defer();
        std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(50 - n % 50));
            responder.send(Response::text(format!("r{n}")));
        });
        Outcome::Deferred
    });
    let mut c = H2Client::prior(&h);
    let ids: Vec<(u32, u64)> = (0..60).map(|i| (c.request("GET", &format!("/{i}"), b""), i)).collect();
    for (id, i) in ids {
        assert_eq!(c.wait(id).body, format!("r{i}").into_bytes());
    }
}

#[test]
fn large_bodies_with_flow_control() {
    let big: Vec<u8> = (0..4 * 1024 * 1024u32).map(|i| (i % 253) as u8).collect();
    let b2 = big.clone();
    let h = serve(move |req: &mut Request<'_>, reply: Reply<'_>| -> Outcome {
        if req.path() == b"/down" {
            reply.body(b2.clone())
        } else {
            reply.blocking(req, |mut r| {
                let body = r.read_body().unwrap();
                Response::text(format!("{} {}", body.len(), body.iter().map(|&b| u64::from(b)).sum::<u64>()))
            })
        }
    });
    let mut c = H2Client::prior(&h);
    let id = c.request("GET", "/down", b"");
    let r = c.wait(id);
    assert_eq!(r.body.len(), big.len());
    assert!(r.body == big);
    // Upload 2 MiB: larger than the body buffer, streamed with flow control.
    let up = &big[..2 * 1024 * 1024];
    let id = c.request("POST", "/up", up);
    let sum: u64 = up.iter().map(|&b| u64::from(b)).sum();
    assert_eq!(c.wait(id).body, format!("{} {sum}", up.len()).into_bytes());
}

#[test]
fn streamed_response_over_h2() {
    let h = serve(|_: &mut Request<'_>, reply: Reply<'_>| -> Outcome {
        let mut sink = reply.sse();
        std::thread::spawn(move || {
            for i in 0..5 {
                sink.send_event(&format!("e{i}")).unwrap();
            }
        });
        Outcome::Done
    });
    let mut c = H2Client::prior(&h);
    let id = c.request("GET", "/events", b"");
    let r = c.wait(id);
    assert_eq!(r.header("content-type"), Some(&b"text/event-stream"[..]));
    assert_eq!(String::from_utf8(r.body).unwrap(), "data: e0\n\ndata: e1\n\ndata: e2\n\ndata: e3\n\ndata: e4\n\n");
}

#[test]
fn upgrade_from_http1() {
    let h = serve(echo);
    let cfg = Config::default();
    let mut s = connect(&h);
    let settings = simoom_h2::http2_settings_header(&cfg);
    s.write_all(format!("GET /up HTTP/1.1\r\nHost: t\r\nConnection: Upgrade, HTTP2-Settings\r\nUpgrade: h2c\r\nHTTP2-Settings: {settings}\r\n\r\n").as_bytes()).unwrap();
    let mut buf = Vec::new();
    let mut tmp = [0u8; 4096];
    let head_end = loop {
        let n = s.read(&mut tmp).unwrap();
        assert!(n > 0);
        buf.extend_from_slice(&tmp[..n]);
        if let Some(i) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
            break i + 4;
        }
    };
    assert!(buf.starts_with(b"HTTP/1.1 101 "), "{}", String::from_utf8_lossy(&buf));
    let rest = buf[head_end..].to_vec();
    let mut c = H2Client::with(s, Connection::client_upgrade(cfg, false), rest);
    let r = c.wait(1);
    assert_eq!(r.body, b"GET /up H2 ");
    // The connection carries on as HTTP/2.
    let id = c.request("GET", "/next", b"");
    assert_eq!(c.wait(id).body, b"GET /next H2 ");
    assert!(h.stats().upgrades >= 1);
}

#[test]
fn panic_gives_500_and_goaway() {
    let h = serve(|req: &mut Request<'_>, reply: Reply<'_>| -> Outcome {
        if req.path() == b"/panic" {
            panic!("expected by the test");
        }
        reply.body_static(b"ok")
    });
    let mut c = H2Client::prior(&h);
    let id = c.request("GET", "/panic", b"");
    assert_eq!(c.wait(id).status, 500);
    let end = Instant::now() + Duration::from_secs(5);
    while !c.goaway && Instant::now() < end {
        c.step();
    }
    assert!(c.goaway);
}

#[test]
fn graceful_stop_sends_goaway_and_finishes_streams() {
    let h = serve(|_: &mut Request<'_>, reply: Reply<'_>| -> Outcome {
        let responder = reply.defer();
        std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(300));
            responder.send(Response::text("finished"));
        });
        Outcome::Deferred
    });
    let mut c = H2Client::prior(&h);
    let id = c.request("GET", "/", b"");
    for _ in 0..3 {
        c.step();
    }
    h.stop(Duration::from_secs(5));
    let r = c.wait(id);
    assert_eq!(r.body, b"finished");
    assert!(c.goaway);
    let end = Instant::now() + Duration::from_secs(5);
    while !c.eof && Instant::now() < end {
        c.step();
    }
    assert!(c.eof, "the connection closes after its streams");
    h.join();
}

#[test]
fn h2_and_http1_share_the_port_and_limits_apply() {
    let limits = Limits { max_body: 1000, ..Limits::default() };
    let h = serve_with(limits, blocking(|r| Response::text(format!("{}", r.body().len()))));
    assert_eq!(get(&h, "/").text(), "0");
    let mut c = H2Client::prior(&h);
    let id = c.request("POST", "/", &[1u8; 2000]);
    assert_eq!(c.wait(id).status, 413);
}

/// Sends `bytes` as the first bytes of a connection and reads until the server closes.
fn first_bytes_reply(h: &simoom::ServerHandle, bytes: &[u8]) -> (Vec<u8>, bool) {
    let mut s = connect(h);
    s.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
    s.write_all(bytes).unwrap();
    let mut out = Vec::new();
    let mut tmp = [0u8; 4096];
    let closed = loop {
        match s.read(&mut tmp) {
            Ok(0) => break true,
            Ok(n) => out.extend_from_slice(&tmp[..n]),
            Err(e) if e.kind() == std::io::ErrorKind::ConnectionReset || e.kind() == std::io::ErrorKind::ConnectionAborted => break true,
            Err(_) => break false,
        }
    };
    (out, closed)
}

/// Splits `b` into HTTP/2 frames `(type, payload)`; `None` if it is not a whole number of frames.
fn frames(mut b: &[u8]) -> Option<Vec<(u8, Vec<u8>)>> {
    let mut v = Vec::new();
    while !b.is_empty() {
        if b.len() < 9 {
            return None;
        }
        let len = (usize::from(b[0]) << 16) | (usize::from(b[1]) << 8) | usize::from(b[2]);
        if b.len() < 9 + len {
            return None;
        }
        v.push((b[3], b[9..9 + len].to_vec()));
        b = &b[9 + len..];
    }
    Some(v)
}

/// h2spec http2/3.5/2: an invalid connection preface on the shared HTTP/1.1 + h2c port is a
/// connection error of type PROTOCOL_ERROR (RFC 9113 §3.4): whole HTTP/2 frames ending in
/// GOAWAY(PROTOCOL_ERROR), then a close, never an HTTP/1 response.
#[test]
fn invalid_preface_gets_goaway_protocol_error_and_close() {
    let h = serve(|_req: &mut Request<'_>, reply: Reply<'_>| -> Outcome { reply.body("ok") });
    let cases: [&[u8]; 4] = [
        b"INVALID CONNECTION PREFACE\r\n\r\n",
        b"PRI * HTTP/2.0\r\n\r\nXX\r\n\r\n",
        b"PRI * HTTP/2.0\r\n\r\nSM\r\n\rX",
        b"PRI * HTTP/2.0\n\nSM\n\n",
    ];
    for c in cases {
        let (out, closed) = first_bytes_reply(&h, c);
        let name = String::from_utf8_lossy(c);
        assert!(closed, "{name:?}: connection not closed");
        assert!(!out.starts_with(b"HTTP/1"), "{name:?}: answered with HTTP/1: {:?}", String::from_utf8_lossy(&out));
        let f = frames(&out).unwrap_or_else(|| panic!("{name:?}: not whole HTTP/2 frames: {out:?}"));
        let (ty, payload) = f.last().unwrap_or_else(|| panic!("{name:?}: no frames"));
        assert_eq!(*ty, 7, "{name:?}: last frame is not GOAWAY: {f:?}");
        assert_eq!(&payload[4..8], &[0, 0, 0, 1], "{name:?}: GOAWAY error is not PROTOCOL_ERROR");
    }
    // HTTP/1.1 on the same port is unaffected, including a bad HTTP/1 version (400) and a
    // pipelined second request that happens to look like a broken preface.
    let (out, _) = first_bytes_reply(&h, b"GET / HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n");
    assert!(out.starts_with(b"HTTP/1.1 200"), "{:?}", String::from_utf8_lossy(&out));
    // Damaged versions (found by the HTTP Garden's mutator) are still HTTP/1 errors.
    for bad in [&b"GET / HTTP/1.x\r\nHost: x\r\n\r\n"[..], b"GET / HT\xbcP/1.1\r\nHost: x\r\n\r\n", b"GET / XTTP/1.1\r\nHost: x\r\n\r\n", b"GET /\r\n\r\n"] {
        let (out, _) = first_bytes_reply(&h, bad);
        assert!(out.starts_with(b"HTTP/1.1 400"), "{:?}: {:?}", String::from_utf8_lossy(bad), String::from_utf8_lossy(&out));
    }
    let (out, _) = first_bytes_reply(&h, b"GET / HTTP/1.1\r\nHost: x\r\n\r\nINVALID CONNECTION PREFACE\r\n\r\n");
    assert!(out.starts_with(b"HTTP/1.1 200"), "{:?}", String::from_utf8_lossy(&out));
    assert!(out.windows(12).any(|w| w == b"HTTP/1.1 400"), "{:?}", String::from_utf8_lossy(&out));
    h.stop(Duration::from_secs(1));
}

// ---------------------------------------------------------------- 2026-11-09 bug sweep

/// Echoes the body length and the trailers, from a buffered or a streamed body (bug 14).
fn trailer_echo(mut req: OwnedRequest) -> Response {
    // on the offload pool (blocking): a streamed body is read with blocking reads
    let (len, trailers) = match req.take_body() {
        Some(mut src) => {
            let mut v = Vec::new();
            src.read_to_end(&mut v).unwrap();
            (v.len(), src.trailers())
        }
        None => (req.body().len(), req.trailers().map(|(n, v)| (n.to_vec(), v.to_vec())).collect()),
    };
    let t: Vec<String> = trailers.iter().map(|(n, v)| format!("{}={}", String::from_utf8_lossy(n), String::from_utf8_lossy(v))).collect();
    Response::text(format!("{len} {}", t.join(",")))
}

fn post_with_trailers(c: &mut H2Client, body: &[u8], trailers: &[(&[u8], &[u8])]) -> u32 {
    let id = c.conn.send_request(b"POST", b"http", b"t", b"/t", &[], false).unwrap();
    let mut sent = 0;
    while sent < body.len() {
        sent += c.conn.send_data(id, &body[sent..], false).unwrap();
        c.flush();
        if sent < body.len() {
            c.step();
        }
    }
    c.conn.send_trailers(id, trailers).unwrap();
    c.flush();
    id
}

#[test]
fn request_trailers_reach_the_service() {
    let h = serve_with(Limits { body_buffer: 1024, ..Limits::default() }, blocking(trailer_echo));
    let mut c = H2Client::prior(&h);
    let id = post_with_trailers(&mut c, b"hello", &[(b"x-sum", b"5"), (b"x-other", b"o")]);
    assert_eq!(c.wait(id).body, b"5 x-sum=5,x-other=o");
    // streamed: larger than body_buffer
    let id = post_with_trailers(&mut c, &vec![b'q'; 5000], &[(b"x-sum", b"5000")]);
    assert_eq!(c.wait(id).body, b"5000 x-sum=5000");
    // trailers on an empty body (HEADERS, then trailing HEADERS)
    let id = post_with_trailers(&mut c, b"", &[(b"x-only", b"1")]);
    assert_eq!(c.wait(id).body, b"0 x-only=1");
}

/// Bug 8: `Upgrade: h2c` with an absolute-form target takes the authority (and :path) from
/// the target, as HTTP/1.1 requests do; Host is ignored.
#[test]
fn h2c_upgrade_with_absolute_form_uses_the_target_authority() {
    let h = serve(echo);
    let cfg = Config::default();
    let mut s = connect(&h);
    let settings = simoom_h2::http2_settings_header(&cfg);
    s.write_all(format!("GET http://real.example:8080/up?q=1 HTTP/1.1\r\nHost: wrong\r\nConnection: Upgrade, HTTP2-Settings\r\nUpgrade: h2c\r\nHTTP2-Settings: {settings}\r\n\r\n").as_bytes()).unwrap();
    let mut buf = Vec::new();
    let mut tmp = [0u8; 4096];
    let head_end = loop {
        let n = s.read(&mut tmp).unwrap();
        assert!(n > 0);
        buf.extend_from_slice(&tmp[..n]);
        if let Some(i) = buf.windows(4).position(|w| w == b"\r\n\r\n") {
            break i + 4;
        }
    };
    assert!(buf.starts_with(b"HTTP/1.1 101 "), "{}", String::from_utf8_lossy(&buf));
    let rest = buf[head_end..].to_vec();
    let mut c = H2Client::with(s, Connection::client_upgrade(cfg, false), rest);
    let r = c.wait(1);
    assert_eq!(r.body, b"GET /up H2 ");
    assert_eq!(r.header("x-host"), Some(&b"real.example:8080"[..]));
}

/// Bug 9: a response header block larger than the client's SETTINGS_MAX_HEADER_LIST_SIZE
/// becomes a bare 500 instead of a block the client would refuse.
#[test]
fn response_headers_over_the_client_limit_become_a_500() {
    let h = serve(|req: &mut Request<'_>, reply: Reply<'_>| -> Outcome {
        let big = "v".repeat(if req.path() == b"/big" { 600 } else { 10 });
        reply.header("x-big", &big).body_static(b"body")
    });
    let cfg = Config { max_header_list_size: 400, ..Config::default() };
    let mut c = H2Client::with(connect(&h), Connection::client(cfg), Vec::new());
    let id = c.request("GET", "/small", b"");
    let r = c.wait(id);
    assert_eq!((r.status, &r.body[..]), (200, &b"body"[..]));
    let id = c.request("GET", "/big", b"");
    let r = c.wait(id);
    assert_eq!(r.status, 500);
    assert!(r.body.is_empty() && r.header("x-big").is_none());
    // the connection carries on
    let id = c.request("GET", "/small", b"");
    assert_eq!(c.wait(id).status, 200);
}

/// Bug 15: the HTTP/2 frame budgets come from `Limits::h2_frame_budgets`.
#[test]
fn frame_budgets_come_from_limits() {
    let limits = Limits { h2_frame_budgets: simoom::FrameBudgets { ping: 3, ..simoom::FrameBudgets::default() }, ..Limits::default() };
    let h = serve_with(limits, echo);
    let mut c = H2Client::prior(&h);
    for i in 0..5u8 {
        c.conn.ping([i; 8]);
    }
    c.flush();
    let end = Instant::now() + Duration::from_secs(5);
    while !c.goaway && Instant::now() < end {
        c.step();
    }
    assert!(c.goaway, "no GOAWAY after 5 PINGs with a budget of 3");
}