chore: optimize --render-try-index

This commit is contained in:
sigoden 2022-06-17 19:02:13 +08:00
parent 68d238d34d
commit af866aaaf4
4 changed files with 22 additions and 9 deletions

View file

@ -59,7 +59,7 @@ OPTIONS:
--allow-delete Allow delete files/folders --allow-delete Allow delete files/folders
--allow-symlink Allow symlink to files/folders outside root directory --allow-symlink Allow symlink to files/folders outside root directory
--render-index Render index.html when requesting a 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 --render-spa Render for single-page application
--cors Enable CORS, sets `Access-Control-Allow-Origin: *` --cors Enable CORS, sets `Access-Control-Allow-Origin: *`
--tls-cert <path> Path to an SSL/TLS certificate to serve with HTTPS --tls-cert <path> Path to an SSL/TLS certificate to serve with HTTPS

View file

@ -88,7 +88,7 @@ fn app() -> Command<'static> {
.arg( .arg(
Arg::new("render-try-index") Arg::new("render-try-index")
.long("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(
Arg::new("render-spa") Arg::new("render-spa")
@ -132,7 +132,7 @@ pub struct Args {
pub allow_symlink: bool, pub allow_symlink: bool,
pub render_index: bool, pub render_index: bool,
pub render_spa: bool, pub render_spa: bool,
pub render_index_fallback: bool, pub render_try_index: bool,
pub cors: bool, pub cors: bool,
pub tls: Option<(Vec<Certificate>, PrivateKey)>, pub tls: Option<(Vec<Certificate>, PrivateKey)>,
} }
@ -168,9 +168,8 @@ impl Args {
let allow_upload = matches.is_present("allow-all") || matches.is_present("allow-upload"); 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_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 allow_symlink = matches.is_present("allow-all") || matches.is_present("allow-symlink");
let render_index = let render_index = matches.is_present("render-index");
matches.is_present("render-index") || matches.is_present("render-try-index"); let render_try_index = matches.is_present("render-try-index");
let render_index_fallback = matches.is_present("render-try-index");
let render_spa = matches.is_present("render-spa"); let render_spa = matches.is_present("render-spa");
let tls = match (matches.value_of("tls-cert"), matches.value_of("tls-key")) { let tls = match (matches.value_of("tls-cert"), matches.value_of("tls-key")) {
(Some(certs_file), Some(key_file)) => { (Some(certs_file), Some(key_file)) => {
@ -194,7 +193,7 @@ impl Args {
allow_upload, allow_upload,
allow_symlink, allow_symlink,
render_index, render_index,
render_index_fallback, render_try_index,
render_spa, render_spa,
tls, tls,
}) })

View file

@ -119,6 +119,7 @@ impl Server {
let allow_delete = self.args.allow_delete; let allow_delete = self.args.allow_delete;
let render_index = self.args.render_index; let render_index = self.args.render_index;
let render_spa = self.args.render_spa; 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 { if !self.args.allow_symlink && !is_miss && !self.is_root_contained(path).await {
status_not_found(&mut res); status_not_found(&mut res);
@ -129,7 +130,9 @@ impl Server {
Method::GET | Method::HEAD => { Method::GET | Method::HEAD => {
let head_only = method == Method::HEAD; let head_only = method == Method::HEAD;
if is_dir { 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) self.handle_render_index(path, headers, head_only, &mut res)
.await?; .await?;
} else if query == "zip" { } else if query == "zip" {
@ -375,7 +378,7 @@ impl Server {
{ {
self.handle_send_file(&index_path, headers, head_only, res) self.handle_send_file(&index_path, headers, head_only, res)
.await?; .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?; self.handle_ls_dir(path, true, head_only, res).await?;
} else { } else {
status_not_found(res) status_not_found(res)

View file

@ -39,6 +39,17 @@ fn render_try_index2(#[with(&["--render-try-index"])] server: TestServer) -> Res
Ok(()) 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] #[rstest]
fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> { fn render_spa(#[with(&["--render-spa"])] server: TestServer) -> Result<(), Error> {
let resp = reqwest::blocking::get(server.url())?; let resp = reqwest::blocking::get(server.url())?;