pub struct Forwarded<'a> { /* private fields */ }
Expand description

A rust representation of the forwarded header.

Implementations

Attempts to parse a Forwarded from headers (or a request or response). Builds a borrowed Forwarded by default. To build an owned Forwarded, use Forwarded::from_headers(...).into_owned()

X-Forwarded-For, -By, and -Proto compatability

This implementation includes fall-back support for the historical unstandardized headers x-forwarded-for, x-forwarded-by, and x-forwarded-proto. If you do not wish to support these headers, use Forwarded::from_forwarded_header. To only support these historical headers and not the standardized Forwarded header, use Forwarded::from_x_headers.

Please note that either way, this implementation will normalize to the standardized Forwarded header, as recommended in rfc7239§7.4

Examples
use trillium_forwarding::Forwarded;

let mut headers = Headers::new();
headers.insert(
    "Forwarded",
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);
let forwarded = Forwarded::from_headers(&headers)?.unwrap();
assert_eq!(forwarded.proto(), Some("https"));
assert_eq!(forwarded.forwarded_for(), vec!["192.0.2.43", "[2001:db8:cafe::17]", "unknown"]);
let mut headers = Headers::new();
headers.insert("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17, unknown");
headers.insert("X-Forwarded-Proto", "https");
let forwarded = Forwarded::from_headers(&headers)?.unwrap();
assert_eq!(forwarded.forwarded_for(), vec!["192.0.2.43", "[2001:db8:cafe::17]", "unknown"]);
assert_eq!(forwarded.proto(), Some("https"));
assert_eq!(
    forwarded.to_string(),
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);

Parse a borrowed Forwarded from the Forwarded header, without x-forwarded-{for,by,proto} fallback

Examples
let mut headers = Headers::new();
headers.insert(
    "Forwarded",
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);
let forwarded = Forwarded::from_forwarded_header(&headers)?.unwrap();
assert_eq!(forwarded.proto(), Some("https"));
assert_eq!(forwarded.forwarded_for(), vec!["192.0.2.43", "[2001:db8:cafe::17]", "unknown"]);
let mut headers = Headers::new();
headers.insert("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17");
assert!(Forwarded::from_forwarded_header(&headers)?.is_none());

Parse a borrowed Forwarded from the historical non-standardized x-forwarded-{for,by,proto} headers, without support for the Forwarded header.

Examples
let mut headers = Headers::new();
headers.insert("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17");
let forwarded = Forwarded::from_headers(&headers)?.unwrap();
assert_eq!(forwarded.forwarded_for(), vec!["192.0.2.43", "[2001:db8:cafe::17]"]);
let mut headers = Headers::new();
headers.insert(
    "Forwarded",
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);
assert!(Forwarded::from_x_headers(&headers)?.is_none());

parse a &str into a borrowed Forwarded

Examples

let forwarded = Forwarded::parse(
    r#"for=192.0.2.43,         for="[2001:db8:cafe::17]", FOR=unknown;proto=https"#
)?;
assert_eq!(forwarded.forwarded_for(), vec!["192.0.2.43", "[2001:db8:cafe::17]", "unknown"]);
assert_eq!(
    forwarded.to_string(),
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);

Transform a borrowed Forwarded into an owned Forwarded. This is a noop if the Forwarded is already owned.

Builds a new empty Forwarded

Adds a for section to this header

Returns the for field of this header

Sets the host field of this header

Returns the host field of this header

Sets the proto field of this header

Returns the proto field of this header

Sets the by field of this header

Returns the by field of this header

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.