diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2025-03-09 06:55:03 +0000 |
---|---|---|
committer | Sven-Hendrik Haase <svenstaro@gmail.com> | 2025-03-09 06:55:03 +0000 |
commit | 3351805fffadcb9747f772ca9ea05e0d6d13619c (patch) | |
tree | d7c666d625d709354fcf03c31deaaf69d7e90fdb /src | |
parent | Fix lints on Windows (diff) | |
download | miniserve-3351805fffadcb9747f772ca9ea05e0d6d13619c.tar.gz miniserve-3351805fffadcb9747f772ca9ea05e0d6d13619c.zip |
Fix dir size calculation for percent-encoded paths with spaces
Diffstat (limited to 'src')
-rw-r--r-- | src/listing.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 13 | ||||
-rw-r--r-- | src/renderer.rs | 2 |
3 files changed, 11 insertions, 6 deletions
diff --git a/src/listing.rs b/src/listing.rs index 6e50ba1..c98670b 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -23,7 +23,7 @@ use self::percent_encode_sets::COMPONENT; /// "percent-encode sets" as defined by WHATWG specs: /// https://url.spec.whatwg.org/#percent-encoded-bytes -mod percent_encode_sets { +pub mod percent_encode_sets { use percent_encoding::{AsciiSet, CONTROLS}; pub const QUERY: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'#').add(b'<').add(b'>'); pub const PATH: &AsciiSet = &QUERY.add(b'?').add(b'`').add(b'{').add(b'}'); diff --git a/src/main.rs b/src/main.rs index adda3f7..a7166ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,7 @@ use dav_server::{ }; use fast_qr::QRBuilder; use log::{error, info, warn}; +use percent_encoding::percent_decode_str; use serde::Deserialize; mod archive; @@ -455,10 +456,14 @@ async fn api( config: web::Data<MiniserveConfig>, ) -> Result<impl Responder, RuntimeError> { match command.into_inner() { - ApiCommand::DirSize(dir) => { - // Convert the relative dir to an absolute path on the system - let sanitized_path = - file_utils::sanitize_path(&dir, true).expect("Expected a path to directory"); + ApiCommand::DirSize(path) => { + // The dir argument might be percent-encoded so let's decode it just in case. + let decoded_path = percent_decode_str(&path) + .decode_utf8() + .map_err(|e| RuntimeError::ParseError(path.clone(), e.to_string()))?; + // Convert the relative dir to an absolute path on the system. + let sanitized_path = file_utils::sanitize_path(&*decoded_path, true) + .expect("Expected a path to directory"); let full_path = config .path .canonicalize() diff --git a/src/renderer.rs b/src/renderer.rs index d6b01c8..6a8b808 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -684,7 +684,7 @@ fn page_header( body: JSON.stringify({ DirSize: dir }) - }).then(resp => resp.text()) + }).then(resp => resp.ok ? resp.text() : "~") } // Initialize shimmer effects for .size-cell elements in .entry-type-directory rows |