rama_http/layer/dns/dns_resolve/
service.rsuse super::DnsResolveMode;
use crate::{HeaderName, Request};
use rama_core::{error::OpaqueError, Context, Service};
use rama_utils::macros::define_inner_service_accessors;
use std::fmt;
pub struct DnsResolveModeService<S> {
inner: S,
header_name: HeaderName,
}
impl<S> DnsResolveModeService<S> {
pub const fn new(inner: S, header_name: HeaderName) -> Self {
Self { inner, header_name }
}
define_inner_service_accessors!();
}
impl<S: fmt::Debug> fmt::Debug for DnsResolveModeService<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DnsResolveModeService")
.field("inner", &self.inner)
.finish()
}
}
impl<S: Clone> Clone for DnsResolveModeService<S> {
fn clone(&self) -> Self {
DnsResolveModeService {
inner: self.inner.clone(),
header_name: self.header_name.clone(),
}
}
}
impl<State, Body, S> Service<State, Request<Body>> for DnsResolveModeService<S>
where
State: Clone + Send + Sync + 'static,
Body: Send + Sync + 'static,
S: Service<
State,
Request<Body>,
Error: Into<rama_core::error::BoxError> + Send + Sync + 'static,
>,
{
type Response = S::Response;
type Error = OpaqueError;
async fn serve(
&self,
mut ctx: Context<State>,
request: Request<Body>,
) -> Result<Self::Response, Self::Error> {
if let Some(header_value) = request.headers().get(&self.header_name) {
let dns_resolve_mode: DnsResolveMode = header_value.try_into()?;
ctx.insert(dns_resolve_mode);
}
self.inner
.serve(ctx, request)
.await
.map_err(|err| OpaqueError::from_boxed(err.into()))
}
}