From 2e2e6a5f32dadc8139946ca2e4e257d65924987c Mon Sep 17 00:00:00 2001
From: _ <_@_>
Date: Fri, 30 Oct 2020 16:46:03 -0500
Subject: [PATCH] Use percent encoding for spaces in file names
---
Cargo.toml | 1 +
src/bin/server.rs | 20 +++++++++++++++-----
todo.md | 2 +-
3 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
index d2bb7bb..e3a6b86 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,6 +14,7 @@ futures = "0.3"
http = "0.2"
hyper = "0.13"
lazy_static = "1.4"
+percent-encoding = "2.1"
regex = "1"
reqwest = { version = "0.10.8", features = ["stream"] }
rmp-serde = "0.14.4"
diff --git a/src/bin/server.rs b/src/bin/server.rs
index 9c724f3..b5f55f9 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -120,7 +120,12 @@ async fn serve_dir (
}
};
- let s = format! ("
{}{}\n", file_name, trailing_slash, file_name, trailing_slash);
+ use std::borrow::Cow;
+ use percent_encoding::*;
+
+ let percent_file_name: Cow = utf8_percent_encode (&file_name, CONTROLS).into ();
+
+ let s = format! ("{}{}\n", percent_file_name, trailing_slash, file_name, trailing_slash);
if tx.send (Ok::<_, Infallible> (s.into_bytes ())).await.is_err ()
{
break;
@@ -287,18 +292,23 @@ async fn handle_all (
//println! ("Step 6");
- let mut path = PathBuf::from ("/home/user");
- path.push (&uri [1..]);
+ use percent_encoding::*;
+
+ let encoded_path = &uri [1..];
+ let path = percent_decode (encoded_path.as_bytes ()).decode_utf8 ().unwrap ();
+
+ let mut full_path = PathBuf::from ("/home/user");
+ full_path.push (&*path);
let response_handle = ResponseHandle {
client,
req_id: &req_id,
};
- if let Ok (dir) = read_dir (&path).await {
+ if let Ok (dir) = read_dir (&full_path).await {
serve_dir (response_handle, dir).await;
}
- else if let Ok (file) = File::open (&path).await {
+ else if let Ok (file) = File::open (&full_path).await {
serve_file (
response_handle,
file,
diff --git a/todo.md b/todo.md
index 81cc28f..7d9e5df 100644
--- a/todo.md
+++ b/todo.md
@@ -1,6 +1,6 @@
-- Handle URL encoding for spaces in file names
- Set up tokens or something so clients can't trivially
impersonate servers
+- Offer list of clients at server root
- Fix possible timing gap when refreshing http_listen (Just have client wait a few seconds?)
- Parameter for server URL
- Parameter for static file serve path