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

A rust representation of the forwarded header.

Implementations§

source§

impl<'a> Forwarded<'a>

source

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

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 http_types_rs::{Request};
use http_types_rs::proxies::Forwarded;

let mut request = Request::get("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"]);
use http_types_rs::{headers::Header, Request};
use http_types_rs::proxies::Forwarded;

let mut request = Request::get("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.header_value(),
    r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
);
source

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

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());
source

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

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());
source

pub fn parse(input: &'a str) -> Result<Self, ParseError>

parse a &str into a borrowed Forwarded

§Examples
use http_types_rs::headers::Header;
use http_types_rs::proxies::Forwarded;

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

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

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

source

pub fn new() -> Self

Builds a new empty Forwarded

source

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

Adds a for section to this header

source

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

Returns the for field of this header

source

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

Sets the host field of this header

source

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

Returns the host field of this header

source

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

Sets the proto field of this header

source

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

Returns the proto field of this header

source

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

Sets the by field of this header

source

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

Returns the by field of this header

Trait Implementations§

source§

impl<'a> Clone for Forwarded<'a>

source§

fn clone(&self) -> Forwarded<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for Forwarded<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> Default for Forwarded<'a>

source§

fn default() -> Forwarded<'a>

Returns the “default value” for a type. Read more
source§

impl Display for Forwarded<'_>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> Header for Forwarded<'a>

source§

fn header_name(&self) -> HeaderName

Access the header’s name.
source§

fn header_value(&self) -> HeaderValue

Access the header’s value.
source§

fn apply_header<H: AsMut<Headers>>(&self, headers: H)

Insert the header name and header value into something that looks like a Headers map.
source§

impl<'a> PartialEq for Forwarded<'a>

source§

fn eq(&self, other: &Forwarded<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> TryFrom<&'a str> for Forwarded<'a>

§

type Error = ParseError

The type returned in the event of a conversion error.
source§

fn try_from(value: &'a str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> Eq for Forwarded<'a>

source§

impl<'a> StructuralPartialEq for Forwarded<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Forwarded<'a>

§

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToHeaderValues for T
where T: Header,

§

type Iter = IntoIter<HeaderValue>

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

fn to_header_values(&self) -> Result<<T as ToHeaderValues>::Iter, Error>

Converts this object to an iterator of resolved HeaderValues.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

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

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more