2016-12-15 08:47:31 +00:00
|
|
|
use response::{Response, Responder};
|
|
|
|
use http::hyper::header;
|
|
|
|
use http::Status;
|
2016-03-28 09:34:09 +00:00
|
|
|
|
2016-11-03 16:05:41 +00:00
|
|
|
/// An empty redirect response to a given URL.
|
|
|
|
///
|
|
|
|
/// This type simplifies returning a redirect response to the client.
|
2016-03-28 09:34:09 +00:00
|
|
|
#[derive(Debug)]
|
2016-12-15 08:47:31 +00:00
|
|
|
pub struct Redirect(Status, String);
|
2016-03-28 09:34:09 +00:00
|
|
|
|
|
|
|
impl Redirect {
|
2016-11-03 16:05:41 +00:00
|
|
|
/// Construct a temporary "see other" (303) redirect response. This is the
|
|
|
|
/// typical response when redirecting a user to another page. This type of
|
|
|
|
/// redirect indicates that the client should look elsewhere, but always via
|
|
|
|
/// a `GET` request, for a given resource.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::response::Redirect;
|
|
|
|
///
|
|
|
|
/// let redirect = Redirect::to("/other_url");
|
|
|
|
/// ```
|
2016-03-28 09:34:09 +00:00
|
|
|
pub fn to(uri: &str) -> Redirect {
|
2016-12-15 08:47:31 +00:00
|
|
|
Redirect(Status::SeeOther, String::from(uri))
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 16:05:41 +00:00
|
|
|
/// Construct a "temporary" (307) redirect response. This response instructs
|
|
|
|
/// the client to reissue the current request to a different URL,
|
|
|
|
/// maintaining the contents of the request identically. This means that,
|
|
|
|
/// for example, a `POST` request will be resent, contents included, to the
|
|
|
|
/// requested URL.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::response::Redirect;
|
|
|
|
///
|
|
|
|
/// let redirect = Redirect::temporary("/other_url");
|
|
|
|
/// ```
|
|
|
|
pub fn temporary(uri: &str) -> Redirect {
|
2016-12-15 08:47:31 +00:00
|
|
|
Redirect(Status::TemporaryRedirect, String::from(uri))
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 16:05:41 +00:00
|
|
|
/// Construct a "permanent" (308) redirect response. This redirect must only
|
|
|
|
/// be used for permanent redirects as it is cached by clients. This
|
|
|
|
/// response instructs the client to reissue requests tot he current URL to
|
|
|
|
/// a different URL, now and in the future, maintaining the contents of the
|
|
|
|
/// request identically. This means that, for example, a `POST` request will
|
|
|
|
/// be resent, contents included, to the requested URL.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::response::Redirect;
|
|
|
|
///
|
|
|
|
/// let redirect = Redirect::permanent("/other_url");
|
|
|
|
/// ```
|
2016-03-28 09:34:09 +00:00
|
|
|
pub fn permanent(uri: &str) -> Redirect {
|
2016-12-15 08:47:31 +00:00
|
|
|
Redirect(Status::PermanentRedirect, String::from(uri))
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
2016-08-02 02:07:36 +00:00
|
|
|
|
2016-11-03 16:05:41 +00:00
|
|
|
/// Construct a temporary "found" (302) redirect response. This response
|
|
|
|
/// instructs the client to reissue the current request to a different URL,
|
|
|
|
/// ideally maintaining the contents of the request identically.
|
|
|
|
/// Unfortunately, different clients may respond differently to this type of
|
|
|
|
/// redirect, so `303` or `307` redirects, which disambiguate, are
|
|
|
|
/// preferred.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::response::Redirect;
|
|
|
|
///
|
|
|
|
/// let redirect = Redirect::found("/other_url");
|
|
|
|
/// ```
|
|
|
|
pub fn found(uri: &str) -> Redirect {
|
2016-12-15 08:47:31 +00:00
|
|
|
Redirect(Status::Found, String::from(uri))
|
2016-11-03 16:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a permanent "moved" (301) redirect response. This response
|
|
|
|
/// should only be used for permanent redirects as it can be cached by
|
|
|
|
/// browsers. Because different clients may respond differently to this type
|
|
|
|
/// of redirect, a `308` redirect, which disambiguates, is preferred.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::response::Redirect;
|
|
|
|
///
|
|
|
|
/// let redirect = Redirect::moved("/other_url");
|
|
|
|
/// ```
|
|
|
|
pub fn moved(uri: &str) -> Redirect {
|
2016-12-15 08:47:31 +00:00
|
|
|
Redirect(Status::MovedPermanently, String::from(uri))
|
2016-08-02 02:07:36 +00:00
|
|
|
}
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 16:05:41 +00:00
|
|
|
/// Constructs a response with the appropriate status code and the given URL in
|
|
|
|
/// the `Location` header field. The body of the response is empty. This
|
|
|
|
/// responder does not fail.
|
2016-12-15 08:47:31 +00:00
|
|
|
impl Responder<'static> for Redirect {
|
|
|
|
fn respond(self) -> Result<Response<'static>, Status> {
|
|
|
|
Response::build()
|
|
|
|
.status(self.0)
|
|
|
|
.header(header::ContentLength(0))
|
|
|
|
.header(header::Location(self.1.clone()))
|
|
|
|
.ok()
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|