rama_http/layer/forwarded/
set_forwarded.rsuse crate::headers::{
ForwardHeader, HeaderMapExt, Via, XForwardedFor, XForwardedHost, XForwardedProto,
};
use crate::Request;
use rama_core::error::BoxError;
use rama_core::{Context, Layer, Service};
use rama_net::address::Domain;
use rama_net::forwarded::{Forwarded, ForwardedElement, NodeId};
use rama_net::http::RequestContext;
use rama_net::stream::SocketInfo;
use rama_utils::macros::all_the_tuples_no_last_special_case;
use std::fmt;
use std::marker::PhantomData;
pub struct SetForwardedHeadersLayer<T = Forwarded> {
by_node: NodeId,
_headers: PhantomData<fn() -> T>,
}
impl<T: fmt::Debug> fmt::Debug for SetForwardedHeadersLayer<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("SetForwardedHeadersLayer")
.field("by_node", &self.by_node)
.field(
"_headers",
&format_args!("{}", std::any::type_name::<fn() -> T>()),
)
.finish()
}
}
impl<T: Clone> Clone for SetForwardedHeadersLayer<T> {
fn clone(&self) -> Self {
Self {
by_node: self.by_node.clone(),
_headers: PhantomData,
}
}
}
impl<T> SetForwardedHeadersLayer<T> {
pub fn forward_by(mut self, node_id: impl Into<NodeId>) -> Self {
self.by_node = node_id.into();
self
}
pub fn set_forward_by(&mut self, node_id: impl Into<NodeId>) -> &mut Self {
self.by_node = node_id.into();
self
}
}
impl<T> SetForwardedHeadersLayer<T> {
pub fn new() -> Self {
Self {
by_node: Domain::from_static("rama").into(),
_headers: PhantomData,
}
}
}
impl Default for SetForwardedHeadersLayer {
fn default() -> Self {
Self::forwarded()
}
}
impl SetForwardedHeadersLayer {
#[inline]
pub fn forwarded() -> Self {
Self::new()
}
}
impl SetForwardedHeadersLayer<Via> {
#[inline]
pub fn via() -> Self {
Self::new()
}
}
impl SetForwardedHeadersLayer<XForwardedFor> {
#[inline]
pub fn x_forwarded_for() -> Self {
Self::new()
}
}
impl SetForwardedHeadersLayer<XForwardedHost> {
#[inline]
pub fn x_forwarded_host() -> Self {
Self::new()
}
}
impl SetForwardedHeadersLayer<XForwardedProto> {
#[inline]
pub fn x_forwarded_proto() -> Self {
Self::new()
}
}
impl<H, S> Layer<S> for SetForwardedHeadersLayer<H> {
type Service = SetForwardedHeadersService<S, H>;
fn layer(&self, inner: S) -> Self::Service {
Self::Service {
inner,
by_node: self.by_node.clone(),
_headers: PhantomData,
}
}
}
pub struct SetForwardedHeadersService<S, T = Forwarded> {
inner: S,
by_node: NodeId,
_headers: PhantomData<fn() -> T>,
}
impl<S: fmt::Debug, T> fmt::Debug for SetForwardedHeadersService<S, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SetForwardedHeadersService")
.field("inner", &self.inner)
.field("by_node", &self.by_node)
.field(
"_headers",
&format_args!("{}", std::any::type_name::<fn() -> T>()),
)
.finish()
}
}
impl<S: Clone, T> Clone for SetForwardedHeadersService<S, T> {
fn clone(&self) -> Self {
SetForwardedHeadersService {
inner: self.inner.clone(),
by_node: self.by_node.clone(),
_headers: PhantomData,
}
}
}
impl<S, T> SetForwardedHeadersService<S, T> {
pub fn forward_by(mut self, node_id: impl Into<NodeId>) -> Self {
self.by_node = node_id.into();
self
}
pub fn set_forward_by(&mut self, node_id: impl Into<NodeId>) -> &mut Self {
self.by_node = node_id.into();
self
}
}
impl<S, T> SetForwardedHeadersService<S, T> {
pub fn new(inner: S) -> Self {
Self {
inner,
by_node: Domain::from_static("rama").into(),
_headers: PhantomData,
}
}
}
impl<S> SetForwardedHeadersService<S> {
#[inline]
pub fn forwarded(inner: S) -> Self {
Self::new(inner)
}
}
impl<S> SetForwardedHeadersService<S, Via> {
#[inline]
pub fn via(inner: S) -> Self {
Self::new(inner)
}
}
impl<S> SetForwardedHeadersService<S, XForwardedFor> {
#[inline]
pub fn x_forwarded_for(inner: S) -> Self {
Self::new(inner)
}
}
impl<S> SetForwardedHeadersService<S, XForwardedHost> {
#[inline]
pub fn x_forwarded_host(inner: S) -> Self {
Self::new(inner)
}
}
impl<S> SetForwardedHeadersService<S, XForwardedProto> {
#[inline]
pub fn x_forwarded_proto(inner: S) -> Self {
Self::new(inner)
}
}
impl<S, H, State, Body> Service<State, Request<Body>> for SetForwardedHeadersService<S, H>
where
S: Service<State, Request<Body>, Error: Into<BoxError>>,
H: ForwardHeader + Send + Sync + 'static,
Body: Send + 'static,
State: Clone + Send + Sync + 'static,
{
type Response = S::Response;
type Error = BoxError;
async fn serve(
&self,
mut ctx: Context<State>,
mut req: Request<Body>,
) -> Result<Self::Response, Self::Error> {
let forwarded: Option<Forwarded> = ctx.get().cloned();
let mut forwarded_element = ForwardedElement::forwarded_by(self.by_node.clone());
if let Some(peer_addr) = ctx.get::<SocketInfo>().map(|socket| *socket.peer_addr()) {
forwarded_element.set_forwarded_for(peer_addr);
}
let request_ctx: &mut RequestContext =
ctx.get_or_try_insert_with_ctx(|ctx| (ctx, &req).try_into())?;
forwarded_element.set_forwarded_host(request_ctx.authority.clone());
if let Ok(forwarded_proto) = (&request_ctx.protocol).try_into() {
forwarded_element.set_forwarded_proto(forwarded_proto);
}
let forwarded = match forwarded {
None => Some(Forwarded::new(forwarded_element)),
Some(mut forwarded) => {
forwarded.append(forwarded_element);
Some(forwarded)
}
};
if let Some(forwarded) = forwarded {
if let Some(header) = H::try_from_forwarded(forwarded.iter()) {
req.headers_mut().typed_insert(header);
}
}
self.inner.serve(ctx, req).await.map_err(Into::into)
}
}
macro_rules! set_forwarded_service_for_tuple {
( $($ty:ident),* $(,)? ) => {
#[allow(non_snake_case)]
impl<S, $($ty),* , State, Body> Service<State, Request<Body>> for SetForwardedHeadersService<S, ($($ty,)*)>
where
$( $ty: ForwardHeader + Send + Sync + 'static, )*
S: Service<State, Request<Body>, Error: Into<BoxError>>,
Body: Send + 'static,
State: Clone + Send + Sync + 'static,
{
type Response = S::Response;
type Error = BoxError;
async fn serve(
&self,
mut ctx: Context<State>,
mut req: Request<Body>,
) -> Result<Self::Response, Self::Error> {
let forwarded: Option<Forwarded> = ctx.get().cloned();
let mut forwarded_element = ForwardedElement::forwarded_by(self.by_node.clone());
if let Some(peer_addr) = ctx.get::<SocketInfo>().map(|socket| *socket.peer_addr()) {
forwarded_element.set_forwarded_for(peer_addr);
}
let request_ctx: &mut RequestContext =
ctx.get_or_try_insert_with_ctx(|ctx| (ctx, &req).try_into())?;
forwarded_element.set_forwarded_host(request_ctx.authority.clone());
if let Ok(forwarded_proto) = (&request_ctx.protocol).try_into() {
forwarded_element.set_forwarded_proto(forwarded_proto);
}
let forwarded = match forwarded {
None => Some(Forwarded::new(forwarded_element)),
Some(mut forwarded) => {
forwarded.append(forwarded_element);
Some(forwarded)
}
};
if let Some(forwarded) = forwarded {
$(
if let Some(header) = $ty::try_from_forwarded(forwarded.iter()) {
req.headers_mut().typed_insert(header);
}
)*
}
self.inner.serve(ctx, req).await.map_err(Into::into)
}
}
};
}
all_the_tuples_no_last_special_case!(set_forwarded_service_for_tuple);
#[cfg(test)]
mod tests {
use super::*;
use crate::{
headers::{TrueClientIp, XClientIp, XRealIp},
IntoResponse, Response, StatusCode,
};
use rama_core::{error::OpaqueError, service::service_fn, Layer};
use std::{convert::Infallible, net::IpAddr};
fn assert_is_service<T: Service<(), Request<()>>>(_: T) {}
async fn dummy_service_fn() -> Result<Response, OpaqueError> {
Ok(StatusCode::OK.into_response())
}
#[test]
fn test_set_forwarded_service_is_service() {
assert_is_service(SetForwardedHeadersService::forwarded(service_fn(
dummy_service_fn,
)));
assert_is_service(SetForwardedHeadersService::via(service_fn(
dummy_service_fn,
)));
assert_is_service(SetForwardedHeadersService::x_forwarded_for(service_fn(
dummy_service_fn,
)));
assert_is_service(SetForwardedHeadersService::x_forwarded_proto(service_fn(
dummy_service_fn,
)));
assert_is_service(SetForwardedHeadersService::x_forwarded_host(service_fn(
dummy_service_fn,
)));
assert_is_service(SetForwardedHeadersService::<_, TrueClientIp>::new(
service_fn(dummy_service_fn),
));
assert_is_service(SetForwardedHeadersService::<_, (TrueClientIp,)>::new(
service_fn(dummy_service_fn),
));
assert_is_service(
SetForwardedHeadersService::<_, (TrueClientIp, XClientIp)>::new(service_fn(
dummy_service_fn,
)),
);
assert_is_service(SetForwardedHeadersLayer::via().layer(service_fn(dummy_service_fn)));
assert_is_service(
SetForwardedHeadersLayer::<XRealIp>::new().layer(service_fn(dummy_service_fn)),
);
assert_is_service(
SetForwardedHeadersLayer::<(XRealIp, XForwardedProto)>::new()
.layer(service_fn(dummy_service_fn)),
);
}
#[tokio::test]
async fn test_set_forwarded_service_forwarded() {
async fn svc(request: Request<()>) -> Result<(), Infallible> {
assert_eq!(
request.headers().get("Forwarded").unwrap(),
"by=rama;host=\"example.com:80\";proto=http"
);
Ok(())
}
let service = SetForwardedHeadersService::forwarded(service_fn(svc));
let req = Request::builder().uri("example.com").body(()).unwrap();
service.serve(Context::default(), req).await.unwrap();
}
#[tokio::test]
async fn test_set_forwarded_service_forwarded_with_chain() {
async fn svc(request: Request<()>) -> Result<(), Infallible> {
assert_eq!(
request.headers().get("Forwarded").unwrap(),
"for=12.23.34.45,by=rama;for=\"127.0.0.1:62345\";host=\"www.example.com:443\";proto=https",
);
Ok(())
}
let service = SetForwardedHeadersService::forwarded(service_fn(svc));
let req = Request::builder()
.uri("https://www.example.com")
.body(())
.unwrap();
let mut ctx = Context::default();
ctx.insert(Forwarded::new(ForwardedElement::forwarded_for(
IpAddr::from([12, 23, 34, 45]),
)));
ctx.insert(SocketInfo::new(None, "127.0.0.1:62345".parse().unwrap()));
service.serve(ctx, req).await.unwrap();
}
#[tokio::test]
async fn test_set_forwarded_service_x_forwarded_for_with_chain() {
async fn svc(request: Request<()>) -> Result<(), Infallible> {
assert_eq!(
request.headers().get("X-Forwarded-For").unwrap(),
"12.23.34.45, 127.0.0.1",
);
Ok(())
}
let service = SetForwardedHeadersService::x_forwarded_for(service_fn(svc));
let req = Request::builder()
.uri("https://www.example.com")
.body(())
.unwrap();
let mut ctx = Context::default();
ctx.insert(Forwarded::new(ForwardedElement::forwarded_for(
IpAddr::from([12, 23, 34, 45]),
)));
ctx.insert(SocketInfo::new(None, "127.0.0.1:62345".parse().unwrap()));
service.serve(ctx, req).await.unwrap();
}
#[tokio::test]
async fn test_set_forwarded_service_forwarded_fully_defined() {
async fn svc(request: Request<()>) -> Result<(), Infallible> {
assert_eq!(
request.headers().get("Forwarded").unwrap(),
"by=12.23.34.45;for=\"127.0.0.1:62345\";host=\"www.example.com:443\";proto=https",
);
Ok(())
}
let service = SetForwardedHeadersService::forwarded(service_fn(svc))
.forward_by(IpAddr::from([12, 23, 34, 45]));
let req = Request::builder()
.uri("https://www.example.com")
.body(())
.unwrap();
let mut ctx = Context::default();
ctx.insert(SocketInfo::new(None, "127.0.0.1:62345".parse().unwrap()));
service.serve(ctx, req).await.unwrap();
}
#[tokio::test]
async fn test_set_forwarded_service_forwarded_fully_defined_with_chain() {
async fn svc(request: Request<()>) -> Result<(), Infallible> {
assert_eq!(
request.headers().get("Forwarded").unwrap(),
"by=rama;for=\"127.0.0.1:62345\";host=\"www.example.com:443\";proto=https",
);
Ok(())
}
let service = SetForwardedHeadersService::forwarded(service_fn(svc));
let req = Request::builder()
.uri("https://www.example.com")
.body(())
.unwrap();
let mut ctx = Context::default();
ctx.insert(SocketInfo::new(None, "127.0.0.1:62345".parse().unwrap()));
service.serve(ctx, req).await.unwrap();
}
}