use std::convert::Infallible;
use std::fs;
use std::io::Read;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::{Duration, SystemTime};
use bytes::Bytes;
use hyper::{Method, Response, StatusCode, Request};
use hyper::service::service_fn;
use http_body_util::Full;
use hyper::body::Incoming;
use hyper_util::rt::TokioExecutor;
use hyper_util::rt::TokioIo;
use hyper_util::server::conn::auto::Builder as AutoBuilder;
use tokio::fs::File;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, ReadBuf};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use tokio::time::timeout;
use crate::error::StaticError;
use crate::handler::{FileBody, ResponseBody};
use crate::minify;
use crate::minify_cache::{MinifyCache, DEFAULT_MINIFY_CACHE_CAPACITY};
use crate::reload::{self, ChangeType, SseBody};
use crate::resolve;
use crate::watcher::{start_watching, Broadcaster};
const ACCEPT_BACKOFF_INITIAL: Duration = Duration::from_millis(10);
const ACCEPT_BACKOFF_MAX: Duration = Duration::from_secs(1);
trait TcpAccept {
async fn accept(&self) -> std::io::Result<(TcpStream, SocketAddr)>;
}
impl TcpAccept for TcpListener {
async fn accept(&self) -> std::io::Result<(TcpStream, SocketAddr)> {
TcpListener::accept(self).await
}
}
struct Backoff {
delay: Duration,
}
impl Backoff {
fn new() -> Self {
Backoff { delay: ACCEPT_BACKOFF_INITIAL }
}
fn next_delay(&mut self) -> Duration {
let delay = self.delay;
self.delay = (self.delay * 2).min(ACCEPT_BACKOFF_MAX);
delay
}
fn reset(&mut self) {
self.delay = ACCEPT_BACKOFF_INITIAL;
}
}
async fn accept_and_permit<L: TcpAccept>(
listener: &L,
backoff: &mut Backoff,
semaphore: &Arc<Semaphore>,
) -> Option<(TcpStream, OwnedSemaphorePermit)> {
loop {
let stream = match listener.accept().await {
Ok((stream, _)) => {
backoff.reset();
stream
}
Err(_) => {
tokio::time::sleep(backoff.next_delay()).await;
continue;
}
};
return match semaphore.clone().acquire_owned().await {
Ok(permit) => Some((stream, permit)),
Err(_) => None,
};
}
}
type ImmutablePredicate = Arc<dyn Fn(&Path) -> bool + Send + Sync>;
#[derive(Clone)]
pub struct Server {
root_canon: PathBuf,
max_connections: usize,
live_reload: bool,
broadcaster: Option<Broadcaster>,
immutable_predicate: Option<ImmutablePredicate>,
minify_cache: Option<Arc<MinifyCache>>,
}
impl Server {
pub fn new(root: &Path) -> Result<Self, StaticError> {
let root_canon = root.canonicalize().map_err(StaticError::Io)?;
Ok(Server {
root_canon,
max_connections: DEFAULT_MAX_CONNECTIONS,
live_reload: false,
broadcaster: None,
immutable_predicate: None,
minify_cache: None,
})
}
pub fn with_max_connections(mut self, max: usize) -> Self {
self.max_connections = max;
self
}
pub fn with_live_reload(mut self) -> Self {
self.live_reload = true;
self
}
pub fn with_immutable_assets<F>(mut self, predicate: F) -> Self
where
F: Fn(&Path) -> bool + Send + Sync + 'static,
{
self.immutable_predicate = Some(Arc::new(predicate));
self
}
fn cache_control_for(&self, path: &Path) -> &'static str {
match &self.immutable_predicate {
Some(predicate) if predicate(path) => "public, max-age=31536000, immutable",
_ => "no-cache",
}
}
pub fn with_minify(mut self) -> Self {
self.minify_cache = Some(Arc::new(MinifyCache::new(DEFAULT_MINIFY_CACHE_CAPACITY)));
self
}
pub fn resolve(&self, request_path: &str) -> Result<PathBuf, StaticError> {
resolve::resolve_with_canonical_root(&self.root_canon, request_path)
}
pub fn handle_request(&self, request_path: &str) -> Response<ResponseBody> {
self.handle_request_with_method(&Method::GET, request_path)
}
pub fn handle_request_with_method(
&self,
method: &Method,
request_path: &str,
) -> Response<ResponseBody> {
self.handle_request_with_headers(method, request_path, None, None)
}
pub async fn run_on(&self, addr: SocketAddr, header_timeout: Duration) -> Result<(u16, ServerHandle), StaticError> {
let listener = TcpListener::bind(addr)
.await
.map_err(StaticError::Io)?;
let port = listener
.local_addr()
.map_err(StaticError::Io)?
.port();
let mut server = self.clone();
if server.live_reload {
let broadcaster = Broadcaster::new();
start_watching(Arc::new(server.root_canon.clone()), broadcaster.clone());
if let Some(cache) = &server.minify_cache {
Arc::clone(cache).subscribe_to_invalidation(&broadcaster);
}
server.broadcaster = Some(broadcaster);
}
let semaphore = Arc::new(Semaphore::new(server.max_connections));
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
let accept_task = tokio::spawn(async move {
let mut backoff = Backoff::new();
let mut join_set: tokio::task::JoinSet<()> = tokio::task::JoinSet::new();
let mut shutdown_pin = std::pin::pin!(shutdown_rx);
let mut shutting_down = false;
loop {
if !shutting_down {
tokio::select! {
accepted = accept_and_permit(&listener, &mut backoff, &semaphore) => {
match accepted {
Some((stream, permit)) => {
let server = server.clone();
join_set.spawn(async move {
let _permit = permit;
serve_connection(stream, server, header_timeout).await;
});
}
None => shutting_down = true,
}
}
_ = shutdown_pin.as_mut() => {
shutting_down = true;
}
}
continue;
}
match join_set.join_next().await {
Some(_) => continue,
None => break,
}
}
});
Ok((port, ServerHandle { shutdown_tx: Some(shutdown_tx), accept_task }))
}
pub async fn run(&self, header_timeout: Duration) -> Result<(u16, ServerHandle), StaticError> {
let addr: SocketAddr = ([127, 0, 0, 1], 0).into();
self.run_on(addr, header_timeout).await
}
pub async fn run_all(&self, port: u16, header_timeout: Duration) -> Result<(u16, ServerHandle), StaticError> {
let addr: SocketAddr = ([0, 0, 0, 0], port).into();
self.run_on(addr, header_timeout).await
}
pub async fn run_ephemeral(&self) -> Result<(u16, ServerHandle), StaticError> {
self.run(Duration::from_secs(30)).await
}
pub async fn handle_request_async(
&self,
method: &Method,
request_path: &str,
if_none_match: Option<&str>,
if_modified_since: Option<&str>,
accept_encoding: Option<&str>,
) -> Response<ResponseBody> {
if method != Method::GET && method != Method::HEAD {
return finish(Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
.header("Allow", "GET, HEAD")
.header("X-Content-Type-Options", "nosniff")
.body(into_response_body(Full::new(Bytes::from("method not allowed\n")))));
}
if *method == Method::GET && request_path == reload::LIVE_RELOAD_PATH {
if let Some(broadcaster) = &self.broadcaster {
let rx = broadcaster.subscribe();
return finish(Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "text/event-stream")
.header("Cache-Control", "no-cache")
.header("Connection", "keep-alive")
.header("X-Content-Type-Options", "nosniff")
.body(ResponseBody::Sse(SseBody::new(rx))));
}
}
let server = self.clone();
let owned_request_path = request_path.to_string();
let resolved = tokio::task::spawn_blocking(move || server.resolve(&owned_request_path)).await;
let resolved = match resolved {
Ok(r) => r,
Err(_) => return internal_error_response(),
};
match resolved {
Ok(path) => {
let decoded_request_path = resolve::decode_request_path(request_path);
if path.file_name().is_some_and(|name| name == "index.html")
&& !decoded_request_path.ends_with('/')
&& !decoded_request_path.ends_with("index.html")
{
let location = format!("{}/", request_path.trim_end_matches('/'));
return finish(Response::builder()
.status(StatusCode::MOVED_PERMANENTLY)
.header("Location", location)
.header("X-Content-Type-Options", "nosniff")
.body(into_response_body(Full::new(Bytes::from("moved\n")))));
}
let file = match File::open(&path).await {
Ok(f) => f,
Err(_) => return internal_error_response(),
};
let metadata = match file.metadata().await {
Ok(m) => m,
Err(_) => return internal_error_response(),
};
let content_type = mime_type_for_path(&path);
let html_injection = self.broadcaster.is_some() && content_type.starts_with("text/html");
let precompressed = if html_injection {
None
} else {
select_precompressed_sidecar(&path, accept_encoding).await
};
let (file, metadata, content_encoding) = match precompressed {
Some((sidecar_file, sidecar_metadata, encoding)) => {
(sidecar_file, sidecar_metadata, Some(encoding))
}
None => (file, metadata, None),
};
let change_type = ChangeType::from_path(&path);
let should_minify = self.minify_cache.is_some()
&& content_encoding.is_none()
&& !html_injection
&& matches!(change_type, ChangeType::Css | ChangeType::Script)
&& !minify::is_already_minified(&path);
let mut file_size = metadata.len();
let etag = generate_etag(&metadata, if should_minify { "-min" } else { "" });
let cache_control = self.cache_control_for(&path);
if let Some(if_none_match) = if_none_match {
if is_etag_match(if_none_match, &etag) {
return finish(Response::builder()
.status(StatusCode::NOT_MODIFIED)
.header("Cache-Control", cache_control)
.header("Vary", "Accept-Encoding")
.header("ETag", etag)
.body(into_response_body(Full::new(Bytes::new()))));
}
}
if let Some(if_modified_since) = if_modified_since {
if is_not_modified_since(if_modified_since, &metadata) {
return finish(Response::builder()
.status(StatusCode::NOT_MODIFIED)
.header("Cache-Control", cache_control)
.header("Vary", "Accept-Encoding")
.header("ETag", etag)
.body(into_response_body(Full::new(Bytes::new()))));
}
}
let body: ResponseBody = if should_minify {
let cache = self.minify_cache.as_ref().expect("should_minify implies minify_cache is Some");
let mtime = metadata.modified().unwrap_or(SystemTime::UNIX_EPOCH);
let minify_result = cache.get_or_minify(&path, mtime, change_type, minify::minify).await;
match minify_result {
Ok(minified) => {
file_size = minified.len() as u64;
if *method == Method::HEAD {
into_response_body(Full::new(Bytes::new()))
} else {
into_response_body(Full::new(minified))
}
}
Err(_) if *method == Method::HEAD => {
into_response_body(Full::new(Bytes::new()))
}
Err(_) => {
let mut buf = Vec::with_capacity(file_size as usize);
let mut file = file;
if file.read_to_end(&mut buf).await.is_err() {
return internal_error_response();
}
into_response_body(Full::new(Bytes::from(buf)))
}
}
} else if *method == Method::HEAD {
into_response_body(Full::new(Bytes::new()))
} else if html_injection {
let mut html = Vec::with_capacity(file_size as usize);
let mut file = file;
if file.read_to_end(&mut html).await.is_err() {
return internal_error_response();
}
reload::inject_reload_script(&mut html);
file_size = html.len() as u64;
into_response_body(Full::new(Bytes::from(html)))
} else {
ResponseBody::Streamed(FileBody::new(file))
};
let mut response = Response::builder()
.status(StatusCode::OK)
.header("X-Content-Type-Options", "nosniff")
.header("Content-Type", content_type)
.header("Content-Length", file_size.to_string())
.header("Cache-Control", cache_control)
.header("Vary", "Accept-Encoding")
.header("ETag", etag);
if let Some(encoding) = content_encoding {
response = response.header("Content-Encoding", encoding);
}
finish(response.body(body))
}
Err(e) => {
let message = e.user_message();
let body = format!("{}\n", message);
finish(Response::builder()
.status(StatusCode::NOT_FOUND)
.header("X-Content-Type-Options", "nosniff")
.body(into_response_body(Full::new(Bytes::from(body)))))
}
}
}
pub fn handle_request_with_headers(
&self,
method: &Method,
request_path: &str,
_range_header: Option<&str>,
_if_range_header: Option<&str>,
) -> Response<ResponseBody> {
if method != Method::GET && method != Method::HEAD {
return finish(Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
.header("Allow", "GET, HEAD")
.header("X-Content-Type-Options", "nosniff")
.body(into_response_body(Full::new(Bytes::from("method not allowed\n")))));
}
match self.resolve(request_path) {
Ok(path) => {
let decoded_request_path = resolve::decode_request_path(request_path);
if path.file_name().is_some_and(|name| name == "index.html")
&& !decoded_request_path.ends_with('/')
&& !decoded_request_path.ends_with("index.html")
{
let location = format!("{}/", request_path.trim_end_matches('/'));
return finish(Response::builder()
.status(StatusCode::MOVED_PERMANENTLY)
.header("Location", location)
.header("X-Content-Type-Options", "nosniff")
.body(into_response_body(Full::new(Bytes::from("moved\n")))));
}
let file = match fs::File::open(&path) {
Ok(f) => f,
Err(_) => return internal_error_response(),
};
let metadata = match file.metadata() {
Ok(m) => m,
Err(_) => return internal_error_response(),
};
let file_size = metadata.len();
let etag = generate_etag(&metadata, "");
let cache_control = self.cache_control_for(&path);
let body_bytes = if *method == Method::HEAD {
Bytes::new()
} else {
let mut buf = Vec::with_capacity(file_size as usize);
let mut file = file;
if file.read_to_end(&mut buf).is_err() {
return internal_error_response();
}
Bytes::from(buf)
};
finish(Response::builder()
.status(StatusCode::OK)
.header("X-Content-Type-Options", "nosniff")
.header("Content-Length", file_size.to_string())
.header("Cache-Control", cache_control)
.header("ETag", etag)
.body(into_response_body(Full::new(body_bytes))))
}
Err(e) => {
let message = e.user_message();
let body = format!("{}\n", message);
finish(Response::builder()
.status(StatusCode::NOT_FOUND)
.header("X-Content-Type-Options", "nosniff")
.body(into_response_body(Full::new(Bytes::from(body)))))
}
}
}
}
const MAX_HEADER_BYTES: usize = 64 * 1024;
#[derive(Debug)]
enum HeaderReadError {
ConnectionClosed,
TooLarge,
#[allow(dead_code)]
Io(std::io::Error),
}
async fn read_header_prefix(stream: &mut TcpStream) -> Result<Vec<u8>, HeaderReadError> {
let mut buf = Vec::new();
let mut chunk = [0u8; 4096];
loop {
let n = stream.read(&mut chunk).await.map_err(HeaderReadError::Io)?;
if n == 0 {
return Err(HeaderReadError::ConnectionClosed);
}
buf.extend_from_slice(&chunk[..n]);
if buf.len() > MAX_HEADER_BYTES {
return Err(HeaderReadError::TooLarge);
}
if buf.windows(4).any(|w| w == b"\r\n\r\n") {
return Ok(buf);
}
}
}
struct PrefixedIo {
prefix: Bytes,
prefix_pos: usize,
inner: TcpStream,
}
impl PrefixedIo {
fn new(prefix: Vec<u8>, inner: TcpStream) -> Self {
PrefixedIo {
prefix: Bytes::from(prefix),
prefix_pos: 0,
inner,
}
}
}
impl AsyncRead for PrefixedIo {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
let this = self.get_mut();
if this.prefix_pos < this.prefix.len() {
let remaining = &this.prefix[this.prefix_pos..];
let n = remaining.len().min(buf.remaining());
buf.put_slice(&remaining[..n]);
this.prefix_pos += n;
return Poll::Ready(Ok(()));
}
Pin::new(&mut this.inner).poll_read(cx, buf)
}
}
impl AsyncWrite for PrefixedIo {
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.get_mut().inner).poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.get_mut().inner).poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.get_mut().inner).poll_shutdown(cx)
}
}
async fn serve_connection(mut stream: TcpStream, server: Server, header_timeout: Duration) {
let prefix = match timeout(header_timeout, read_header_prefix(&mut stream)).await {
Ok(Ok(prefix)) => prefix,
Ok(Err(_)) | Err(_) => return,
};
let io = TokioIo::new(PrefixedIo::new(prefix, stream));
let svc = service_fn(move |req: Request<Incoming>| {
let server = server.clone();
async move {
let method = req.method().clone();
let path = req.uri().path().to_string();
let if_none_match = req.headers().get("if-none-match").and_then(|v| v.to_str().ok());
let if_modified_since = req.headers().get("if-modified-since").and_then(|v| v.to_str().ok());
let accept_encoding = req.headers().get("accept-encoding").and_then(|v| v.to_str().ok());
let resp = server
.handle_request_async(&method, &path, if_none_match, if_modified_since, accept_encoding)
.await;
Ok::<_, Infallible>(resp)
}
});
let _ = AutoBuilder::new(TokioExecutor::new()).serve_connection(io, svc).await;
}
pub struct ServerHandle {
shutdown_tx: Option<tokio::sync::oneshot::Sender<()>>,
accept_task: tokio::task::JoinHandle<()>,
}
const DEFAULT_SHUTDOWN_DRAIN_TIMEOUT: Duration = Duration::from_secs(5);
impl ServerHandle {
pub async fn shutdown(self) {
self.shutdown_with_timeout(DEFAULT_SHUTDOWN_DRAIN_TIMEOUT).await;
}
pub async fn shutdown_with_timeout(mut self, drain_timeout: Duration) {
if let Some(tx) = self.shutdown_tx.take() {
let _ = tx.send(());
}
if timeout(drain_timeout, &mut self.accept_task).await.is_err() {
self.accept_task.abort();
}
}
}
fn into_response_body(body: Full<Bytes>) -> ResponseBody {
ResponseBody::Buffered(body)
}
fn finish(built: Result<Response<ResponseBody>, hyper::http::Error>) -> Response<ResponseBody> {
built.unwrap_or_else(|_| bad_request_response())
}
const DEFAULT_MAX_CONNECTIONS: usize = 1024;
fn internal_error_response() -> Response<ResponseBody> {
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.header("X-Content-Type-Options", "nosniff")
.body(into_response_body(Full::new(Bytes::from(
"internal server error\n",
))))
.unwrap()
}
fn bad_request_response() -> Response<ResponseBody> {
Response::builder()
.status(StatusCode::BAD_REQUEST)
.header("X-Content-Type-Options", "nosniff")
.body(into_response_body(Full::new(Bytes::from("bad request\n"))))
.unwrap()
}
fn preferred_encodings(accept_encoding: Option<&str>) -> Vec<(&'static str, &'static str)> {
let Some(accept_encoding) = accept_encoding else {
return Vec::new();
};
let mut encodings = Vec::new();
if accept_encoding.contains("br") {
encodings.push(("br", ".br"));
}
if accept_encoding.contains("gzip") {
encodings.push(("gzip", ".gz"));
}
encodings
}
async fn select_precompressed_sidecar(
path: &Path,
accept_encoding: Option<&str>,
) -> Option<(File, fs::Metadata, &'static str)> {
for (encoding, ext) in preferred_encodings(accept_encoding) {
let mut sidecar = path.as_os_str().to_os_string();
sidecar.push(ext);
let sidecar_path = PathBuf::from(sidecar);
debug_assert_eq!(
sidecar_path.parent(),
path.parent(),
"sidecar path must stay in the same directory as the already-resolved path"
);
if let Ok(sidecar_file) = File::open(&sidecar_path).await {
if let Ok(sidecar_metadata) = sidecar_file.metadata().await {
return Some((sidecar_file, sidecar_metadata, encoding));
}
}
}
None
}
fn generate_etag(metadata: &fs::Metadata, variant_suffix: &str) -> String {
let size = metadata.len();
let mtime = metadata
.modified()
.ok()
.and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok())
.map(|d| d.as_secs())
.unwrap_or(0);
format!("\"{}-{}{}\"", size, mtime, variant_suffix)
}
fn mime_type_for_path(path: &Path) -> &'static str {
path.extension()
.and_then(|ext| ext.to_str())
.and_then(|ext| match ext.to_lowercase().as_str() {
"html" | "htm" => Some("text/html; charset=utf-8"),
"css" => Some("text/css; charset=utf-8"),
"js" => Some("application/javascript; charset=utf-8"),
"json" => Some("application/json; charset=utf-8"),
"svg" => Some("image/svg+xml"),
"png" => Some("image/png"),
"jpg" | "jpeg" => Some("image/jpeg"),
"gif" => Some("image/gif"),
"webp" => Some("image/webp"),
"ico" => Some("image/x-icon"),
"woff" => Some("font/woff"),
"woff2" => Some("font/woff2"),
"ttf" => Some("font/ttf"),
"md" | "markdown" => Some("text/markdown; charset=utf-8"),
"txt" => Some("text/plain; charset=utf-8"),
"xml" => Some("application/xml"),
"pdf" => Some("application/pdf"),
"zip" => Some("application/zip"),
_ => None,
})
.unwrap_or("application/octet-stream")
}
fn is_etag_match(if_none_match: &str, etag: &str) -> bool {
if if_none_match == "*" {
return true;
}
if_none_match.split(',').any(|tag| tag.trim() == etag)
}
fn is_not_modified_since(if_modified_since: &str, metadata: &fs::Metadata) -> bool {
let file_mtime = metadata
.modified()
.ok()
.and_then(|t| t.duration_since(SystemTime::UNIX_EPOCH).ok())
.map(|d| d.as_secs())
.unwrap_or(0);
if let Ok(client_time) = if_modified_since.parse::<u64>() {
return file_mtime <= client_time;
}
false
}
#[cfg(test)]
mod precompressed_sidecar_tests {
use super::*;
#[tokio::test]
async fn sidecar_never_leaves_the_resolved_files_directory() {
let root = tempfile::TempDir::new().unwrap();
let sub = root.path().join("assets");
fs::create_dir(&sub).unwrap();
let resolved = sub.join("app.js");
fs::write(&resolved, b"plain").unwrap();
fs::write(sub.join("app.js.br"), b"brotli-bytes").unwrap();
fs::write(sub.join("app.js.gz"), b"gzip-bytes").unwrap();
let (_, _, encoding) = select_precompressed_sidecar(&resolved, Some("br, gzip"))
.await
.expect("both sidecars present, br should be preferred");
assert_eq!(encoding, "br", "br must be preferred over gzip when both are accepted");
let (_, _, encoding) = select_precompressed_sidecar(&resolved, Some("gzip"))
.await
.expect("gzip sidecar present");
assert_eq!(encoding, "gzip");
assert!(
select_precompressed_sidecar(&resolved, None).await.is_none(),
"no Accept-Encoding header should never select a sidecar"
);
}
#[test]
fn preferred_encodings_prefers_br_and_ignores_unmatched_directives() {
assert_eq!(preferred_encodings(None), Vec::new());
assert_eq!(preferred_encodings(Some("identity")), Vec::new());
assert_eq!(preferred_encodings(Some("gzip, br")), vec![("br", ".br"), ("gzip", ".gz")]);
assert_eq!(preferred_encodings(Some("gzip")), vec![("gzip", ".gz")]);
}
}
#[cfg(test)]
mod file_body_tests {
use super::*;
use crate::handler::FILE_CHUNK_SIZE;
use http_body_util::BodyExt;
#[tokio::test]
async fn file_body_yields_multiple_bounded_chunks_not_one_buffered_frame() {
let dir = tempfile::TempDir::new().unwrap();
let path = dir.path().join("big.bin");
let content = vec![7u8; FILE_CHUNK_SIZE * 3 + 12_345];
fs::write(&path, &content).unwrap();
let file = File::open(&path).await.unwrap();
let mut body = FileBody::new(file);
let mut frame_count = 0usize;
let mut max_frame_len = 0usize;
let mut reassembled = Vec::new();
while let Some(frame) = body.frame().await {
let frame = frame.unwrap();
let data = frame.into_data().unwrap();
frame_count += 1;
max_frame_len = max_frame_len.max(data.len());
reassembled.extend_from_slice(&data);
}
assert!(
frame_count > 1,
"expected the file to be delivered as multiple frames, got {frame_count}"
);
assert!(
max_frame_len <= FILE_CHUNK_SIZE,
"no single frame should exceed the chunk size ({FILE_CHUNK_SIZE}), got {max_frame_len}"
);
assert_eq!(reassembled, content, "reassembled chunks must match original file content exactly");
}
}
#[cfg(test)]
mod accept_tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
#[test]
fn backoff_doubles_up_to_max() {
let mut backoff = Backoff::new();
let mut last = backoff.next_delay();
assert_eq!(last, ACCEPT_BACKOFF_INITIAL);
for _ in 0..20 {
last = backoff.next_delay();
}
assert_eq!(last, ACCEPT_BACKOFF_MAX);
}
#[test]
fn backoff_reset_returns_to_initial_delay() {
let mut backoff = Backoff::new();
backoff.next_delay();
backoff.next_delay();
backoff.reset();
assert_eq!(backoff.next_delay(), ACCEPT_BACKOFF_INITIAL);
}
struct FlakyListener {
inner: TcpListener,
remaining_failures: AtomicUsize,
attempts: Mutex<Vec<tokio::time::Instant>>,
}
impl TcpAccept for FlakyListener {
async fn accept(&self) -> std::io::Result<(TcpStream, SocketAddr)> {
self.attempts.lock().unwrap().push(tokio::time::Instant::now());
if self.remaining_failures.fetch_sub(1, Ordering::SeqCst) > 0 {
Err(std::io::Error::other("simulated accept error"))
} else {
TcpAccept::accept(&self.inner).await
}
}
}
#[tokio::test(start_paused = true)]
async fn accept_loop_backs_off_between_repeated_errors_instead_of_busy_spinning() {
let inner = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
let addr = inner.local_addr().unwrap();
let flaky = FlakyListener {
inner,
remaining_failures: AtomicUsize::new(5),
attempts: Mutex::new(Vec::new()),
};
tokio::spawn(async move {
let _ = TcpStream::connect(addr).await;
});
let semaphore = Arc::new(Semaphore::new(1));
let mut backoff = Backoff::new();
let result = accept_and_permit(&flaky, &mut backoff, &semaphore).await;
assert!(result.is_some(), "accept should eventually succeed once the flaky listener stops failing");
let recorded = flaky.attempts.lock().unwrap();
assert_eq!(recorded.len(), 6, "5 failures then 1 success");
let expected_gaps = [
ACCEPT_BACKOFF_INITIAL,
ACCEPT_BACKOFF_INITIAL * 2,
ACCEPT_BACKOFF_INITIAL * 4,
ACCEPT_BACKOFF_INITIAL * 8,
ACCEPT_BACKOFF_INITIAL * 16,
];
for (i, expected) in expected_gaps.iter().enumerate() {
let gap = recorded[i + 1] - recorded[i];
assert_eq!(
gap, *expected,
"gap between attempt {i} and {} should reflect the backoff delay, not a busy spin",
i + 1
);
}
}
}
#[cfg(test)]
mod finish_tests {
use super::*;
#[test]
fn finish_degrades_to_400_on_invalid_header_value_instead_of_panicking() {
let built = Response::builder()
.status(StatusCode::OK)
.header("X-Test", "invalid\r\nvalue")
.body(into_response_body(Full::new(Bytes::new())));
assert!(built.is_err(), "CR/LF in a header value should be rejected by the builder");
let response = finish(built);
assert_eq!(
response.status(),
StatusCode::BAD_REQUEST,
"finish() should degrade to 400 rather than panicking on an invalid header value"
);
}
}
#[cfg(test)]
mod header_prefix_tests {
use super::*;
use tokio::io::AsyncWriteExt;
async fn connected_pair() -> (TcpStream, TcpStream) {
let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
let addr = listener.local_addr().unwrap();
let client = TcpStream::connect(addr).await.unwrap();
let (server_side, _) = listener.accept().await.unwrap();
(server_side, client)
}
#[tokio::test]
async fn reads_exactly_up_to_and_including_the_terminating_blank_line() {
let (mut server_side, mut client) = connected_pair().await;
client
.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n")
.await
.unwrap();
let prefix = read_header_prefix(&mut server_side).await.unwrap_or_else(|_| {
panic!("expected a complete header block to be read");
});
assert_eq!(prefix, b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
}
#[tokio::test]
async fn assembles_a_header_block_split_across_multiple_writes() {
let (mut server_side, mut client) = connected_pair().await;
client.write_all(b"GET /page HTTP/1.1\r\n").await.unwrap();
client.write_all(b"Host: localhost\r\n").await.unwrap();
client.write_all(b"\r\n").await.unwrap();
let prefix = read_header_prefix(&mut server_side).await.unwrap_or_else(|_| {
panic!("expected a complete header block to be read across multiple writes");
});
assert_eq!(prefix, b"GET /page HTTP/1.1\r\nHost: localhost\r\n\r\n");
}
#[tokio::test]
async fn preserves_bytes_sent_past_the_header_block() {
let (mut server_side, mut client) = connected_pair().await;
let first = b"GET /a HTTP/1.1\r\nHost: localhost\r\n\r\n";
let second = b"GET /b HTTP/1.1\r\nHost: localhost\r\n\r\n";
let mut sent = Vec::new();
sent.extend_from_slice(first);
sent.extend_from_slice(second);
client.write_all(&sent).await.unwrap();
let prefix = read_header_prefix(&mut server_side).await.unwrap_or_else(|_| {
panic!("expected a complete header block to be read");
});
assert_eq!(&prefix, &sent, "pipelined bytes past the first header block must survive intact");
}
#[tokio::test]
async fn errors_with_connection_closed_when_client_disconnects_before_headers_complete() {
let (mut server_side, client) = connected_pair().await;
drop(client);
match read_header_prefix(&mut server_side).await {
Err(HeaderReadError::ConnectionClosed) => {}
Err(_) => panic!("expected ConnectionClosed, got a different error variant"),
Ok(_) => panic!("expected an error, got a complete header block from a closed connection"),
}
}
#[tokio::test]
async fn errors_with_too_large_once_max_header_bytes_is_exceeded_without_a_terminator() {
let (mut server_side, mut client) = connected_pair().await;
let garbage = vec![b'a'; MAX_HEADER_BYTES + 1];
client.write_all(&garbage).await.unwrap();
match read_header_prefix(&mut server_side).await {
Err(HeaderReadError::TooLarge) => {}
Err(_) => panic!("expected TooLarge, got a different error variant"),
Ok(_) => panic!("expected an error, got a complete header block from unterminated garbage"),
}
}
#[tokio::test]
async fn prefixed_io_replays_the_prefix_before_reading_from_the_live_socket() {
let (server_side, mut client) = connected_pair().await;
let mut io = PrefixedIo::new(b"buffered-prefix".to_vec(), server_side);
client.write_all(b"-live-bytes").await.unwrap();
let mut collected = Vec::new();
let mut chunk = [0u8; 8];
while collected.len() < b"buffered-prefix-live-bytes".len() {
let n = io.read(&mut chunk).await.unwrap();
assert!(n > 0, "read returned 0 before all expected bytes arrived");
collected.extend_from_slice(&chunk[..n]);
}
assert_eq!(collected, b"buffered-prefix-live-bytes");
}
}