From 1db263efae2853cf90167047acbfa835a844c9c8 Mon Sep 17 00:00:00 2001 From: sigoden Date: Wed, 14 Aug 2024 08:57:46 +0800 Subject: [PATCH] refactor: some query params work as flag and must not accept a value (#431) --- src/server.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/server.rs b/src/server.rs index 404ada4..414f578 100644 --- a/src/server.rs +++ b/src/server.rs @@ -251,7 +251,7 @@ impl Server { Method::GET | Method::HEAD => { if is_dir { if render_try_index { - if allow_archive && query_params.contains_key("zip") { + if allow_archive && has_query_flag(&query_params, "zip") { if !allow_archive { status_not_found(&mut res); return Ok(res); @@ -291,7 +291,7 @@ impl Server { &mut res, ) .await?; - } else if query_params.contains_key("zip") { + } else if has_query_flag(&query_params, "zip") { if !allow_archive { status_not_found(&mut res); return Ok(res); @@ -321,13 +321,13 @@ impl Server { .await?; } } else if is_file { - if query_params.contains_key("edit") { + if has_query_flag(&query_params, "edit") { self.handle_edit_file(path, DataKind::Edit, head_only, user, &mut res) .await?; - } else if query_params.contains_key("view") { + } else if has_query_flag(&query_params, "view") { self.handle_edit_file(path, DataKind::View, head_only, user, &mut res) .await?; - } else if query_params.contains_key("hash") { + } else if has_query_flag(&query_params, "hash") { self.handle_hash_file(path, head_only, &mut res).await?; } else { self.handle_send_file(path, headers, head_only, &mut res) @@ -1134,7 +1134,7 @@ impl Server { } else { paths.sort_by(|v1, v2| v1.sort_by_name(v2)) } - if query_params.contains_key("simple") { + if has_query_flag(query_params, "simple") { let output = paths .into_iter() .map(|v| { @@ -1174,7 +1174,7 @@ impl Server { user, paths, }; - let output = if query_params.contains_key("json") { + let output = if has_query_flag(query_params, "json") { res.headers_mut() .typed_insert(ContentType::from(mime_guess::mime::APPLICATION_JSON)); serde_json::to_string_pretty(&data)? @@ -1786,3 +1786,10 @@ async fn sha256_file(path: &Path) -> Result { let result = hasher.finalize(); Ok(format!("{:x}", result)) } + +fn has_query_flag(query_params: &HashMap, name: &str) -> bool { + query_params + .get(name) + .map(|v| v.is_empty()) + .unwrap_or_default() +}