aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--readme.md2
-rw-r--r--src/modules/hosts.rs12
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
})