Skip to main content

mini_static/
server.rs

1use std::convert::Infallible;
2use std::fs;
3use std::net::SocketAddr;
4use std::path::{Path, PathBuf};
5use std::pin::Pin;
6use std::sync::Arc;
7use std::task::{Context, Poll};
8use std::time::{Duration, SystemTime};
9
10use bytes::Bytes;
11use hyper::{HeaderMap, Method, Request, Response, StatusCode};
12use hyper::body::Incoming;
13use hyper::http::response::Builder;
14use hyper::service::service_fn;
15use http_body_util::Full;
16use hyper_util::rt::TokioExecutor;
17use hyper_util::rt::TokioIo;
18use hyper_util::server::conn::auto::Builder as AutoBuilder;
19use tokio::fs::File;
20use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, ReadBuf};
21use tokio::net::{TcpListener, TcpStream};
22use tokio::sync::{OwnedSemaphorePermit, Semaphore};
23use tokio::time::timeout;
24
25use crate::bundle;
26use crate::error::StaticError;
27use crate::handler::{FileBody, ResponseBody};
28use crate::minify::{self, MinifyError};
29use crate::minify_cache::{MinifyCache, DEFAULT_MINIFY_CACHE_CAPACITY};
30use crate::reload::{self, ChangeType, SseBody};
31use crate::resolve;
32use crate::watcher::{start_watching, Broadcaster};
33
34const ACCEPT_BACKOFF_INITIAL: Duration = Duration::from_millis(10);
35const ACCEPT_BACKOFF_MAX: Duration = Duration::from_secs(1);
36
37/// Default maximum concurrent connections, overridable via `Server::with_max_connections`.
38const DEFAULT_MAX_CONNECTIONS: usize = 1024;
39
40/// A source of accepted TCP connections. Abstracted so the accept-error backoff below
41/// can be exercised against a listener that fails on demand, without needing to provoke
42/// real OS-level accept errors (e.g. EMFILE) in tests.
43trait TcpAccept {
44    async fn accept(&self) -> std::io::Result<(TcpStream, SocketAddr)>;
45}
46
47impl TcpAccept for TcpListener {
48    async fn accept(&self) -> std::io::Result<(TcpStream, SocketAddr)> {
49        TcpListener::accept(self).await
50    }
51}
52
53/// Accept a connection and reserve it a connection-limit permit.
54///
55/// `backoff` retries a failed `accept()` after an exponentially growing delay (reset on
56/// the next success, capped at `ACCEPT_BACKOFF_MAX`) instead of ending the accept loop,
57/// so a sustained failure — the process being out of file descriptors, say — degrades
58/// into periodic retries rather than a CPU-bound busy spin or a permanently dead server.
59///
60/// Returns `None` only if the semaphore itself has been closed (never happens in normal
61/// operation, since nothing ever calls `close()` on it — handled so a caller can still
62/// fail safely rather than panic).
63async fn accept_and_permit<L: TcpAccept>(
64    listener: &L,
65    backoff: &mut Duration,
66    semaphore: &Arc<Semaphore>,
67) -> Option<(TcpStream, OwnedSemaphorePermit)> {
68    loop {
69        let stream = match listener.accept().await {
70            Ok((stream, _)) => {
71                *backoff = ACCEPT_BACKOFF_INITIAL;
72                stream
73            }
74            Err(_) => {
75                tokio::time::sleep(*backoff).await;
76                *backoff = (*backoff * 2).min(ACCEPT_BACKOFF_MAX);
77                continue;
78            }
79        };
80        return semaphore.clone().acquire_owned().await.ok().map(|permit| (stream, permit));
81    }
82}
83
84/// A predicate deciding whether a resolved file path should get an immutable cache
85/// policy; see [`Server::with_immutable_assets`].
86type ImmutablePredicate = Arc<dyn Fn(&Path) -> bool + Send + Sync>;
87
88/// A static file server for serving files securely from a root directory.
89///
90/// `Server` canonicalizes the root directory once at creation time and uses the
91/// canonical form for all subsequent requests, avoiding repeated filesystem calls.
92///
93/// # Security
94///
95/// The server protects against:
96/// - Path traversal attacks (e.g., `../../etc/passwd`)
97/// - Accessing files outside the root via symlinks
98/// - Disclosing filesystem structure (traversal and missing files both return 404)
99///
100/// # Cloning
101///
102/// `Server` is cheap to clone: a `PathBuf`, a couple of primitives, and an `Arc`'d
103/// predicate closure. Multiple clones can be used concurrently in async tasks without
104/// synchronization overhead.
105///
106/// # Example
107///
108/// ```no_run
109/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
110/// use mini_static::Server;
111/// use std::path::Path;
112/// use std::time::Duration;
113///
114/// let server = Server::new(Path::new("./public"))?;
115/// let (port, _handle) = server.run(Duration::from_secs(30)).await?;
116/// println!("Server running on port {}", port);
117/// # Ok(())
118/// # }
119/// ```
120#[derive(Clone)]
121pub struct Server {
122    root_canon: PathBuf,
123    max_connections: usize,
124    live_reload: bool,
125    broadcaster: Option<Broadcaster>,
126    immutable_predicate: Option<ImmutablePredicate>,
127    minify_cache: Option<Arc<MinifyCache>>,
128    bundle_css: bool,
129}
130
131impl Server {
132    /// Create a new server with the given root directory.
133    ///
134    /// Canonicalizes the root once at startup. All subsequent requests use the
135    /// canonical root without re-canonicalizing it, making this suitable for long-lived servers.
136    ///
137    /// # Errors
138    ///
139    /// Returns `Err(StaticError::Io)` if the root cannot be canonicalized (e.g., doesn't exist,
140    /// no read permissions).
141    pub fn new(root: &Path) -> Result<Self, StaticError> {
142        let root_canon = root.canonicalize().map_err(StaticError::Io)?;
143        Ok(Server {
144            root_canon,
145            max_connections: DEFAULT_MAX_CONNECTIONS,
146            live_reload: false,
147            broadcaster: None,
148            immutable_predicate: None,
149            minify_cache: None,
150            bundle_css: false,
151        })
152    }
153
154    /// Set the maximum number of connections served concurrently (default 1024).
155    ///
156    /// Once this many connections are in flight, `run()`'s accept loop stops accepting
157    /// new ones — without pausing the accept loop, a client that opens a connection and
158    /// sends nothing (see the header-read timeout docs on [`Server::run_on`]) could
159    /// otherwise be used, in enough parallel copies, to exhaust the process's file
160    /// descriptors or memory with no bound at all.
161    pub fn with_max_connections(mut self, max: usize) -> Self {
162        self.max_connections = max;
163        self
164    }
165
166    /// Enable live-reload for this server (disabled by default).
167    ///
168    /// Once enabled, the `run*` methods start a background watcher (mtime polling,
169    /// bounded 500ms interval — see [`crate::start_watching`]) over the server's root
170    /// the first time the server actually starts accepting connections, and:
171    ///
172    /// - serve a live-reload SSE stream at [`crate::LIVE_RELOAD_PATH`], broadcasting a
173    ///   change event (with [`crate::ChangeType`]) whenever a served file is added,
174    ///   modified, or removed;
175    /// - inject a small `<script>` into every served `text/html` response that connects
176    ///   to that stream and reloads the page (or hot-swaps stylesheet `<link>`s, for CSS
177    ///   changes) — no manual client wiring required.
178    ///
179    /// This is meant for local development, not production: leave it disabled (the
180    /// default) for any server serving real traffic. A typical call site gates it behind
181    /// `#[cfg(debug_assertions)]` so a release build never pays for the watcher or the
182    /// injected script.
183    ///
184    /// # Example
185    ///
186    /// ```no_run
187    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
188    /// use mini_static::Server;
189    /// use std::path::Path;
190    ///
191    /// let server = Server::new(Path::new("./public"))?;
192    /// #[cfg(debug_assertions)]
193    /// let server = server.with_live_reload();
194    /// # Ok(())
195    /// # }
196    /// ```
197    pub fn with_live_reload(mut self) -> Self {
198        self.live_reload = true;
199        self
200    }
201
202    /// Serve files matching `predicate` with a long-lived, immutable cache policy
203    /// instead of the default `Cache-Control: no-cache`.
204    ///
205    /// `predicate` is evaluated against each resolved file's path; a match sends
206    /// `Cache-Control: public, max-age=31536000, immutable` on that file's 200 and 304
207    /// responses. This is correct only for fingerprinted assets (e.g.
208    /// `main.a1b2c3.js`) where a content change always produces a new filename —
209    /// caching a mutable filename indefinitely would serve stale content to every
210    /// client that already has it cached.
211    ///
212    /// # Example
213    ///
214    /// ```no_run
215    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
216    /// use mini_static::Server;
217    /// use std::path::Path;
218    ///
219    /// let server = Server::new(Path::new("./public"))?
220    ///     .with_immutable_assets(|path| {
221    ///         path.file_name()
222    ///             .and_then(|name| name.to_str())
223    ///             .is_some_and(|name| name.contains(".fingerprint."))
224    ///     });
225    /// # Ok(())
226    /// # }
227    /// ```
228    pub fn with_immutable_assets<F>(mut self, predicate: F) -> Self
229    where
230        F: Fn(&Path) -> bool + Send + Sync + 'static,
231    {
232        self.immutable_predicate = Some(Arc::new(predicate));
233        self
234    }
235
236    /// The `Cache-Control` header value for a resolved file path: the immutable policy
237    /// if `with_immutable_assets`'s predicate matches, `no-cache` otherwise.
238    fn cache_control_for(&self, path: &Path) -> &'static str {
239        match &self.immutable_predicate {
240            Some(predicate) if predicate(path) => "public, max-age=31536000, immutable",
241            _ => "no-cache",
242        }
243    }
244
245    /// Enable in-memory CSS/JS minification for this server (disabled by default).
246    ///
247    /// A `.css`/`.js`/`.mjs` response is minified at most once per source mtime: a hit
248    /// serves cached bytes, a miss reads and minifies the file and caches the result
249    /// (see [`crate::minify`]). Files matching `*.min.css`/`*.min.js`
250    /// are served as-is — minifying already-minified input is wasted work at best and
251    /// a correctness risk at worst. If a precompressed sidecar (see
252    /// [`Server::run_on`]'s docs) matches the request, its bytes are served directly
253    /// and minification is skipped, since a sidecar already represents whatever a
254    /// build step decided the final bytes should be. A file that fails to minify (rare
255    /// malformed CSS/JS) is served unminified rather than failing the request.
256    ///
257    /// When `with_live_reload()` is also enabled, the cache drops an entry as soon as the
258    /// same file-change event that drives live-reload arrives, instead of only noticing the
259    /// change reactively on that file's next request.
260    ///
261    /// # Example
262    ///
263    /// ```no_run
264    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
265    /// use mini_static::Server;
266    /// use std::path::Path;
267    ///
268    /// let server = Server::new(Path::new("./public"))?.with_minify();
269    /// # Ok(())
270    /// # }
271    /// ```
272    pub fn with_minify(mut self) -> Self {
273        self.minify_cache = Some(Arc::new(MinifyCache::new(DEFAULT_MINIFY_CACHE_CAPACITY)));
274        self
275    }
276
277    /// Enable `@import` bundling for CSS files (disabled by default).
278    ///
279    /// Every `.css` response becomes a bundle entry point: `@import` statements are
280    /// resolved and inlined (recursively, within the server root only) before minification.
281    /// A `.css` file with no `@import` statements bundles to itself — identical output to
282    /// plain `with_minify()` alone. Files referenced via `@import` may be outside the web root
283    /// (in a `styles/` subdirectory tree) but are still bound by the server's root boundary:
284    /// `@import "../../etc/passwd"` is rejected.
285    ///
286    /// Requires `with_minify()` to also be enabled. If called without it, bundling is silently
287    /// disabled — a no-op for v1 while we validate the feature. A future version may decouple
288    /// bundling from minification.
289    ///
290    /// # Example
291    ///
292    /// ```no_run
293    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
294    /// use mini_static::Server;
295    /// use std::path::Path;
296    ///
297    /// let server = Server::new(Path::new("./public"))?
298    ///     .with_minify()
299    ///     .with_css_bundling();
300    /// # Ok(())
301    /// # }
302    /// ```
303    pub fn with_css_bundling(mut self) -> Self {
304        self.bundle_css = true;
305        self
306    }
307
308    /// Resolve a request path under the server's root.
309    ///
310    /// This is a lower-level API for resolving paths without generating HTTP responses.
311    /// For most use cases, prefer [`Server::handle_request`] or the `run*` methods.
312    ///
313    /// # Returns
314    ///
315    /// - `Ok(PathBuf)` if the path resolves to a file within root.
316    /// - `Err(StaticError)` if the path is invalid, missing, or attempts traversal.
317    pub fn resolve(&self, request_path: &str) -> Result<PathBuf, StaticError> {
318        resolve::resolve_with_canonical_root(&self.root_canon, request_path)
319    }
320
321    /// Run the server on a specific address with a configurable header-read timeout.
322    ///
323    /// Spawns the server in a background Tokio task and returns immediately with the
324    /// assigned port number and a [`ServerHandle`]. Call `handle.shutdown().await` to
325    /// stop accepting new connections and wait for in-flight connections to finish.
326    /// Dropping the handle instead leaves the server running for the life of the process.
327    ///
328    /// # Header-Read Timeout
329    ///
330    /// Connections that don't send complete HTTP headers within `header_timeout` are closed.
331    /// This prevents slowloris attacks and resource exhaustion from incomplete requests. The
332    /// timeout applies only to the header-read phase — once a complete header block has been
333    /// read, the connection is handed off with no further time bound, so long-lived response
334    /// bodies (e.g. the live-reload SSE stream from [`Server::with_live_reload`]) are not cut
335    /// off mid-stream.
336    ///
337    /// # Precompressed Sidecars
338    ///
339    /// If a request's `Accept-Encoding` allows `br` or `gzip` (preferring `br`) and a
340    /// sibling `<path>.br`/`<path>.gz` exists next to the resolved file, its bytes are
341    /// served instead with a matching `Content-Encoding`. Every file response carries
342    /// `Vary: Accept-Encoding` so intermediate caches don't serve the wrong variant to a
343    /// differently-capable client.
344    ///
345    /// # Arguments
346    ///
347    /// * `addr` - Socket address to bind to (e.g., `127.0.0.1:0` for loopback ephemeral,
348    ///   or `0.0.0.0:8080` to bind all interfaces on a fixed port).
349    /// * `header_timeout` - Maximum time to wait for complete HTTP headers on each connection.
350    ///
351    /// # Returns
352    ///
353    /// - `Ok((u16, ServerHandle))` with the assigned port number and a handle for graceful shutdown.
354    /// - `Err(StaticError::Io)` if binding to the socket fails.
355    pub async fn run_on(&self, addr: SocketAddr, header_timeout: Duration) -> Result<(u16, ServerHandle), StaticError> {
356        let listener = TcpListener::bind(addr).await.map_err(StaticError::Io)?;
357        let port = listener.local_addr().map_err(StaticError::Io)?.port();
358
359        let mut server = self.clone();
360        if server.live_reload {
361            let broadcaster = Broadcaster::new();
362            start_watching(Arc::new(server.root_canon.clone()), broadcaster.clone());
363            if let Some(cache) = &server.minify_cache {
364                Arc::clone(cache).subscribe_to_invalidation(&broadcaster);
365            }
366            server.broadcaster = Some(broadcaster);
367        }
368        let semaphore = Arc::new(Semaphore::new(server.max_connections));
369        let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
370
371        let accept_task = tokio::spawn(async move {
372            let mut backoff = ACCEPT_BACKOFF_INITIAL;
373            let mut join_set: tokio::task::JoinSet<()> = tokio::task::JoinSet::new();
374            let mut shutdown_pin = std::pin::pin!(shutdown_rx);
375            let mut shutting_down = false;
376
377            loop {
378                if !shutting_down {
379                    // The accept-and-permit step and the shutdown signal race in a single
380                    // `select!` so shutdown can preempt a pending accept or a permit wait
381                    // cleanly, at any point — not just between loop iterations.
382                    tokio::select! {
383                        accepted = accept_and_permit(&listener, &mut backoff, &semaphore) => {
384                            match accepted {
385                                Some((stream, permit)) => {
386                                    let server = server.clone();
387                                    join_set.spawn(async move {
388                                        let _permit = permit;
389                                        serve_connection(stream, server, header_timeout).await;
390                                    });
391                                }
392                                None => shutting_down = true,
393                            }
394                        }
395                        _ = shutdown_pin.as_mut() => {
396                            shutting_down = true;
397                        }
398                    }
399                    continue;
400                }
401
402                // Stop accepting; drain already-spawned connections before returning.
403                match join_set.join_next().await {
404                    Some(_) => continue,
405                    None => break,
406                }
407            }
408        });
409
410        Ok((port, ServerHandle { shutdown_tx: Some(shutdown_tx), accept_task }))
411    }
412
413    /// Run the server on loopback (127.0.0.1), binding an ephemeral port.
414    ///
415    /// Thin wrapper around [`Server::run_on`] — see it for the header-read timeout and
416    /// sidecar semantics, and for what the returned [`ServerHandle`] does.
417    pub async fn run(&self, header_timeout: Duration) -> Result<(u16, ServerHandle), StaticError> {
418        self.run_on(([127, 0, 0, 1], 0).into(), header_timeout).await
419    }
420
421    /// Run the server on all interfaces (0.0.0.0) at `port` (0 for an ephemeral port).
422    ///
423    /// Useful for containerized deployments and reverse-proxy setups. Thin wrapper
424    /// around [`Server::run_on`] — see it for the header-read timeout and sidecar
425    /// semantics, and for what the returned [`ServerHandle`] does.
426    pub async fn run_all(&self, port: u16, header_timeout: Duration) -> Result<(u16, ServerHandle), StaticError> {
427        self.run_on(([0, 0, 0, 0], port).into(), header_timeout).await
428    }
429
430    /// Run the server on loopback with the default 30-second header-read timeout.
431    ///
432    /// The recommended entry point for tests and lightweight services that don't need a
433    /// custom timeout. Thin wrapper around [`Server::run`].
434    ///
435    /// # Example
436    ///
437    /// ```no_run
438    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
439    /// use mini_static::Server;
440    /// use std::path::Path;
441    ///
442    /// let server = Server::new(Path::new("./public"))?;
443    /// let (port, handle) = server.run_ephemeral().await?;
444    /// println!("Server ready on http://127.0.0.1:{}", port);
445    /// handle.shutdown().await;
446    /// # Ok(())
447    /// # }
448    /// ```
449    pub async fn run_ephemeral(&self) -> Result<(u16, ServerHandle), StaticError> {
450        self.run(DEFAULT_HEADER_TIMEOUT).await
451    }
452
453    /// Produce the HTTP response for a request, streaming file bodies to the client.
454    ///
455    /// This is the crate's single request-handling path: the `run*` accept loop calls it,
456    /// and so should any async server embedding `mini-static` as a fallback route (e.g.
457    /// `mini-unified`). It never blocks the calling task — path resolution runs on Tokio's
458    /// blocking-thread pool via `spawn_blocking`, and the file is read via async I/O.
459    ///
460    /// File responses are backed by `FileBody`, which hands hyper one 64 KB chunk at a
461    /// time as `poll_frame` is driven: memory use stays bounded to one chunk per in-flight
462    /// response regardless of file size.
463    ///
464    /// `headers` are the request's headers; `If-None-Match` (304 on a matching ETag) and
465    /// `Accept-Encoding` (precompressed sidecar selection, see [`Server::run_on`]) are the
466    /// ones read today. Only `GET` and `HEAD` are allowed; anything else gets a 405 with an
467    /// `Allow` header. Missing files and traversal attempts both get an identical 404, so a
468    /// response never discloses whether a path exists outside the root.
469    pub async fn handle_request(
470        &self,
471        method: &Method,
472        request_path: &str,
473        headers: &HeaderMap,
474    ) -> Response<ResponseBody> {
475        if method != Method::GET && method != Method::HEAD {
476            return text(
477                response(StatusCode::METHOD_NOT_ALLOWED).header("Allow", "GET, HEAD"),
478                "method not allowed\n",
479            );
480        }
481
482        // Live-reload SSE stream — only reachable when `with_live_reload()` was called
483        // and the server was started via a `run*` method (those are the only paths that
484        // populate `broadcaster`).
485        if *method == Method::GET && request_path == reload::LIVE_RELOAD_PATH {
486            if let Some(broadcaster) = &self.broadcaster {
487                return finish(
488                    response(StatusCode::OK)
489                        .header("Content-Type", "text/event-stream")
490                        .header("Cache-Control", "no-cache")
491                        .header("Connection", "keep-alive")
492                        .body(ResponseBody::Sse(SseBody::new(broadcaster.subscribe()))),
493                );
494            }
495        }
496
497        // `resolve()` does blocking filesystem syscalls (`canonicalize()`, up to two per
498        // request). Running those directly in this `async fn` would block whichever
499        // Tokio worker thread happens to be driving it, stalling every other task
500        // scheduled on that thread for the duration of the syscalls. `spawn_blocking`
501        // moves the work onto Tokio's dedicated blocking thread pool instead.
502        let server = self.clone();
503        let owned_request_path = request_path.to_string();
504        let resolved = tokio::task::spawn_blocking(move || server.resolve(&owned_request_path)).await;
505        let path = match resolved {
506            Err(_) => return internal_error_response(),
507            Ok(Err(e)) => return text(response(StatusCode::NOT_FOUND), format!("{}\n", e.user_message())),
508            Ok(Ok(path)) => path,
509        };
510
511        // A directory served via its `index.html` needs a trailing slash to establish the
512        // correct base for the page's relative links. Compare against the *decoded*
513        // request path so a percent-encoded explicit request for index.html (e.g.
514        // `/docs/index.htm%6c`) is recognized as such instead of producing a redirect to a
515        // still-encoded, broken Location.
516        let decoded_request_path = resolve::decode_request_path(request_path);
517        if path.file_name().is_some_and(|name| name == "index.html")
518            && !decoded_request_path.ends_with('/')
519            && !decoded_request_path.ends_with("index.html")
520        {
521            // `location` is built from the (attacker-controlled) request path; `finish()`
522            // degrades to 400 instead of panicking if it ever contains bytes invalid in a
523            // header value.
524            let location = format!("{}/", request_path.trim_end_matches('/'));
525            return text(
526                response(StatusCode::MOVED_PERMANENTLY).header("Location", location),
527                "moved\n",
528            );
529        }
530
531        let Ok(file) = File::open(&path).await else {
532            return internal_error_response();
533        };
534        let Ok(metadata) = file.metadata().await else {
535            return internal_error_response();
536        };
537
538        let content_type = mime_type_for_path(&path);
539        // Live-reload HTML injection needs the original, uncompressed bytes to splice the
540        // reload script into — never substitute a precompressed sidecar on this path.
541        let html_injection = self.broadcaster.is_some() && content_type.starts_with("text/html");
542
543        let accept_encoding = header_str(headers, "accept-encoding");
544        let sidecar = if html_injection {
545            None
546        } else {
547            select_precompressed_sidecar(&path, accept_encoding).await
548        };
549        let (mut file, metadata, content_encoding) = match sidecar {
550            Some((sidecar_file, sidecar_metadata, encoding)) => (sidecar_file, sidecar_metadata, Some(encoding)),
551            None => (file, metadata, None),
552        };
553
554        // Minification and bundling are skipped for a served precompressed sidecar (already final bytes
555        // from a build step) and for the live-reload HTML injection path (needs the
556        // original text to splice into).
557        let change_type = ChangeType::from_path(&path);
558        let should_minify = self.minify_cache.as_ref().is_some()
559            && content_encoding.is_none()
560            && !html_injection
561            && matches!(change_type, ChangeType::Css | ChangeType::Script)
562            && !minify::is_already_minified(&path);
563
564        let etag = generate_etag(&metadata, if should_minify { "-min" } else { "" });
565        let cache_control = self.cache_control_for(&path);
566
567        if header_str(headers, "if-none-match").is_some_and(|value| is_etag_match(value, &etag)) {
568            return finish(
569                Response::builder()
570                    .status(StatusCode::NOT_MODIFIED)
571                    .header("Cache-Control", cache_control)
572                    .header("Vary", "Accept-Encoding")
573                    .header("ETag", etag)
574                    .body(ResponseBody::Buffered(Full::new(Bytes::new()))),
575            );
576        }
577
578        // `Some` when the served representation differs from the file's raw bytes and had
579        // to be built in memory; `None` means stream the open file as-is. Computed before
580        // the HEAD check below because RFC 9110 requires a HEAD response's headers —
581        // `Content-Length` included — to match what a GET would send, even though the body
582        // itself is dropped.
583        let transformed: Option<Bytes> = if should_minify {
584            let cache = self.minify_cache.as_ref().unwrap();
585            let bundle_enabled = self.bundle_css && matches!(change_type, ChangeType::Css);
586
587            if bundle_enabled {
588                let root_canon = self.root_canon.clone();
589                let entry_path = path.clone();
590                match cache
591                    .get_or_bundle_css(&path, bundle::bundle_and_minify_css(&root_canon, &entry_path))
592                    .await
593                {
594                    Ok(bytes) => Some(bytes),
595                    Err(e) => {
596                        log_bundle_error(&e);
597                        let mut raw = Vec::new();
598                        if file.read_to_end(&mut raw).await.is_err() {
599                            return internal_error_response();
600                        }
601                        Some(Bytes::from(raw))
602                    }
603                }
604            } else {
605                let mtime = metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH);
606                match minified_or_raw(cache, &path, mtime, change_type, &mut file).await {
607                    Ok(bytes) => Some(bytes),
608                    Err(_) => return internal_error_response(),
609                }
610            }
611        } else if html_injection {
612            let mut html = Vec::with_capacity(metadata.len() as usize);
613            if file.read_to_end(&mut html).await.is_err() {
614                return internal_error_response();
615            }
616            reload::inject_reload_script(&mut html);
617            Some(Bytes::from(html))
618        } else {
619            None
620        };
621
622        let file_size = transformed.as_ref().map_or(metadata.len(), |bytes| bytes.len() as u64);
623
624        // HEAD must not return a body (RFC 9110).
625        let body = if *method == Method::HEAD {
626            ResponseBody::Buffered(Full::new(Bytes::new()))
627        } else {
628            match transformed {
629                Some(bytes) => ResponseBody::Buffered(Full::new(bytes)),
630                None => ResponseBody::Streamed(FileBody::new(file)),
631            }
632        };
633
634        let mut builder = response(StatusCode::OK)
635            .header("Content-Type", content_type)
636            .header("Content-Length", file_size.to_string())
637            .header("Cache-Control", cache_control)
638            .header("Vary", "Accept-Encoding")
639            .header("ETag", etag);
640        if let Some(encoding) = content_encoding {
641            builder = builder.header("Content-Encoding", encoding);
642        }
643        finish(builder.body(body))
644    }
645}
646
647/// Minified bytes for `path`, falling back to the file's raw bytes if the source is
648/// malformed — a rare but real possibility (a hand-edited file, a build tool's bug). A
649/// broken minify step shouldn't take down an otherwise-servable file.
650///
651/// # Errors
652///
653/// Returns `Err` only if the fallback read of `file` itself fails.
654async fn minified_or_raw(
655    cache: &MinifyCache,
656    path: &Path,
657    mtime: SystemTime,
658    change_type: ChangeType,
659    file: &mut File,
660) -> Result<Bytes, MinifyError> {
661    if let Ok(minified) = cache.get_or_minify(path, mtime, change_type, minify::minify).await {
662        return Ok(minified);
663    }
664    let mut raw = Vec::new();
665    file.read_to_end(&mut raw).await.map_err(MinifyError::Io)?;
666    Ok(Bytes::from(raw))
667}
668
669/// Ceiling on how many bytes `read_header_prefix` buffers before giving up. Without this,
670/// a client that trickles bytes forever without ever sending the terminating blank line
671/// could grow the buffer without limit — the header-read timeout alone doesn't bound
672/// memory, only wall-clock time, and a sufficiently patient sender could still send
673/// unbounded data before the deadline fires.
674const MAX_HEADER_BYTES: usize = 64 * 1024;
675
676/// Why `read_header_prefix` gave up before seeing a complete header block. Every variant
677/// is a legitimate reason to drop the connection — none is treated specially by the
678/// caller today, but the distinction is worth preserving for anyone debugging this later.
679#[derive(Debug)]
680enum HeaderReadError {
681    /// The client closed the connection (or shut down its write half) before sending a
682    /// complete header block.
683    ConnectionClosed,
684    /// More than `MAX_HEADER_BYTES` were buffered without seeing `\r\n\r\n`.
685    TooLarge,
686    /// The underlying socket read failed. Kept rather than discarded so a future `log`
687    /// feature has the real I/O error to report instead of an opaque unit variant.
688    #[allow(dead_code)]
689    Io(std::io::Error),
690}
691
692/// Reads from `stream` until a complete HTTP header block (`\r\n\r\n`) has been buffered,
693/// returning every byte read so far — which may include bytes past the header block
694/// (request body, or a second pipelined request) if the client sent them in the same
695/// read. Callers pair this with `tokio::time::timeout` to bound how long the header phase
696/// itself may take; this function has no timeout of its own, only the size ceiling in
697/// `MAX_HEADER_BYTES`.
698async fn read_header_prefix(stream: &mut TcpStream) -> Result<Vec<u8>, HeaderReadError> {
699    let mut buf = Vec::new();
700    let mut chunk = [0u8; 4096];
701
702    loop {
703        let n = stream.read(&mut chunk).await.map_err(HeaderReadError::Io)?;
704        if n == 0 {
705            return Err(HeaderReadError::ConnectionClosed);
706        }
707        buf.extend_from_slice(&chunk[..n]);
708
709        if buf.len() > MAX_HEADER_BYTES {
710            return Err(HeaderReadError::TooLarge);
711        }
712        // Only the tail can hold a terminator this read completed: the `n` new bytes plus
713        // the 3 before them. Rescanning the whole buffer every time would make the header
714        // read quadratic in the bytes received.
715        let scan_from = buf.len().saturating_sub(n + 3);
716        if buf[scan_from..].windows(4).any(|w| w == b"\r\n\r\n") {
717            return Ok(buf);
718        }
719    }
720}
721
722/// Wraps an accepted `TcpStream` whose header block has already been drained into
723/// `prefix` (by `read_header_prefix`, ahead of the connection being handed to hyper).
724/// Reads replay `prefix` first, then fall through to the live socket — so hyper sees
725/// exactly the byte stream it would have seen without the pre-read, just sourced from two
726/// buffers back-to-back instead of one continuous one. Writes pass straight through.
727struct PrefixedIo {
728    prefix: Bytes,
729    prefix_pos: usize,
730    inner: TcpStream,
731}
732
733impl PrefixedIo {
734    fn new(prefix: Vec<u8>, inner: TcpStream) -> Self {
735        PrefixedIo {
736            prefix: Bytes::from(prefix),
737            prefix_pos: 0,
738            inner,
739        }
740    }
741}
742
743impl AsyncRead for PrefixedIo {
744    fn poll_read(
745        self: Pin<&mut Self>,
746        cx: &mut Context<'_>,
747        buf: &mut ReadBuf<'_>,
748    ) -> Poll<std::io::Result<()>> {
749        let this = self.get_mut();
750        if this.prefix_pos < this.prefix.len() {
751            let remaining = &this.prefix[this.prefix_pos..];
752            let n = remaining.len().min(buf.remaining());
753            buf.put_slice(&remaining[..n]);
754            this.prefix_pos += n;
755            return Poll::Ready(Ok(()));
756        }
757        Pin::new(&mut this.inner).poll_read(cx, buf)
758    }
759}
760
761impl AsyncWrite for PrefixedIo {
762    fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
763        Pin::new(&mut self.get_mut().inner).poll_write(cx, buf)
764    }
765
766    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
767        Pin::new(&mut self.get_mut().inner).poll_flush(cx)
768    }
769
770    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
771        Pin::new(&mut self.get_mut().inner).poll_shutdown(cx)
772    }
773}
774
775/// Wires an accepted connection up to the hyper HTTP/1 service.
776///
777/// `header_timeout` bounds only the header-read phase (`read_header_prefix`, run before
778/// hyper ever sees the connection). Once a complete header block has been read, the
779/// connection is handed to hyper with no further time bound — deliberately, since a
780/// response body may legitimately outlive `header_timeout` by design (the live-reload SSE
781/// stream is the motivating case: it stays open until a watched file changes, which may
782/// be minutes or hours after the request). Wrapping the whole connection lifetime in
783/// `header_timeout` — the prior implementation — silently truncated exactly that stream
784/// once `header_timeout` elapsed, aborting the response mid-write after headers had
785/// already been sent (the client observes this as a chunked-encoding error, not a clean
786/// close). The connection-count ceiling (`Server::with_max_connections`) is what bounds
787/// resource use from connections held open indefinitely, not this timeout.
788async fn serve_connection(mut stream: TcpStream, server: Server, header_timeout: Duration) {
789    let prefix = match timeout(header_timeout, read_header_prefix(&mut stream)).await {
790        Ok(Ok(prefix)) => prefix,
791        Ok(Err(_)) | Err(_) => return,
792    };
793
794    let io = TokioIo::new(PrefixedIo::new(prefix, stream));
795    let svc = service_fn(move |req: Request<Incoming>| {
796        let server = server.clone();
797        async move {
798            let resp = server
799                .handle_request(req.method(), req.uri().path(), req.headers())
800                .await;
801            Ok::<_, Infallible>(resp)
802        }
803    });
804    let _ = AutoBuilder::new(TokioExecutor::new()).serve_connection(io, svc).await;
805}
806
807/// Default header-read timeout used by [`Server::run_ephemeral`].
808const DEFAULT_HEADER_TIMEOUT: Duration = Duration::from_secs(30);
809
810/// Default grace period `ServerHandle::shutdown()` waits for in-flight connections to
811/// finish on their own before aborting whatever is left. A connection with no
812/// self-imposed end — an open live-reload SSE stream, or any keep-alive connection whose
813/// peer simply never closes it — would otherwise let `shutdown()` hang forever waiting
814/// for it to finish naturally. Every wait in this crate has a stated upper bound;
815/// shutdown is no exception.
816const DEFAULT_SHUTDOWN_DRAIN_TIMEOUT: Duration = Duration::from_secs(5);
817
818/// A handle to a server started by one of the `Server::run*` methods.
819///
820/// Dropping this handle without calling `shutdown()` leaves the server running in the
821/// background for the life of the process. Call `shutdown()` to stop accepting new
822/// connections and wait for already-accepted connections to finish before returning.
823pub struct ServerHandle {
824    shutdown_tx: Option<tokio::sync::oneshot::Sender<()>>,
825    accept_task: tokio::task::JoinHandle<()>,
826}
827
828impl ServerHandle {
829    /// Stop accepting new connections and wait up to `DEFAULT_SHUTDOWN_DRAIN_TIMEOUT`
830    /// (5s) for in-flight connections to finish on their own. Equivalent to
831    /// `shutdown_with_timeout(DEFAULT_SHUTDOWN_DRAIN_TIMEOUT)` — see that method for what
832    /// happens to connections still open once the grace period elapses.
833    pub async fn shutdown(self) {
834        self.shutdown_with_timeout(DEFAULT_SHUTDOWN_DRAIN_TIMEOUT).await;
835    }
836
837    /// Stop accepting new connections and wait up to `drain_timeout` for in-flight
838    /// connections to finish on their own.
839    ///
840    /// Connections still open once `drain_timeout` elapses are aborted rather than
841    /// waited on further: dropping the accept task drops its `JoinSet`, which aborts
842    /// every task still tracked in it (see `tokio::task::JoinSet`'s own drop behavior) —
843    /// which in turn drops each connection's socket, closing it. This is what bounds
844    /// shutdown when a connection has no natural end of its own (the live-reload SSE
845    /// stream is the motivating case: it stays open until a watched file changes, which
846    /// may never happen before the process needs to exit).
847    pub async fn shutdown_with_timeout(mut self, drain_timeout: Duration) {
848        if let Some(tx) = self.shutdown_tx.take() {
849            let _ = tx.send(());
850        }
851        if timeout(drain_timeout, &mut self.accept_task).await.is_err() {
852            self.accept_task.abort();
853        }
854    }
855}
856
857/// Read a request header as a `&str`, or `None` if it's absent or not valid ASCII.
858fn header_str<'h>(headers: &'h HeaderMap, name: &str) -> Option<&'h str> {
859    headers.get(name).and_then(|value| value.to_str().ok())
860}
861
862/// Start a response carrying the baseline security header every response in this crate
863/// sends. The 304 path is the one exception and builds its own — a 304 repeats only the
864/// caching validators, not the full header set.
865fn response(status: StatusCode) -> Builder {
866    Response::builder()
867        .status(status)
868        .header("X-Content-Type-Options", "nosniff")
869}
870
871/// Finish `builder` with a plain-text body. `&'static str` bodies borrow rather than
872/// allocate; `String` bodies (the 404 message) are moved in.
873fn text(builder: Builder, body: impl Into<Bytes>) -> Response<ResponseBody> {
874    finish(builder.body(ResponseBody::Buffered(Full::new(body.into()))))
875}
876
877/// Finishes building a response, degrading to a generic 400 instead of panicking if any
878/// header value turns out to be invalid for use as an HTTP header value.
879///
880/// Every header value that reaches `Response::builder()` in this module is either a
881/// static string or formatted from internal, already-validated data (a byte count, an
882/// mtime, a fixed method list) — none of it can actually fail today. But `.unwrap()`
883/// on that assumption is exactly the kind of thing that turns "can't happen" into a
884/// production panic the day someone adds a header built from new input without
885/// re-deriving that guarantee. Routing every response through this one fallible path
886/// means that mistake fails safe instead of panicking.
887fn finish(built: Result<Response<ResponseBody>, hyper::http::Error>) -> Response<ResponseBody> {
888    built.unwrap_or_else(|_| bad_request_response())
889}
890
891// `internal_error_response()` and `bad_request_response()` are the fallback responses
892// `finish()` itself degrades to — every header and body here is a fixed string with no
893// external input, so `.body(...)` cannot fail. They can't be routed through `finish()`
894// without it degrading to itself on failure.
895fn internal_error_response() -> Response<ResponseBody> {
896    response(StatusCode::INTERNAL_SERVER_ERROR)
897        .body(ResponseBody::Buffered(Full::new(Bytes::from_static(
898            b"internal server error\n",
899        ))))
900        .unwrap()
901}
902
903fn bad_request_response() -> Response<ResponseBody> {
904    response(StatusCode::BAD_REQUEST)
905        .body(ResponseBody::Buffered(Full::new(Bytes::from_static(b"bad request\n"))))
906        .unwrap()
907}
908
909/// Log a bundle error at error level. Traversal/cycle/depth/count violations are
910/// misconfiguration and should be visible in logs even though the response falls back
911/// to raw-file serve (never a 500). Parse/IO failures are rarer and worth logging too.
912fn log_bundle_error(error: &bundle::BundleError) {
913    eprintln!("bundle error: {:?}", error);
914}
915
916/// `Content-Encoding` name and sidecar file extension for each supported precompressed
917/// variant, in preference order — brotli wins when a client accepts both and both
918/// sidecars exist.
919const SIDECAR_ENCODINGS: [(&str, &str); 2] = [("br", ".br"), ("gzip", ".gz")];
920
921/// Whether `accept_encoding` allows `encoding`.
922///
923/// Matches by substring rather than parsing `q`-value weights or the `identity`/`*`
924/// directives — a lighter-weight negotiation than a general HTTP client would need,
925/// sufficient for deciding between two static sidecar files.
926fn accepts_encoding(accept_encoding: Option<&str>, encoding: &str) -> bool {
927    accept_encoding.is_some_and(|header| header.contains(encoding))
928}
929
930/// Look for a precompressed sidecar (`<path>.br` / `<path>.gz`) matching the client's
931/// `Accept-Encoding`, and return its open file, metadata, and encoding name if found.
932///
933/// `path` must already be the fully resolved, canonicalized path `resolve()` produced.
934/// The sidecar path is built by appending an extension to it — never by re-resolving a
935/// modified request path — so this lookup can't become a second traversal surface: any
936/// path this function reads is provably a sibling of a path `resolve()` already cleared.
937async fn select_precompressed_sidecar(
938    path: &Path,
939    accept_encoding: Option<&str>,
940) -> Option<(File, fs::Metadata, &'static str)> {
941    for (encoding, ext) in SIDECAR_ENCODINGS {
942        if !accepts_encoding(accept_encoding, encoding) {
943            continue;
944        }
945        let mut sidecar = path.as_os_str().to_os_string();
946        sidecar.push(ext);
947        let sidecar_path = PathBuf::from(sidecar);
948
949        // Tripwire for the traversal boundary: a sidecar path built by appending a suffix
950        // must stay in the same directory as `path` (which `resolve()` already proved is
951        // inside root). `ext` is always one of the two static literals in
952        // `SIDECAR_ENCODINGS`, never derived from request input, so this can only fire if
953        // a future change starts deriving `sidecar` some other way.
954        debug_assert_eq!(
955            sidecar_path.parent(),
956            path.parent(),
957            "sidecar path must stay in the same directory as the already-resolved path"
958        );
959
960        if let Ok(sidecar_file) = File::open(&sidecar_path).await {
961            if let Ok(sidecar_metadata) = sidecar_file.metadata().await {
962                return Some((sidecar_file, sidecar_metadata, encoding));
963            }
964        }
965    }
966    None
967}
968
969/// Generate an ETag for a file based on modification time and size.
970///
971/// `variant_suffix` distinguishes a served representation that differs from the raw
972/// source bytes without needing to read/transform the file just to compute a tag: pass
973/// `"-min"` when the response will be minified, `""` otherwise. Without this, turning
974/// `with_minify()` on for an already-served, already-cached file wouldn't change its
975/// ETag at all (the source file's size and mtime are unchanged) — a client that cached
976/// the unminified `200` would keep matching on `If-None-Match` and get `304`s forever,
977/// never seeing the now-minified bytes until the source file's mtime actually changes.
978///
979/// Format: `"<size>-<mtime_secs><variant_suffix>"`
980fn generate_etag(metadata: &fs::Metadata, variant_suffix: &str) -> String {
981    let mtime = metadata
982        .modified()
983        .ok()
984        .and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok())
985        .map(|d| d.as_secs())
986        .unwrap_or(0);
987    format!("\"{}-{}{}\"", metadata.len(), mtime, variant_suffix)
988}
989
990/// Determine MIME type from file path extension.
991fn mime_type_for_path(path: &Path) -> &'static str {
992    let ext = path
993        .extension()
994        .and_then(|ext| ext.to_str())
995        .unwrap_or_default()
996        .to_lowercase();
997
998    match ext.as_str() {
999        "html" | "htm" => "text/html; charset=utf-8",
1000        "css" => "text/css; charset=utf-8",
1001        "js" => "application/javascript; charset=utf-8",
1002        "json" => "application/json; charset=utf-8",
1003        "svg" => "image/svg+xml",
1004        "png" => "image/png",
1005        "jpg" | "jpeg" => "image/jpeg",
1006        "gif" => "image/gif",
1007        "webp" => "image/webp",
1008        "ico" => "image/x-icon",
1009        "woff" => "font/woff",
1010        "woff2" => "font/woff2",
1011        "ttf" => "font/ttf",
1012        "md" | "markdown" => "text/markdown; charset=utf-8",
1013        "txt" => "text/plain; charset=utf-8",
1014        "xml" => "application/xml",
1015        "pdf" => "application/pdf",
1016        "zip" => "application/zip",
1017        _ => "application/octet-stream",
1018    }
1019}
1020
1021/// Check if the If-None-Match header matches the current ETag.
1022/// Handles both exact match and wildcard (*) comparison per RFC 9110.
1023fn is_etag_match(if_none_match: &str, etag: &str) -> bool {
1024    if if_none_match == "*" {
1025        return true;
1026    }
1027    if_none_match.split(',').any(|tag| tag.trim() == etag)
1028}
1029
1030#[cfg(test)]
1031mod precompressed_sidecar_tests {
1032    use super::*;
1033
1034    // `select_precompressed_sidecar` only ever appends a static extension literal
1035    // (".br"/".gz") to the `path` it's given — it never re-joins against `root` or
1036    // re-parses a request-path string, so it structurally cannot become a second
1037    // traversal surface the way re-running `resolve()` on modified input could. This
1038    // test locks that in by construction: the sidecar it finds must live in exactly
1039    // the same directory as the resolved file, for every encoding preference branch.
1040    #[tokio::test]
1041    async fn sidecar_never_leaves_the_resolved_files_directory() {
1042        let root = tempfile::TempDir::new().unwrap();
1043        let sub = root.path().join("assets");
1044        fs::create_dir(&sub).unwrap();
1045        let resolved = sub.join("app.js");
1046        fs::write(&resolved, b"plain").unwrap();
1047        fs::write(sub.join("app.js.br"), b"brotli-bytes").unwrap();
1048        fs::write(sub.join("app.js.gz"), b"gzip-bytes").unwrap();
1049
1050        let (_, _, encoding) = select_precompressed_sidecar(&resolved, Some("br, gzip"))
1051            .await
1052            .expect("both sidecars present, br should be preferred");
1053        assert_eq!(encoding, "br", "br must be preferred over gzip when both are accepted");
1054
1055        let (_, _, encoding) = select_precompressed_sidecar(&resolved, Some("gzip"))
1056            .await
1057            .expect("gzip sidecar present");
1058        assert_eq!(encoding, "gzip");
1059
1060        assert!(
1061            select_precompressed_sidecar(&resolved, None).await.is_none(),
1062            "no Accept-Encoding header should never select a sidecar"
1063        );
1064    }
1065
1066    #[test]
1067    fn accepts_encoding_matches_only_listed_directives() {
1068        assert!(!accepts_encoding(None, "br"));
1069        assert!(!accepts_encoding(Some("identity"), "br"));
1070        assert!(!accepts_encoding(Some("identity"), "gzip"));
1071        assert!(accepts_encoding(Some("gzip, br"), "br"));
1072        assert!(accepts_encoding(Some("gzip"), "gzip"));
1073        assert!(!accepts_encoding(Some("gzip"), "br"));
1074    }
1075}
1076
1077#[cfg(test)]
1078mod file_body_tests {
1079    use super::*;
1080    use crate::handler::FILE_CHUNK_SIZE;
1081    use http_body_util::BodyExt;
1082
1083    // Disproves the prior implementation, which read every chunk into a `Vec` and
1084    // only wrapped the whole result in a single `Full` frame at the end — that
1085    // implementation would fail this test with `frame_count == 1` and
1086    // `max_frame_len == file size`, regardless of `FILE_CHUNK_SIZE`.
1087    #[tokio::test]
1088    async fn file_body_yields_multiple_bounded_chunks_not_one_buffered_frame() {
1089        let dir = tempfile::TempDir::new().unwrap();
1090        let path = dir.path().join("big.bin");
1091        let content = vec![7u8; FILE_CHUNK_SIZE * 3 + 12_345];
1092        fs::write(&path, &content).unwrap();
1093
1094        let file = File::open(&path).await.unwrap();
1095        let mut body = FileBody::new(file);
1096
1097        let mut frame_count = 0usize;
1098        let mut max_frame_len = 0usize;
1099        let mut reassembled = Vec::new();
1100
1101        while let Some(frame) = body.frame().await {
1102            let frame = frame.unwrap();
1103            let data = frame.into_data().unwrap();
1104            frame_count += 1;
1105            max_frame_len = max_frame_len.max(data.len());
1106            reassembled.extend_from_slice(&data);
1107        }
1108
1109        assert!(
1110            frame_count > 1,
1111            "expected the file to be delivered as multiple frames, got {frame_count}"
1112        );
1113        assert!(
1114            max_frame_len <= FILE_CHUNK_SIZE,
1115            "no single frame should exceed the chunk size ({FILE_CHUNK_SIZE}), got {max_frame_len}"
1116        );
1117        assert_eq!(reassembled, content, "reassembled chunks must match original file content exactly");
1118    }
1119}
1120
1121#[cfg(test)]
1122mod accept_tests {
1123    use super::*;
1124    use std::sync::atomic::{AtomicUsize, Ordering};
1125    use std::sync::Mutex;
1126
1127    /// Fails `accept()` a fixed number of times, recording the (paused, virtual)
1128    /// instant of each attempt, before delegating to a real listener so the caller can
1129    /// eventually succeed.
1130    struct FlakyListener {
1131        inner: TcpListener,
1132        remaining_failures: AtomicUsize,
1133        attempts: Mutex<Vec<tokio::time::Instant>>,
1134    }
1135
1136    impl TcpAccept for FlakyListener {
1137        async fn accept(&self) -> std::io::Result<(TcpStream, SocketAddr)> {
1138            self.attempts.lock().unwrap().push(tokio::time::Instant::now());
1139            if self.remaining_failures.fetch_sub(1, Ordering::SeqCst) > 0 {
1140                Err(std::io::Error::other("simulated accept error"))
1141            } else {
1142                TcpAccept::accept(&self.inner).await
1143            }
1144        }
1145    }
1146
1147    // Disproves the prior implementation, which broke out of the accept loop entirely
1148    // on the first `accept()` error — permanently ending the server. This test would
1149    // also fail against a naive `continue`-only fix (no backoff): the recorded gaps
1150    // between attempts would collapse to ~0 (a busy spin) instead of the expected
1151    // exponentially growing delays.
1152    #[tokio::test(start_paused = true)]
1153    async fn accept_loop_backs_off_between_repeated_errors_instead_of_busy_spinning() {
1154        let inner = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
1155        let addr = inner.local_addr().unwrap();
1156
1157        let flaky = FlakyListener {
1158            inner,
1159            remaining_failures: AtomicUsize::new(5),
1160            attempts: Mutex::new(Vec::new()),
1161        };
1162
1163        tokio::spawn(async move {
1164            let _ = TcpStream::connect(addr).await;
1165        });
1166
1167        let semaphore = Arc::new(Semaphore::new(1));
1168        let mut backoff = ACCEPT_BACKOFF_INITIAL;
1169        let result = accept_and_permit(&flaky, &mut backoff, &semaphore).await;
1170        assert!(result.is_some(), "accept should eventually succeed once the flaky listener stops failing");
1171
1172        let recorded = flaky.attempts.lock().unwrap();
1173        assert_eq!(recorded.len(), 6, "5 failures then 1 success");
1174
1175        let expected_gaps = [
1176            ACCEPT_BACKOFF_INITIAL,
1177            ACCEPT_BACKOFF_INITIAL * 2,
1178            ACCEPT_BACKOFF_INITIAL * 4,
1179            ACCEPT_BACKOFF_INITIAL * 8,
1180            ACCEPT_BACKOFF_INITIAL * 16,
1181        ];
1182        for (i, expected) in expected_gaps.iter().enumerate() {
1183            let gap = recorded[i + 1] - recorded[i];
1184            assert_eq!(
1185                gap, *expected,
1186                "gap between attempt {i} and {} should reflect the backoff delay, not a busy spin",
1187                i + 1
1188            );
1189        }
1190
1191        // The delay must stop doubling at the cap rather than growing without bound.
1192        let mut capped = ACCEPT_BACKOFF_MAX;
1193        capped = (capped * 2).min(ACCEPT_BACKOFF_MAX);
1194        assert_eq!(capped, ACCEPT_BACKOFF_MAX);
1195    }
1196
1197    // A successful accept must clear the accumulated delay, so an isolated error later
1198    // on doesn't inherit a second-long wait from an unrelated earlier failure.
1199    #[tokio::test(start_paused = true)]
1200    async fn a_successful_accept_resets_the_backoff() {
1201        let inner = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
1202        let addr = inner.local_addr().unwrap();
1203        let flaky = FlakyListener {
1204            inner,
1205            remaining_failures: AtomicUsize::new(3),
1206            attempts: Mutex::new(Vec::new()),
1207        };
1208        tokio::spawn(async move {
1209            let _ = TcpStream::connect(addr).await;
1210        });
1211
1212        let semaphore = Arc::new(Semaphore::new(1));
1213        let mut backoff = ACCEPT_BACKOFF_INITIAL * 32;
1214        accept_and_permit(&flaky, &mut backoff, &semaphore).await;
1215
1216        assert_eq!(
1217            backoff, ACCEPT_BACKOFF_INITIAL,
1218            "the delay must return to its initial value once an accept succeeds"
1219        );
1220    }
1221}
1222
1223#[cfg(test)]
1224mod finish_tests {
1225    use super::*;
1226
1227    // Disproves a bare `.unwrap()` on the same builder: CR/LF is not a legal header
1228    // value byte (it would enable header/response splitting), so this construction is
1229    // guaranteed to make `.body(...)` return `Err`. Every real call site in this module
1230    // only ever builds header values from static strings or internally-formatted
1231    // numbers, so this test can't happen through normal use — it exists to prove
1232    // `finish()`'s fallback path actually works, not to exercise a reachable case.
1233    #[test]
1234    fn finish_degrades_to_400_on_invalid_header_value_instead_of_panicking() {
1235        let built = Response::builder()
1236            .status(StatusCode::OK)
1237            .header("X-Test", "invalid\r\nvalue")
1238            .body(ResponseBody::Buffered(Full::new(Bytes::new())));
1239        assert!(built.is_err(), "CR/LF in a header value should be rejected by the builder");
1240
1241        let response = finish(built);
1242        assert_eq!(
1243            response.status(),
1244            StatusCode::BAD_REQUEST,
1245            "finish() should degrade to 400 rather than panicking on an invalid header value"
1246        );
1247    }
1248}
1249
1250#[cfg(test)]
1251mod header_prefix_tests {
1252    use super::*;
1253    use tokio::io::AsyncWriteExt;
1254
1255    /// Binds an ephemeral listener, connects a client to it, and returns both ends —
1256    /// `(server_side, client_side)` — so a test can drive `read_header_prefix` against a
1257    /// real socket without a full `Server`/`serve_connection` in the loop.
1258    async fn connected_pair() -> (TcpStream, TcpStream) {
1259        let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
1260        let addr = listener.local_addr().unwrap();
1261        let client = TcpStream::connect(addr).await.unwrap();
1262        let (server_side, _) = listener.accept().await.unwrap();
1263        (server_side, client)
1264    }
1265
1266    #[tokio::test]
1267    async fn reads_exactly_up_to_and_including_the_terminating_blank_line() {
1268        let (mut server_side, mut client) = connected_pair().await;
1269
1270        client
1271            .write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
1272            .await
1273            .unwrap();
1274
1275        let prefix = read_header_prefix(&mut server_side).await.unwrap_or_else(|_| {
1276            panic!("expected a complete header block to be read");
1277        });
1278
1279        assert_eq!(prefix, b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
1280    }
1281
1282    // Disproves a version that only inspects the newest chunk for `\r\n\r\n`: writing the
1283    // blank line in a separate write (and thus, almost always, a separate read) after the
1284    // rest of the headers would make that version wait forever, since the terminator
1285    // never appears within a single chunk. Also pins the tail-only scan in
1286    // `read_header_prefix` — a terminator straddling two reads must still be seen.
1287    #[tokio::test]
1288    async fn assembles_a_header_block_split_across_multiple_writes() {
1289        let (mut server_side, mut client) = connected_pair().await;
1290
1291        client.write_all(b"GET /page HTTP/1.1\r\nHost: localhost\r").await.unwrap();
1292        client.write_all(b"\n\r\n").await.unwrap();
1293
1294        let prefix = read_header_prefix(&mut server_side).await.unwrap_or_else(|_| {
1295            panic!("expected a complete header block to be read across multiple writes");
1296        });
1297
1298        assert_eq!(prefix, b"GET /page HTTP/1.1\r\nHost: localhost\r\n\r\n");
1299    }
1300
1301    // Bytes past the header block (a pipelined second request, here) must be preserved
1302    // verbatim in the returned prefix — `PrefixedIo` depends on this to replay them to
1303    // hyper untouched.
1304    #[tokio::test]
1305    async fn preserves_bytes_sent_past_the_header_block() {
1306        let (mut server_side, mut client) = connected_pair().await;
1307
1308        let first = b"GET /a HTTP/1.1\r\nHost: localhost\r\n\r\n";
1309        let second = b"GET /b HTTP/1.1\r\nHost: localhost\r\n\r\n";
1310        let mut sent = Vec::new();
1311        sent.extend_from_slice(first);
1312        sent.extend_from_slice(second);
1313        client.write_all(&sent).await.unwrap();
1314
1315        let prefix = read_header_prefix(&mut server_side).await.unwrap_or_else(|_| {
1316            panic!("expected a complete header block to be read");
1317        });
1318
1319        assert_eq!(&prefix, &sent, "pipelined bytes past the first header block must survive intact");
1320    }
1321
1322    #[tokio::test]
1323    async fn errors_with_connection_closed_when_client_disconnects_before_headers_complete() {
1324        let (mut server_side, client) = connected_pair().await;
1325        drop(client);
1326
1327        match read_header_prefix(&mut server_side).await {
1328            Err(HeaderReadError::ConnectionClosed) => {}
1329            Err(_) => panic!("expected ConnectionClosed, got a different error variant"),
1330            Ok(_) => panic!("expected an error, got a complete header block from a closed connection"),
1331        }
1332    }
1333
1334    // Disproves an unbounded buffer: without the `MAX_HEADER_BYTES` check, this would
1335    // hang consuming memory forever instead of erroring, since the client never sends the
1336    // terminating blank line.
1337    #[tokio::test]
1338    async fn errors_with_too_large_once_max_header_bytes_is_exceeded_without_a_terminator() {
1339        let (mut server_side, mut client) = connected_pair().await;
1340
1341        let garbage = vec![b'a'; MAX_HEADER_BYTES + 1];
1342        client.write_all(&garbage).await.unwrap();
1343
1344        match read_header_prefix(&mut server_side).await {
1345            Err(HeaderReadError::TooLarge) => {}
1346            Err(_) => panic!("expected TooLarge, got a different error variant"),
1347            Ok(_) => panic!("expected an error, got a complete header block from unterminated garbage"),
1348        }
1349    }
1350
1351    #[tokio::test]
1352    async fn prefixed_io_replays_the_prefix_before_reading_from_the_live_socket() {
1353        let (server_side, mut client) = connected_pair().await;
1354        let mut io = PrefixedIo::new(b"buffered-prefix".to_vec(), server_side);
1355
1356        client.write_all(b"-live-bytes").await.unwrap();
1357
1358        let mut collected = Vec::new();
1359        let mut chunk = [0u8; 8];
1360        while collected.len() < b"buffered-prefix-live-bytes".len() {
1361            let n = io.read(&mut chunk).await.unwrap();
1362            assert!(n > 0, "read returned 0 before all expected bytes arrived");
1363            collected.extend_from_slice(&chunk[..n]);
1364        }
1365
1366        assert_eq!(collected, b"buffered-prefix-live-bytes");
1367    }
1368}