From 2cee4b459492136d616e5863c54754b135e41572 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Fri, 2 Jul 2021 06:57:43 -0700 Subject: [PATCH] Make '&Host' a request guard. --- core/lib/src/request/from_request.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/core/lib/src/request/from_request.rs b/core/lib/src/request/from_request.rs index 29f25e9a..ce2cd519 100644 --- a/core/lib/src/request/from_request.rs +++ b/core/lib/src/request/from_request.rs @@ -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 = outcome::Outcome; @@ -129,6 +130,10 @@ impl IntoOutcome for Result { /// /// _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 { + match request.host() { + Some(host) => Success(host), + None => Forward(()) + } + } +} + #[crate::async_trait] impl<'r> FromRequest<'r> for &'r Route { type Error = std::convert::Infallible;