index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2020-04-21 6:29:35.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2020-04-21 6:29:35.0 +00:00:00
commit
59777d7c47de86e7c5968d3eacf4c28afcd1eea9 [patch]
tree
cfd26704f21dff5ffcae2cad7e112e12e678c5a0
parent
e40016dfdadfdd8204360efee04ef64eaadf68dc
download
59777d7c47de86e7c5968d3eacf4c28afcd1eea9.tar.gz

refactor: empty response body on HEAD requests



Diff

 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<Response> {
    fn after(&self, req: &mut Request, resp: Response) -> IronResult<Response> {
        let content_type = "text/html".parse::<mime::Mime>().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)
    }
}