This commit is contained in:
Mateusz Gruszczyński committed 2026-11-08 11:16:41 +01:00
1 parent d7fb869783
commit 0cde2c8a18
21 files changed
+195 -45

No files matched your search

+74
View File
@@ -139,6 +139,73 @@ impl Snapshot {
}
None
}
/// Specificity of the vhost frontend matching `host` (an exact name beats any wildcard,
/// a longer wildcard suffix beats a shorter one).
pub fn frontend_rank(&self, host: &str, local: SocketAddr, tls: bool) -> Option<usize> {
let mut rank = None;
Self::find_host_pattern(host, |pattern| {
let found = self.frontends.get(pattern).is_some_and(|frontends| {
frontends
.iter()
.any(|f| f.bindings.iter().any(|l| l.matches(local, tls)))
});
if found {
rank = Some(host_pattern_rank(pattern, host));
Some(())
} else {
None
}
});
rank
}
/// Specificity of the redirect host matching `host` and `path` (see `frontend_rank`).
pub fn redirect_rank(
&self,
host: &str,
path: &str,
local: SocketAddr,
tls: bool,
) -> Option<usize> {
let mut rank = None;
Self::find_host_pattern(host, |pattern| {
let found = self.redirects.get(pattern).is_some_and(|redirects| {
redirects.iter().any(|redirect| {
redirect
.bindings
.iter()
.any(|binding| binding.matches(local, tls))
&& redirect.rules.iter().any(|rule| rule.matches(path, tls))
})
});
if found {
rank = Some(host_pattern_rank(pattern, host));
Some(())
} else {
None
}
});
rank
}
/// Specificity of the 404 host matching `host` (see `frontend_rank`).
pub fn not_found_rank(&self, host: &str, local: SocketAddr, tls: bool) -> Option<usize> {
let mut rank = None;
Self::find_host_pattern(host, |pattern| {
let found = self.not_found_hosts.get(pattern).is_some_and(|items| {
items.iter().any(|item| {
item.bindings
.iter()
.any(|binding| binding.matches(local, tls))
})
});
if found {
rank = Some(host_pattern_rank(pattern, host));
Some(())
} else {
None
}
});
rank
}
pub fn redirect_on(
&self,
host: &str,
@@ -204,6 +271,13 @@ impl Snapshot {
})
}
}
fn host_pattern_rank(pattern: &str, host: &str) -> usize {
if pattern == host {
usize::MAX
} else {
pattern.len()
}
}
fn select_route<'a, F>(
routes: &'a [Arc<CompiledRoute>],
path: &str,