http-net 0.4.0

Sans-IO HTTP/1.1 server and client: strict RFC 9112 parsing, smuggling defences, bounded buffers, keep-alive, chunked bodies, upgrades, a blocking std adapter, no dependencies.
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Allocation proofs, with a counting global allocator.
//!
//! The core claims that steady-state parsing allocates nothing (the input
//! buffer and the field index are reused across requests) and that memory
//! per connection is bounded by `Limits`, never by input. Both claims are
//! checked here by counting every allocation the test thread makes while a
//! measurement is running. The counters are per thread, so
//! allocations made by the test harness on other threads are not counted.

use std::{
    alloc::{GlobalAlloc, Layout, System},
    cell::Cell,
};

use http_net::{
    ClientConn, ClientEvent, HttpError, Limits, Request, Response, ServerConn, ServerEvent,
};

struct Counting;

/// Per-thread counters: tests run in parallel threads, and each measures
/// only its own allocations.
struct Counters {
    measuring: Cell<bool>,
    allocations: Cell<usize>,
    live: Cell<usize>,
    peak: Cell<usize>,
}

thread_local! {
    static COUNTERS: Counters = const {
        Counters {
            measuring: Cell::new(false),
            allocations: Cell::new(0),
            live: Cell::new(0),
            peak: Cell::new(0),
        }
    };
}

/// Records an allocation (or growth) of `grown` bytes and a release of
/// `shrunk` bytes, if this thread is measuring.
fn record(new_allocation: bool, grown: usize, shrunk: usize) {
    let _ = COUNTERS.try_with(|c| {
        if !c.measuring.get() {
            return;
        }
        if new_allocation {
            c.allocations.set(c.allocations.get() + 1);
        }
        // Frees of memory allocated before the measurement started are not
        // counted against it, so saturate rather than wrap.
        let live = (c.live.get() + grown).saturating_sub(shrunk);
        c.live.set(live);
        c.peak.set(c.peak.get().max(live));
    });
}

// SAFETY: every method forwards to `System` with the caller's arguments
// unchanged; the counters have no effect on the memory returned.
unsafe impl GlobalAlloc for Counting {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        record(true, layout.size(), 0);
        // SAFETY: forwarded unchanged.
        unsafe { System.alloc(layout) }
    }

    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        record(true, layout.size(), 0);
        // SAFETY: forwarded unchanged.
        unsafe { System.alloc_zeroed(layout) }
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        record(false, 0, layout.size());
        // SAFETY: forwarded unchanged.
        unsafe { System.dealloc(ptr, layout) }
    }

    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
        record(true, new_size, layout.size());
        // SAFETY: forwarded unchanged.
        unsafe { System.realloc(ptr, layout, new_size) }
    }
}

#[global_allocator]
static GLOBAL: Counting = Counting;

/// What one measurement saw.
#[derive(Debug)]
struct Measured {
    allocations: usize,
    peak_bytes: usize,
}

/// Runs `f` with counting switched on for this thread.
fn measure<T>(f: impl FnOnce() -> T) -> (T, Measured) {
    COUNTERS.with(|c| {
        c.allocations.set(0);
        c.live.set(0);
        c.peak.set(0);
        c.measuring.set(true);
    });
    let value = f();
    let measured = COUNTERS.with(|c| {
        c.measuring.set(false);
        Measured {
            allocations: c.allocations.get(),
            peak_bytes: c.peak.get(),
        }
    });
    (value, measured)
}

#[test]
fn test_counter_sees_allocations_so_zero_is_not_vacuous() {
    let (v, m) = measure(|| {
        let mut conn = ServerConn::new();
        let _ = conn.feed(b"GET / HTTP/1.1\r\nHost: a\r\n\r\n");
        let n = conn.poll().is_ok();
        (n, std::hint::black_box(Vec::<u8>::with_capacity(64)))
    });
    assert!(v.0);
    // At least the buffer, the field index, and the vector above.
    assert!(m.allocations >= 3, "{m:?}");
    assert!(m.peak_bytes >= 4096 + 64, "{m:?}");
}

/// The most the field index can hold: `max_headers` records of 16 bytes,
/// rounded up to the doubling growth of a `Vec`.
fn field_index_bytes(limits: &Limits) -> usize {
    limits.max_headers().next_power_of_two() * 16
}

/// A browser GET with twelve fields.
const BROWSER_GET: &[u8] = b"GET /assets/app.js?v=3 HTTP/1.1\r\n\
Host: app.example.com\r\n\
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0 Safari/537.36\r\n\
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n\
Accept-Language: en-US,en;q=0.9\r\n\
Accept-Encoding: gzip, deflate, br\r\n\
Referer: https://app.example.com/\r\n\
Cookie: sid=0123456789abcdef; theme=dark\r\n\
Connection: keep-alive\r\n\
Sec-Fetch-Dest: script\r\n\
Sec-Fetch-Mode: no-cors\r\n\
Sec-Fetch-Site: same-origin\r\n\
Cache-Control: no-cache\r\n\r\n";

const POST_LENGTH: &[u8] =
    b"POST /api/notes HTTP/1.1\r\nHost: app.example.com\r\nContent-Type: text/plain\r\nContent-Length: 11\r\n\r\nhello world";

const POST_CHUNKED: &[u8] = b"POST /api/upload HTTP/1.1\r\nHost: app.example.com\r\nTransfer-Encoding: chunked\r\n\r\n5;name=a\r\nhello\r\n6\r\n world\r\n0\r\nX-Checksum: 42\r\n\r\n";

/// Feeds one whole request and answers it. Allocation-free on its own: the
/// assertions only format on failure.
fn serve_one(conn: &mut ServerConn, request: &[u8], response: &Response, out: &mut Vec<u8>) {
    let mut fed = 0;
    let mut body = 0usize;
    loop {
        match conn.poll() {
            Ok(ServerEvent::NeedInput) => {
                assert!(
                    fed < request.len(),
                    "server wants input after a whole request"
                );
                fed += conn.feed(&request[fed..]);
            }
            Ok(ServerEvent::Request(_)) => {}
            Ok(ServerEvent::Body(b)) => body += b.len(),
            Ok(ServerEvent::RequestEnd) => {
                out.clear();
                conn.respond(response, out).expect("respond");
                assert!(fed == request.len() && body <= request.len());
                return;
            }
            Ok(other) => panic!("unexpected server event {other:?}"),
            Err(e) => panic!("server refused a valid request: {e:?}"),
        }
    }
}

#[test]
fn test_server_keep_alive_steady_state_allocates_nothing() {
    let mut conn = ServerConn::new();
    let response = Response::text(200, "ok").header("cache-control", "no-store");
    let mut out = Vec::with_capacity(4096);
    let requests = [BROWSER_GET, POST_LENGTH, POST_CHUNKED];

    // Warm-up: the buffer and the field index grow to their working size.
    for _ in 0..64 {
        for request in requests {
            serve_one(&mut conn, request, &response, &mut out);
        }
    }

    let ((), m) = measure(|| {
        for i in 0..10_000 {
            serve_one(&mut conn, requests[i % requests.len()], &response, &mut out);
        }
    });
    assert_eq!(
        m.allocations, 0,
        "10,000 keep-alive requests allocated {m:?}"
    );
}

#[test]
fn test_server_pipelined_steady_state_allocates_nothing() {
    // Three requests in one read: pipelined input waits in the same buffer.
    let mut batch = Vec::new();
    batch.extend_from_slice(BROWSER_GET);
    batch.extend_from_slice(POST_LENGTH);
    batch.extend_from_slice(POST_CHUNKED);
    let mut conn = ServerConn::new();
    let response = Response::new(204);
    let mut out = Vec::with_capacity(4096);

    let run = |conn: &mut ServerConn, out: &mut Vec<u8>| {
        let mut fed = 0;
        let mut served = 0;
        while served < 3 {
            match conn.poll() {
                Ok(ServerEvent::NeedInput) => {
                    assert!(fed < batch.len(), "server wants input after the batch");
                    fed += conn.feed(&batch[fed..]);
                }
                Ok(ServerEvent::Request(_) | ServerEvent::Body(_)) => {}
                Ok(ServerEvent::RequestEnd) => {
                    out.clear();
                    conn.respond(&response, out).expect("respond");
                    served += 1;
                }
                Ok(other) => panic!("unexpected server event {other:?}"),
                Err(e) => panic!("server refused a valid request: {e:?}"),
            }
        }
    };
    for _ in 0..64 {
        run(&mut conn, &mut out);
    }
    let ((), m) = measure(|| {
        for _ in 0..3_334 {
            run(&mut conn, &mut out);
        }
    });
    assert_eq!(m.allocations, 0, "pipelined requests allocated {m:?}");
}

const RESPONSE_JSON: &[u8] = b"HTTP/1.1 200 OK\r\n\
Content-Type: application/json\r\n\
Cache-Control: private, max-age=0\r\n\
Set-Cookie: sid=0123456789abcdef; HttpOnly\r\n\
Date: Wed, 21 Oct 2026 07:28:00 GMT\r\n\
Content-Length: 27\r\n\r\n\
{\"ok\":true,\"items\":[1,2,3]}";

const RESPONSE_CHUNKED: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nWiki\r\n5\r\npedia\r\n0\r\n\r\n";

const RESPONSE_INTERIM_THEN_204: &[u8] =
    b"HTTP/1.1 103 Early Hints\r\nLink: </app.css>; rel=preload\r\n\r\nHTTP/1.1 204 No Content\r\n\r\n";

fn fetch_one(conn: &mut ClientConn, request: &Request, response: &[u8], out: &mut Vec<u8>) {
    out.clear();
    conn.send_request(request, out).expect("send");
    let mut fed = 0;
    let mut ended = false;
    loop {
        match conn.poll() {
            Ok(ClientEvent::NeedInput) => {
                assert!(
                    fed < response.len(),
                    "client wants input after a whole response"
                );
                fed += conn.feed(&response[fed..]);
            }
            Ok(ClientEvent::Response(_) | ClientEvent::Body(_)) => {}
            Ok(ClientEvent::ResponseEnd) => ended = true,
            Ok(ClientEvent::Idle) => {
                assert!(ended && fed == response.len());
                return;
            }
            Ok(other) => panic!("unexpected client event {other:?}"),
            Err(e) => panic!("client refused a valid response: {e:?}"),
        }
    }
}

#[test]
fn test_client_keep_alive_steady_state_allocates_nothing() {
    let mut conn = ClientConn::new();
    let request = Request::get("/api/items")
        .header("host", "api.example.com")
        .header("accept", "application/json")
        .header("authorization", "Bearer abc.def.ghi");
    let mut out = Vec::with_capacity(4096);
    let responses = [RESPONSE_JSON, RESPONSE_CHUNKED, RESPONSE_INTERIM_THEN_204];

    for _ in 0..64 {
        for response in responses {
            fetch_one(&mut conn, &request, response, &mut out);
        }
    }
    let ((), m) = measure(|| {
        for i in 0..10_000 {
            fetch_one(
                &mut conn,
                &request,
                responses[i % responses.len()],
                &mut out,
            );
        }
    });
    assert_eq!(
        m.allocations, 0,
        "10,000 keep-alive responses allocated {m:?}"
    );
}

/// Feeds `total` bytes of `head` followed by `filler` repeated, in 16 KiB
/// reads, to a connection created inside the measurement. Returns the error
/// the stream ended with (`None` if it was all accepted) and the peak bytes.
fn hostile_stream(
    limits: Limits,
    head: &[u8],
    filler: &[u8],
    total: usize,
) -> (Option<HttpError>, Measured) {
    // The read buffer is prepared before measuring, so only the connection's
    // own memory is counted.
    let mut read = Vec::with_capacity(16 * 1024 + filler.len());
    measure(|| {
        let mut conn = ServerConn::with_limits(limits);
        let mut sent = 0usize;
        let mut head_left = head;
        // Bytes of `read` not yet taken by the connection.
        let mut pending = 0..0;
        loop {
            match conn.poll() {
                Ok(ServerEvent::NeedInput) => {
                    if pending.is_empty() {
                        if sent >= total {
                            return None;
                        }
                        read.clear();
                        let take = head_left.len().min(16 * 1024);
                        read.extend_from_slice(&head_left[..take]);
                        head_left = &head_left[take..];
                        while read.len() + filler.len() <= 16 * 1024 {
                            read.extend_from_slice(filler);
                        }
                        pending = 0..read.len();
                    }
                    let n = conn.feed(&read[pending.clone()]);
                    assert!(n > 0, "buffer full while the parser waits for input");
                    pending.start += n;
                    sent += n;
                }
                Ok(ServerEvent::Request(_) | ServerEvent::Body(_)) => {}
                Ok(other) => panic!("unexpected server event {other:?}"),
                Err(e) => return Some(e),
            }
        }
    })
}

const TEN_MIB: usize = 10 * 1024 * 1024;

fn assert_bounded(limits: &Limits, m: &Measured) {
    // The bound: the buffer's hard capacity (`2 * (start line + header
    // bytes) + 16 KiB`), the field index, and a small
    // constant for the connection's other state (scanners, decoder).
    let bound = limits.buffer_capacity() + field_index_bytes(limits) + 4096;
    assert!(
        m.peak_bytes <= bound,
        "peak {} bytes over the bound {bound} ({m:?})",
        m.peak_bytes
    );
}

#[test]
fn test_endless_header_lines_stay_bounded_and_end_headers_too_large() {
    let limits = Limits::new();
    let line = [b"X-Filler: ".as_slice(), &[b'a'; 188], b"\r\n"].concat();
    let (err, m) = hostile_stream(limits, b"GET / HTTP/1.1\r\nHost: a\r\n", &line, TEN_MIB);
    assert_eq!(err, Some(HttpError::HeadersTooLarge));
    assert_bounded(&limits, &m);
}

#[test]
fn test_endless_start_line_stays_bounded_and_ends_start_line_too_long() {
    let limits = Limits::new();
    let (err, m) = hostile_stream(limits, b"GET /", b"aaaaaaaaaaaaaaaa", TEN_MIB);
    assert_eq!(err, Some(HttpError::StartLineTooLong));
    assert_bounded(&limits, &m);
}

#[test]
fn test_endless_chunk_extensions_stay_bounded_and_end_extensions_too_large() {
    let limits = Limits::new();
    let chunk = [b"1;".as_slice(), &[b'e'; 100], b"=v\r\nx\r\n"].concat();
    let (err, m) = hostile_stream(
        limits,
        b"POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n",
        &chunk,
        TEN_MIB,
    );
    assert_eq!(err, Some(HttpError::ChunkExtensionsTooLarge));
    assert_bounded(&limits, &m);
}

#[test]
fn test_huge_declared_body_is_refused_on_the_header_line() {
    let limits = Limits::new();
    let (err, m) = hostile_stream(
        limits,
        b"POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 10995116277760\r\n\r\n",
        b"zzzzzzzzzzzzzzzz",
        TEN_MIB,
    );
    assert_eq!(err, Some(HttpError::BodyTooLarge));
    assert_bounded(&limits, &m);
}

#[test]
fn test_huge_allowed_body_streams_ten_mib_without_growth() {
    // With an unlimited body budget the 10 MiB stream is accepted as body
    // events; memory still stays at the buffer's capacity.
    let limits = Limits::new().with_max_body(u64::MAX);
    let (err, m) = hostile_stream(
        limits,
        b"POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 10995116277760\r\n\r\n",
        b"zzzzzzzzzzzzzzzz",
        TEN_MIB,
    );
    assert_eq!(err, None);
    assert_bounded(&limits, &m);
}

#[test]
fn test_endless_chunks_stay_bounded_under_a_large_body_budget() {
    let limits = Limits::new().with_max_body(u64::MAX);
    let (err, m) = hostile_stream(
        limits,
        b"POST / HTTP/1.1\r\nHost: a\r\nTransfer-Encoding: chunked\r\n\r\n",
        b"10\r\n0123456789abcdef\r\n",
        TEN_MIB,
    );
    assert_eq!(err, None);
    assert_bounded(&limits, &m);
}

const POST_TRAILERS: &[u8] = b"POST /api/upload HTTP/1.1\r\nHost: app.example.com\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\nContent-Digest: sha-256=:x:\r\nX-Rows: 1\r\n\r\n";

/// NFR-4 with trailers reported (FR-19): the trailer index is reused like
/// the field index, so 10,000 requests with trailers allocate nothing after
/// warm-up.
#[test]
fn test_server_reported_trailers_steady_state_allocates_nothing() {
    let mut conn = ServerConn::new();
    conn.set_report_trailers(true);
    let response = Response::new(204);
    let mut out = Vec::with_capacity(4096);
    let mut run = |conn: &mut ServerConn| {
        let mut fed = 0;
        let mut trailers = 0;
        loop {
            match conn.poll() {
                Ok(ServerEvent::NeedInput) => {
                    assert!(
                        fed < POST_TRAILERS.len(),
                        "server wants input after the request"
                    );
                    fed += conn.feed(&POST_TRAILERS[fed..]);
                }
                Ok(ServerEvent::Request(_) | ServerEvent::Body(_)) => {}
                Ok(ServerEvent::Trailers(t)) => trailers += t.len(),
                Ok(ServerEvent::RequestEnd) => {
                    out.clear();
                    conn.respond(&response, &mut out).expect("respond");
                    assert!(trailers == 2, "trailers not reported");
                    return;
                }
                Ok(other) => panic!("unexpected server event {other:?}"),
                Err(e) => panic!("server refused a valid request: {e:?}"),
            }
        }
    };
    for _ in 0..64 {
        run(&mut conn);
    }
    let ((), m) = measure(|| {
        for _ in 0..10_000 {
            run(&mut conn);
        }
    });
    assert_eq!(m.allocations, 0, "requests with trailers allocated {m:?}");
}

const RESPONSE_HINTS_TRAILERS: &[u8] = b"HTTP/1.1 103 Early Hints\r\nLink: </a.css>; rel=preload\r\n\r\nHTTP/1.1 103 Early Hints\r\nLink: </b.js>; rel=preload\r\n\r\nHTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n2\r\nok\r\n0\r\nServer-Timing: db;dur=5\r\n\r\n";

/// NFR-4 on the client with interim responses and trailers reported
/// (FR-39, FR-19): nothing is allocated per response after warm-up.
#[test]
fn test_client_reported_interim_and_trailers_steady_state_allocates_nothing() {
    let mut conn = ClientConn::new();
    conn.set_report_informational(true);
    conn.set_report_trailers(true);
    let request = Request::get("/page").header("host", "app.example.com");
    let mut out = Vec::with_capacity(4096);
    let mut run = |conn: &mut ClientConn| {
        out.clear();
        conn.send_request(&request, &mut out).expect("send");
        let mut fed = 0;
        let (mut hints, mut trailers, mut ended) = (0, 0, false);
        loop {
            match conn.poll() {
                Ok(ClientEvent::NeedInput) => {
                    assert!(
                        fed < RESPONSE_HINTS_TRAILERS.len(),
                        "client wants more input"
                    );
                    fed += conn.feed(&RESPONSE_HINTS_TRAILERS[fed..]);
                }
                Ok(ClientEvent::Informational(_)) => hints += 1,
                Ok(ClientEvent::Trailers(t)) => trailers += t.len(),
                Ok(ClientEvent::Response(_) | ClientEvent::Body(_)) => {}
                Ok(ClientEvent::ResponseEnd) => ended = true,
                Ok(ClientEvent::Idle) => {
                    assert!(ended && hints == 2 && trailers == 1);
                    return;
                }
                Ok(other) => panic!("unexpected client event {other:?}"),
                Err(e) => panic!("client refused a valid response: {e:?}"),
            }
        }
    };
    for _ in 0..64 {
        run(&mut conn);
    }
    let ((), m) = measure(|| {
        for _ in 0..10_000 {
            run(&mut conn);
        }
    });
    assert_eq!(
        m.allocations, 0,
        "responses with hints and trailers allocated {m:?}"
    );
}