2016-10-08 02:09:05 +00:00
|
|
|
use response::{ResponseOutcome, Outcome, Responder};
|
2016-10-04 00:09:13 +00:00
|
|
|
use http::hyper::{header, FreshHyperResponse, StatusCode};
|
2016-03-28 09:34:09 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Redirect(StatusCode, String);
|
|
|
|
|
|
|
|
impl Redirect {
|
|
|
|
pub fn to(uri: &str) -> Redirect {
|
2016-08-02 02:07:36 +00:00
|
|
|
Redirect(StatusCode::Found, String::from(uri))
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn created(uri: &str) -> Redirect {
|
|
|
|
Redirect(StatusCode::Created, String::from(uri))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn other(uri: &str) -> Redirect {
|
|
|
|
Redirect(StatusCode::SeeOther, String::from(uri))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn permanent(uri: &str) -> Redirect {
|
|
|
|
Redirect(StatusCode::PermanentRedirect, String::from(uri))
|
|
|
|
}
|
2016-08-02 02:07:36 +00:00
|
|
|
|
|
|
|
pub fn temporary(uri: &str) -> Redirect {
|
|
|
|
Redirect(StatusCode::TemporaryRedirect, String::from(uri))
|
|
|
|
}
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Responder for Redirect {
|
2016-10-08 02:09:05 +00:00
|
|
|
fn respond<'b>(&mut self, mut res: FreshHyperResponse<'b>) -> ResponseOutcome<'b> {
|
2016-03-28 09:34:09 +00:00
|
|
|
res.headers_mut().set(header::ContentLength(0));
|
|
|
|
res.headers_mut().set(header::Location(self.1.clone()));
|
|
|
|
*(res.status_mut()) = self.0;
|
|
|
|
res.send(b"").unwrap();
|
2016-10-08 02:09:05 +00:00
|
|
|
Outcome::Success
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|