sofie-http 0.4.0

The HTTP server, areas, routes and the handler model.
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
use std::convert::Infallible;
use std::future::poll_fn;
use std::io;
use std::net::SocketAddr;
use std::num::NonZeroUsize;
use std::pin::{Pin, pin};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::task::Poll;
use std::time::Duration;

use http::header::CONNECTION;
use http::{HeaderValue, Request, StatusCode, Version};
use hyper::body::Incoming;
use hyper::service::{Service, service_fn};
use hyper_util::rt::{TokioExecutor, TokioIo, TokioTimer};
use hyper_util::server::conn::auto::Builder;
use hyper_util::server::graceful::{GracefulShutdown, Watcher};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{OwnedSemaphorePermit, Semaphore};

use sofie_auth::SigningKeys;
use sofie_core::Result;

use crate::App;
use crate::answer::{self, Response};
#[cfg(feature = "engine")]
use crate::assets::Assets;
use crate::cookie::CookieNames;
use crate::deadline::REQUEST_TIMEOUT;
use crate::health::Endpoints;
use crate::peers::{PeerSlot, Peers, Refusals};
use crate::pipeline::Pipeline;
use crate::session::Sessions;
use crate::silence::Silenced;
use crate::target::route_table;

const HEADER_READ_TIMEOUT: Duration = Duration::from_secs(10);
/// hyper's ~408 KB default lets each connection hold that much; nginx allows 32 KB.
const MAX_HEADER_BUFFER: usize = 32 * 1024;
/// Each stream may buffer a body up to its route's limit, so a client's share stays small.
const MAX_CONCURRENT_STREAMS: u32 = 32;
/// Past it the connection is closed (Rapid Reset, CVE-2023-44487).
const MAX_PENDING_RESETS: usize = 20;
/// RUSTSEC-2024-0003.
const MAX_ERROR_RESETS: usize = 256;
/// Long enough for a request accepted just before shutdown to read its headers, run and commit.
const SHUTDOWN_GRACE: Duration = HEADER_READ_TIMEOUT
    .saturating_add(REQUEST_TIMEOUT)
    .saturating_add(Duration::from_secs(5));
/// Past the cap, new connections wait in the kernel's backlog instead of exhausting descriptors.
const DEFAULT_MAX_CONNECTIONS: NonZeroUsize = NonZeroUsize::new(4096).unwrap();
/// Accept fails mostly when file descriptors run out; retrying at once spins.
const ACCEPT_BACKOFF: Duration = Duration::from_millis(100);
const REFUSED_LIFETIME: Duration = HEADER_READ_TIMEOUT;
#[cfg(feature = "tls")]
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);

pub(crate) async fn serve(
    mut app: App,
    listener: TcpListener,
    shutdown: impl Future<Output = ()>,
) -> Result<()> {
    let cookies = CookieNames::of_serving(&app.serving)?;
    #[cfg(any(feature = "idempotency", feature = "shield"))]
    let mut background = crate::background::Background::default();
    #[cfg(feature = "gates")]
    crate::gate::prepare(&mut app)?;
    #[cfg(feature = "cors")]
    crate::cors::prepare(&mut app)?;
    #[cfg(feature = "engine")]
    crate::content_security::prepare(&mut app)?;
    #[cfg(feature = "etag")]
    crate::etag::identify_build()?;
    #[cfg(feature = "islands")]
    crate::islands::install(&mut app.screens, &app.islands)?;
    #[cfg(feature = "shield")]
    let shield = crate::shield::Guard::start(&mut app, &mut background)?;
    #[cfg(feature = "live")]
    let live = crate::live::Live::new(&app.routes)?;
    let router = route_table(
        &app.routes,
        app.areas,
        &app.names,
        #[cfg(feature = "engine")]
        &app.screens,
    )?;
    #[cfg(feature = "tls")]
    let tls = app.serving.tls.take();
    let max_connections = app
        .serving
        .max_connections
        .unwrap_or(DEFAULT_MAX_CONNECTIONS);
    let keys = match app.serving.keys.take() {
        Some(keys) => keys,
        None => SigningKeys::from_environment()?,
    };
    #[cfg(feature = "idempotency")]
    background.spawn(crate::keyed::sweep_expired_keys(app.db.clone()));
    #[cfg(all(feature = "toolbar", debug_assertions))]
    let toolbar = crate::toolbar::Toolbar::new(&app.routes, cookies.clone());
    let pipeline = Pipeline {
        router,
        db: app.db,
        sessions: Sessions {
            keys: Arc::new(keys),
            #[cfg(any(feature = "tokens", feature = "session-list"))]
            members: app.members,
            cookies: cookies.clone(),
            #[cfg(feature = "session-list")]
            records: crate::session_cache::SessionCache::default(),
        },
        #[cfg(feature = "trusted-proxies")]
        proxies: std::mem::take(&mut app.serving.proxies),
        #[cfg(feature = "engine")]
        assets: served_assets(&app.screens),
        #[cfg(feature = "engine")]
        screens: app.screens,
        #[cfg(feature = "gates")]
        gates: app.gates,
        endpoints: Endpoints {
            #[cfg(feature = "browser-telemetry")]
            browser: crate::browser::Browser::new(
                &app.names,
                &app.routes,
                app.serving.browser_sample,
                cookies,
            ),
            serving: app.serving,
            #[cfg(all(feature = "toolbar", debug_assertions))]
            toolbar,
        },
        #[cfg(feature = "shield")]
        shield,
        #[cfg(feature = "tokens")]
        tokens: crate::bearer::Tokens::default(),
        #[cfg(feature = "steps")]
        steps: app.steps,
        #[cfg(feature = "live")]
        live,
        #[cfg(feature = "screen-cache")]
        screen_cache: crate::screen_cache::ScreenCache::default(),
    };
    let connections = Connections {
        builder: connection_builder(),
        graceful: GracefulShutdown::new(),
        pipeline: Arc::new(pipeline),
        permits: Arc::new(Semaphore::new(max_connections.get())),
        peers: Peers::default(),
        refusals: Refusals::default(),
        proxies_hinted: AtomicBool::new(false),
        #[cfg(feature = "tls")]
        tls,
    };
    run(connections, listener, shutdown).await;
    Ok(())
}

struct Connections {
    builder: Builder<TokioExecutor>,
    graceful: GracefulShutdown,
    pipeline: Arc<Pipeline>,
    permits: Arc<Semaphore>,
    peers: Peers,
    refusals: Refusals,
    proxies_hinted: AtomicBool,
    #[cfg(feature = "tls")]
    tls: Option<crate::Tls>,
}

async fn run(connections: Connections, listener: TcpListener, shutdown: impl Future<Output = ()>) {
    let mut shutdown = pin!(shutdown);
    while let Some((permit, accepted)) =
        next_connection(&listener, &connections.permits, shutdown.as_mut()).await
    {
        match accepted {
            Ok((stream, peer)) => connections.serve(stream, peer, permit),
            Err(error) => {
                tracing::warn!(
                    error.kind = "accept",
                    error = %error,
                    "accepting a connection failed"
                );
                tokio::time::sleep(ACCEPT_BACKOFF).await;
            }
        }
    }
    drop(listener);
    #[cfg(feature = "live")]
    connections.pipeline.live.close();
    if tokio::time::timeout(SHUTDOWN_GRACE, connections.graceful.shutdown())
        .await
        .is_err()
    {
        tracing::warn!(
            error.kind = "shutdown",
            "open connections did not finish within the grace period"
        );
    }
}

/// `shutdown` is checked first, so a stopping server accepts nothing more.
async fn next_connection(
    listener: &TcpListener,
    permits: &Arc<Semaphore>,
    mut shutdown: Pin<&mut impl Future<Output = ()>>,
) -> Option<(OwnedSemaphorePermit, io::Result<(TcpStream, SocketAddr)>)> {
    let mut acquiring = pin!(Arc::clone(permits).acquire_owned());
    let mut permit = None;
    poll_fn(|context| {
        if shutdown.as_mut().poll(context).is_ready() {
            return Poll::Ready(None);
        }
        if permit.is_none() {
            let acquired = std::task::ready!(acquiring.as_mut().poll(context));
            permit = Some(acquired.expect("the connection semaphore is never closed"));
        }
        let accepted = std::task::ready!(listener.poll_accept(context));
        Poll::Ready(permit.take().map(|permit| (permit, accepted)))
    })
    .await
}

fn connection_builder() -> Builder<TokioExecutor> {
    let mut builder = Builder::new(TokioExecutor::new());
    builder
        .http1()
        .timer(TokioTimer::new())
        .header_read_timeout(HEADER_READ_TIMEOUT)
        .max_buf_size(MAX_HEADER_BUFFER);
    builder
        .http2()
        .timer(TokioTimer::new())
        .max_concurrent_streams(MAX_CONCURRENT_STREAMS)
        .max_pending_accept_reset_streams(MAX_PENDING_RESETS)
        .max_local_error_reset_streams(MAX_ERROR_RESETS);
    builder
}

impl Connections {
    /// A trusted proxy or loopback speaks for many clients, so it is held to no share.
    fn peer_slot(&self, peer: SocketAddr) -> Result<Option<PeerSlot>, ()> {
        let address = peer.ip().to_canonical();
        #[cfg(feature = "trusted-proxies")]
        if self.pipeline.proxies.is_trusted(address) {
            return Ok(None);
        }
        if address.is_loopback() {
            return Ok(None);
        }
        self.peers.admit(address).map(Some).ok_or(())
    }

    fn serve(&self, stream: TcpStream, peer: SocketAddr, permit: OwnedSemaphorePermit) {
        if let Err(error) = stream.set_nodelay(true) {
            tracing::warn!(
                error.kind = "nodelay",
                error = %error,
                "disabling Nagle's algorithm failed"
            );
        }
        let Ok(slot) = self.peer_slot(peer) else {
            self.refused();
            let Some(answering) = self.refusals.admit(permit) else {
                return;
            };
            return self.spawn(
                (stream, peer),
                service_fn(refuse),
                answering,
                Some(REFUSED_LIFETIME),
            );
        };
        let pipeline = Arc::clone(&self.pipeline);
        let service = service_fn(move |request| {
            let pipeline = Arc::clone(&pipeline);
            #[cfg(feature = "storage")]
            return async move {
                Ok::<_, Infallible>(crate::storage::answer(&pipeline, request, peer).await)
            };
            #[cfg(all(feature = "live", not(feature = "storage")))]
            return async move {
                Ok::<_, Infallible>(crate::live::answer(&pipeline, request, peer).await)
            };
            #[cfg(not(any(feature = "live", feature = "storage")))]
            async move {
                Ok::<_, Infallible>(pipeline.handle(request, peer).await)
            }
        });
        self.spawn((stream, peer), service, (permit, slot), None);
    }

    fn refused(&self) {
        if let Some(refused) = self.refusals.counted(std::time::Instant::now()) {
            tracing::warn!(
                error.kind = "connections",
                refused,
                "refused connections from addresses that hold their share already"
            );
        }
        #[cfg(feature = "trusted-proxies")]
        let proxies_named = !self.pipeline.proxies.is_empty();
        #[cfg(not(feature = "trusted-proxies"))]
        let proxies_named = false;
        if proxies_named || self.proxies_hinted.swap(true, Ordering::Relaxed) {
            return;
        }
        tracing::warn!(
            error.kind = "connections",
            "one address reached the connection cap and no proxy is trusted; behind a load \
             balancer, name it in SOFIE_TRUSTED_PROXIES (feature `trusted-proxies`)"
        );
    }

    fn spawn<S, B>(
        &self,
        (stream, peer): (TcpStream, SocketAddr),
        service: S,
        held: impl Send + 'static,
        lifetime: Option<Duration>,
    ) where
        S: Service<Request<Incoming>, Response = http::Response<B>, Error = Infallible>
            + Send
            + 'static,
        S::Future: Send + 'static,
        B: hyper::body::Body + Send + 'static,
        B::Data: Send,
        B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
    {
        #[cfg(feature = "shield")]
        let shield = self.pipeline.shield.clone();
        let failed = move |error: &(dyn std::error::Error + 'static)| {
            if ends_routinely(error) {
                tracing::debug!(error = %error, "a connection ended");
                return;
            }
            tracing::warn!(error.kind = "connection", error = %error, "a connection failed");
            #[cfg(feature = "shield")]
            shield.connection_failed(peer.ip());
            #[cfg(not(feature = "shield"))]
            let _ = peer;
        };
        let stream = Silenced::new(stream);
        let served = Served {
            builder: self.builder.clone(),
            watcher: self.graceful.watcher(),
            service,
            lifetime,
        };
        #[cfg(feature = "tls")]
        if let Some(tls) = &self.tls {
            let accepting = tls.acceptor.accept(stream);
            tokio::spawn(async move {
                match tokio::time::timeout(HANDSHAKE_TIMEOUT, accepting).await {
                    Ok(Ok(stream)) => served.run(stream, failed).await,
                    Ok(Err(error)) => failed(&error),
                    Err(elapsed) => failed(&elapsed),
                }
                drop(held);
            });
            return;
        }
        tokio::spawn(async move {
            served.run(stream, failed).await;
            drop(held);
        });
    }
}

struct Served<S> {
    builder: Builder<TokioExecutor>,
    watcher: Watcher,
    service: S,
    /// Bounds a refused connection, which HTTP/2 would otherwise keep open for more streams.
    lifetime: Option<Duration>,
}

impl<S> Served<S> {
    async fn run<I, B>(self, io: I, failed: impl FnOnce(&(dyn std::error::Error + 'static)))
    where
        I: AsyncRead + AsyncWrite + Unpin + Send + 'static,
        S: Service<Request<Incoming>, Response = http::Response<B>, Error = Infallible>
            + Send
            + 'static,
        S::Future: Send + 'static,
        B: hyper::body::Body + Send + 'static,
        B::Data: Send,
        B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
    {
        let connection = self
            .builder
            .serve_connection(TokioIo::new(io), self.service)
            .into_owned();
        let connection = self.watcher.watch(connection);
        let outcome = match self.lifetime {
            Some(lifetime) => match tokio::time::timeout(lifetime, connection).await {
                Ok(outcome) => outcome,
                Err(_) => return,
            },
            None => connection.await,
        };
        if let Err(error) = outcome {
            failed(&*error);
        }
    }
}

/// Header and TLS handshake timeouts, resets and drops are a network's routine, not protocol
/// violations.
fn ends_routinely(error: &(dyn std::error::Error + 'static)) -> bool {
    std::iter::successors(Some(error), |error| error.source()).any(|cause| {
        if cause.is::<tokio::time::error::Elapsed>() {
            return true;
        }
        if let Some(hyper) = cause.downcast_ref::<hyper::Error>() {
            return hyper.is_timeout()
                || hyper.is_incomplete_message()
                || hyper.is_closed()
                || hyper.is_canceled()
                || hyper.is_body_write_aborted()
                || hyper.is_shutdown();
        }
        cause.downcast_ref::<std::io::Error>().is_some_and(|io| {
            matches!(
                io.kind(),
                std::io::ErrorKind::ConnectionReset
                    | std::io::ErrorKind::ConnectionAborted
                    | std::io::ErrorKind::BrokenPipe
                    | std::io::ErrorKind::UnexpectedEof
                    | std::io::ErrorKind::TimedOut
            )
        })
    })
}

/// HTTP/2 forbids `Connection`; there the lifetime ends the connection instead.
async fn refuse(request: Request<Incoming>) -> Result<Response, Infallible> {
    let mut response = answer::response(
        StatusCode::SERVICE_UNAVAILABLE,
        Some(answer::TEXT),
        "Too many connections from this address; try again shortly.",
    );
    if request.version() < Version::HTTP_2 {
        response
            .headers_mut()
            .insert(CONNECTION, HeaderValue::from_static("close"));
    }
    Ok(response)
}

/// The engine's own files, and the island and client bundles the app's build compiled.
#[cfg(feature = "engine")]
fn served_assets(screens: &crate::Screens) -> Assets {
    let assets = Assets::embedded();
    #[cfg(feature = "islands")]
    let assets = assets.with_islands(screens);
    #[cfg(feature = "clients")]
    let assets = assets.with_clients(screens);
    let _ = screens;
    assets
}

/// In memory, as a paused clock outruns loopback TCP, whose bytes arrive asynchronously on macOS.
#[cfg(test)]
mod tests {
    use std::time::Duration;

    use tokio::io::{AsyncReadExt, AsyncWriteExt, DuplexStream, duplex};
    use tokio::time::Instant;

    use super::*;

    fn connected() -> DuplexStream {
        served(service_fn(|_| async {
            Ok::<_, Infallible>(http::Response::new(
                http_body_util::Empty::<bytes::Bytes>::new(),
            ))
        }))
    }

    fn served<S, B>(service: S) -> DuplexStream
    where
        S: hyper::service::Service<
                http::Request<hyper::body::Incoming>,
                Response = http::Response<B>,
                Error = Infallible,
            > + Send
            + 'static,
        S::Future: Send + 'static,
        B: hyper::body::Body<Data = bytes::Bytes, Error = Infallible> + Send + 'static,
    {
        let (client, server) = duplex(64 * 1024);
        let connection = connection_builder()
            .serve_connection(TokioIo::new(Silenced::new(server)), service)
            .into_owned();
        tokio::spawn(connection);
        client
    }

    async fn closed(client: &mut DuplexStream) -> (Duration, String) {
        let started = Instant::now();
        let mut answer = Vec::new();
        client.read_to_end(&mut answer).await.unwrap();
        (started.elapsed(), String::from_utf8(answer).unwrap())
    }

    fn within_a_second_of(elapsed: Duration, seconds: u64) -> bool {
        (Duration::from_secs(seconds)..Duration::from_secs(seconds + 1)).contains(&elapsed)
    }

    #[tokio::test(start_paused = true)]
    async fn a_client_that_sends_nothing_is_closed_after_ten_seconds() {
        let mut client = connected();

        let (elapsed, answer) = closed(&mut client).await;

        assert!(within_a_second_of(elapsed, 10), "{elapsed:?}");
        assert_eq!(answer, "");
    }

    #[tokio::test(start_paused = true)]
    async fn half_an_http2_preface_is_answered_400_after_a_minute_of_silence() {
        let mut client = connected();
        client.write_all(b"PRI * HTTP/2.0\r\n").await.unwrap();

        let (elapsed, answer) = closed(&mut client).await;

        assert!(within_a_second_of(elapsed, 60), "{elapsed:?}");
        assert!(answer.starts_with("HTTP/1.1 400"), "{answer}");
    }

    #[tokio::test(start_paused = true)]
    async fn an_idle_kept_alive_connection_is_closed_after_ten_seconds() {
        let mut client = connected();
        client
            .write_all(b"GET / HTTP/1.1\r\nHost: shop.test\r\n\r\n")
            .await
            .unwrap();

        let (elapsed, answer) = closed(&mut client).await;

        assert!(within_a_second_of(elapsed, 10), "{elapsed:?}");
        assert!(answer.starts_with("HTTP/1.1 200"), "{answer}");
    }

    #[tokio::test(start_paused = true)]
    async fn a_connection_over_its_share_is_answered_503_and_closed() {
        let mut client = served(service_fn(refuse));
        client
            .write_all(b"GET / HTTP/1.1\r\nHost: shop.test\r\n\r\n")
            .await
            .unwrap();

        let (elapsed, answer) = closed(&mut client).await;

        assert!(elapsed < Duration::from_secs(1), "{elapsed:?}");
        assert!(answer.starts_with("HTTP/1.1 503"), "{answer}");
        assert!(answer.contains("connection: close"), "{answer}");
    }

    #[tokio::test(start_paused = true)]
    async fn a_reset_or_timed_out_connection_ends_routinely() {
        for kind in [
            std::io::ErrorKind::ConnectionReset,
            std::io::ErrorKind::TimedOut,
        ] {
            assert!(ends_routinely(&std::io::Error::from(kind)), "{kind:?}");
        }
        let stalled_handshake = tokio::time::timeout(Duration::ZERO, std::future::pending::<()>())
            .await
            .unwrap_err();
        assert!(ends_routinely(&stalled_handshake));
        assert!(!ends_routinely(&std::io::Error::from(
            std::io::ErrorKind::InvalidData
        )));
    }
}