v0.34.0_1

This commit is contained in:
Mateusz Gruszczyński committed 2026-10-17 20:34:13 +02:00
1 parent 9b3d6731cc
commit 00f29f92c8
68 files changed
+2599 -513

No files matched your search

+31 -21
View File
@@ -1,17 +1,16 @@
impl Snapshot {
fn host_patterns(host: &str) -> Vec<String> {
let mut patterns = vec![host.to_owned()];
for (index, _) in host.match_indices('.') {
let suffix = &host[index + 1..];
if suffix.contains('.') {
patterns.push(format!("*.{suffix}"));
}
}
patterns
// Exact host: no allocation. Wildcards: lazy and most-specific-first.
fn host_patterns(host: &str) -> impl Iterator<Item = std::borrow::Cow<'_, str>> {
std::iter::once(std::borrow::Cow::Borrowed(host)).chain(
host.match_indices('.').filter_map(move |(index, _)| {
let suffix = &host[index + 1..];
suffix.contains('.').then(|| std::borrow::Cow::Owned(format!("*.{suffix}")))
}),
)
}
pub fn host_allowed(&self, host: &str, peer: IpAddr) -> bool {
for pattern in Self::host_patterns(host) {
if let Some(acl) = self.host_acls.get(&pattern) {
if let Some(acl) = self.host_acls.get(pattern.as_ref()) {
return acl.allows(peer);
}
}
@@ -53,8 +52,19 @@ impl Snapshot {
local: SocketAddr,
tls: bool,
) -> Option<&CompiledRoute> {
self.select_on_arc(host, path, local, tls).map(Arc::as_ref)
}
/// Shared compiled-route handle for middleware and forward().
pub fn select_on_arc(
&self,
host: &str,
path: &str,
local: SocketAddr,
tls: bool,
) -> Option<&Arc<CompiledRoute>> {
for pattern in Self::host_patterns(host) {
let fallback = self.fallbacks.get(&pattern).and_then(|routes| {
let fallback = self.fallbacks.get(pattern.as_ref()).and_then(|routes| {
routes
.iter()
.find(|route| route.bindings.iter().any(|l| l.matches(local, tls)))
@@ -65,7 +75,7 @@ impl Snapshot {
if let Some(fallback) = fallback {
return self
.routes
.get(&pattern)
.get(pattern.as_ref())
.and_then(|routes| {
select_route(routes, path, |r| {
r.bindings.iter().any(|l| l.matches(local, tls))
@@ -76,7 +86,6 @@ impl Snapshot {
}
None
}
pub fn frontend(
&self,
host: &str,
@@ -84,7 +93,7 @@ impl Snapshot {
tls: bool,
) -> Option<&Arc<CompiledFrontend>> {
for pattern in Self::host_patterns(host) {
if let Some(frontend) = self.frontends.get(&pattern).and_then(|frontends| {
if let Some(frontend) = self.frontends.get(pattern.as_ref()).and_then(|frontends| {
frontends
.iter()
.find(|f| f.bindings.iter().any(|l| l.matches(local, tls)))
@@ -155,7 +164,7 @@ impl Snapshot {
&crate::redirect::CompiledRule,
)> {
for pattern in Self::host_patterns(host) {
if let Some((redirect, rule)) = self.redirects.get(&pattern).and_then(|redirects| {
if let Some((redirect, rule)) = self.redirects.get(pattern.as_ref()).and_then(|redirects| {
redirects.iter().find_map(|redirect| {
if !redirect
.bindings
@@ -184,7 +193,7 @@ impl Snapshot {
tls: bool,
) -> Option<&Arc<crate::not_found::CompiledHost>> {
for pattern in Self::host_patterns(host) {
if let Some(item) = self.not_found_hosts.get(&pattern).and_then(|items| {
if let Some(item) = self.not_found_hosts.get(pattern.as_ref()).and_then(|items| {
items.iter().find(|item| {
item.bindings
.iter()
@@ -202,14 +211,15 @@ impl Snapshot {
for pattern in Self::host_patterns(host) {
if let Some(fallback) = self
.fallbacks
.get(&pattern)
.get(pattern.as_ref())
.and_then(|routes| routes.first())
{
return self
.routes
.get(&pattern)
.get(pattern.as_ref())
.and_then(|routes| select_route(routes, path, |_| true))
.or(Some(fallback));
.or(Some(fallback))
.map(Arc::as_ref);
}
}
None
@@ -217,10 +227,10 @@ impl Snapshot {
}
fn select_route<'a, F>(
routes: &'a [CompiledRoute],
routes: &'a [Arc<CompiledRoute>],
path: &str,
allowed: F,
) -> Option<&'a CompiledRoute>
) -> Option<&'a Arc<CompiledRoute>>
where
F: Fn(&CompiledRoute) -> bool,
{