Title here
Summary here
Here’s one example of a warp filter that will provide you the user agent (if present) and the remote address (IPv4) associated with a request.
use warp::Filter;
use warp::Rejection;
use std::net::SocketAddr;
#[derive(Debug, Clone)]
pub struct RemoteInfo {
pub remote_addr: Option<SocketAddr>,
pub user_agent: Option<String>,
}
pub fn with_remote_info()
-> impl Filter<Extract = (RemoteInfo,), Error = Rejection> + Copy {
warp::filters::addr::remote()
.and(warp::header::optional::<String>("user-agent"))
.map(|addr: Option<SocketAddr>, agent: Option<String>| RemoteInfo { remote_addr: addr, user_agent: agent })
}
Usage example:
let some_route = warp::path!("some"/"route")
.and(warp::post())
.and(with_remote_info())
.and(warp::body::json())
.and_then(some_route_handler);