From 063b8dbc5192a46a0f9e30c5efcd1047aca8bbbb Mon Sep 17 00:00:00 2001 From: wyhaya Date: Mon, 8 Jul 2019 11:05:48 +0800 Subject: fix parent directory --- src/listing.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index ee9c581..4a69108 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -138,7 +138,6 @@ pub fn directory_listing( let base = Path::new(serve_path); let random_route = format!("/{}", random_route.unwrap_or_default()); let is_root = base.parent().is_none() || req.path() == random_route; - let page_parent = base.parent().map(|p| p.display().to_string()); let current_dir = match base.strip_prefix(random_route) { Ok(c_d) => Path::new("/").join(c_d), Err(_) => base.to_path_buf(), @@ -285,6 +284,19 @@ pub fn directory_listing( .chunked() .body(Body::Streaming(Box::new(rx)))) } else { + // Redirect to directory + if !renderer::has_trailing(&serve_path) { + let query = match req.query_string() { + "" => String::new(), + _ => format!("?{}", req.query_string()) + }; + return Ok( + HttpResponse::MovedPermanenty() + .header("Location", format!("{}/{}", serve_path, query)) + .body("301") + ); + } + Ok(HttpResponse::Ok() .content_type("text/html; charset=utf-8") .body( @@ -292,7 +304,6 @@ pub fn directory_listing( serve_path, entries, is_root, - page_parent, query_params.sort, query_params.order, default_color_scheme, -- cgit v1.2.3