index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2021-05-18 15:14:28.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2021-05-18 15:14:28.0 +00:00:00
commit
80a1723644ae80a4db94f0a6813399abde1e995b [patch]
tree
bd3f6ea8be5fa6b00d4e1910baad1095469242e4
parent
104a3b57ff6feeab43529cb56ec200fe0be8b96f
download
80a1723644ae80a4db94f0a6813399abde1e995b.tar.gz

refactor: enable --directory-listing (-z) option



Diff

 src/handler.rs |  9 ++++++---
 src/server.rs  | 12 +++++++++---
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/handler.rs b/src/handler.rs
index 7fa45c2..4a9ec48 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -5,12 +5,15 @@ use crate::{compression, control_headers, static_files};
use crate::{error::Result, error_page};

/// Main server request handler.
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();

    // TODO: replace `dir_listing` arg with the corresponding config option
    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) => {
            // Compression on demand based on`Accept-Encoding` header
            let mut resp = compression::auto(method, headers, resp)?;
diff --git a/src/server.rs b/src/server.rs
index 25c47e9..ae40d51 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -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 {

        // TODO: CORS support

        // Directory listing option
        let dir_listing = opts.directory_listing.unwrap_or_default();

        // Spawn a new Tokio asynchronous server task with its given options
        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
                            }
                        }))
                    }
                });