pub struct SetForwardedHeadersLayer<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 SetForwardedHeadersLayer::new
. Multiple headers (in order) can also be set
by specifying multiple types as a tuple.
The following headers are supported out of the box with each their own constructor:
SetForwardedHeadersLayer::forwarded
: the standardForwarded
headerRFC 7239
;SetForwardedHeadersLayer::via
: the canonicalVia
header (non-standard);SetForwardedHeadersLayer::x_forwarded_for
: the canonicalX-Forwarded-For
header (non-standard);SetForwardedHeadersLayer::x_forwarded_host
: the canonicalX-Forwarded-Host
header (non-standard);SetForwardedHeadersLayer::x_forwarded_proto
: the canonicalX-Forwarded-Proto
header (non-standard).
The “by” property is set to rama
by default. Use SetForwardedHeadersLayer::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-Ip
andTrue-Client-Ip
.
There are no SetForwardedHeadersLayer
constructors for these headers,
but you can use the SetForwardedHeadersLayer::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
header.
use rama_net::stream::SocketInfo;
use rama_http::Request;
use rama_core::service::service_fn;
use rama_http::{headers::XRealIp, layer::forwarded::SetForwardedHeadersLayer};
use rama_core::{Context, Service, Layer};
use std::convert::Infallible;
async fn svc(_ctx: Context<State>, request: Request<Body>) -> Result<(), Infallible> {
// ...
}
let service = SetForwardedHeadersLayer::<XRealIp>::new()
.layer(service_fn(svc));
service.serve(ctx, req).await.unwrap();
Implementations§
Source§impl<T> SetForwardedHeadersLayer<T>
impl<T> SetForwardedHeadersLayer<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> SetForwardedHeadersLayer<T>
impl<T> SetForwardedHeadersLayer<T>
Source§impl SetForwardedHeadersLayer<XForwardedFor>
impl SetForwardedHeadersLayer<XForwardedFor>
Sourcepub fn x_forwarded_for() -> Self
pub fn x_forwarded_for() -> Self
Create a new SetForwardedHeadersLayer
for the canonical [X-Forwarded-For
] header.
Source§impl SetForwardedHeadersLayer<XForwardedHost>
impl SetForwardedHeadersLayer<XForwardedHost>
Sourcepub fn x_forwarded_host() -> Self
pub fn x_forwarded_host() -> Self
Create a new SetForwardedHeadersLayer
for the canonical [X-Forwarded-Host
] header.
Source§impl SetForwardedHeadersLayer<XForwardedProto>
impl SetForwardedHeadersLayer<XForwardedProto>
Sourcepub fn x_forwarded_proto() -> Self
pub fn x_forwarded_proto() -> Self
Create a new SetForwardedHeadersLayer
for the canonical [X-Forwarded-Proto
] header.
Trait Implementations§
Source§impl<T: Clone> Clone for SetForwardedHeadersLayer<T>
impl<T: Clone> Clone for SetForwardedHeadersLayer<T>
Source§impl<T: Debug> Debug for SetForwardedHeadersLayer<T>
impl<T: Debug> Debug for SetForwardedHeadersLayer<T>
Source§impl Default for SetForwardedHeadersLayer
impl Default for SetForwardedHeadersLayer
Auto Trait Implementations§
impl<T> Freeze for SetForwardedHeadersLayer<T>
impl<T> RefUnwindSafe for SetForwardedHeadersLayer<T>
impl<T> Send for SetForwardedHeadersLayer<T>
impl<T> Sync for SetForwardedHeadersLayer<T>
impl<T> Unpin for SetForwardedHeadersLayer<T>
impl<T> UnwindSafe for SetForwardedHeadersLayer<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