fix: don't search on empty query string (#140)

* fix: don't search on empty query string

* refactor

Co-authored-by: sigoden <sigoden@gmail.com>
This commit is contained in:
David Politis 2022-11-10 21:02:55 +11:00 committed by GitHub
parent bd07783cde
commit 1a9990f04e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 32 deletions

View file

@ -37,7 +37,7 @@
<div class="icon"> <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> <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> </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 /> <input type="submit" hidden />
</form> </form>
</div> </div>

View file

@ -386,11 +386,12 @@ impl Server {
res: &mut Response, res: &mut Response,
) -> BoxResult<()> { ) -> BoxResult<()> {
let mut paths: Vec<PathItem> = vec![]; let mut paths: Vec<PathItem> = vec![];
let search = query_params.get("q").unwrap().to_lowercase();
if !search.is_empty() {
let path_buf = path.to_path_buf(); let path_buf = path.to_path_buf();
let hidden = Arc::new(self.args.hidden.to_vec()); let hidden = Arc::new(self.args.hidden.to_vec());
let hidden = hidden.clone(); let hidden = hidden.clone();
let running = self.running.clone(); let running = self.running.clone();
let search = query_params.get("q").unwrap().to_lowercase();
let search_paths = tokio::task::spawn_blocking(move || { let search_paths = tokio::task::spawn_blocking(move || {
let mut it = WalkDir::new(&path_buf).into_iter(); let mut it = WalkDir::new(&path_buf).into_iter();
let mut paths: Vec<PathBuf> = vec![]; let mut paths: Vec<PathBuf> = vec![];
@ -423,6 +424,7 @@ impl Server {
paths.push(item); paths.push(item);
} }
} }
}
self.send_index(path, paths, true, query_params, head_only, res) self.send_index(path, paths, true, query_params, head_only, res)
} }

View file

@ -98,6 +98,15 @@ fn head_dir_search(#[with(&["-A"])] server: TestServer) -> Result<(), Error> {
Ok(()) 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] #[rstest]
fn get_file(server: TestServer) -> Result<(), Error> { fn get_file(server: TestServer) -> Result<(), Error> {
let resp = reqwest::blocking::get(format!("{}index.html", server.url()))?; let resp = reqwest::blocking::get(format!("{}index.html", server.url()))?;