Struct http_types_rs::proxies::Forwarded
source · pub struct Forwarded<'a> { /* private fields */ }
Expand description
A rust representation of the forwarded header.
Implementations§
source§impl<'a> Forwarded<'a>
impl<'a> Forwarded<'a>
sourcepub fn from_headers(
headers: &'a impl AsRef<Headers>
) -> Result<Option<Self>, ParseError>
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"#
);
sourcepub fn from_forwarded_header(
headers: &'a impl AsRef<Headers>
) -> Result<Option<Self>, ParseError>
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());
sourcepub fn from_x_headers(
headers: &'a impl AsRef<Headers>
) -> Result<Option<Self>, ParseError>
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());
sourcepub fn parse(input: &'a str) -> Result<Self, ParseError>
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"#
);
sourcepub fn into_owned(self) -> Forwarded<'static>
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.
sourcepub fn add_for(&mut self, forwarded_for: impl Into<Cow<'a, str>>)
pub fn add_for(&mut self, forwarded_for: impl Into<Cow<'a, str>>)
Adds a for
section to this header
sourcepub fn forwarded_for(&self) -> Vec<&str>
pub fn forwarded_for(&self) -> Vec<&str>
Returns the for
field of this header
Trait Implementations§
source§impl<'a> Header for Forwarded<'a>
impl<'a> Header for Forwarded<'a>
source§fn header_name(&self) -> HeaderName
fn header_name(&self) -> HeaderName
source§fn header_value(&self) -> HeaderValue
fn header_value(&self) -> HeaderValue
source§impl<'a> PartialEq for Forwarded<'a>
impl<'a> PartialEq for Forwarded<'a>
impl<'a> Eq for Forwarded<'a>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> ToHeaderValues for Twhere
T: Header,
impl<T> ToHeaderValues for Twhere
T: Header,
§type Iter = IntoIter<HeaderValue>
type Iter = IntoIter<HeaderValue>
source§fn to_header_values(&self) -> Result<<T as ToHeaderValues>::Iter, Error>
fn to_header_values(&self) -> Result<<T as ToHeaderValues>::Iter, Error>
HeaderValues
.