From cdc2e8c14824a5d58154c5df42d1bc79495d69ea Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 14 Mar 2025 13:31:41 +0100 Subject: hosts: :fallback and :none special hosts --- readme.md | 2 ++ src/modules/hosts.rs | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 507ca34..03bdbc5 100644 --- a/readme.md +++ b/readme.md @@ -95,6 +95,8 @@ themselves; in that case the request is passed on. - **module `hosts`** - Hands over the requests to different modules depending on the `host` header. + Special keys are `:fallback` which handles hosts that are not found in the + map and `:none` to handle requests without a host header. - Takes a map from hostname (string) to handler (module) - **module `paths`** diff --git a/src/modules/hosts.rs b/src/modules/hosts.rs index 6bd213a..20c6c85 100644 --- a/src/modules/hosts.rs +++ b/src/modules/hosts.rs @@ -30,10 +30,16 @@ impl Node for Hosts { .headers() .get(HOST) .and_then(|e| e.to_str().ok()) - .map(remove_port) - .ok_or(ServiceError::NoHost)?; + .map(remove_port); - let node = self.0.get(host).ok_or(ServiceError::UnknownHost)?; + let node = match host { + Some(host) => self + .0 + .get(host) + .or_else(|| self.0.get(":fallback")) + .ok_or(ServiceError::UnknownHost)?, + None => self.0.get(":none").ok_or(ServiceError::NoHost)?, + }; node.handle(context, request).await }) -- cgit v1.2.3-70-g09d2