From 7f37d4a775d30416fc609696ef0f0448702c9aec Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 25 Mar 2024 14:45:38 +0100 Subject: [PATCH] work around weird canonicalize() behaviour on windows --- src/main.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index fada7ed..83de7c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,10 +113,18 @@ fn handle_request(request: String, stream: &mut TcpStream) -> bool { fn get_file(request: Request) -> Option<(Content, bool)> { const MAX_SIZE: usize = 1024 * 1024 * 8; - let path = PathBuf::from(format!("./{}", &request.path)) + let current_dir = env::current_dir().unwrap(); + + let path = current_dir + .join(request.path.strip_prefix('/')?) .canonicalize() .ok()?; - if path.strip_prefix(env::current_dir().unwrap()).is_err() { + + if path + .strip_prefix(current_dir.canonicalize().unwrap()) + .is_err() + { + println!("illegal path: {}", request.path); return None; }