warp/filters/addr.rs
1//! Socket Address filters.
2
3use std::convert::Infallible;
4use std::net::SocketAddr;
5
6use crate::filter::{filter_fn_one, Filter};
7
8/// Creates a `Filter` to get the remote address of the connection.
9///
10/// If the underlying transport doesn't use socket addresses, this will yield
11/// `None`.
12///
13/// # Example
14///
15/// ```
16/// use std::net::SocketAddr;
17/// use warp::Filter;
18///
19/// let route = warp::addr::remote()
20/// .map(|addr: Option<SocketAddr>| {
21/// println!("remote address = {:?}", addr);
22/// });
23/// ```
24pub fn remote() -> impl Filter<Extract = (Option<SocketAddr>,), Error = Infallible> + Copy {
25 filter_fn_one(|route| futures_util::future::ok(route.remote_addr()))
26}