From 013be3c6b8facf5b2d7efbbaf1727f0a375b509a Mon Sep 17 00:00:00 2001 From: DeltaManiac Date: Thu, 8 Nov 2018 21:25:18 +0530 Subject: [PATCH 1/7] handle HTTP HEAD --- src/router.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/router.rs b/src/router.rs index de4ce6550..a04f7cabc 100644 --- a/src/router.rs +++ b/src/router.rs @@ -64,6 +64,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) From aeba74d4f5dfe8b6d53e7d68d511553710354441 Mon Sep 17 00:00:00 2001 From: DeltaManiac Date: Fri, 9 Nov 2018 20:16:53 +0530 Subject: [PATCH 2/7] Fallback to HTTP GET implementation if HEAD implementaion is not present --- src/router.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/router.rs b/src/router.rs index a04f7cabc..03a690ffb 100644 --- a/src/router.rs +++ b/src/router.rs @@ -25,9 +25,18 @@ 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))) + // 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 && + !self.table.route(path).unwrap().0.endpoints.contains_key(&http::Method::HEAD){ + self.table + .route(path) + .and_then(|(r, p)| Some((r.endpoints.get(&http::Method::GET)?, p))) + } else { + self.table + .route(path) + .and_then(|(r, p)| Some((r.endpoints.get(method)?, p))) + } } } From 07b0dfb05eb4bf3cb446ed7ea89176d840653db5 Mon Sep 17 00:00:00 2001 From: DeltaManiac Date: Fri, 9 Nov 2018 20:22:05 +0530 Subject: [PATCH 3/7] Rustfmt --- src/router.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/router.rs b/src/router.rs index 03a690ffb..48a8dedc9 100644 --- a/src/router.rs +++ b/src/router.rs @@ -27,16 +27,23 @@ impl Router { ) -> Option<(&'a BoxedEndpoint, RouteMatch<'a>)> { // 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 && - !self.table.route(path).unwrap().0.endpoints.contains_key(&http::Method::HEAD){ - self.table - .route(path) - .and_then(|(r, p)| Some((r.endpoints.get(&http::Method::GET)?, p))) - } else { - self.table - .route(path) - .and_then(|(r, p)| Some((r.endpoints.get(method)?, p))) - } + if method == http::Method::HEAD + && !self + .table + .route(path) + .unwrap() + .0 + .endpoints + .contains_key(&http::Method::HEAD) + { + self.table + .route(path) + .and_then(|(r, p)| Some((r.endpoints.get(&http::Method::GET)?, p))) + } else { + self.table + .route(path) + .and_then(|(r, p)| Some((r.endpoints.get(method)?, p))) + } } } From 6e4ba702bd0db72a3d20e21c2748ec5fcec4f799 Mon Sep 17 00:00:00 2001 From: DeltaManiac Date: Sun, 11 Nov 2018 11:24:10 +0530 Subject: [PATCH 4/7] Refactor --- src/router.rs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/router.rs b/src/router.rs index 48a8dedc9..6becad726 100644 --- a/src/router.rs +++ b/src/router.rs @@ -25,24 +25,13 @@ impl Router { path: &'a str, method: &http::Method, ) -> Option<(&'a BoxedEndpoint, RouteMatch<'a>)> { + let route = self.table.route(path).unwrap(); // 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 - && !self - .table - .route(path) - .unwrap() - .0 - .endpoints - .contains_key(&http::Method::HEAD) - { - self.table - .route(path) - .and_then(|(r, p)| Some((r.endpoints.get(&http::Method::GET)?, p))) + if method == http::Method::HEAD && !route.0.endpoints.contains_key(&http::Method::HEAD) { + Some((route.0.endpoints.get(&http::Method::GET)?, route.1)) } else { - self.table - .route(path) - .and_then(|(r, p)| Some((r.endpoints.get(method)?, p))) + Some((route.0.endpoints.get(method)?, route.1)) } } } From a3c6549ada591174d65ad02097870a0d74d9518d Mon Sep 17 00:00:00 2001 From: DeltaManiac Date: Mon, 12 Nov 2018 05:51:28 +0530 Subject: [PATCH 5/7] changed unwrap to try --- src/router.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/router.rs b/src/router.rs index 84ba86b26..98afb2dbc 100644 --- a/src/router.rs +++ b/src/router.rs @@ -25,7 +25,7 @@ impl Router { path: &'a str, method: &http::Method, ) -> Option<(&'a BoxedEndpoint, RouteMatch<'a>)> { - let route = self.table.route(path).unwrap(); + let route = 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.0.endpoints.contains_key(&http::Method::HEAD) { From a61617d90e381636b33ce39a5c8dd52c2b7bfced Mon Sep 17 00:00:00 2001 From: DeltaManiac Date: Tue, 13 Nov 2018 15:54:18 +0530 Subject: [PATCH 6/7] Readability Enhancements --- src/router.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/router.rs b/src/router.rs index 98afb2dbc..b02119db9 100644 --- a/src/router.rs +++ b/src/router.rs @@ -25,13 +25,13 @@ impl Router { path: &'a str, method: &http::Method, ) -> Option<(&'a BoxedEndpoint, RouteMatch<'a>)> { - let route = self.table.route(path)?; + 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.0.endpoints.contains_key(&http::Method::HEAD) { - Some((route.0.endpoints.get(&http::Method::GET)?, route.1)) + if method == http::Method::HEAD && !route.endpoints.contains_key(&http::Method::HEAD) { + Some((route.endpoints.get(&http::Method::GET)?, route_match)) } else { - Some((route.0.endpoints.get(method)?, route.1)) + Some((route.endpoints.get(method)?, route_match)) } } } From e25e1d2ded3a4df1bfe2f2af5c1ffe33951729fe Mon Sep 17 00:00:00 2001 From: DeltaManiac Date: Tue, 13 Nov 2018 15:58:43 +0530 Subject: [PATCH 7/7] rustfmt --- src/router.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/router.rs b/src/router.rs index b02119db9..5f075f339 100644 --- a/src/router.rs +++ b/src/router.rs @@ -25,7 +25,7 @@ impl Router { path: &'a str, method: &http::Method, ) -> Option<(&'a BoxedEndpoint, RouteMatch<'a>)> { - let (route,route_match) = self.table.route(path)?; + 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) {