Use 'Uri' instead of 'String' in 'Redirect'.

This commit is contained in:
Sergio Benitez 2017-09-15 04:00:50 -07:00
parent cced491ec9
commit 32eeae1733
3 changed files with 15 additions and 15 deletions

View File

@ -9,7 +9,7 @@ use rocket::response::Redirect;
#[get("/")] #[get("/")]
fn root() -> Redirect { fn root() -> Redirect {
Redirect::to("/login") Redirect::to(uri!(login))
} }
#[get("/login")] #[get("/login")]

View File

@ -144,7 +144,7 @@ use self::priv_encode_set::PATH_ENCODE_SET;
/// ///
/// #[get("/name/<name>")] /// #[get("/name/<name>")]
/// fn redirector(name: Name) -> Redirect { /// fn redirector(name: Name) -> Redirect {
/// Redirect::to(uri!(real: name).as_str()) /// Redirect::to(uri!(real: name))
/// } /// }
/// ///
/// #[get("/<name>")] /// #[get("/<name>")]

View File

@ -1,13 +1,13 @@
use request::Request; use request::Request;
use response::{Response, Responder}; use response::{Response, Responder};
use http::hyper::header; use http::uri::Uri;
use http::Status; use http::Status;
/// An empty redirect response to a given URL. /// An empty redirect response to a given URL.
/// ///
/// This type simplifies returning a redirect response to the client. /// This type simplifies returning a redirect response to the client.
#[derive(Debug)] #[derive(Debug)]
pub struct Redirect(Status, String); pub struct Redirect(Status, Uri<'static>);
impl Redirect { impl Redirect {
/// Construct a temporary "see other" (303) redirect response. This is the /// Construct a temporary "see other" (303) redirect response. This is the
@ -23,8 +23,8 @@ impl Redirect {
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// let redirect = Redirect::to("/other_url"); /// let redirect = Redirect::to("/other_url");
/// ``` /// ```
pub fn to(uri: &str) -> Redirect { pub fn to<U: Into<Uri<'static>>>(uri: U) -> Redirect {
Redirect(Status::SeeOther, String::from(uri)) Redirect(Status::SeeOther, uri.into())
} }
/// Construct a "temporary" (307) redirect response. This response instructs /// Construct a "temporary" (307) redirect response. This response instructs
@ -41,8 +41,8 @@ impl Redirect {
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// let redirect = Redirect::temporary("/other_url"); /// let redirect = Redirect::temporary("/other_url");
/// ``` /// ```
pub fn temporary(uri: &str) -> Redirect { pub fn temporary<U: Into<Uri<'static>>>(uri: U) -> Redirect {
Redirect(Status::TemporaryRedirect, String::from(uri)) Redirect(Status::TemporaryRedirect, uri.into())
} }
/// Construct a "permanent" (308) redirect response. This redirect must only /// Construct a "permanent" (308) redirect response. This redirect must only
@ -60,8 +60,8 @@ impl Redirect {
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// let redirect = Redirect::permanent("/other_url"); /// let redirect = Redirect::permanent("/other_url");
/// ``` /// ```
pub fn permanent(uri: &str) -> Redirect { pub fn permanent<U: Into<Uri<'static>>>(uri: U) -> Redirect {
Redirect(Status::PermanentRedirect, String::from(uri)) Redirect(Status::PermanentRedirect, uri.into())
} }
/// Construct a temporary "found" (302) redirect response. This response /// Construct a temporary "found" (302) redirect response. This response
@ -79,8 +79,8 @@ impl Redirect {
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// let redirect = Redirect::found("/other_url"); /// let redirect = Redirect::found("/other_url");
/// ``` /// ```
pub fn found(uri: &str) -> Redirect { pub fn found<U: Into<Uri<'static>>>(uri: U) -> Redirect {
Redirect(Status::Found, String::from(uri)) Redirect(Status::Found, uri.into())
} }
/// Construct a permanent "moved" (301) redirect response. This response /// Construct a permanent "moved" (301) redirect response. This response
@ -96,8 +96,8 @@ impl Redirect {
/// # #[allow(unused_variables)] /// # #[allow(unused_variables)]
/// let redirect = Redirect::moved("/other_url"); /// let redirect = Redirect::moved("/other_url");
/// ``` /// ```
pub fn moved(uri: &str) -> Redirect { pub fn moved<U: Into<Uri<'static>>>(uri: U) -> Redirect {
Redirect(Status::MovedPermanently, String::from(uri)) Redirect(Status::MovedPermanently, uri.into())
} }
} }
@ -108,7 +108,7 @@ impl Responder<'static> for Redirect {
fn respond_to(self, _: &Request) -> Result<Response<'static>, Status> { fn respond_to(self, _: &Request) -> Result<Response<'static>, Status> {
Response::build() Response::build()
.status(self.0) .status(self.0)
.header(header::Location(self.1)) .raw_header("Location", self.1.to_string())
.ok() .ok()
} }
} }