From bda49d53c413e9c9a96a1cf7c903b7a72c834be8 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 26 Nov 2021 22:49:02 +0100 Subject: [PATCH] Normalizing paths now works as intended --- src/proxy.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/proxy.rs b/src/proxy.rs index bfcb0e0..2667aad 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -6,7 +6,9 @@ use reqwest::{ Method as ReqMethod, }; use rocket::{ + response::Redirect, http::uri::Segments, + http::ext::IntoOwned, fs::Options, data::ToByteUnit, http::{Method, Status}, @@ -95,13 +97,23 @@ impl Handler for ProxyServer return Outcome::Failure(Status::BadRequest); } - let path_val = path.unwrap(); - let path_str = path_val.to_str(); // Unwrap is safe because we check for is_none() beforehand + let path = path.unwrap(); + let path_str = path.to_str(); // Unwrap is safe because we check for is_none() beforehand if path_str.is_none() { return Outcome::Failure(Status::BadRequest); } + if self.options.contains(Options::NormalizeDirs) && !req.uri().path().ends_with('/') { + let normal = req.uri().map_path(|p| format!("{}/", p)) + .expect("adding a trailing slash to a known good path => valid path") + .into_owned(); + + return Outcome::from_or_forward(req, data, Redirect::permanent(normal)); + } + + let path_str = path_str.unwrap(); + let query_part = req .uri() .query() @@ -110,7 +122,7 @@ impl Handler for ProxyServer let url = format!( "{}{}{}", self.root, - path_str.unwrap(), // Unwrap is safe because we check for is_none() beforehand + path_str, query_part );