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(-)
@@ -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";
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;
@@ -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()
);
}
}
@@ -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");