refactor: enable --directory-listing (-z) option
Diff
src/handler.rs | 9 ++++++---
src/server.rs | 12 +++++++++---
2 files changed, 15 insertions(+), 6 deletions(-)
@@ -5,12 +5,15 @@ use crate::{compression, control_headers, static_files};
use crate::{error::Result, error_page};
pub async fn handle_request(base: &Path, req: &Request<Body>) -> Result<Response<Body>> {
pub async fn handle_request(
base: &Path,
dir_listing: bool,
req: &Request<Body>,
) -> Result<Response<Body>> {
let headers = req.headers();
let method = req.method();
match static_files::handle_request(method, headers, base, req.uri().path(), true).await {
match static_files::handle_request(method, headers, base, req.uri().path(), dir_listing).await {
Ok(resp) => {
let mut resp = compression::auto(method, headers, resp)?;
@@ -57,7 +57,6 @@ impl Server {
logger::init(&opts.log_level)?;
tracing::info!("runtime worker threads {}", self.threads);
tracing::info!("runtime max blocking threads {}", self.threads);
let ip = opts.host.parse::<IpAddr>()?;
let addr = SocketAddr::from((ip, opts.port));
@@ -76,6 +75,9 @@ impl Server {
let dir_listing = opts.directory_listing.unwrap_or_default();
let threads = self.threads;
@@ -91,7 +93,9 @@ impl Server {
async move {
Ok::<_, error::Error>(service_fn(move |req| {
let root_dir = root_dir.clone();
async move { handler::handle_request(root_dir.as_ref(), &req).await }
async move {
handler::handle_request(root_dir.as_ref(), dir_listing, &req).await
}
}))
}
});
@@ -125,7 +129,9 @@ impl Server {
async move {
Ok::<_, error::Error>(service_fn(move |req| {
let root_dir = root_dir.clone();
async move { handler::handle_request(root_dir.as_ref(), &req).await }
async move {
handler::handle_request(root_dir.as_ref(), dir_listing, &req).await
}
}))
}
});