From 034263eebfb7272cdeb8f5981e9bb909b9c3074a Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Tue, 9 Jun 2020 01:51:43 +0200 Subject: [PATCH] fix: wrong default content type for 404/50x error pages resolves #20 --- 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 = "

404

Content could not found

"; const PAGE_50X: &str = "

50x

SERVICE is temporarily unavailable due an unexpected error

"; -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 { let mut no_status_error = false; + let content_type = "text/html" + .parse::() + .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::().unwrap(); + + assert_eq!( + content_type.0, + "text/html".parse::().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::() .expect("Unable to create a default content type header"); // CORS support -- libgit2 1.7.2