From af866aaaf443dda5e255373cd50d5fac167b3d3d Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 17 Jun 2022 19:02:13 +0800 Subject: [PATCH] chore: optimize --render-try-index --- README.md | 2 +- src/args.rs | 11 +++++------ src/server.rs | 7 +++++-- tests/render.rs | 11 +++++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9fd702b..3e375c6 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ OPTIONS: --allow-delete Allow delete files/folders --allow-symlink Allow symlink to files/folders outside root directory --render-index Render index.html when requesting a directory - --render-try-index Try rendering index.html when requesting a directory + --render-try-index Render index.html if it exists when requesting a directory --render-spa Render for single-page application --cors Enable CORS, sets `Access-Control-Allow-Origin: *` --tls-cert Path to an SSL/TLS certificate to serve with HTTPS diff --git a/src/args.rs b/src/args.rs index 2701540..d5e3897 100644 --- a/src/args.rs +++ b/src/args.rs @@ -88,7 +88,7 @@ fn app() -> Command<'static> { .arg( Arg::new("render-try-index") .long("render-try-index") - .help("Try rendering index.html when requesting a directory"), + .help("Render index.html if it exists when requesting a directory"), ) .arg( Arg::new("render-spa") @@ -132,7 +132,7 @@ pub struct Args { pub allow_symlink: bool, pub render_index: bool, pub render_spa: bool, - pub render_index_fallback: bool, + pub render_try_index: bool, pub cors: bool, pub tls: Option<(Vec, PrivateKey)>, } @@ -168,9 +168,8 @@ impl Args { let allow_upload = matches.is_present("allow-all") || matches.is_present("allow-upload"); let allow_delete = matches.is_present("allow-all") || matches.is_present("allow-delete"); let allow_symlink = matches.is_present("allow-all") || matches.is_present("allow-symlink"); - let render_index = - matches.is_present("render-index") || matches.is_present("render-try-index"); - let render_index_fallback = matches.is_present("render-try-index"); + let render_index = matches.is_present("render-index"); + let render_try_index = matches.is_present("render-try-index"); let render_spa = matches.is_present("render-spa"); let tls = match (matches.value_of("tls-cert"), matches.value_of("tls-key")) { (Some(certs_file), Some(key_file)) => { @@ -194,7 +193,7 @@ impl Args { allow_upload, allow_symlink, render_index, - render_index_fallback, + render_try_index, render_spa, tls, }) diff --git a/src/server.rs b/src/server.rs index 18503a8..bacde12 100644 --- a/src/server.rs +++ b/src/server.rs @@ -119,6 +119,7 @@ impl Server { let allow_delete = self.args.allow_delete; let render_index = self.args.render_index; let render_spa = self.args.render_spa; + let render_try_index = self.args.render_try_index; if !self.args.allow_symlink && !is_miss && !self.is_root_contained(path).await { status_not_found(&mut res); @@ -129,7 +130,9 @@ impl Server { Method::GET | Method::HEAD => { let head_only = method == Method::HEAD; if is_dir { - if render_index || render_spa { + if render_try_index && query == "zip" { + self.handle_zip_dir(path, head_only, &mut res).await?; + } else if render_index || render_spa || render_try_index { self.handle_render_index(path, headers, head_only, &mut res) .await?; } else if query == "zip" { @@ -375,7 +378,7 @@ impl Server { { self.handle_send_file(&index_path, headers, head_only, res) .await?; - } else if self.args.render_index_fallback { + } else if self.args.render_try_index { self.handle_ls_dir(path, true, head_only, res).await?; } else { status_not_found(res) diff --git a/tests/render.rs b/tests/render.rs index 70f041b..9611113 100644 --- a/tests/render.rs +++ b/tests/render.rs @@ -39,6 +39,17 @@ fn render_try_index2(#[with(&["--render-try-index"])] server: TestServer) -> Res Ok(()) } +#[rstest] +fn render_try_index3(#[with(&["--render-try-index"])] server: TestServer) -> Result<(), Error> { + let resp = reqwest::blocking::get(format!("{}{}?zip", server.url(), DIR_NO_INDEX))?; + assert_eq!(resp.status(), 200); + assert_eq!( + resp.headers().get("content-type").unwrap(), + "application/zip" + ); + Ok(()) +} + #[rstest] fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> { let resp = reqwest::blocking::get(server.url())?;