diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2019-07-24 07:57:56 +0000 |
---|---|---|
committer | Sven-Hendrik Haase <svenstaro@gmail.com> | 2019-07-24 07:57:56 +0000 |
commit | 2c0b1bf34ddcd03be84fe55f2b19b7a3e13606f3 (patch) | |
tree | f0ab38c2ac9caee46c2f67dbed34066e9198f6f8 /src | |
parent | Merge pull request #158 from wyhaya/master (diff) | |
download | miniserve-2c0b1bf34ddcd03be84fe55f2b19b7a3e13606f3.tar.gz miniserve-2c0b1bf34ddcd03be84fe55f2b19b7a3e13606f3.zip |
Fix tests
Diffstat (limited to 'src')
-rw-r--r-- | src/listing.rs | 26 | ||||
-rw-r--r-- | src/renderer.rs | 22 |
2 files changed, 20 insertions, 28 deletions
diff --git a/src/listing.rs b/src/listing.rs index 4a69108..c5ee59e 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -135,6 +135,19 @@ pub fn directory_listing<S>( upload_route: String, ) -> Result<HttpResponse, io::Error> { let serve_path = req.path(); + + // In case the current path is a directory, we want to make sure that the current URL ends + // on a slash ("/"). + if !serve_path.ends_with('/') { + let query = match req.query_string() { + "" => String::new(), + _ => format!("?{}", req.query_string()), + }; + return Ok(HttpResponse::MovedPermanenty() + .header("Location", format!("{}/{}", serve_path, query)) + .body("301")); + } + 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; @@ -284,19 +297,6 @@ pub fn directory_listing<S>( .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( diff --git a/src/renderer.rs b/src/renderer.rs index af8f5d7..cbad557 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -215,19 +215,11 @@ fn archive_button( } } -// Is there a trailing "/" -pub fn has_trailing(s: &str) -> bool { - match s.chars().last() { - Some(d) => d == '/', - None => false - } -} - -// Add trailing "/" if conditions permit -fn add_trailing(link: &str) -> String { - if has_trailing(&link) { +/// Ensure that there's always a trailing slash behind the `link`. +fn make_link_with_trailing_slash(link: &str) -> String { + if link.ends_with('/') { link.to_string() - }else { + } else { format!("{}/", link) } } @@ -244,7 +236,7 @@ fn parametrized_link( if let Some(order) = sort_order { let parametrized_link = format!( "{}?sort={}&order={}", - add_trailing(&link), + make_link_with_trailing_slash(&link), method, order ); @@ -260,12 +252,12 @@ fn parametrized_link( if color_scheme != default_color_scheme { return format!( "{}?theme={}", - add_trailing(&link), + make_link_with_trailing_slash(&link), color_scheme.to_slug() ); } - add_trailing(&link) + make_link_with_trailing_slash(&link) } /// Partial: table header link |