Skip to content

#23 Handle HEAD requests #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

Expand Down Expand Up @@ -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)
Expand Down