rama_http/matcher/
domain.rsuse crate::Request;
use rama_core::{context::Extensions, Context};
use rama_net::address::{Domain, Host};
use rama_net::http::RequestContext;
#[derive(Debug, Clone)]
pub struct DomainMatcher {
domain: Domain,
sub: bool,
}
impl DomainMatcher {
pub fn exact(domain: Domain) -> Self {
Self { domain, sub: false }
}
pub fn sub(domain: Domain) -> Self {
Self { domain, sub: true }
}
}
impl<State, Body> rama_core::matcher::Matcher<State, Request<Body>> for DomainMatcher {
fn matches(
&self,
ext: Option<&mut Extensions>,
ctx: &Context<State>,
req: &Request<Body>,
) -> bool {
let host = match ctx.get::<RequestContext>() {
Some(req_ctx) => req_ctx.authority.host().clone(),
None => {
let req_ctx: RequestContext = match (ctx, req).try_into() {
Ok(req_ctx) => req_ctx,
Err(err) => {
tracing::error!(error = %err, "DomainMatcher: failed to lazy-make the request ctx");
return false;
}
};
let host = req_ctx.authority.host().clone();
if let Some(ext) = ext {
ext.insert(req_ctx);
}
host
}
};
match host {
Host::Name(domain) => {
if self.sub {
tracing::trace!("DomainMatcher: ({}).is_parent_of({})", self.domain, domain);
self.domain.is_parent_of(&domain)
} else {
tracing::trace!("DomainMatcher: ({}) == ({})", self.domain, domain);
self.domain == domain
}
}
Host::Address(_) => {
tracing::trace!("DomainMatcher: ignore request host address");
false
}
}
}
}