Skip to content

Commit

Permalink
Support HEAD requests (resolves #292) (#363)
Browse files Browse the repository at this point in the history
* Support HEAD requests

* Remove body from error responses too
  • Loading branch information
kotx authored Feb 3, 2025
1 parent 5c1e15c commit 9e47bc3
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
}
}))
}
Expand Down

0 comments on commit 9e47bc3

Please sign in to comment.