From 116cbbfcfe052808b0305a67a77eeb4f411f62c0 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Sun, 14 Feb 2021 02:41:00 +0100 Subject: [PATCH] refactor: decode url path --- Cargo.lock | 9 ++++++++- Cargo.toml | 1 + src/server.rs | 2 +- src/staticfile_middleware/staticfile.rs | 12 +++++++----- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 503e664..cf206cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -501,6 +501,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" [[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + +[[package]] name = "phf" version = "0.7.24" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -872,6 +878,7 @@ dependencies = [ "mime_guess", "nix", "openssl", + "percent-encoding 2.1.0", "signal", "structopt", "tempdir", @@ -1055,7 +1062,7 @@ checksum = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" dependencies = [ "idna", "matches", - "percent-encoding", + "percent-encoding 1.0.1", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 0a9c641..fec4ee2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,6 +38,7 @@ mime_guess = "1.8" structopt = { version = "0.3", default-features = false } time = "0.1" url = "1.4" +percent-encoding = "2.1" [target.'cfg(not(windows))'.dependencies.nix] version = "0.14" diff --git a/src/server.rs b/src/server.rs index 2550d32..2db456b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -109,5 +109,5 @@ fn handle_signals() { #[cfg(windows)] fn handle_signals() { - println!("TODO: Windows signals...") + // TODO: Windows signals... } diff --git a/src/staticfile_middleware/staticfile.rs b/src/staticfile_middleware/staticfile.rs index f3e9fd4..a44bbf7 100644 --- a/src/staticfile_middleware/staticfile.rs +++ b/src/staticfile_middleware/staticfile.rs @@ -8,6 +8,7 @@ use iron::middleware::Handler; use iron::modifiers::Header; use iron::prelude::*; use iron::status; +use percent_encoding::percent_decode_str; use std::fs::{File, Metadata}; use std::path::{Path, PathBuf}; use std::time::{SystemTime, UNIX_EPOCH}; @@ -40,17 +41,17 @@ impl Staticfile { } fn resolve_path(&self, path: &[&str]) -> Result> { - let current_dirname = path[0]; + let current_dirname = percent_decode_str(path[0]).decode_utf8()?; let asserts_dirname = self.assets.iter().last().unwrap().to_str().unwrap(); let mut is_assets = false; - let path_resolved = if current_dirname == asserts_dirname { + let path_resolved = if current_dirname.as_ref() == asserts_dirname { // Assets path validation resolve is_assets = true; let mut res = self.assets.clone(); for component in path.iter().skip(1) { - res.push(component); + res.push(percent_decode_str(component).decode_utf8()?.as_ref()); } res @@ -58,7 +59,7 @@ impl Staticfile { // Root path validation resolve let mut res = self.root.clone(); for component in path { - res.push(component); + res.push(percent_decode_str(component).decode_utf8()?.as_ref()); } res @@ -146,6 +147,7 @@ impl Handler for Staticfile { name = format!("{}/", name); filesize = String::from("-") } + let uri = format!("{}{}", current_path, name); let modified = parse_last_modified(meta.modified().unwrap()).unwrap(); @@ -161,7 +163,7 @@ impl Handler for Staticfile { } let page_str = format!( - "Index of {}

Index of {}

{}


", current_path, current_path, entries_str + "Index of {}

Index of {}

{}


", current_path, current_path, entries_str ); let len = page_str.len() as u64; let content_encoding = ContentEncoding(vec![Encoding::Identity]); -- libgit2 1.7.2