pub struct GetForwardedHeadersLayer<T = Forwarded> { /* private fields */ }
Expand description
Layer to extract Forwarded
information from the specified T
headers.
This layer can be used to extract the Forwarded
information from any specified header T
,
as long as the header implements the ForwardHeader
trait. Multiple headers can be specified
as a tuple, and the layer will extract information from them all, and combine the information.
Please take into consideration the following when combining headers:
- The last header in the tuple will take precedence over the previous headers, if the same information is present in multiple headers.
- Headers that can contain multiple elements, (e.g. X-Forwarded-For, Via) will combine their elements in the order as specified. That does however mean that in case one header has less elements then the other, that the combination down the line will not be accurate.
The following headers are supported by default:
GetForwardedHeadersLayer::forwarded
: The standardForwarded
headerRFC 7239
.GetForwardedHeadersLayer::via
: The canonicalVia
headerRFC 7230
.GetForwardedHeadersLayer::x_forwarded_for
: The canonical [X-Forwarded-For
] headerRFC 7239
.GetForwardedHeadersLayer::x_forwarded_host
: The canonical [X-Forwarded-Host
] headerRFC 7239
.GetForwardedHeadersLayer::x_forwarded_proto
: The canonical [X-Forwarded-Proto
] headerRFC 7239
.
Rama also has the following headers already implemented for you to use:
X-Real-Ip
,X-Client-Ip
,Client-Ip
,Cf-Connecting-Ip
andTrue-Client-Ip
.
There are no GetForwardedHeadersLayer
constructors for these headers,
but you can use the GetForwardedHeadersLayer::new
constructor and pass the header type as a type parameter,
alone or in a tuple with other headers.
§Example
This example shows you can extract the client IP from the X-Forwarded-For
header in case your application is behind a proxy which sets this header.
use rama_core::{
service::service_fn,
Context, Service, Layer,
};
use rama_http::{headers::Forwarded, layer::forwarded::GetForwardedHeadersLayer, Request};
use std::{convert::Infallible, net::IpAddr};
#[tokio::main]
async fn main() {
let service = GetForwardedHeadersLayer::x_forwarded_for()
.layer(service_fn(|ctx: Context<()>, _| async move {
let forwarded = ctx.get::<Forwarded>().unwrap();
assert_eq!(forwarded.client_ip(), Some(IpAddr::from([12, 23, 34, 45])));
assert!(forwarded.client_proto().is_none());
// ...
Ok::<_, Infallible>(())
}));
let req = Request::builder()
.header("X-Forwarded-For", "12.23.34.45")
.body(())
.unwrap();
service.serve(Context::default(), req).await.unwrap();
}
Implementations§
Source§impl<T> GetForwardedHeadersLayer<T>
impl<T> GetForwardedHeadersLayer<T>
Source§impl GetForwardedHeadersLayer<XForwardedFor>
impl GetForwardedHeadersLayer<XForwardedFor>
Sourcepub fn x_forwarded_for() -> Self
pub fn x_forwarded_for() -> Self
Create a new GetForwardedHeadersLayer
for the canonical [X-Forwarded-For
] header.
Source§impl GetForwardedHeadersLayer<XForwardedHost>
impl GetForwardedHeadersLayer<XForwardedHost>
Sourcepub fn x_forwarded_host() -> Self
pub fn x_forwarded_host() -> Self
Create a new GetForwardedHeadersLayer
for the canonical [X-Forwarded-Host
] header.
Source§impl GetForwardedHeadersLayer<XForwardedProto>
impl GetForwardedHeadersLayer<XForwardedProto>
Sourcepub fn x_forwarded_proto() -> Self
pub fn x_forwarded_proto() -> Self
Create a new GetForwardedHeadersLayer
for the canonical [X-Forwarded-Proto
] header.
Trait Implementations§
Source§impl<T: Clone> Clone for GetForwardedHeadersLayer<T>
impl<T: Clone> Clone for GetForwardedHeadersLayer<T>
Source§impl<T: Debug> Debug for GetForwardedHeadersLayer<T>
impl<T: Debug> Debug for GetForwardedHeadersLayer<T>
Source§impl Default for GetForwardedHeadersLayer
impl Default for GetForwardedHeadersLayer
Auto Trait Implementations§
impl<T> Freeze for GetForwardedHeadersLayer<T>
impl<T> RefUnwindSafe for GetForwardedHeadersLayer<T>
impl<T> Send for GetForwardedHeadersLayer<T>
impl<T> Sync for GetForwardedHeadersLayer<T>
impl<T> Unpin for GetForwardedHeadersLayer<T>
impl<T> UnwindSafe for GetForwardedHeadersLayer<T>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more