From 3bc7ac303bcc5299b476c29ea1f4ad956b32fb60 Mon Sep 17 00:00:00 2001 From: Christopher Armstrong Date: Wed, 7 Feb 2018 11:33:29 -0600 Subject: [PATCH] Fix windows-path handling for deeply nested paths too --- src/fs.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index ca245f007..28260e36a 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -283,8 +283,14 @@ impl Handler for StaticFiles { if path.is_dir() { if let Some(ref redir_index) = self.index { - let new_path = format!("{}{}/{}", - req.path(), relpath.to_string_lossy(), redir_index); + // TODO: Don't redirect, just return the index content. + // TODO: It'd be nice if there were a good usable URL manipulation library + let mut new_path: String = req.path().to_owned(); + for el in relpath.iter() { + new_path.push_str(&el.to_string_lossy()); + new_path.push('/'); + } + new_path.push_str(redir_index); Ok(FilesystemElement::Redirect( HTTPFound .build() @@ -348,5 +354,23 @@ mod tests { let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap(); assert_eq!(resp.status(), StatusCode::FOUND); assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/guide/index.html"); + + let mut req = HttpRequest::default(); + req.match_info_mut().add("tail", "guide/"); + + let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap(); + assert_eq!(resp.status(), StatusCode::FOUND); + assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/guide/index.html"); + } + + #[test] + fn test_redirect_to_index_nested() { + let mut st = StaticFiles::new(".", false).index_file("Cargo.toml"); + let mut req = HttpRequest::default(); + req.match_info_mut().add("tail", "examples/basics"); + + let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap(); + assert_eq!(resp.status(), StatusCode::FOUND); + assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/examples/basics/Cargo.toml"); } }