Skip to main content

parse_x_forwarded_for

Function parse_x_forwarded_for 

Source
pub fn parse_x_forwarded_for<'a>(
    lines: &'a [&'a [u8]],
    limits: &Limits,
) -> Result<XffChain<'a>, ParseError>
Expand description

Parses every X-Forwarded-For field line.

There is no RFC: the de facto grammar accepted is comma-separated IPv4 and IPv6 literals with OWS, brackets optional around IPv6, and an optional :port after IPv4 or bracketed IPv6. Anything else, an empty member included, is refused (D-016).

§Errors

ErrorKind::Malformed; ErrorKind::TooManyHops beyond Limits::max_hops; each with ParseError::line set.

§Examples

use parse_net::{forwarded::parse_x_forwarded_for, Limits};

let lines: [&[u8]; 2] = [b"203.0.113.9:4711, [2001:db8::1]:443", b"2001:db8::2"];
assert_eq!(parse_x_forwarded_for(&lines, &Limits::DEFAULT)?.len(), 3);
assert!(parse_x_forwarded_for(&[&b"1.2.3.4, , 5.6.7.8"[..]], &Limits::DEFAULT).is_err());