2016-04-06 10:26:43 +00:00
|
|
|
use handler::Handler;
|
|
|
|
use codegen::StaticCatchInfo;
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use term_painter::ToStyle;
|
|
|
|
use term_painter::Color::*;
|
|
|
|
|
|
|
|
pub struct Catcher {
|
|
|
|
pub code: u16,
|
|
|
|
pub handler: Handler,
|
2016-04-06 20:50:02 +00:00
|
|
|
is_default: bool
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Catcher {
|
|
|
|
pub fn new(code: u16, handler: Handler) -> Catcher {
|
2016-04-06 20:50:02 +00:00
|
|
|
Catcher::new_with_default(code, handler, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_with_default(code: u16, handler: Handler, default: bool) -> Catcher {
|
2016-04-06 10:26:43 +00:00
|
|
|
Catcher {
|
|
|
|
code: code,
|
|
|
|
handler: handler,
|
2016-04-06 20:50:02 +00:00
|
|
|
is_default: default
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-06 20:50:02 +00:00
|
|
|
|
|
|
|
pub fn is_default(&self) -> bool {
|
|
|
|
self.is_default
|
|
|
|
}
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a StaticCatchInfo> for Catcher {
|
|
|
|
fn from(info: &'a StaticCatchInfo) -> Catcher {
|
|
|
|
Catcher::new(info.code, info.handler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Catcher {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2016-04-06 20:50:02 +00:00
|
|
|
write!(f, "{}", Blue.paint(&self.code))
|
2016-04-06 10:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-06 20:50:02 +00:00
|
|
|
|
|
|
|
pub mod defaults {
|
|
|
|
use request::Request;
|
|
|
|
use response::Response;
|
|
|
|
use super::Catcher;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
pub fn get() -> HashMap<u16, Catcher> {
|
|
|
|
let mut map = HashMap::new();
|
|
|
|
map.insert(404, Catcher::new_with_default(404, not_found, true));
|
|
|
|
map.insert(500, Catcher::new_with_default(500, internal_error, true));
|
|
|
|
map
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn not_found(_request: Request) -> Response {
|
|
|
|
// FIXME: Need a way to pass in the status code.
|
|
|
|
Response::new("\
|
|
|
|
<head>\
|
|
|
|
<meta charset=\"utf-8\">\
|
|
|
|
<title>404: Not Found</title>\
|
|
|
|
</head>\
|
|
|
|
<body align=\"center\">\
|
|
|
|
<h1>404: Not Found</h1>\
|
|
|
|
<p>The page you were looking for could not be found.<p>\
|
|
|
|
<hr />\
|
|
|
|
<small>Rocket</small>\
|
|
|
|
</body>\
|
|
|
|
")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn internal_error(_request: Request) -> Response {
|
|
|
|
// FIXME: Need a way to pass in the status code.
|
|
|
|
Response::new("\
|
|
|
|
<head>\
|
|
|
|
<meta charset=\"utf-8\">\
|
|
|
|
<title>404: Not Found</title>\
|
|
|
|
</head>\
|
|
|
|
<body align=\"center\">\
|
|
|
|
<h1>500: Internal Server Error</h1>\
|
|
|
|
<p>The server encountered a problem processing your request.<p>\
|
|
|
|
<hr />\
|
|
|
|
<small>Rocket</small>\
|
|
|
|
")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|