Skip to main content

warp/filters/
addr.rs

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