rama_http_backend/client/proxy/layer/
proxy_auth_header.rsuse rama_core::{Context, Layer, Service};
use rama_http_types::{
headers::{HeaderMapExt, ProxyAuthorization},
Request,
};
use rama_net::{address::ProxyAddress, http::RequestContext, user::ProxyCredential};
use std::{fmt, future::Future};
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct SetProxyAuthHttpHeaderLayer;
impl SetProxyAuthHttpHeaderLayer {
pub fn new() -> Self {
Self::default()
}
}
impl<S> Layer<S> for SetProxyAuthHttpHeaderLayer {
type Service = SetProxyAuthHttpHeaderService<S>;
fn layer(&self, inner: S) -> Self::Service {
SetProxyAuthHttpHeaderService::new(inner)
}
}
pub struct SetProxyAuthHttpHeaderService<S> {
inner: S,
}
impl<S: fmt::Debug> fmt::Debug for SetProxyAuthHttpHeaderService<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SetProxyAuthHttpHeaderService")
.field("inner", &self.inner)
.finish()
}
}
impl<S: Clone> Clone for SetProxyAuthHttpHeaderService<S> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
impl<S> SetProxyAuthHttpHeaderService<S> {
pub const fn new(inner: S) -> Self {
Self { inner }
}
}
impl<S, State, Body> Service<State, Request<Body>> for SetProxyAuthHttpHeaderService<S>
where
S: Service<State, Request<Body>>,
State: Clone + Send + Sync + 'static,
Body: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
fn serve(
&self,
mut ctx: Context<State>,
mut req: Request<Body>,
) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send + '_ {
let maybe_request_ctx = ctx
.get_or_try_insert_with_ctx::<RequestContext, _>(|ctx| (ctx, &req).try_into())
.ok();
if !maybe_request_ctx
.map(|ctx| ctx.protocol.is_secure())
.unwrap_or_default()
{
if let Some(pa) = ctx.get::<ProxyAddress>() {
if let Some(credential) = pa.credential.clone() {
match credential {
ProxyCredential::Basic(basic) => {
tracing::trace!("inserted proxy Basic credentials into (http) request");
req.headers_mut().typed_insert(ProxyAuthorization(basic))
}
ProxyCredential::Bearer(bearer) => {
tracing::trace!(
"inserted proxy Bearer credentials into (http) request"
);
req.headers_mut().typed_insert(ProxyAuthorization(bearer))
}
}
}
}
}
self.inner.serve(ctx, req)
}
}