Fix windows-path handling for deeply nested paths too

This commit is contained in:
Christopher Armstrong 2018-02-07 11:33:29 -06:00
parent 5bba3b8c96
commit 3bc7ac303b
1 changed files with 26 additions and 2 deletions

View File

@ -283,8 +283,14 @@ impl<S> Handler<S> for StaticFiles {
if path.is_dir() { if path.is_dir() {
if let Some(ref redir_index) = self.index { if let Some(ref redir_index) = self.index {
let new_path = format!("{}{}/{}", // TODO: Don't redirect, just return the index content.
req.path(), relpath.to_string_lossy(), redir_index); // 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( Ok(FilesystemElement::Redirect(
HTTPFound HTTPFound
.build() .build()
@ -348,5 +354,23 @@ mod tests {
let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap(); let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap();
assert_eq!(resp.status(), StatusCode::FOUND); assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(resp.headers().get(header::LOCATION).unwrap(), "/guide/index.html"); 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");
} }
} }