Skip to main content

actus_server/
server.rs

1//! [`Server`] — the hyper-based HTTP server that owns the request lifecycle:
2//! routing, body limiting, the middleware chain, CORS, compression, and the
3//! HTTP-correctness stamping (`Allow`, `Vary`), with graceful shutdown.
4
5#[cfg(feature = "compression")]
6use crate::compression::CompressionLayer;
7use crate::cors::CorsLayer;
8use crate::error::ServerError;
9use crate::middleware::{Middleware, MiddlewareChain, Outcome};
10use crate::request::Request;
11use crate::router::Router;
12#[cfg(feature = "websocket")]
13use crate::websocket;
14#[cfg(feature = "websocket")]
15use actus_reply::ProblemDetails;
16use actus_reply::{Finalizer, ReplyData, WebError};
17use bytes::Bytes;
18#[cfg(feature = "websocket")]
19use http::{HeaderValue, StatusCode, header};
20use http_body_util::combinators::BoxBody;
21use hyper::body::Incoming;
22use hyper::service::service_fn;
23use hyper::{Request as HyperRequest, Response as HyperResponse};
24use std::future::Future;
25use std::net::SocketAddr;
26use std::sync::Arc;
27use std::time::Duration;
28use tokio::net::TcpListener;
29use tokio::sync::Semaphore;
30use tokio::task::JoinSet;
31use tracing::{Instrument, Level, error, info, span, warn};
32
33type ResponseBody = BoxBody<Bytes, WebError>;
34
35/// One kibibyte (1024 bytes). For readable byte-size limits, e.g.
36/// `#[controller(max_body_bytes = 4 * KIB)]`.
37pub const KIB: usize = 1024;
38/// One mebibyte (1024 × 1024 bytes), e.g. `Server::with_max_body_bytes(2 * MIB)`.
39pub const MIB: usize = 1024 * KIB;
40/// One gibibyte (1024 × 1024 × 1024 bytes).
41pub const GIB: usize = 1024 * MIB;
42
43/// Default cap on the request body Actus will buffer: **2 MiB** — a safe
44/// ceiling for the common case (JSON APIs, forms). Endpoints that accept larger
45/// bodies (uploads) opt in via [`Server::with_max_body_bytes`] or a
46/// per-controller `#[controller(max_body_bytes = …)]`. Matches axum's default.
47pub const DEFAULT_MAX_BODY_BYTES: usize = 2 * MIB;
48
49/// Default grace period for in-flight connections to finish after a
50/// shutdown signal: 30 seconds. Override with [`Server::with_drain_deadline`].
51pub const DEFAULT_DRAIN_DEADLINE: Duration = Duration::from_secs(30);
52
53/// The main Actus server.
54pub struct Server {
55    router: Arc<Router>,
56    middleware_chain: Arc<MiddlewareChain>,
57    finalizer: Arc<Finalizer>,
58    max_body_bytes: usize,
59    cors: Option<Arc<CorsLayer>>,
60    #[cfg(feature = "compression")]
61    compression: Option<Arc<CompressionLayer>>,
62    /// `Some(d)` caps each request's total time (parse → middleware →
63    /// handler → after-chain → finalize) at `d`; an over-budget request is
64    /// aborted and replied with `504 Gateway Timeout`. `None` disables the
65    /// per-request timer (the default).
66    request_timeout: Option<Duration>,
67    /// Grace period for in-flight connections to drain after shutdown.
68    drain_deadline: Duration,
69    /// Cap on concurrent connection tasks. `Some(n)` installs an
70    /// `Arc<Semaphore>` of `n` permits in the accept loop; while at
71    /// capacity, the loop pauses on permit acquisition and new SYNs queue
72    /// in the kernel's accept backlog (`SOMAXCONN`), at which point the
73    /// kernel drops them. `None` is unbounded.
74    max_connections: Option<usize>,
75    /// Cap on the total bytes being buffered across all in-flight body
76    /// reads. `Some(n)` installs a byte-permit semaphore; each
77    /// `collect_body_capped` reserves its per-request cap upfront and
78    /// releases the permits when the body is buffered or rejected. Refuses
79    /// excess requests with `503 Service Unavailable` (via `WebError::Busy`).
80    /// `None` is unbounded.
81    max_inflight_body_bytes: Option<Arc<Semaphore>>,
82    /// `Some(d)` is forwarded to hyper's `http1::Builder::header_read_timeout`
83    /// — bounds how long after starting to read request headers we'll wait
84    /// before dropping the connection. Catches slowloris and clients that
85    /// TCP-connect-and-send-nothing. `None` leaves hyper's default (none).
86    header_read_timeout: Option<Duration>,
87}
88
89impl Server {
90    /// Create a server for `router` with default settings: no middleware, no
91    /// CORS, the default body cap, and no DoS limits. Configure it with the
92    /// `with_*` builder methods, then call [`run`](Self::run).
93    pub fn new(router: Router) -> Self {
94        Self {
95            router: Arc::new(router),
96            middleware_chain: Arc::new(MiddlewareChain::new()),
97            finalizer: Arc::new(Finalizer::new()),
98            max_body_bytes: DEFAULT_MAX_BODY_BYTES,
99            cors: None,
100            #[cfg(feature = "compression")]
101            compression: None,
102            request_timeout: None,
103            drain_deadline: DEFAULT_DRAIN_DEADLINE,
104            max_connections: None,
105            max_inflight_body_bytes: None,
106            header_read_timeout: None,
107        }
108    }
109
110    /// Adds a middleware to the server's request processing chain.
111    pub fn with_middleware(mut self, middleware: impl Middleware + 'static) -> Self {
112        let mut chain = Arc::try_unwrap(self.middleware_chain).unwrap_or_else(|arc| (*arc).clone());
113        chain.add(middleware);
114        self.middleware_chain = Arc::new(chain);
115        self
116    }
117
118    /// Enables CORS with the given policy. The server then answers preflight
119    /// (`OPTIONS`) requests itself and adds the `Access-Control-*` headers to
120    /// every cross-origin response (including error responses). See
121    /// [`CorsLayer`].
122    pub fn with_cors(mut self, cors: CorsLayer) -> Self {
123        self.cors = Some(Arc::new(cors));
124        self
125    }
126
127    /// Enables response compression (gzip / brotli). For each response Actus
128    /// picks an encoding from the request's `Accept-Encoding` and compresses
129    /// buffered, compressible bodies above the layer's size threshold. See
130    /// [`CompressionLayer`]. *(Requires the `compression` feature.)*
131    #[cfg(feature = "compression")]
132    pub fn with_compression(mut self, layer: CompressionLayer) -> Self {
133        self.compression = Some(Arc::new(layer));
134        self
135    }
136
137    /// Caps the request body Actus will buffer (default
138    /// [`DEFAULT_MAX_BODY_BYTES`] = 2 MiB). A larger body is rejected with
139    /// `413 Payload Too Large` before it can exhaust memory — the limit
140    /// bounds buffered bytes, so it also covers chunked bodies that lie about
141    /// (or omit) `Content-Length`.
142    ///
143    /// `0` is accepted and means "reject every non-empty body" — typically
144    /// only useful on a strictly-GET surface that should never see a body.
145    pub fn with_max_body_bytes(mut self, max: usize) -> Self {
146        self.max_body_bytes = max;
147        self
148    }
149
150    /// Cap the total time any single request may take — body parse,
151    /// middleware `before`, handler, middleware `after`, and finalization
152    /// combined. An over-budget request is aborted (the handler's future
153    /// is dropped) and the client gets `504 Gateway Timeout`. No timeout
154    /// is set by default.
155    ///
156    /// **Scope.** The timer covers the request/response exchange. A
157    /// WebSocket upgrade succeeds inside the timer (the `101` is the
158    /// response); the post-upgrade conversation runs in its own task and
159    /// is not bound by this timeout.
160    ///
161    /// **Effect of an over-budget request.** When the timer elapses the
162    /// in-flight future is dropped, which cancels whatever the handler
163    /// was awaiting (DB query, channel recv, etc.). The 504 reply is
164    /// one-shot — the after-chain doesn't run on it (by definition,
165    /// some component upstream was unresponsive; running more risks
166    /// hanging again).
167    pub fn with_request_timeout(mut self, d: Duration) -> Self {
168        self.request_timeout = Some(d);
169        self
170    }
171
172    /// Override the grace period for in-flight connections after a
173    /// shutdown signal (default [`DEFAULT_DRAIN_DEADLINE`] = 30 s).
174    /// Anything still running at the deadline is hard-aborted via
175    /// `JoinSet::shutdown`. Use a longer value for surfaces that hold
176    /// long-lived connections (large file downloads, WebSockets);
177    /// a shorter value for fast-iteration dev workflows. `Duration::ZERO`
178    /// aborts every in-flight task immediately.
179    pub fn with_drain_deadline(mut self, d: Duration) -> Self {
180        self.drain_deadline = d;
181        self
182    }
183
184    /// Cap concurrent connection tasks at `n`. While the cap is held, the
185    /// accept loop pauses on permit acquisition; new SYNs queue in the
186    /// kernel's accept backlog and (once that fills, governed by
187    /// `SOMAXCONN`) get dropped at the OS level. No userland reject /
188    /// no-503-per-conn cost — the kernel handles the spillover.
189    ///
190    /// Each connection task holds its permit until it ends, including the
191    /// post-handshake WebSocket conversation. Size accordingly: a
192    /// `with_max_connections(N)` server can hold `N` long-lived WebSockets
193    /// *before* it stops accepting new connections of any kind.
194    ///
195    /// Unbounded by default (no semaphore installed).
196    pub fn with_max_connections(mut self, n: usize) -> Self {
197        self.max_connections = Some(n);
198        self
199    }
200
201    /// Cap the total bytes being buffered across all in-flight body reads
202    /// at `n`. Each request reserves its per-request cap (see
203    /// `with_max_body_bytes`) from this global budget upfront; if the
204    /// budget is exhausted, the request is refused with `503 Service
205    /// Unavailable` (via [`WebError::Busy`]) and a short `Retry-After`.
206    ///
207    /// Together with [`Self::with_max_connections`] this puts a hard
208    /// ceiling on the framework's memory under adversarial load:
209    /// `with_max_connections(C) * with_max_body_bytes(B)` is the *worst*
210    /// case absent this knob; with it, the ceiling is `min(C * B, this
211    /// value)`.
212    ///
213    /// Pre-reserving the per-request cap over-counts (a 1 KB request
214    /// reserves up to its full cap); the alternative — incremental
215    /// per-chunk byte accounting — is more code for the same effective
216    /// ceiling, and a request that has already started buffering can't
217    /// be sensibly aborted partway through anyway.
218    ///
219    /// `n` is clamped to `u32::MAX` internally (Tokio's `Semaphore`
220    /// permit count uses `u32`); for practical deployments this is no
221    /// limit (4 GiB).
222    ///
223    /// Unbounded by default.
224    pub fn with_max_inflight_body_bytes(mut self, n: usize) -> Self {
225        // u32 cap is a tokio Semaphore constraint, not a design choice.
226        let n_capped = n.min(u32::MAX as usize);
227        self.max_inflight_body_bytes = Some(Arc::new(Semaphore::new(n_capped)));
228        self
229    }
230
231    /// Bound how long after starting to read request headers we'll wait
232    /// before dropping the connection. Forwards to hyper's
233    /// `http1::Builder::header_read_timeout`. Catches slowloris (sending
234    /// headers one byte at a time) and clients that TCP-connect-and-send-
235    /// nothing — the most common file-descriptor-exhaustion attack on a
236    /// keep-alive HTTP server.
237    ///
238    /// Note: hyper 1.x doesn't have a separate "idle between requests"
239    /// timeout (after a complete request, an idle keep-alive connection
240    /// stays open until either side closes or the OS-level TCP keep-alive
241    /// fires). If that matters for your deployment, either disable
242    /// keep-alive entirely upstream of Actus or rely on the OS knobs.
243    ///
244    /// No timeout by default (hyper's default).
245    pub fn with_header_read_timeout(mut self, d: Duration) -> Self {
246        self.header_read_timeout = Some(d);
247        self
248    }
249
250    /// Runs the server on `127.0.0.1:port` (loopback only). For a different
251    /// bind address — e.g. `0.0.0.0:port` to accept connections from other
252    /// hosts in a container — use [`Server::run_on`].
253    ///
254    /// Listens for SIGTERM/SIGINT (Unix) or Ctrl-C (cross-platform) and
255    /// shuts down gracefully: stops accepting new connections, signals
256    /// in-flight connections to finish, and waits up to 30 seconds for
257    /// them to drain before returning.
258    pub async fn run(self, port: u16) -> Result<(), ServerError> {
259        self.run_on(SocketAddr::from(([127, 0, 0, 1], port))).await
260    }
261
262    /// Like [`Server::run`] but binds an arbitrary address. Pass
263    /// `0.0.0.0:port` (or `[::]:port`) to accept connections from other hosts.
264    pub async fn run_on(self, addr: SocketAddr) -> Result<(), ServerError> {
265        self.run_with_shutdown_on(addr, default_shutdown_signal())
266            .await
267    }
268
269    /// Like [`Server::run`] but with a custom shutdown trigger (a future that,
270    /// when it resolves, starts the graceful drain). Binds `127.0.0.1:port`;
271    /// see [`Server::run_with_shutdown_on`] for a custom bind address. Useful
272    /// for tests or for embedding the server in a larger supervision tree.
273    pub async fn run_with_shutdown(
274        self,
275        port: u16,
276        shutdown: impl Future<Output = ()> + Send + 'static,
277    ) -> Result<(), ServerError> {
278        self.run_with_shutdown_on(SocketAddr::from(([127, 0, 0, 1], port)), shutdown)
279            .await
280    }
281
282    /// Bind `addr`, then serve exactly as
283    /// [`Server::run_with_shutdown_listener`] — the general form — does.
284    /// [`Server::run`], [`Server::run_on`], and [`Server::run_with_shutdown`]
285    /// are thin wrappers over this; this is a thin bind over the listener
286    /// form.
287    ///
288    /// **Drain bound.** Once `shutdown` resolves the server stops accepting
289    /// and signals every in-flight connection to wind down. The drain
290    /// deadline defaults to [`DEFAULT_DRAIN_DEADLINE`] (30 s); override
291    /// with [`Server::with_drain_deadline`]. Anything still running at
292    /// the deadline is hard-aborted via `JoinSet::shutdown`. In particular,
293    /// long-lived connections (WebSockets, slow downloads, kept-alive idle
294    /// clients) and any connection task that raced the shutdown notification
295    /// and missed it both get aborted at the deadline rather than draining
296    /// gracefully.
297    pub async fn run_with_shutdown_on(
298        self,
299        addr: SocketAddr,
300        shutdown: impl Future<Output = ()> + Send + 'static,
301    ) -> Result<(), ServerError> {
302        let listener = TcpListener::bind(addr).await?;
303        self.run_with_shutdown_listener(listener, shutdown).await
304    }
305
306    /// Like [`Server::run_on`] but serves on a listener the caller already
307    /// bound (or inherited), with the default SIGTERM / SIGINT shutdown
308    /// trigger. See [`Server::run_with_shutdown_listener`] for what a
309    /// pre-bound listener buys and how to adopt an inherited one.
310    pub async fn run_listener(self, listener: TcpListener) -> Result<(), ServerError> {
311        self.run_with_shutdown_listener(listener, default_shutdown_signal())
312            .await
313    }
314
315    /// The most general form: serve on a listener the caller already bound
316    /// (or inherited) until `shutdown` resolves, then drain. Every other
317    /// `run*` method is a wrapper over this one.
318    ///
319    /// **Why hand the server a listener.** Two cases the bind-it-yourself
320    /// forms can't express:
321    ///
322    /// - **Socket activation** — a supervisor (systemd's `LISTEN_FDS`
323    ///   protocol, launchd, …) owns the listening socket and passes it to
324    ///   the process it spawns. Because the socket outlives the process,
325    ///   connections arriving during a restart queue in the kernel's accept
326    ///   backlog instead of being refused, and the next process serves them.
327    /// - **Race-free embedding and tests** — bind `127.0.0.1:0`, keep the
328    ///   listener, and pass it in; no bind-drop-rebind window in which the
329    ///   port can be lost, and requests may connect before the accept loop
330    ///   even starts (the kernel queues them).
331    ///
332    /// An inherited `std::net::TcpListener` must be set non-blocking before
333    /// conversion — tokio requires it, and a supervisor passes the fd
334    /// blocking:
335    ///
336    /// ```no_run
337    /// # async fn doc(server: actus_server::Server, inherited: std::net::TcpListener)
338    /// # -> Result<(), actus_server::ServerError> {
339    /// inherited.set_nonblocking(true)?;
340    /// let listener = tokio::net::TcpListener::from_std(inherited)?;
341    /// server.run_with_shutdown_listener(listener, std::future::pending()).await
342    /// # }
343    /// ```
344    ///
345    /// **Drain bound.** As [`Server::run_with_shutdown_on`]: once `shutdown`
346    /// resolves the server stops accepting (the listener is dropped — under
347    /// socket activation the *socket* stays open in the supervisor, which is
348    /// the point) and in-flight connections get [`Server::with_drain_deadline`]
349    /// (default [`DEFAULT_DRAIN_DEADLINE`] = 30 s) to finish before being
350    /// aborted. A streaming response that never completes (SSE) always rides
351    /// to the deadline.
352    pub async fn run_with_shutdown_listener(
353        self,
354        listener: TcpListener,
355        shutdown: impl Future<Output = ()> + Send + 'static,
356    ) -> Result<(), ServerError> {
357        match listener.local_addr() {
358            Ok(addr) => info!("Server listening on http://{}", addr),
359            // An inherited fd can decline local_addr (exotic socket family);
360            // serving still works, so log what we know and carry on.
361            Err(_) => info!("Server listening on an inherited socket"),
362        }
363
364        let app = Arc::new(self);
365        // Per-connection cancellation: once `Notify::notify_waiters` fires,
366        // every in-flight task wakes up and asks hyper to gracefully close
367        // its connection (finishing the current response, then exiting).
368        let notify = Arc::new(tokio::sync::Notify::new());
369        let mut tasks: JoinSet<()> = JoinSet::new();
370
371        // Optional cap on concurrent connections. When at-capacity the
372        // accept loop pauses on permit acquisition; new SYNs queue in the
373        // kernel accept backlog (SOMAXCONN) and get dropped at the OS
374        // level once that fills. Each spawned connection task moves its
375        // permit in; the permit releases when the task exits.
376        let conn_permits = app.max_connections.map(|n| Arc::new(Semaphore::new(n)));
377
378        tokio::pin!(shutdown);
379
380        loop {
381            tokio::select! {
382                // Accept-branch: acquire a connection permit first (when
383                // a cap is configured), then accept. The outer select
384                // races this against shutdown so a paused-at-capacity
385                // accept loop still notices the shutdown signal.
386                accept_with_permit = async {
387                    let permit = match &conn_permits {
388                        Some(s) => Some(s.clone().acquire_owned().await.expect("semaphore never closed")),
389                        None => None,
390                    };
391                    let result = listener.accept().await;
392                    (result, permit)
393                } => {
394                    let (accept_result, permit) = accept_with_permit;
395                    let (stream, _peer) = match accept_result {
396                        Ok(s) => s,
397                        Err(e) => {
398                            error!("accept error: {}", e);
399                            // permit released when this branch ends — fine
400                            continue;
401                        }
402                    };
403                    let io = hyper_util::rt::TokioIo::new(stream);
404                    let app = app.clone();
405                    let notify = notify.clone();
406                    let header_timeout = app.header_read_timeout;
407                    tasks.spawn(async move {
408                        // The permit (if any) lives for the connection's
409                        // lifetime; releasing happens at task drop.
410                        let _permit = permit;
411
412                        let mut builder = hyper::server::conn::http1::Builder::new();
413                        if let Some(d) = header_timeout {
414                            builder.header_read_timeout(d);
415                        }
416                        let conn = builder.serve_connection(
417                            io,
418                            service_fn(move |req| app.clone().handle_request(req)),
419                        );
420                        // With the `websocket` feature, allow `101 Switching
421                        // Protocols` responses to hand off the connection.
422                        #[cfg(feature = "websocket")]
423                        let conn = conn.with_upgrades();
424                        tokio::pin!(conn);
425                        tokio::select! {
426                            res = conn.as_mut() => {
427                                if let Err(err) = res {
428                                    error!("Error serving connection: {}", err);
429                                }
430                            }
431                            _ = notify.notified() => {
432                                conn.as_mut().graceful_shutdown();
433                                if let Err(err) = conn.await {
434                                    error!("Error during graceful shutdown: {}", err);
435                                }
436                            }
437                        }
438                    });
439                }
440                // Reap finished connection tasks so the `JoinSet` doesn't grow
441                // without bound over the server's lifetime — and so a panicked
442                // connection task is logged promptly, not only at shutdown.
443                joined = tasks.join_next(), if !tasks.is_empty() => {
444                    match joined {
445                        Some(Err(e)) if e.is_panic() => error!("Connection task panicked: {}", e),
446                        Some(Err(e)) => error!("Connection task failed: {}", e),
447                        Some(Ok(())) | None => {}
448                    }
449                }
450                _ = &mut shutdown => {
451                    info!("Shutdown signal received; draining in-flight requests");
452                    break;
453                }
454            }
455        }
456
457        // Stop accepting; signal connections to wind down.
458        drop(listener);
459        notify.notify_waiters();
460
461        // Drain. The grace period is configurable via
462        // `Server::with_drain_deadline` (default 30 s).
463        let drain_deadline = tokio::time::sleep(app.drain_deadline);
464        tokio::pin!(drain_deadline);
465        loop {
466            tokio::select! {
467                next = tasks.join_next() => {
468                    match next {
469                        Some(Ok(())) => {}
470                        Some(Err(e)) if e.is_panic() => {
471                            error!("Connection task panicked: {}", e);
472                        }
473                        Some(Err(e)) => {
474                            error!("Connection task failed: {}", e);
475                        }
476                        None => break,
477                    }
478                }
479                _ = &mut drain_deadline => {
480                    warn!("Drain deadline exceeded; aborting {} connection(s)", tasks.len());
481                    tasks.shutdown().await;
482                    break;
483                }
484            }
485        }
486
487        info!("Server shutdown complete");
488        Ok(())
489    }
490
491    /// Stamp the configured CORS response headers onto `response` (no-op when
492    /// CORS isn't enabled, or the request had no allowed `Origin`). Applied to
493    /// *every* outgoing response — success and error alike — so the browser
494    /// can read 4xx/5xx bodies.
495    fn with_cors_headers(
496        &self,
497        request: &Request,
498        mut response: HyperResponse<ResponseBody>,
499    ) -> HyperResponse<ResponseBody> {
500        if let Some(cors) = &self.cors {
501            cors.apply(&request.headers, response.headers_mut(), false);
502        }
503        response
504    }
505
506    /// Fulfil a `ReplyData::Upgrade` from a handler when the request was
507    /// genuinely a WebSocket handshake: send `101 Switching Protocols` and
508    /// spawn the handler on the upgraded connection. (The "handler returned
509    /// Upgrade but the request wasn't a handshake" case is rewritten to a
510    /// 426 reply in [`Self::finalize_reply`] before reaching this method, so
511    /// it can flow through the after-chain like any other error.)
512    ///
513    /// No CORS headers on the `101`: WebSocket handshakes are scoped by
514    /// browser origin checks (the handler inspects `Origin` itself before
515    /// calling `ws::upgrade`), not by the CORS protocol — `Access-Control-*`
516    /// on a `101` is meaningless to the browser.
517    #[cfg(feature = "websocket")]
518    async fn complete_ws_upgrade(
519        &self,
520        handler: Box<dyn std::any::Any + Send>,
521        ws_upgrade: (hyper::upgrade::OnUpgrade, HeaderValue),
522    ) -> HyperResponse<ResponseBody> {
523        // `ReplyData::Upgrade` is only constructible via `ws::upgrade(...)`,
524        // which always boxes an `UpgradeTask`. A failing downcast would mean
525        // a crate-internal invariant is broken; surface as a panic rather
526        // than silently producing a 500.
527        let task = handler
528            .downcast::<websocket::UpgradeTask>()
529            .expect("ReplyData::Upgrade always carries an UpgradeTask");
530        let (on_upgrade, accept) = ws_upgrade;
531        tokio::spawn(websocket::run_upgrade(on_upgrade, *task));
532        let mut resp = self.finalizer.build_response(ReplyData::Empty).await;
533        *resp.status_mut() = StatusCode::SWITCHING_PROTOCOLS;
534        let h = resp.headers_mut();
535        h.insert(header::CONNECTION, HeaderValue::from_static("upgrade"));
536        h.insert(header::UPGRADE, HeaderValue::from_static("websocket"));
537        h.insert(header::SEC_WEBSOCKET_ACCEPT, accept);
538        resp
539    }
540
541    /// Build the error reply for `error` and route it through
542    /// [`finalize_reply`](Self::finalize_reply), so the after-chain,
543    /// compression, and CORS apply to errors exactly as they do to handler
544    /// successes. This is the canonical way to produce a `WebError`
545    /// response anywhere a `Request` exists.
546    async fn finalize_error(
547        &self,
548        error: WebError,
549        request: &Request,
550        #[cfg(feature = "websocket")] ws_upgrade: Option<(hyper::upgrade::OnUpgrade, HeaderValue)>,
551    ) -> HyperResponse<ResponseBody> {
552        let data = self.finalizer.error_to_reply(error);
553        self.finalize_reply(
554            data,
555            request,
556            #[cfg(feature = "websocket")]
557            ws_upgrade,
558        )
559        .await
560    }
561
562    /// Run the after-middleware chain, then turn the reply into a `Response`
563    /// via [`dispatch_reply`](Self::dispatch_reply).
564    ///
565    /// **After-chain runs on every reply with a body and a `Request`.** That
566    /// includes handler successes, `Outcome::Respond` short-circuits, *and*
567    /// every error the application produced (404 / 405 / 401 / 400 / a
568    /// handler-returned `Err(WebError)`, etc.). The README's promise that a
569    /// request-id stamper "still fires on a short-circuit" generalizes to
570    /// every reply — that's the contract this method enforces.
571    ///
572    /// **Exceptions** (the after-chain *doesn't* run):
573    /// - **101 Switching Protocols** — a WebSocket-handshake success has no
574    ///   HTTP body to decorate, and the upgrade machinery consumes the
575    ///   connection.
576    /// - **Pre-parse failures** — a request that fails before
577    ///   [`Request::from_hyper`] returns a skeleton (e.g. malformed HTTP
578    ///   from hyper itself) has no `Request` to give the hook. The body-cap
579    ///   413 and truncated-body 400 are *not* exceptions here: `from_hyper`
580    ///   now returns a skeleton `Request` on those, so they do run through
581    ///   the after-chain.
582    /// - **CORS preflight 204** — synthesized before middleware or routing;
583    ///   not an application request (see [`Self::handle_request`]).
584    async fn finalize_reply(
585        &self,
586        #[allow(unused_mut)] mut data: ReplyData,
587        request: &Request,
588        #[cfg(feature = "websocket")] ws_upgrade: Option<(hyper::upgrade::OnUpgrade, HeaderValue)>,
589    ) -> HyperResponse<ResponseBody> {
590        // If the handler returned `ws::upgrade(...)` but the request isn't a
591        // real WebSocket handshake, rewrite to a 426 error reply *here* so
592        // it flows through the same after-chain / compression / CORS path as
593        // any other error. Only the success-handshake path (Upgrade reply +
594        // ws_upgrade present) keeps the after-chain bypass — a 101 has no
595        // HTTP body to decorate.
596        #[cfg(feature = "websocket")]
597        if matches!(data, ReplyData::Upgrade(_)) && ws_upgrade.is_none() {
598            data = self.finalizer.error_to_reply(WebError::Problem(
599                ProblemDetails::new(StatusCode::UPGRADE_REQUIRED, "WebSocket Upgrade Required")
600                    .detail("this endpoint expects a WebSocket handshake"),
601            ));
602        }
603
604        let needs_after_chain = !matches!(data, ReplyData::Upgrade(_));
605        if needs_after_chain
606            && let Err(e) = self
607                .middleware_chain
608                .process_response(request, &mut data)
609                .await
610        {
611            // After-chain itself errored. Build a plain error response
612            // (no further after-chain — recursion prevention) so a buggy
613            // hook can't infinite-loop the request.
614            return self.with_cors_headers(request, self.finalizer.build_error(e).await);
615        }
616        self.dispatch_reply(
617            data,
618            request,
619            #[cfg(feature = "websocket")]
620            ws_upgrade,
621        )
622        .await
623    }
624
625    /// Turn a handler's (or a short-circuiting middleware's) `ReplyData` into a
626    /// fully processed response: WebSocket upgrade if it's an `Upgrade` reply;
627    /// otherwise compress (if enabled), finalize, and stamp CORS / `Vary`.
628    async fn dispatch_reply(
629        &self,
630        #[allow(unused_mut)] mut data: ReplyData,
631        request: &Request,
632        #[cfg(feature = "websocket")] ws_upgrade: Option<(hyper::upgrade::OnUpgrade, HeaderValue)>,
633    ) -> HyperResponse<ResponseBody> {
634        // A handler that returned `ws::upgrade(...)`: complete the handshake
635        // instead of finalizing a body. `finalize_reply` only lets us reach
636        // here for an `Upgrade` reply when the request *was* a real
637        // handshake (otherwise it rewrote the reply to a 426 error), so
638        // `ws_upgrade` is guaranteed `Some` on this branch.
639        #[cfg(feature = "websocket")]
640        if matches!(data, ReplyData::Upgrade(_)) {
641            let ReplyData::Upgrade(handler) = data else {
642                unreachable!()
643            };
644            let ws_upgrade =
645                ws_upgrade.expect("finalize_reply rewrites Upgrade-without-handshake to 426");
646            return self.complete_ws_upgrade(handler, ws_upgrade).await;
647        }
648        // Compression is the last transform — after response middleware,
649        // before the bytes leave. (Only buffered, compressible bodies above
650        // the threshold are touched.)
651        #[cfg(feature = "compression")]
652        if let Some(c) = &self.compression {
653            data = c.compress_reply(
654                data,
655                request
656                    .headers
657                    .get("accept-encoding")
658                    .and_then(|v| v.to_str().ok()),
659            );
660        }
661        let response = self.finalizer.build_response(data).await;
662        let response = self.with_cors_headers(request, response);
663        #[cfg(feature = "compression")]
664        let response = crate::compression::tag_vary_if_encoded(response);
665        response
666    }
667
668    /// Handles an individual incoming `hyper::Request`.
669    ///
670    /// Wraps [`handle_request_inner`](Self::handle_request_inner) in a
671    /// per-request timeout when one is configured (see
672    /// [`Server::with_request_timeout`]); a timed-out request gets a
673    /// one-shot `504 Gateway Timeout` (no after-chain, since by definition
674    /// something upstream was unresponsive).
675    ///
676    /// Every reply with a `Request` flows through
677    /// [`finalize_reply`](Self::finalize_reply) — handler successes,
678    /// `Outcome::Respond` short-circuits, and *every* error (middleware
679    /// `Err`, body parse failure, 404 / 405 from the router, handler-returned
680    /// `Err`, even the 413 / 400 from the body-cap path). The after-chain,
681    /// compression, and CORS apply uniformly. CORS preflight is the one
682    /// short-circuit that bypasses the pipeline — it's HTTP-protocol traffic
683    /// rather than an application request.
684    async fn handle_request(
685        self: Arc<Self>,
686        req: HyperRequest<Incoming>,
687    ) -> Result<HyperResponse<ResponseBody>, hyper::Error> {
688        let timeout = self.request_timeout;
689        let app = self.clone();
690        let inner = app.handle_request_inner(req);
691        match timeout {
692            None => inner.await,
693            Some(d) => match tokio::time::timeout(d, inner).await {
694                Ok(r) => r,
695                Err(_) => {
696                    warn!(timeout = ?d, "request exceeded configured timeout");
697                    Ok(self.finalizer.build_error(WebError::Timeout).await)
698                }
699            },
700        }
701    }
702
703    /// The actual request pipeline. Split from
704    /// [`handle_request`](Self::handle_request) so the latter can wrap it
705    /// in a timeout when one is configured.
706    ///
707    /// **Lifecycle order:**
708    ///
709    /// 1. capture WS upgrade (if request looks like a handshake)
710    /// 2. build the `Request` skeleton (no body yet)
711    /// 3. CORS preflight short-circuit (uses headers only)
712    /// 4. match controller — 404 short-circuits *without* buffering the
713    ///    body (efficiency win on adversarial bad-path requests); then stamp
714    ///    the matched controller's rate-limit class onto the request, so a
715    ///    `before` middleware (which only gets `&Request`) can read it
716    /// 5. buffer the body, capped per the resolved policy (today: server-
717    ///    wide; soon, per-controller / per-route)
718    /// 6. middleware `before`
719    /// 7. `to_params` (Content-Type-driven body parse)
720    /// 8. dispatch via the already-matched controller
721    /// 9. middleware `after` + finalize
722    ///
723    /// The "route before buffer" order is what lets the body cap depend on
724    /// the matched route — without it, the framework would have to commit
725    /// to a single cap before knowing where the request is headed.
726    async fn handle_request_inner(
727        self: Arc<Self>,
728        #[allow(unused_mut)] mut req: HyperRequest<Incoming>,
729    ) -> Result<HyperResponse<ResponseBody>, hyper::Error> {
730        let request_span = span!(Level::INFO, "request");
731        async move {
732            // 1. Capture the WS upgrade handshake (if any) before
733            //    `from_hyper_parts` consumes the request: the `OnUpgrade`
734            //    future and the derived `Sec-WebSocket-Accept`. (See
735            //    `websocket` module docs for why this happens up front.)
736            #[cfg(feature = "websocket")]
737            let ws_upgrade: Option<(hyper::upgrade::OnUpgrade, HeaderValue)> =
738                if websocket::is_upgrade_request(req.method(), req.headers()) {
739                    websocket::accept_key(req.headers())
740                        .map(|accept| (hyper::upgrade::on(&mut req), accept))
741                } else {
742                    None
743                };
744
745            // 2. Build the skeleton (method / path / query / headers); the
746            //    body stream is held aside for step 5.
747            let (mut request, body_stream) = Request::from_hyper_parts(req);
748
749            // 3. CORS preflight: synthesize the 204 ourselves before any
750            //    application-layer work. Preflights are HTTP-protocol
751            //    traffic; neither `before` nor `after` middleware runs on
752            //    them (see `CLAUDE.md` principle 1).
753            if let Some(cors) = &self.cors
754                && CorsLayer::is_preflight(&request.method, &request.headers)
755            {
756                let mut resp = self.finalizer.build_response(ReplyData::Empty).await;
757                cors.apply(&request.headers, resp.headers_mut(), true);
758                return Ok(resp);
759            }
760
761            // 4. Match controller. A path that hits nothing is 404 *before*
762            //    body buffering — a 10 MiB POST to a non-existent URL no
763            //    longer wastes 10 MiB of memory.
764            let route_match = match self.router.match_controller(&request.path_parts) {
765                Some(rm) => rm,
766                None => {
767                    return Ok(self
768                        .finalize_error(
769                            WebError::NotFound,
770                            &request,
771                            #[cfg(feature = "websocket")]
772                            ws_upgrade,
773                        )
774                        .await);
775                }
776            };
777
778            // 4b. Stamp the matched controller's rate-limit class onto the
779            //     request (the skeleton predates routing, so it was `None`).
780            //     This is the one piece of routing context a `before`
781            //     middleware can't otherwise see — it gets `&mut Request`,
782            //     not the matched controller. An application rate-limit
783            //     middleware reads `request.rate_limit_class` and applies its
784            //     own per-class policy; the framework owns the label and the
785            //     `429` response, not the limiter. (Set before the body
786            //     buffer so it survives `collect_body` and is present for the
787            //     whole pipeline, including the after-chain on error replies.)
788            request.rate_limit_class = route_match.controller.actus_rate_limit();
789
790            // 5. Resolve the effective body cap: the matched controller's
791            //    `#[controller(max_body_bytes = …)]` if it set one, otherwise
792            //    the server-wide `with_max_body_bytes` cap (default 2 MiB). A
793            //    future Phase 2 adds per-route overrides at the top of this
794            //    fall-through.
795            //
796            //    The error path returns the same skeleton so the after-chain
797            //    still has a `Request`.
798            let effective_cap = route_match
799                .controller
800                .actus_max_body_bytes()
801                .unwrap_or(self.max_body_bytes);
802            request = match request
803                .collect_body(
804                    body_stream,
805                    effective_cap,
806                    self.max_inflight_body_bytes.as_ref(),
807                )
808                .await
809            {
810                Ok(r) => r,
811                Err((request, e)) => {
812                    warn!("rejecting request before parse: {}", e);
813                    return Ok(self
814                        .finalize_error(
815                            e,
816                            &request,
817                            #[cfg(feature = "websocket")]
818                            ws_upgrade,
819                        )
820                        .await);
821                }
822            };
823
824            // 6. Middleware `before` chain.
825            let pre_data: Option<ReplyData> =
826                match self.middleware_chain.process_request(&mut request).await {
827                    Ok(Outcome::Continue) => None,
828                    Ok(Outcome::Respond(data)) => Some(data),
829                    Err(e) => {
830                        return Ok(self
831                            .finalize_error(
832                                e,
833                                &request,
834                                #[cfg(feature = "websocket")]
835                                ws_upgrade,
836                            )
837                            .await);
838                    }
839                };
840
841            // A `before` hook short-circuited with a reply — skip routing
842            // and the handler, but still run the after-chain.
843            if let Some(data) = pre_data {
844                return Ok(self
845                    .finalize_reply(
846                        data,
847                        &request,
848                        #[cfg(feature = "websocket")]
849                        ws_upgrade,
850                    )
851                    .await);
852            }
853
854            // 7. Body parse (JSON / form / opaque, per Content-Type).
855            //    Malformed body → 400 through the after-chain.
856            let params = match request.to_params() {
857                Ok(p) => p,
858                Err(e) => {
859                    return Ok(self
860                        .finalize_error(
861                            e,
862                            &request,
863                            #[cfg(feature = "websocket")]
864                            ws_upgrade,
865                        )
866                        .await);
867                }
868            };
869
870            // 8. Dispatch via the matched controller. 405 (verb mismatch
871            //    inside the controller) and handler-returned errors both
872            //    come back through here.
873            match route_match
874                .controller
875                .actus_dispatch(&route_match.action, params)
876                .await
877            {
878                Ok(data) => Ok(self
879                    .finalize_reply(
880                        data,
881                        &request,
882                        #[cfg(feature = "websocket")]
883                        ws_upgrade,
884                    )
885                    .await),
886                Err(e) => Ok(self
887                    .finalize_error(
888                        e,
889                        &request,
890                        #[cfg(feature = "websocket")]
891                        ws_upgrade,
892                    )
893                    .await),
894            }
895        }
896        .instrument(request_span)
897        .await
898    }
899}
900
901/// Default shutdown trigger: resolves on SIGTERM, SIGINT (Unix), or Ctrl-C
902/// (Windows). This is what [`Server::run`] uses; for tests or embedding,
903/// see [`Server::run_with_shutdown`].
904async fn default_shutdown_signal() {
905    #[cfg(unix)]
906    {
907        use tokio::signal::unix::{SignalKind, signal};
908        let mut sigterm = signal(SignalKind::terminate()).expect("install SIGTERM handler");
909        let mut sigint = signal(SignalKind::interrupt()).expect("install SIGINT handler");
910        tokio::select! {
911            _ = sigterm.recv() => info!("Received SIGTERM"),
912            _ = sigint.recv() => info!("Received SIGINT"),
913        }
914    }
915    #[cfg(not(unix))]
916    {
917        tokio::signal::ctrl_c()
918            .await
919            .expect("install Ctrl-C handler");
920        info!("Received Ctrl-C");
921    }
922}