From 1a9990f04e5a0aaefb330873f8daece896a0f2ad Mon Sep 17 00:00:00 2001 From: David Politis Date: Thu, 10 Nov 2022 21:02:55 +1100 Subject: [PATCH] fix: don't search on empty query string (#140) * fix: don't search on empty query string * refactor Co-authored-by: sigoden --- assets/index.html | 2 +- src/server.rs | 64 ++++++++++++++++++++++++----------------------- tests/http.rs | 9 +++++++ 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/assets/index.html b/assets/index.html index 7a4cbe1..7432da2 100644 --- a/assets/index.html +++ b/assets/index.html @@ -37,7 +37,7 @@
- + diff --git a/src/server.rs b/src/server.rs index e6ed1ff..77deb8d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -386,41 +386,43 @@ impl Server { res: &mut Response, ) -> BoxResult<()> { let mut paths: Vec = vec![]; - let path_buf = path.to_path_buf(); - let hidden = Arc::new(self.args.hidden.to_vec()); - let hidden = hidden.clone(); - let running = self.running.clone(); let search = query_params.get("q").unwrap().to_lowercase(); - let search_paths = tokio::task::spawn_blocking(move || { - let mut it = WalkDir::new(&path_buf).into_iter(); - let mut paths: Vec = vec![]; - while let Some(Ok(entry)) = it.next() { - if !running.load(Ordering::SeqCst) { - break; - } - let entry_path = entry.path(); - let base_name = get_file_name(entry_path); - let file_type = entry.file_type(); - if is_hidden(&hidden, base_name) { - if file_type.is_dir() { - it.skip_current_dir(); + if !search.is_empty() { + let path_buf = path.to_path_buf(); + let hidden = Arc::new(self.args.hidden.to_vec()); + let hidden = hidden.clone(); + let running = self.running.clone(); + let search_paths = tokio::task::spawn_blocking(move || { + let mut it = WalkDir::new(&path_buf).into_iter(); + let mut paths: Vec = vec![]; + while let Some(Ok(entry)) = it.next() { + if !running.load(Ordering::SeqCst) { + break; } - continue; + let entry_path = entry.path(); + let base_name = get_file_name(entry_path); + let file_type = entry.file_type(); + if is_hidden(&hidden, base_name) { + if file_type.is_dir() { + it.skip_current_dir(); + } + continue; + } + if !base_name.to_lowercase().contains(&search) { + continue; + } + if entry.path().symlink_metadata().is_err() { + continue; + } + paths.push(entry_path.to_path_buf()); } - if !base_name.to_lowercase().contains(&search) { - continue; + paths + }) + .await?; + for search_path in search_paths.into_iter() { + if let Ok(Some(item)) = self.to_pathitem(search_path, path.to_path_buf()).await { + paths.push(item); } - if entry.path().symlink_metadata().is_err() { - continue; - } - paths.push(entry_path.to_path_buf()); - } - paths - }) - .await?; - for search_path in search_paths.into_iter() { - if let Ok(Some(item)) = self.to_pathitem(search_path, path.to_path_buf()).await { - paths.push(item); } } self.send_index(path, paths, true, query_params, head_only, res) diff --git a/tests/http.rs b/tests/http.rs index 1c95c67..7cb3b77 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -98,6 +98,15 @@ fn head_dir_search(#[with(&["-A"])] server: TestServer) -> Result<(), Error> { Ok(()) } +#[rstest] +fn empty_search(#[with(&["-A"])] server: TestServer) -> Result<(), Error> { + let resp = reqwest::blocking::get(format!("{}?q=", server.url()))?; + assert_eq!(resp.status(), 200); + let paths = utils::retrieve_index_paths(&resp.text()?); + assert!(paths.is_empty()); + Ok(()) +} + #[rstest] fn get_file(server: TestServer) -> Result<(), Error> { let resp = reqwest::blocking::get(format!("{}index.html", server.url()))?;