Impl 'FromRequest' for 'IpAddr'.

Closes #1414.
This commit is contained in:
George Cheng 2020-09-03 10:03:00 +08:00 committed by Sergio Benitez
parent 080d586a35
commit 0673986c32
1 changed files with 18 additions and 1 deletions

View File

@ -1,5 +1,5 @@
use std::fmt::Debug;
use std::net::SocketAddr;
use std::net::{IpAddr, SocketAddr};
use futures::future::BoxFuture;
@ -153,6 +153,11 @@ impl<S, E> IntoOutcome<S, (Status, E), ()> for Result<S, E> {
/// Extracts the [`ContentType`] from the incoming request. If the request
/// didn't specify a Content-Type, the request is forwarded.
///
/// * **IpAddr**
///
/// Extracts the client ip address of the incoming request as an [`IpAddr`].
/// If the client's IP address is not known, the request is forwarded.
///
/// * **SocketAddr**
///
/// Extracts the remote address of the incoming request as a [`SocketAddr`].
@ -442,6 +447,18 @@ impl<'a, 'r> FromRequest<'a, 'r> for &'a ContentType {
}
}
#[crate::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for IpAddr {
type Error = std::convert::Infallible;
async fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
match request.client_ip() {
Some(addr) => Success(addr),
None => Forward(())
}
}
}
#[crate::async_trait]
impl<'a, 'r> FromRequest<'a, 'r> for SocketAddr {
type Error = std::convert::Infallible;