refactor: some query params work as flag and must not accept a value (#431)

This commit is contained in:
sigoden 2024-08-14 08:57:46 +08:00 committed by GitHub
parent 5d26103ea2
commit 1db263efae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -251,7 +251,7 @@ impl Server {
Method::GET | Method::HEAD => { Method::GET | Method::HEAD => {
if is_dir { if is_dir {
if render_try_index { if render_try_index {
if allow_archive && query_params.contains_key("zip") { if allow_archive && has_query_flag(&query_params, "zip") {
if !allow_archive { if !allow_archive {
status_not_found(&mut res); status_not_found(&mut res);
return Ok(res); return Ok(res);
@ -291,7 +291,7 @@ impl Server {
&mut res, &mut res,
) )
.await?; .await?;
} else if query_params.contains_key("zip") { } else if has_query_flag(&query_params, "zip") {
if !allow_archive { if !allow_archive {
status_not_found(&mut res); status_not_found(&mut res);
return Ok(res); return Ok(res);
@ -321,13 +321,13 @@ impl Server {
.await?; .await?;
} }
} else if is_file { } 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) self.handle_edit_file(path, DataKind::Edit, head_only, user, &mut res)
.await?; .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) self.handle_edit_file(path, DataKind::View, head_only, user, &mut res)
.await?; .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?; self.handle_hash_file(path, head_only, &mut res).await?;
} else { } else {
self.handle_send_file(path, headers, head_only, &mut res) self.handle_send_file(path, headers, head_only, &mut res)
@ -1134,7 +1134,7 @@ impl Server {
} else { } else {
paths.sort_by(|v1, v2| v1.sort_by_name(v2)) 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 let output = paths
.into_iter() .into_iter()
.map(|v| { .map(|v| {
@ -1174,7 +1174,7 @@ impl Server {
user, user,
paths, paths,
}; };
let output = if query_params.contains_key("json") { let output = if has_query_flag(query_params, "json") {
res.headers_mut() res.headers_mut()
.typed_insert(ContentType::from(mime_guess::mime::APPLICATION_JSON)); .typed_insert(ContentType::from(mime_guess::mime::APPLICATION_JSON));
serde_json::to_string_pretty(&data)? serde_json::to_string_pretty(&data)?
@ -1786,3 +1786,10 @@ async fn sha256_file(path: &Path) -> Result<String> {
let result = hasher.finalize(); let result = hasher.finalize();
Ok(format!("{:x}", result)) Ok(format!("{:x}", result))
} }
fn has_query_flag(query_params: &HashMap<String, String>, name: &str) -> bool {
query_params
.get(name)
.map(|v| v.is_empty())
.unwrap_or_default()
}