diff options
author | metamuffin <metamuffin@disroot.org> | 2025-03-14 13:31:41 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-03-14 13:31:41 +0100 |
commit | cdc2e8c14824a5d58154c5df42d1bc79495d69ea (patch) | |
tree | 542f8e75cd97af40bbbef49b971c3702df1761eb | |
parent | 28cec8f77e8fc9247a6f067736b08ed27e9f4088 (diff) | |
download | gnix-cdc2e8c14824a5d58154c5df42d1bc79495d69ea.tar gnix-cdc2e8c14824a5d58154c5df42d1bc79495d69ea.tar.bz2 gnix-cdc2e8c14824a5d58154c5df42d1bc79495d69ea.tar.zst |
hosts: :fallback and :none special hosts
-rw-r--r-- | readme.md | 2 | ||||
-rw-r--r-- | src/modules/hosts.rs | 12 |
2 files changed, 11 insertions, 3 deletions
@@ -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 }) |