diff --git a/src/router.rs b/src/router.rs index c6f228fb8..5f075f339 100644 --- a/src/router.rs +++ b/src/router.rs @@ -25,9 +25,14 @@ impl Router { path: &'a str, method: &http::Method, ) -> Option<(&'a BoxedEndpoint, 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 Resource { self.method(http::Method::GET, ep) } + /// Add an endpoint for `HEAD` requests + pub fn head, U>(&mut self, ep: T) { + self.method(http::Method::HEAD, ep) + } + /// Add an endpoint for `PUT` requests pub fn put, U>(&mut self, ep: T) { self.method(http::Method::PUT, ep)