]> OzVa Git service - ozva-cloud/commitdiff
fix: don't search on empty query string (#140)
authorDavid Politis <davidpolitis@users.noreply.github.com>
Thu, 10 Nov 2022 10:02:55 +0000 (21:02 +1100)
committerGitHub <noreply@github.com>
Thu, 10 Nov 2022 10:02:55 +0000 (18:02 +0800)
* fix: don't search on empty query string

* refactor

Co-authored-by: sigoden <sigoden@gmail.com>
assets/index.html
src/server.rs
tests/http.rs

index 7a4cbe111d173347c424b2942a52795195b66eda..7432da21378501670c100608fee7d0d41f45fd8d 100644 (file)
@@ -37,7 +37,7 @@
       <div class="icon">
         <svg width="16" height="16" fill="currentColor" viewBox="0 0 16 16"><path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/></svg>
       </div>
-      <input id="search" name="q" type="text" maxlength="128" autocomplete="off" tabindex="1">
+      <input id="search" name="q" type="text" maxlength="128" autocomplete="off" tabindex="1" required>
       <input type="submit" hidden />
     </form>
   </div>
index e6ed1ff949419c50845bc3f6f80b198d01a7a860..77deb8d1427dc52fcf93c14289abacb145f8d3ce 100644 (file)
@@ -386,41 +386,43 @@ impl Server {
         res: &mut Response,
     ) -> BoxResult<()> {
         let mut paths: Vec<PathItem> = 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<PathBuf> = 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<PathBuf> = vec![];
+                while let Some(Ok(entry)) = it.next() {
+                    if !running.load(Ordering::SeqCst) {
+                        break;
                     }
-                    continue;
-                }
-                if !base_name.to_lowercase().contains(&search) {
-                    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 entry.path().symlink_metadata().is_err() {
-                    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);
                 }
-                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)
index 1c95c671173ff0fce55602fd95b34954d82a3bf1..7cb3b77431b43a2d8c7f690a79a58b9ca7fdfdf5 100644 (file)
@@ -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()))?;