index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2020-06-01 22:04:24.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2020-06-01 22:04:24.0 +00:00:00
commit
c0d28918d45f0b523cea50ebc51abb794c334334 [patch]
tree
4e8f0ec07d9d4617dc117d134381f9cd1cda5e2c
parent
044b4a975da6034f3096924a5fe1b195bd9618be
download
c0d28918d45f0b523cea50ebc51abb794c334334.tar.gz

refactor: prefer const for default content type



Diff

 src/error_page.rs | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/src/error_page.rs b/src/error_page.rs
index 82f8608..4c05c6e 100644
--- a/src/error_page.rs
+++ b/src/error_page.rs
@@ -1,4 +1,3 @@
use iron::mime;
use iron::prelude::*;
use iron::status;
use iron::AfterMiddleware;
@@ -8,6 +7,7 @@ use std::path::Path;
const PAGE_404: &str = "<h2>404</h2><p>Content could not found</p>";
const PAGE_50X: &str =
    "<h2>50x</h2><p>SERVICE is temporarily unavailable due an unexpected error</p>";
const CONTENT_TYPE: &str = "text/html";

/// Custom Error pages middleware for Iron
pub struct ErrorPage {
@@ -38,28 +38,27 @@ impl ErrorPage {

impl AfterMiddleware for ErrorPage {
    fn after(&self, req: &mut Request, resp: Response) -> IronResult<Response> {
        let content_type = "text/html".parse::<mime::Mime>().unwrap();
        let mut no_status_error = false;

        let mut resp = match resp.status {
            Some(status::NotFound) => {
                Response::with((content_type, status::NotFound, self.page404.as_str()))
                Response::with((CONTENT_TYPE, status::NotFound, self.page404.as_str()))
            }
            Some(status::InternalServerError) => Response::with((
                content_type,
                CONTENT_TYPE,
                status::InternalServerError,
                self.page50x.as_str(),
            )),
            Some(status::BadGateway) => {
                Response::with((content_type, status::BadGateway, self.page50x.as_str()))
                Response::with((CONTENT_TYPE, status::BadGateway, self.page50x.as_str()))
            }
            Some(status::ServiceUnavailable) => Response::with((
                content_type,
                CONTENT_TYPE,
                status::ServiceUnavailable,
                self.page50x.as_str(),
            )),
            Some(status::GatewayTimeout) => {
                Response::with((content_type, status::GatewayTimeout, self.page50x.as_str()))
                Response::with((CONTENT_TYPE, status::GatewayTimeout, self.page50x.as_str()))
            }
            _ => {
                no_status_error = true;
@@ -69,7 +68,7 @@ impl AfterMiddleware for ErrorPage {

        // Empty response body only on HEAD requests and status error (404,50x)
        if req.method == iron::method::Head && !no_status_error {
            resp.set_mut(Vec::new());
            resp.set_mut(vec![]);
        }

        Ok(resp)