Support HEAD requests (resolves #292) (#363)

* Support HEAD requests

* Remove body from error responses too
This commit is contained in:
Kot C 2025-02-02 20:49:46 -06:00 committed by GitHub
parent 5c1e15c359
commit 9e47bc37c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -238,8 +238,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;
@ -251,17 +257,21 @@ 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)
}
Err(msg) => new_boilerplate(def_headers, req_headers, 500, Body::from(msg)).await,
Err(msg) => new_boilerplate(def_headers, req_headers, 500, if is_head { Body::empty() } else { Body::from(msg) }).await,
}
}
.boxed()
}
// If there was a routing error
Err(e) => new_boilerplate(def_headers, req_headers, 404, e.into()).boxed(),
Err(e) => new_boilerplate(def_headers, req_headers, 404, if is_head { Body::empty() } else { e.into() }).boxed(),
}
}))
}