Skip to content

Commit

Permalink
feat: log virtual-hosts information per request (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed May 12, 2024
1 parent d4046d9 commit 778477e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/handler.rs
Expand Up @@ -235,7 +235,7 @@ impl RequestHandler {
if let Some(advanced) = &self.opts.advanced_opts {
// If the "Host" header matches any virtual_host, change the root directory
if let Some(root) =
virtual_hosts::get_real_root(req.headers(), advanced.virtual_hosts.as_deref())
virtual_hosts::get_real_root(req, advanced.virtual_hosts.as_deref())
{
base_path = root;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -194,7 +194,7 @@ pub mod static_files;
#[cfg_attr(docsrs, doc(cfg(feature = "http2")))]
pub mod tls;
pub mod transport;
pub mod virtual_hosts;
pub(crate) mod virtual_hosts;
#[cfg(windows)]
#[cfg_attr(docsrs, doc(cfg(windows)))]
pub mod winservice;
Expand Down
15 changes: 11 additions & 4 deletions src/virtual_hosts.rs
Expand Up @@ -6,21 +6,28 @@
//! Module that allows to determine a virtual hostname.
//!

use headers::HeaderMap;
use hyper::header::HOST;
use hyper::Request;
use std::path::PathBuf;

use crate::settings::VirtualHosts;

/// It returns different root directory if the "Host" header matches a virtual hostname.
pub fn get_real_root<'a>(
headers: &HeaderMap,
pub(crate) fn get_real_root<'a, T>(
req: &mut Request<T>,
vhosts_opts: Option<&'a [VirtualHosts]>,
) -> Option<&'a PathBuf> {
if let Some(vhosts) = vhosts_opts {
if let Ok(host_str) = headers.get(HOST)?.to_str() {
if let Ok(host_str) = req.headers().get(HOST)?.to_str() {
for vhost in vhosts {
if vhost.host == host_str {
tracing::info!(
"virtual host matched: vhost={} vhost_root={} method={} uri={}",
vhost.host,
vhost.root.display(),
req.method(),
req.uri(),
);
return Some(&vhost.root);
}
}
Expand Down

0 comments on commit 778477e

Please sign in to comment.