From 59777d7c47de86e7c5968d3eacf4c28afcd1eea9 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Tue, 21 Apr 2020 08:29:35 +0200 Subject: [PATCH] refactor: empty response body on HEAD requests --- src/error_page.rs | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/error_page.rs b/src/error_page.rs index 34c1aa7..832533e 100644 --- a/src/error_page.rs +++ b/src/error_page.rs @@ -1,4 +1,6 @@ +use iron::headers::ContentLength; use iron::mime; +use iron::modifiers::Header; use iron::prelude::*; use iron::status; use iron::AfterMiddleware; @@ -37,36 +39,37 @@ impl ErrorPage { } impl AfterMiddleware for ErrorPage { - fn after(&self, _: &mut Request, res: Response) -> IronResult { + fn after(&self, req: &mut Request, resp: Response) -> IronResult { let content_type = "text/html".parse::().unwrap(); - - match res.status { - Some(status::NotFound) => Ok(Response::with(( - content_type, - status::NotFound, - self.page404.as_str(), - ))), - Some(status::InternalServerError) => Ok(Response::with(( + let mut resp = match resp.status { + Some(status::NotFound) => { + Response::with((content_type, status::NotFound, self.page404.as_str())) + } + Some(status::InternalServerError) => Response::with(( content_type, status::InternalServerError, self.page50x.as_str(), - ))), - Some(status::BadGateway) => Ok(Response::with(( - content_type, - status::BadGateway, - self.page50x.as_str(), - ))), - Some(status::ServiceUnavailable) => Ok(Response::with(( + )), + Some(status::BadGateway) => { + Response::with((content_type, status::BadGateway, self.page50x.as_str())) + } + Some(status::ServiceUnavailable) => Response::with(( content_type, status::ServiceUnavailable, self.page50x.as_str(), - ))), - Some(status::GatewayTimeout) => Ok(Response::with(( - content_type, - status::GatewayTimeout, - self.page50x.as_str(), - ))), - _ => Ok(res), + )), + Some(status::GatewayTimeout) => { + Response::with((content_type, status::GatewayTimeout, self.page50x.as_str())) + } + _ => resp, + }; + + // Empty response body on HEAD requests + if req.method == iron::method::Head { + resp.set_mut(Vec::new()); + resp.set_mut(Header(ContentLength(0))); } + + Ok(resp) } } -- libgit2 1.7.2