From b09e948423ec9f5089c2329a191a363682e2668d Mon Sep 17 00:00:00 2001 From: Kot Date: Mon, 27 Jan 2025 18:25:59 -0600 Subject: [PATCH] Support HEAD requests --- src/server.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/server.rs b/src/server.rs index 15c56ad..7ba64a3 100644 --- a/src/server.rs +++ b/src/server.rs @@ -240,8 +240,14 @@ impl Server { path.pop(); } + // Replace HEAD with GET for routing + let (method, is_head) = match req.method() { + &Method::HEAD => (&Method::GET, true), + method => (method, false), + }; + // Match the visited path with an added route - match router.recognize(&format!("/{}{}", req.method().as_str(), path)) { + match router.recognize(&format!("/{}{}", method.as_str(), path)) { // If a route was configured for this path Ok(found) => { let mut parammed = req; @@ -253,7 +259,11 @@ impl Server { match func.await { Ok(mut res) => { res.headers_mut().extend(def_headers); - let _ = compress_response(&req_headers, &mut res).await; + if is_head { + *res.body_mut() = Body::empty(); + } else { + let _ = compress_response(&req_headers, &mut res).await; + } Ok(res) }