pub struct GetForwardedHeaderLayer<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.
The following headers are supported by default:
GetForwardedHeaderLayer::forwarded: The standardForwardedheaderRFC 7239.GetForwardedHeaderLayer::via: The canonicalViaheaderRFC 7230.GetForwardedHeaderLayer::x_forwarded_for: The canonical [X-Forwarded-For] headerRFC 7239.GetForwardedHeaderLayer::x_forwarded_host: The canonical [X-Forwarded-Host] headerRFC 7239.GetForwardedHeaderLayer::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-IpandTrue-Client-Ip.
There are no GetForwardedHeaderLayer constructors for these headers,
but you can use the GetForwardedHeaderLayer::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::Forwarded, layer::forwarded::GetForwardedHeaderLayer, Request};
use std::{convert::Infallible, net::IpAddr};
#[tokio::main]
async fn main() {
let service = GetForwardedHeaderLayer::x_forwarded_for()
.into_layer(service_fn(async |ctx: Context<()>, _| {
let forwarded = ctx.get::<rama_net::forwarded::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> GetForwardedHeaderLayer<T>
impl<T> GetForwardedHeaderLayer<T>
Source§impl GetForwardedHeaderLayer<XForwardedFor>
impl GetForwardedHeaderLayer<XForwardedFor>
Sourcepub fn x_forwarded_for() -> Self
pub fn x_forwarded_for() -> Self
Create a new GetForwardedHeaderLayer for the canonical [X-Forwarded-For] header.
Source§impl GetForwardedHeaderLayer<XForwardedHost>
impl GetForwardedHeaderLayer<XForwardedHost>
Sourcepub fn x_forwarded_host() -> Self
pub fn x_forwarded_host() -> Self
Create a new GetForwardedHeaderLayer for the canonical [X-Forwarded-Host] header.
Source§impl GetForwardedHeaderLayer<XForwardedProto>
impl GetForwardedHeaderLayer<XForwardedProto>
Sourcepub fn x_forwarded_proto() -> Self
pub fn x_forwarded_proto() -> Self
Create a new GetForwardedHeaderLayer for the canonical [X-Forwarded-Proto] header.
Trait Implementations§
Source§impl<T: Clone> Clone for GetForwardedHeaderLayer<T>
impl<T: Clone> Clone for GetForwardedHeaderLayer<T>
Source§impl<T: Debug> Debug for GetForwardedHeaderLayer<T>
impl<T: Debug> Debug for GetForwardedHeaderLayer<T>
Source§impl Default for GetForwardedHeaderLayer
impl Default for GetForwardedHeaderLayer
Source§impl<H, S> Layer<S> for GetForwardedHeaderLayer<H>
impl<H, S> Layer<S> for GetForwardedHeaderLayer<H>
Source§type Service = GetForwardedHeaderService<S, H>
type Service = GetForwardedHeaderService<S, H>
Source§fn layer(&self, inner: S) -> Self::Service
fn layer(&self, inner: S) -> Self::Service
Source§fn into_layer(self, inner: S) -> Self::Service
fn into_layer(self, inner: S) -> Self::Service
layer but consuming self after the service was created. Read moreAuto Trait Implementations§
impl<T> Freeze for GetForwardedHeaderLayer<T>
impl<T> RefUnwindSafe for GetForwardedHeaderLayer<T>
impl<T> Send for GetForwardedHeaderLayer<T>
impl<T> Sync for GetForwardedHeaderLayer<T>
impl<T> Unpin for GetForwardedHeaderLayer<T>
impl<T> UnwindSafe for GetForwardedHeaderLayer<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