refactor: decode url path
Diff
Cargo.lock | 9 ++++++++-
Cargo.toml | 1 +
src/server.rs | 2 +-
src/staticfile_middleware/staticfile.rs | 12 +++++++-----
4 files changed, 17 insertions(+), 7 deletions(-)
@@ -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]]
@@ -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"
@@ -109,5 +109,5 @@ fn handle_signals() {
#[cfg(windows)]
fn handle_signals() {
println!("TODO: Windows signals...")
}
@@ -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<PathBuf, Box<dyn error::Error>> {
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 {
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 {
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!(
"<html><head><title>Index of {}</title></head><body><h1>Index of {}</h1><table style=\"min-width:680px;\"><tr><th colspan=\"3\"><hr></th></tr>{}<tr><th colspan=\"3\"><hr></th></tr></table></body></html>", current_path, current_path, entries_str
"<html><head><meta charset=\"utf-8\"><title>Index of {}</title></head><body><h1>Index of {}</h1><table style=\"min-width:680px;\"><tr><th colspan=\"3\"><hr></th></tr>{}<tr><th colspan=\"3\"><hr></th></tr></table></body></html>", current_path, current_path, entries_str
);
let len = page_str.len() as u64;
let content_encoding = ContentEncoding(vec![Encoding::Identity]);