pub struct SetForwardedHeaderLayer<T = Forwarded> { /* private fields */ }Expand description
Layer to write Forwarded information for this proxy,
added to the end of the chain of forwarded information already known.
This layer can set any header as long as you have a ForwardHeader implementation
for the header you want to set. You can pass it as the type to the layer when creating
the layer using SetForwardedHeaderLayer::new.
The following headers are supported out of the box with each their own constructor:
SetForwardedHeaderLayer::forwarded: the standardForwardedheaderRFC 7239;SetForwardedHeaderLayer::via: the canonicalViaheader (non-standard);SetForwardedHeaderLayer::x_forwarded_for: the canonicalX-Forwarded-Forheader (non-standard);SetForwardedHeaderLayer::x_forwarded_host: the canonicalX-Forwarded-Hostheader (non-standard);SetForwardedHeaderLayer::x_forwarded_proto: the canonicalX-Forwarded-Protoheader (non-standard).
The “by” property is set to rama by default. Use SetForwardedHeaderLayer::forward_by to overwrite this,
typically with the actual IPv4/IPv6 address of your proxy.
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 SetForwardedHeaderLayer constructors for these headers,
but you can use the SetForwardedHeaderLayer::new constructor and pass the header type as a type parameter,
alone or in a tuple with other headers.
§Example
This example shows how you could expose the real Client IP using the [X-Real-IP][crate::headers::XRealIp] header.
use rama_net::stream::SocketInfo;
use rama_http::Request;
use rama_core::service::service_fn;
use rama_http::{headers::forwarded::XRealIp, layer::forwarded::SetForwardedHeaderLayer};
use rama_core::{Context, Service, Layer};
use std::convert::Infallible;
async fn svc(_ctx: Context<State>, request: Request<Body>) -> Result<(), Infallible> {
// ...
}
let service = SetForwardedHeaderLayer::<XRealIp>::new()
.into_layer(service_fn(svc));
service.serve(ctx, req).await.unwrap();Implementations§
Source§impl<T> SetForwardedHeaderLayer<T>
impl<T> SetForwardedHeaderLayer<T>
Sourcepub fn forward_by(self, node_id: impl Into<NodeId>) -> Self
pub fn forward_by(self, node_id: impl Into<NodeId>) -> Self
Set the given NodeId as the “by” property, identifying this proxy.
Default of None will be set to rama otherwise.
Sourcepub fn set_forward_by(&mut self, node_id: impl Into<NodeId>) -> &mut Self
pub fn set_forward_by(&mut self, node_id: impl Into<NodeId>) -> &mut Self
Set the given NodeId as the “by” property, identifying this proxy.
Default of None will be set to rama otherwise.
Source§impl<T> SetForwardedHeaderLayer<T>
impl<T> SetForwardedHeaderLayer<T>
Source§impl SetForwardedHeaderLayer<XForwardedFor>
impl SetForwardedHeaderLayer<XForwardedFor>
Sourcepub fn x_forwarded_for() -> Self
pub fn x_forwarded_for() -> Self
Create a new SetForwardedHeaderLayer for the canonical [X-Forwarded-For] header.
Source§impl SetForwardedHeaderLayer<XForwardedHost>
impl SetForwardedHeaderLayer<XForwardedHost>
Sourcepub fn x_forwarded_host() -> Self
pub fn x_forwarded_host() -> Self
Create a new SetForwardedHeaderLayer for the canonical [X-Forwarded-Host] header.
Source§impl SetForwardedHeaderLayer<XForwardedProto>
impl SetForwardedHeaderLayer<XForwardedProto>
Sourcepub fn x_forwarded_proto() -> Self
pub fn x_forwarded_proto() -> Self
Create a new SetForwardedHeaderLayer for the canonical [X-Forwarded-Proto] header.
Trait Implementations§
Source§impl<T: Clone> Clone for SetForwardedHeaderLayer<T>
impl<T: Clone> Clone for SetForwardedHeaderLayer<T>
Source§impl<T: Debug> Debug for SetForwardedHeaderLayer<T>
impl<T: Debug> Debug for SetForwardedHeaderLayer<T>
Source§impl Default for SetForwardedHeaderLayer
impl Default for SetForwardedHeaderLayer
Source§impl<H, S> Layer<S> for SetForwardedHeaderLayer<H>
impl<H, S> Layer<S> for SetForwardedHeaderLayer<H>
Source§type Service = SetForwardedHeaderService<S, H>
type Service = SetForwardedHeaderService<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 SetForwardedHeaderLayer<T>
impl<T> RefUnwindSafe for SetForwardedHeaderLayer<T>
impl<T> Send for SetForwardedHeaderLayer<T>
impl<T> Sync for SetForwardedHeaderLayer<T>
impl<T> Unpin for SetForwardedHeaderLayer<T>
impl<T> UnwindSafe for SetForwardedHeaderLayer<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