serve-net 0.4.0

The network service engine for Rust: listeners over net-io, ingress from direct clients, reverse proxies (PROXY protocol or trusted forwarded headers), Unix sockets, and FastCGI, a per-connection engine with a slot for any sans-IO protocol engine, admission, drain, and restarts without dropped connections.
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Calling a service with raw request bytes, without sockets or threads.
//!
//! The request goes through the real HTTP engine, the real head decision,
//! body policy, admission, and reply encoding, with the default hooks. A
//! malformed request returns the refusal serve-net would send.
//!
//! ```
//! use serve_net::testing::{respond, TestRequest};
//!
//! let site = serve_net::handler(|req, reply| {
//!     if req.client().addr().is_some_and(|a| a.is_loopback()) {
//!         reply.text(200, "local");
//!     } else {
//!         reply.text(403, "no");
//!     }
//! });
//! assert_eq!(respond(&site, b"GET / HTTP/1.1\r\nHost: a\r\n\r\n").text(), "local");
//! let remote = TestRequest::new(b"GET / HTTP/1.1\r\nHost: a\r\n\r\n").client("198.51.100.4:5000".parse().unwrap());
//! assert_eq!(remote.send(&site).status, 403);
//! // Malformed input never reaches the service.
//! assert_eq!(respond(&site, b"GET / HTTP/1.1\r\n\r\n").status, 400);
//! ```

use std::collections::VecDeque;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};

use crate::Finished;
use crate::client::{ClientInfo, Scheme};
use crate::config::{KeepAlive, Limits, Profile, ProfileId, ProfileTable, ProxySource};
use crate::conn::{Conn, Io, ListenerRuntime, Phase, Shared, Step, Timeout};
use crate::envelope::Envelope;
use crate::hooks::{
    Admit, DefaultHooks, HookSet, Hooks, InFlightCap, Inline, NoCheckpoint, NoObserver,
};
use crate::http::{HttpFamily, HttpHead, HttpReply, HttpRequest, HttpService};
use crate::net::{ConnectionId, NoControl, WorkerId};
use crate::reply::{PostFail, Waker};
use crate::service::{HeadAction, Outcome, WorkClass};

/// Calls `service` with `request` (raw HTTP/1.1 bytes) from 127.0.0.1.
pub fn respond<S: HttpService>(service: &S, request: &[u8]) -> TestReply {
    TestRequest::new(request).send(service)
}

/// A request to send to a service in a test.
#[must_use]
#[derive(Debug)]
pub struct TestRequest<'a> {
    raw: &'a [u8],
    client: SocketAddr,
    scheme: Scheme,
    profile: Option<Profile>,
    proxy: Option<ProxySource>,
}

impl<'a> TestRequest<'a> {
    /// A request from 127.0.0.1:40000 over plain HTTP.
    pub fn new(request: &'a [u8]) -> Self {
        TestRequest {
            raw: request,
            client: SocketAddr::from(([127, 0, 0, 1], 40000)),
            scheme: Scheme::Http,
            profile: None,
            proxy: None,
        }
    }

    /// The socket peer.
    pub fn client(mut self, addr: SocketAddr) -> Self {
        self.client = addr;
        self
    }

    /// The scheme of the connection (TLS on for `Https`).
    pub fn scheme(mut self, scheme: Scheme) -> Self {
        self.scheme = scheme;
        self
    }

    /// The profile the request runs under (limits, keep-alive, body policy).
    pub fn profile(mut self, profile: Profile) -> Self {
        self.profile = Some(profile);
        self
    }

    /// Sends it as if behind a proxy configured with `source`.
    pub fn behind(mut self, source: ProxySource) -> Self {
        self.proxy = Some(source);
        self
    }

    /// Runs the request through serve-net and the service.
    pub fn send<S: HttpService>(self, service: &S) -> TestReply {
        let head = self.raw.starts_with(b"HEAD ");
        let mut profile = self.profile.unwrap_or_else(|| Profile::new(ProfileId(0)));
        profile.id = ProfileId(0);
        let (shared, _) = shared(ByRef(service), profile, self.proxy, self.scheme);
        let mut io = SimIo::new(Instant::now());
        let client = ClientInfo::direct(self.client, self.scheme == Scheme::Https);
        let mut conn = Conn::new(
            Arc::clone(&shared),
            0,
            io.connection,
            client,
            io.now,
            io.worker,
        );
        let mut step = conn.on_open(&mut io, false);
        let mut input: &[u8] = self.raw;
        let mut eof = false;
        // Drive the machine until it closes or stalls with nothing left to give it.
        for _ in 0..10_000 {
            match step {
                Step::Close | Step::Abort => break,
                Step::Write => step = conn.on_writable(&mut io),
                Step::Park { .. } => {
                    if let Some((token, value)) = io.take_wake() {
                        step = conn.on_wake(&mut io, token, value);
                    } else {
                        step = conn.on_timeout(&mut io, Timeout::Park);
                    }
                }
                Step::Read => {
                    if !input.is_empty() {
                        let (n, next) = conn.on_data(&mut io, input);
                        input = &input[n.min(input.len())..];
                        step = next;
                        if n == 0 && next == Step::Read {
                            // Nothing consumed and nothing to do: give it EOF.
                            input = &[];
                        }
                    } else if !eof {
                        eof = true;
                        step = conn.on_peer_closed(&mut io);
                    } else {
                        step = conn.on_timeout(&mut io, Timeout::Exchange);
                    }
                }
            }
        }
        conn.on_close(crate::net::CloseReason::PeerClosed);
        TestReply::parse(&io.written, head)
    }
}

/// What a service answered in a test.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TestReply {
    /// The status, or 0 when nothing was written.
    pub status: u16,
    /// The fields, in order.
    pub headers: Vec<(String, Vec<u8>)>,
    /// The body.
    pub body: Vec<u8>,
    /// Whether the reply closed the connection.
    pub closes: bool,
}

impl TestReply {
    /// The first field with this name (ASCII case-insensitive).
    #[must_use]
    pub fn header(&self, name: &str) -> Option<&[u8]> {
        self.headers
            .iter()
            .find(|(n, _)| n.eq_ignore_ascii_case(name))
            .map(|(_, v)| v.as_slice())
    }

    /// The body as UTF-8; `""` when it is not.
    #[must_use]
    pub fn text(&self) -> &str {
        core::str::from_utf8(&self.body).unwrap_or("")
    }

    fn parse(bytes: &[u8], asked_head: bool) -> TestReply {
        let mut fields = [http_net::FieldSpan::default(); 128];
        let asked = http_net::Asked {
            head: asked_head,
            connect: false,
        };
        let mut at = 0;
        // Skip interim responses (100 Continue).
        loop {
            let Ok(http_net::Parsed::Complete { head, len }) = http_net::parse_response_head(
                &bytes[at..],
                &http_net::Limits::new(),
                asked,
                &mut fields,
            ) else {
                return TestReply {
                    status: 0,
                    headers: Vec::new(),
                    body: Vec::new(),
                    closes: true,
                };
            };
            let view = head.view();
            if view.is_informational() {
                at += len;
                continue;
            }
            let body_start = at + len;
            let n = match view.framing() {
                http_net::BodyFraming::Length(n) => usize::try_from(n).unwrap_or(usize::MAX),
                _ => 0,
            };
            let body = bytes
                .get(body_start..body_start.saturating_add(n).min(bytes.len()))
                .unwrap_or(&[])
                .to_vec();
            return TestReply {
                status: view.status(),
                headers: view
                    .headers()
                    .map(|(n, v)| (n.to_owned(), v.to_vec()))
                    .collect(),
                body,
                closes: !view.keep_alive(),
            };
        }
    }
}

/// One keep-alive connection to a service, kept across requests, for tests of
/// keep-alive, pipelining, and per-connection behaviour.
///
/// ```
/// use serve_net::testing::TestConnection;
///
/// let mut conn = TestConnection::new(serve_net::handler(|req, reply| { reply.text(200, req.path()); }));
/// let replies = conn.send(b"GET /a HTTP/1.1\r\nHost: x\r\n\r\nGET /b HTTP/1.1\r\nHost: x\r\n\r\n");
/// assert_eq!(replies.len(), 2);
/// assert_eq!(replies[1].text(), "/b");
/// assert!(conn.is_open());
/// ```
pub struct TestConnection<S: HttpService> {
    conn: Conn<HttpFamily, S, DefaultHooks>,
    io: SimIo,
    open: bool,
}

impl<S: HttpService> TestConnection<S> {
    /// A connection from 127.0.0.1:40000 to `service` with the default profile.
    pub fn new(service: S) -> Self {
        Self::with_profile(service, Profile::new(ProfileId(0)))
    }

    /// A connection whose requests run under `profile`.
    pub fn with_profile(service: S, profile: Profile) -> Self {
        Self::build(service, profile, None)
    }

    /// A connection from a proxy at 127.0.0.1 configured with `source`, so
    /// forwarded headers decide the client of each request; requests run
    /// under `profile`.
    pub fn behind(service: S, source: ProxySource, profile: Profile) -> Self {
        Self::build(service, profile, Some(source))
    }

    fn build(service: S, mut profile: Profile, proxy: Option<ProxySource>) -> Self {
        profile.id = ProfileId(0);
        let (shared, _) = shared(service, profile, proxy, Scheme::Http);
        let mut io = SimIo::new(Instant::now());
        let client = ClientInfo::direct(SocketAddr::from(([127, 0, 0, 1], 40000)), false);
        let mut conn = Conn::new(shared, 0, io.connection, client, io.now, io.worker);
        let open = conn.on_open(&mut io, false) == Step::Read;
        TestConnection { conn, io, open }
    }

    /// Sends `bytes` and returns every reply they produced.
    pub fn send(&mut self, bytes: &[u8]) -> Vec<TestReply> {
        let mut out = Vec::new();
        self.send_into(bytes, &mut out);
        let mut replies = Vec::new();
        let mut rest = out.as_slice();
        while !rest.is_empty() {
            let reply = TestReply::parse(rest, false);
            let used = reply_len(rest).max(1);
            replies.push(reply);
            rest = &rest[used.min(rest.len())..];
        }
        replies
    }

    /// Sends `bytes` and appends the raw bytes written to `out`. Allocates
    /// nothing once the connection and `out` have warmed up.
    pub fn send_into(&mut self, bytes: &[u8], out: &mut Vec<u8>) {
        let mut input = bytes;
        let mut step = Step::Read;
        while self.open {
            match step {
                Step::Close | Step::Abort => {
                    self.open = false;
                    self.conn.on_close(crate::net::CloseReason::Shutdown);
                }
                Step::Write => step = self.conn.on_writable(&mut self.io),
                Step::Park { .. } => match self.io.take_wake() {
                    Some((token, value)) => step = self.conn.on_wake(&mut self.io, token, value),
                    None => break,
                },
                Step::Read if input.is_empty() => break,
                Step::Read => {
                    let (n, next) = self.conn.on_data(&mut self.io, input);
                    input = &input[n.min(input.len())..];
                    step = next;
                    if n == 0 && next == Step::Read {
                        break;
                    }
                }
            }
        }
        out.extend_from_slice(&self.io.written);
        self.io.written.clear();
    }

    /// Whether the connection is still open.
    #[must_use]
    pub fn is_open(&self) -> bool {
        self.open
    }
}

impl<S: HttpService> Drop for TestConnection<S> {
    fn drop(&mut self) {
        if self.open {
            self.open = false;
            self.conn.on_close(crate::net::CloseReason::PeerClosed);
        }
    }
}

impl<S: HttpService> core::fmt::Debug for TestConnection<S> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("TestConnection")
            .field("open", &self.open)
            .finish()
    }
}

/// The length of the first response in `bytes` (head plus content-length body).
fn reply_len(bytes: &[u8]) -> usize {
    let mut fields = [http_net::FieldSpan::default(); 128];
    let asked = http_net::Asked {
        head: false,
        connect: false,
    };
    let mut at = 0;
    loop {
        let Ok(http_net::Parsed::Complete { head, len }) = http_net::parse_response_head(
            &bytes[at..],
            &http_net::Limits::new(),
            asked,
            &mut fields,
        ) else {
            return bytes.len();
        };
        let view = head.view();
        if view.is_informational() {
            at += len;
            continue;
        }
        let n = match view.framing() {
            http_net::BodyFraming::Length(n) => usize::try_from(n).unwrap_or(usize::MAX),
            _ => 0,
        };
        return at.saturating_add(len).saturating_add(n);
    }
}

/// Lets the machine borrow a service for the length of a test.
struct ByRef<'s, S>(&'s S);

impl<S: HttpService> HttpService for ByRef<'_, S> {
    const WORK: WorkClass = S::WORK;
    fn head(&self, head: &HttpHead<'_>, env: &mut Envelope) -> HeadAction {
        self.0.head(head, env)
    }
    fn call(&self, request: &HttpRequest<'_>, env: &Envelope, reply: &mut HttpReply) -> Outcome {
        self.0.call(request, env, reply)
    }
    fn done(&self, env: &Envelope, finished: &Finished) {
        self.0.done(env, finished);
    }
}

/// Builds the shared state of a one-listener server with the default hooks.
/// The shared state of a test server and a copy of its profile table.
pub(crate) type TestShared<S, H> = (Arc<Shared<HttpFamily, S, H>>, ProfileTable);

pub(crate) fn shared<S: HttpService>(
    service: S,
    profile: Profile,
    proxy: Option<ProxySource>,
    scheme: Scheme,
) -> TestShared<S, DefaultHooks> {
    shared_with(
        service,
        profile,
        proxy,
        scheme,
        HookSet {
            admit: InFlightCap::new(),
            dispatch: Inline,
            checkpoint: NoCheckpoint,
            observer: NoObserver,
            control: Arc::new(NoControl),
            gate: crate::net::AllowAll,
            events: crate::net::NoopSink,
            tls: net_io::NoTls,
        },
    )
}

/// As [`shared`], with given hooks.
#[allow(clippy::type_complexity)] // the hook set's eight slots, as in `ServerBuilder`
pub(crate) fn shared_with<S: HttpService, A, D, K, O, G, E, C, T>(
    service: S,
    profile: Profile,
    proxy: Option<ProxySource>,
    scheme: Scheme,
    hooks: HookSet<A, D, K, O, G, E, C, T>,
) -> TestShared<S, HookSet<A, D, K, O, G, E, C, T>>
where
    HookSet<A, D, K, O, G, E, C, T>:
        Hooks<Admit = A, Dispatch = D, Checkpoint = K, Observer = O, Control = C>,
    A: Admit,
    C: crate::net::ControlLine,
{
    let profiles = ProfileTable::compile(
        core::slice::from_ref(&profile),
        Limits::new(),
        KeepAlive::new(),
    );
    let copy = ProfileTable::compile(
        core::slice::from_ref(&profile),
        Limits::new(),
        KeepAlive::new(),
    );
    let mut admit = hooks.admit;
    admit.prepare(1, &profiles);
    let listener = ListenerRuntime {
        name: "test".into(),
        profile: ProfileId(0),
        proxy,
        declared_scheme: scheme,
        unix: None,
        cap: crate::conn::session::Cap::new(None),
    };
    let shared = Shared {
        family: HttpFamily,
        service,
        admit,
        dispatch: hooks.dispatch,
        checkpoint: hooks.checkpoint,
        observer: hooks.observer,
        control: Arc::new(crate::running::Merged::new(hooks.control)),
        tables: Arc::new(crate::snapshot::Snapshot::new(crate::conn::Tables {
            profile_caps: vec![crate::conn::session::Cap::new(None)],
            profiles,
            listeners: vec![Some(listener)],
        })),
        counters: Arc::new(crate::stats::Counters::new(1)),
    };
    (Arc::new(shared), copy)
}

/// A transport simulated in memory, with a clock that moves only when told.
pub(crate) struct SimIo {
    pub(crate) now: Instant,
    pub(crate) connection: ConnectionId,
    pub(crate) worker: WorkerId,
    pub(crate) written: Vec<u8>,
    /// Bytes `push` accepts before reporting the output full; `None` = unlimited.
    pub(crate) budget: Option<usize>,
    pub(crate) deadline: Option<Instant>,
    pub(crate) phase: Phase,
    pub(crate) draining: bool,
    pub(crate) pressure: f32,
    /// The per-connection idle override the machine last set.
    pub(crate) idle_timeout: Option<Duration>,
    pub(crate) wakes: Arc<SimWaker>,
}

/// Records posted wakes.
#[derive(Default)]
pub(crate) struct SimWaker {
    pub(crate) posted: Mutex<VecDeque<(ConnectionId, u32, u64)>>,
    pub(crate) full: std::sync::atomic::AtomicBool,
}

impl Waker for SimWaker {
    fn post(&self, connection: ConnectionId, token: u32, value: u64) -> Result<(), PostFail> {
        if self.full.load(std::sync::atomic::Ordering::Acquire) {
            return Err(PostFail::Full);
        }
        self.posted
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .push_back((connection, token, value));
        Ok(())
    }
}

impl SimIo {
    pub(crate) fn new(now: Instant) -> Self {
        SimIo {
            now,
            connection: SIM_CONNECTION,
            worker: WorkerId::first(),
            written: Vec::with_capacity(16 * 1024),
            budget: None,
            deadline: None,
            phase: Phase::Idle,
            draining: false,
            pressure: 0.0,
            idle_timeout: None,
            wakes: Arc::new(SimWaker::default()),
        }
    }

    pub(crate) fn take_wake(&mut self) -> Option<(u32, u64)> {
        let mut q = self
            .wakes
            .posted
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        q.pop_front().map(|(_, t, v)| (t, v))
    }

    #[cfg(test)]
    pub(crate) fn advance(&mut self, by: Duration) {
        self.now += by;
    }
}

/// The id of simulated connections (checked at compile time).
const SIM_CONNECTION: ConnectionId = match ConnectionId::new(1) {
    Some(id) => id,
    None => panic!("1 is a valid connection id"),
};

impl Io for SimIo {
    fn now(&self) -> Instant {
        self.now
    }
    fn set_exchange_deadline(&mut self, at: Option<Instant>) {
        self.deadline = at;
    }
    fn set_phase(&mut self, phase: Phase) {
        self.phase = phase;
    }
    fn set_idle_timeout(&mut self, timeout: Option<Duration>) {
        self.idle_timeout = timeout;
    }
    fn begin_reply(&mut self) {}
    fn push(&mut self, bytes: &[u8]) -> usize {
        let n = match &mut self.budget {
            None => bytes.len(),
            Some(left) => {
                let n = bytes.len().min(*left);
                *left -= n;
                n
            }
        };
        self.written.extend_from_slice(&bytes[..n]);
        n
    }
    fn draining(&self) -> bool {
        self.draining
    }
    fn pressure(&self) -> f32 {
        self.pressure
    }
    fn worker(&self) -> WorkerId {
        self.worker
    }
    fn waker(&self) -> Arc<dyn Waker> {
        Arc::clone(&self.wakes) as Arc<dyn Waker>
    }
}