Skip to content

Commit 0894aa1

Browse files
DeltaManiacaturon
authored andcommitted
#23 Handle HEAD requests (#31)
Handle HTTP HEAD and fall back to HTTP GET implementation if HEAD implementation is not present
1 parent 2995b44 commit 0894aa1

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/router.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ impl<Data> Router<Data> {
2525
path: &'a str,
2626
method: &http::Method,
2727
) -> Option<(&'a BoxedEndpoint<Data>, RouteMatch<'a>)> {
28-
self.table
29-
.route(path)
30-
.and_then(|(r, p)| Some((r.endpoints.get(method)?, p)))
28+
let (route, route_match) = self.table.route(path)?;
29+
// If it is a HTTP HEAD request then check if there is a callback in the endpoints map
30+
// if not then fallback to the behavior of HTTP GET else proceed as usual
31+
if method == http::Method::HEAD && !route.endpoints.contains_key(&http::Method::HEAD) {
32+
Some((route.endpoints.get(&http::Method::GET)?, route_match))
33+
} else {
34+
Some((route.endpoints.get(method)?, route_match))
35+
}
3136
}
3237
}
3338

@@ -62,6 +67,11 @@ impl<Data> Resource<Data> {
6267
self.method(http::Method::GET, ep)
6368
}
6469

70+
/// Add an endpoint for `HEAD` requests
71+
pub fn head<T: Endpoint<Data, U>, U>(&mut self, ep: T) {
72+
self.method(http::Method::HEAD, ep)
73+
}
74+
6575
/// Add an endpoint for `PUT` requests
6676
pub fn put<T: Endpoint<Data, U>, U>(&mut self, ep: T) {
6777
self.method(http::Method::PUT, ep)

0 commit comments

Comments
 (0)