remove extra header allocation

This commit is contained in:
Robin Appelman 2020-12-03 23:17:36 +01:00
commit 411f7a40a7

View file

@ -10,6 +10,9 @@ use warp::Filter;
/// This uses the "x-forwarded-for" or "x-real-ip" headers set by reverse proxies. /// This uses the "x-forwarded-for" or "x-real-ip" headers set by reverse proxies.
/// To stop clients from abusing these headers, only headers set by trusted remotes will be accepted. /// To stop clients from abusing these headers, only headers set by trusted remotes will be accepted.
/// ///
/// Note that if multiple forwarded-for addresses are present, wich can be the case when using nested reverse proxies,
/// all proxies in the chain have to be within the list of trusted proxies.
///
/// ## Example /// ## Example
/// ///
/// ```no_run /// ```no_run
@ -44,17 +47,26 @@ pub fn real_ip(
/// Creates a `Filter` that extracts the ip addresses from the the "forwarded for" chain /// Creates a `Filter` that extracts the ip addresses from the the "forwarded for" chain
pub fn get_forwarded_for() -> impl Filter<Extract = (Vec<IpAddr>,), Error = Infallible> + Clone { pub fn get_forwarded_for() -> impl Filter<Extract = (Vec<IpAddr>,), Error = Infallible> + Clone {
warp::header::<String>("x-forwarded-for") warp::header("x-forwarded-for")
.map(|forwarded: String| { .map(|list: IpList| list.0)
forwarded
.split(',')
.map(str::trim)
.map(IpAddr::from_str)
.filter_map(Result::ok)
.collect::<Vec<IpAddr>>()
})
.or(warp::header("x-real-ip").map(|ip| vec![ip])) .or(warp::header("x-real-ip").map(|ip| vec![ip]))
.unify() .unify()
.or(warp::any().map(|| vec![])) .or(warp::any().map(|| vec![]))
.unify() .unify()
} }
/// Newtype so we can implement FromStr
struct IpList(Vec<IpAddr>);
impl FromStr for IpList {
type Err = <IpAddr as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let vec = s
.split(',')
.map(str::trim)
.map(IpAddr::from_str)
.collect::<Result<Vec<_>, _>>()?;
Ok(IpList(vec))
}
}