forked from static-web-server/static-web-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
714897a
commit c74c06d
Showing
11 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Virtual Hosting | ||
|
||
**SWS** provides rudimentary support for name-based [virtual hosting](https://en.wikipedia.org/wiki/Virtual_hosting#Name-based). This allows you to serve files from different root directories depending on the ["Host" header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/host) of the request, with all other settings staying the same. | ||
|
||
!!! warning "All other settings are the same!" | ||
Each virtual host has to have all the same settings (aside from `root`). If using TLS, your certificates will have to cover all virtual host names as Subject Alternative Names (SANs). Also, beware of other conflicting settings like redirects and rewrites. If you find yourself needing different settings for different virtual hosts, it is recommended to run multiple instances of SWS. | ||
|
||
Virtual hosting can be useful for serving more than one static website from the same SWS instance, if it's not otherwise feasible to run multiple instances of SWS. Browsers will automatically send a `Host` header which matches the hostname in the URL bar, which is how HTTP servers are able to tell which "virtual" host that the client is accessing. | ||
|
||
By default, SWS will always serve files from the main `root` directory. If you configure virtual hosting and the "Host" header matches, SWS will instead look for files in an alternate root directory you specify. | ||
|
||
## Examples | ||
|
||
```toml | ||
# By default, all requests are served from here | ||
root = "/var/www/html" | ||
|
||
[advanced] | ||
|
||
[[advanced.virtual-hosts]] | ||
# But if the "Host" header matches this... | ||
host = "sales.example.com" | ||
# ...then files will be served from here instead | ||
root = "/var/sales/html" | ||
|
||
[[advanced.virtual-hosts]] | ||
host = "blog.example.com" | ||
root = "/var/blog/html" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
// This file is part of Static Web Server. | ||
// See https://static-web-server.net/ for more information | ||
// Copyright (C) 2019-present Jose Quintana <joseluisq.net> | ||
|
||
//! Module that allows to rewrite request URLs with pattern matching support. | ||
//! | ||
use hyper::{header::HOST, HeaderMap}; | ||
use std::path::PathBuf; | ||
|
||
use crate::settings::VirtualHosts; | ||
|
||
/// It returns different root dir if the "Host" header matches a virtual hostname. | ||
pub fn get_real_root<'a>( | ||
vhosts_vec: &'a Option<Vec<VirtualHosts>>, | ||
headers: &HeaderMap, | ||
) -> Option<&'a PathBuf> { | ||
if let Some(vhosts) = vhosts_vec { | ||
if let Ok(host_str) = headers.get(HOST)?.to_str() { | ||
for vhost in vhosts { | ||
if vhost.host == host_str { | ||
return Some(&vhost.root); | ||
} | ||
} | ||
} | ||
} | ||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#![forbid(unsafe_code)] | ||
#![deny(warnings)] | ||
#![deny(rust_2018_idioms)] | ||
#![deny(dead_code)] | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use static_web_server::settings::file::Settings; | ||
use std::path::{Path, PathBuf}; | ||
|
||
#[tokio::test] | ||
async fn toml_file_parsing() { | ||
let config_path = Path::new("tests/toml/config.toml"); | ||
let settings = Settings::read(config_path).unwrap(); | ||
let root = settings.general.unwrap().root.unwrap(); | ||
assert_eq!(root, PathBuf::from("docker/public")); | ||
|
||
let virtual_hosts = settings.advanced.unwrap().virtual_hosts.unwrap(); | ||
let expected_roots = [PathBuf::from("docker"), PathBuf::from("docker/abc")]; | ||
for vhost in virtual_hosts { | ||
if let Some(other_root) = &vhost.root { | ||
assert!(expected_roots.contains(other_root)); | ||
} else { | ||
panic!("Could not determine value of advanced.virtual-hosts.root") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters