index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2020-06-08 23:51:43.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2020-06-08 23:51:43.0 +00:00:00
commit
034263eebfb7272cdeb8f5981e9bb909b9c3074a [patch]
tree
84c48109438d2b45e54dc96bdc9c933a43b89af9
parent
5e670be0f96e035d4e114a2df429e9c214acce3b
download
034263eebfb7272cdeb8f5981e9bb909b9c3074a.tar.gz

fix: wrong default content type for 404/50x error pages

resolves #20

Diff

 src/error_page.rs  | 16 ++++++++++------
 src/main.rs        | 33 ++++++++++++++++++++++++++++++++-
 src/staticfiles.rs |  3 ++-
 3 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/src/error_page.rs b/src/error_page.rs
index 4c05c6e..1b15877 100644
--- a/src/error_page.rs
+++ b/src/error_page.rs
@@ -1,3 +1,4 @@
use iron::mime;
use iron::prelude::*;
use iron::status;
use iron::AfterMiddleware;
@@ -7,7 +8,6 @@ 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 {
@@ -40,25 +40,29 @@ impl AfterMiddleware for ErrorPage {
    fn after(&self, req: &mut Request, resp: Response) -> IronResult<Response> {
        let mut no_status_error = false;

        let content_type = "text/html"
            .parse::<mime::Mime>()
            .expect("Unable to create a default content type header");

        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;
diff --git a/src/main.rs b/src/main.rs
index d82fbc9..e4485f0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -80,7 +80,7 @@ mod test {
    use self::hyper::header::Headers;
    use self::iron_test::{request, response};
    use self::tempdir::TempDir;
    use iron::headers::ContentLength;
    use iron::headers::{ContentLength, ContentType};
    use iron::status;

    struct TestFilesystemSetup(TempDir);
@@ -245,4 +245,35 @@ mod test {

        assert_eq!(response.status, Some(status::MethodNotAllowed));
    }

    #[test]
    fn staticfile_valid_content_type_for_404() {
        let root = TestFilesystemSetup::new();
        root.dir("root");

        let assets = TestFilesystemSetup::new();
        assets.dir("assets");

        let opts = Options::from_args();

        let files = StaticFiles::new(StaticFilesOptions {
            root_dir: root.path().to_str().unwrap().to_string(),
            assets_dir: assets.path().to_str().unwrap().to_string(),
            page_50x_path: opts.page50x,
            page_404_path: opts.page404,
            cors_allow_origins: "".to_string(),
        });

        let res = request::head("http://127.0.0.1/unknown", Headers::new(), &files.handle())
            .expect("Response was a http error");

        assert_eq!(res.status, Some(status::NotFound));

        let content_type = res.headers.get::<ContentType>().unwrap();

        assert_eq!(
            content_type.0,
            "text/html".parse::<iron::mime::Mime>().unwrap()
        );
    }
}
diff --git a/src/staticfiles.rs b/src/staticfiles.rs
index ba32bfc..7649ac8 100644
--- a/src/staticfiles.rs
+++ b/src/staticfiles.rs
@@ -3,6 +3,7 @@ use crate::gzip::GzipMiddleware;
use crate::helpers;
use crate::logger::{log_server, Logger};

use iron::mime;
use iron::prelude::*;
use iron_cors::CorsMiddleware;
use iron_staticfile_middleware::{Cache, GuessContentType, ModifyWith, Prefix, Staticfile};
@@ -64,7 +65,7 @@ impl StaticFiles {
        let one_day = Duration::new(60 * 60 * 24, 0);
        let one_year = Duration::new(60 * 60 * 24 * 365, 0);
        let default_content_type = "text/html"
            .parse()
            .parse::<mime::Mime>()
            .expect("Unable to create a default content type header");

        // CORS support