[][src]Struct tide::http::proxies::Forwarded

pub struct Forwarded<'a> { /* fields omitted */ }

A rust representation of the forwarded header.

Implementations

impl<'a> Forwarded<'a>[src]

pub fn from_headers(
    headers: &'a impl AsRef<Headers>
) -> Result<Option<Forwarded<'a>>, ParseError>
[src]

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

let mut request = Request::new(Get, Url::parse("http://_/")?);
request.insert_header(
    "Forwarded",
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);
let forwarded = Forwarded::from_headers(&request)?.unwrap();
assert_eq!(forwarded.proto(), Some("https"));
assert_eq!(forwarded.forwarded_for(), vec!["192.0.2.43", "[2001:db8:cafe::17]", "unknown"]);
let mut request = Request::new(Get, Url::parse("http://_/")?);
request.insert_header("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17, unknown");
request.insert_header("X-Forwarded-Proto", "https");
let forwarded = Forwarded::from_headers(&request)?.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.value()?,
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);

pub fn from_forwarded_header(
    headers: &'a impl AsRef<Headers>
) -> Result<Option<Forwarded<'a>>, ParseError>
[src]

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

Examples

let mut request = Request::new(Get, Url::parse("http://_/")?);
request.insert_header(
    "Forwarded",
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);
let forwarded = Forwarded::from_forwarded_header(&request)?.unwrap();
assert_eq!(forwarded.proto(), Some("https"));
assert_eq!(forwarded.forwarded_for(), vec!["192.0.2.43", "[2001:db8:cafe::17]", "unknown"]);
let mut request = Request::new(Get, Url::parse("http://_/")?);
request.insert_header("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17");
assert!(Forwarded::from_forwarded_header(&request)?.is_none());

pub fn from_x_headers(
    headers: &'a impl AsRef<Headers>
) -> Result<Option<Forwarded<'a>>, ParseError>
[src]

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

Examples

let mut request = Request::new(Get, Url::parse("http://_/")?);
request.insert_header("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17");
let forwarded = Forwarded::from_headers(&request)?.unwrap();
assert_eq!(forwarded.forwarded_for(), vec!["192.0.2.43", "[2001:db8:cafe::17]"]);
let mut request = Request::new(Get, Url::parse("http://_/")?);
request.insert_header(
    "Forwarded",
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);
assert!(Forwarded::from_x_headers(&request)?.is_none());

pub fn parse(input: &'a str) -> Result<Forwarded<'a>, ParseError>[src]

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.value()?,
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);

pub fn into_owned(self) -> Forwarded<'static>[src]

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

pub fn apply(&self, headers: impl AsMut<Headers>)[src]

Insert a header that represents this Forwarded.

Example

let mut response = http_types::Response::new(200);
let mut forwarded = http_types::proxies::Forwarded::new();
forwarded.add_for("192.0.2.43");
forwarded.add_for("[2001:db8:cafe::17]");
forwarded.set_proto("https");
forwarded.apply(&mut response);
assert_eq!(response["Forwarded"], r#"for=192.0.2.43, for="[2001:db8:cafe::17]";proto=https"#);

pub fn value(&self) -> Result<String, Error>[src]

Builds a Forwarded header as a String.

Example

let mut forwarded = http_types::proxies::Forwarded::new();
forwarded.add_for("_haproxy");
forwarded.add_for("[2001:db8:cafe::17]");
forwarded.set_proto("https");
assert_eq!(forwarded.value()?, r#"for=_haproxy, for="[2001:db8:cafe::17]";proto=https"#);

pub fn new() -> Forwarded<'a>[src]

Builds a new empty Forwarded

pub fn add_for(&mut self, forwarded_for: impl Into<Cow<'a, str>>)[src]

Adds a for section to this header

pub fn forwarded_for(&self) -> Vec<&str>[src]

Returns the for field of this header

pub fn set_host(&mut self, host: impl Into<Cow<'a, str>>)[src]

Sets the host field of this header

pub fn host(&self) -> Option<&str>[src]

Returns the host field of this header

pub fn set_proto(&mut self, proto: impl Into<Cow<'a, str>>)[src]

Sets the proto field of this header

pub fn proto(&self) -> Option<&str>[src]

Returns the proto field of this header

pub fn set_by(&mut self, by: impl Into<Cow<'a, str>>)[src]

Sets the by field of this header

pub fn by(&self) -> Option<&str>[src]

Returns the by field of this header

Trait Implementations

impl<'a> Clone for Forwarded<'a>[src]

impl<'a> Debug for Forwarded<'a>[src]

impl<'a> Default for Forwarded<'a>[src]

impl<'_> Display for Forwarded<'_>[src]

impl<'_, '_> ToHeaderValues for &'_ Forwarded<'_>[src]

type Iter = IntoIter<HeaderValue>

Returned iterator over header values which this type may correspond to.

impl<'_> ToHeaderValues for Forwarded<'_>[src]

type Iter = IntoIter<HeaderValue>

Returned iterator over header values which this type may correspond to.

impl<'a> TryFrom<&'a str> for Forwarded<'a>[src]

type Error = ParseError

The type returned in the event of a conversion error.

Auto Trait Implementations

impl<'a> RefUnwindSafe for Forwarded<'a>

impl<'a> Send for Forwarded<'a>

impl<'a> Sync for Forwarded<'a>

impl<'a> Unpin for Forwarded<'a>

impl<'a> UnwindSafe for Forwarded<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,