Make '&Host' a request guard.

This commit is contained in:
Sergio Benitez 2021-07-02 06:57:43 -07:00
parent c58b43700c
commit 2cee4b4594
1 changed files with 18 additions and 1 deletions

View File

@ -5,7 +5,8 @@ use crate::{Request, Route};
use crate::outcome::{self, IntoOutcome};
use crate::outcome::Outcome::*;
use crate::http::{Status, ContentType, Accept, Method, CookieJar, uri::Origin};
use crate::http::{Status, ContentType, Accept, Method, CookieJar};
use crate::http::uri::{Host, Origin};
/// Type alias for the `Outcome` of a `FromRequest` conversion.
pub type Outcome<S, E> = outcome::Outcome<S, (Status, E), ()>;
@ -129,6 +130,10 @@ impl<S, E> IntoOutcome<S, (Status, E), ()> for Result<S, E> {
///
/// _This implementation always returns successfully._
///
/// * **&Host**
///
/// Extracts the [`Host`] from the incoming request.
///
/// * **&Route**
///
/// Extracts the [`Route`] from the request if one is available. If a route
@ -401,6 +406,18 @@ impl<'r> FromRequest<'r> for &'r Origin<'r> {
}
}
#[crate::async_trait]
impl<'r> FromRequest<'r> for &'r Host<'r> {
type Error = std::convert::Infallible;
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
match request.host() {
Some(host) => Success(host),
None => Forward(())
}
}
}
#[crate::async_trait]
impl<'r> FromRequest<'r> for &'r Route {
type Error = std::convert::Infallible;