Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@ pub struct Wiki {
paths: Vec<PathBuf>,
}

fn get_http_error_as_html(status: iron::status::Status) -> (iron::mime::Mime, iron::status::Status, &'static str) {

match status {
status::NotFound =>
(ContentType::html().0, status, "
<html>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay since we want to have it hard coded what about the usage of include_str!?

<head><title>404 Not Found</title></head>
<body>
<h1>Not found</h1>
<p>The requested page was not found on this server.</p>
</body>
</html>
"),

status::InternalServerError => (ContentType::html().0, status, "
<html>
<head><title>500 Internal server error</title></head>
<body>
<h1>Internal server error</h1>
</body>
</html>
"),

_ => (ContentType::html().0, status, "")
}
}

impl Wiki {
/// Create a new `Wiki` instance
pub fn new() -> Self {
Expand Down Expand Up @@ -168,18 +195,18 @@ impl Wiki {
}

if !path.exists() {
return Ok(Response::with(status::InternalServerError));
return Ok(Response::with(get_http_error_as_html(status::NotFound)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the function(in_function(in_function(...))) calls we ca add a new Trait to iron::status::Status which returns our HTML in case on an error.

}

let mut f = match File::open(path) {
Ok(v) => v,
_ => return Ok(Response::with(status::NotFound)),
_ => return Ok(Response::with(get_http_error_as_html(status::NotFound))),
};

let mut buffer = String::new();
match f.read_to_string(&mut buffer) {
Ok(v) => v,
_ => return Ok(Response::with(status::InternalServerError)),
_ => return Ok(Response::with(get_http_error_as_html(status::InternalServerError))),
};

/* Content type needs to be determined from the file rather than assuming html */
Expand Down