Handle HEAD requests
Handle  HTTP HEAD and fall back to HTTP GET implementation if HEAD implementation is not present
@@ -25,9 +25,14 @@ impl<Data> Router<Data> {
path: &'a str,
method: &http::Method,
) -> Option<(&'a BoxedEndpoint<Data>, RouteMatch<'a>)> {
- self.table
- .route(path)
- .and_then(|(r, p)| Some((r.endpoints.get(method)?, p)))
+ let (route, route_match) = self.table.route(path)?;
+ // If it is a HTTP HEAD request then check if there is a callback in the endpoints map
+ // if not then fallback to the behavior of HTTP GET else proceed as usual
+ if method == http::Method::HEAD && !route.endpoints.contains_key(&http::Method::HEAD) {
+ Some((route.endpoints.get(&http::Method::GET)?, route_match))
+ } else {
+ Some((route.endpoints.get(method)?, route_match))
+ }
}
}
@@ -62,6 +67,11 @@ impl<Data> Resource<Data> {
self.method(http::Method::GET, ep)
}
+ /// Add an endpoint for `HEAD` requests
+ pub fn head<T: Endpoint<Data, U>, U>(&mut self, ep: T) {
+ self.method(http::Method::HEAD, ep)
+ }
+
/// Add an endpoint for `PUT` requests
pub fn put<T: Endpoint<Data, U>, U>(&mut self, ep: T) {
self.method(http::Method::PUT, ep)
