From 319398d6b437c73b8f0b596c4e8b3782b5bd7777 Mon Sep 17 00:00:00 2001 From: Programatik Date: Thu, 19 Aug 2021 12:49:57 +0300 Subject: [PATCH 1/4] fix directory issue --- tower-http/src/services/fs/serve_dir.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tower-http/src/services/fs/serve_dir.rs b/tower-http/src/services/fs/serve_dir.rs index f5136d9d..56440ac2 100644 --- a/tower-http/src/services/fs/serve_dir.rs +++ b/tower-http/src/services/fs/serve_dir.rs @@ -86,8 +86,12 @@ impl Service> for ServeDir { HeaderValue::from_str(&append_slash_on_path(uri).to_string()).unwrap(); return Ok(Output::Redirect(location)); } - } else if append_index_html_on_directories && is_dir(&full_path).await { - full_path.push("index.html"); + } else if is_dir(&full_path).await { + if append_index_html_on_directories { + full_path.push("index.html"); + } else { + return Ok(Output::NotFound); + } } let guess = mime_guess::from_path(&full_path); @@ -146,6 +150,7 @@ fn append_slash_on_path(uri: Uri) -> Uri { enum Output { File(File, HeaderValue), Redirect(HeaderValue), + NotFound, } type BoxFuture = Pin + Send + Sync + 'static>>; @@ -178,6 +183,15 @@ impl Future for ResponseFuture { return Poll::Ready(Ok(res)); } + Ok(Output::NotFound) => { + let res = Response::builder() + .status(StatusCode::NOT_FOUND) + .body(empty_body()) + .unwrap(); + + return Poll::Ready(Ok(res)); + } + Err(err) => { return Poll::Ready( super::response_from_io_error(err).map(|res| res.map(ResponseBody)), From 0fd2ab41815262fecf02372792807747af755cfd Mon Sep 17 00:00:00 2001 From: Programatik Date: Thu, 19 Aug 2021 14:23:02 +0300 Subject: [PATCH 2/4] add test for ServeDir change --- tower-http/src/services/fs/serve_dir.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tower-http/src/services/fs/serve_dir.rs b/tower-http/src/services/fs/serve_dir.rs index 56440ac2..acffa5c7 100644 --- a/tower-http/src/services/fs/serve_dir.rs +++ b/tower-http/src/services/fs/serve_dir.rs @@ -305,6 +305,20 @@ mod tests { assert_eq!(location, "/src/"); } + #[tokio::test] + async fn empty_directory_without_index() { + let svc = ServeDir::new(".").append_index_html_on_directories(false); + + let req = Request::new(Body::empty()); + let res = svc.oneshot(req).await.unwrap(); + + assert_eq!(res.status(), StatusCode::NOT_FOUND); + assert!(res.headers().get(header::CONTENT_TYPE).is_none()); + + let body = body_into_text(res.into_body()).await; + assert!(body.is_empty()); + } + async fn body_into_text(body: B) -> String where B: HttpBody + Unpin, From 391b449f69e3c69b494ef2c69b7616e3fdb73070 Mon Sep 17 00:00:00 2001 From: Programatik Date: Thu, 19 Aug 2021 17:11:44 +0300 Subject: [PATCH 3/4] add changes to changelog --- tower-http/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tower-http/CHANGELOG.md b/tower-http/CHANGELOG.md index 0dbef82c..fd40c8c6 100644 --- a/tower-http/CHANGELOG.md +++ b/tower-http/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -None. +- Fix a bug which happens when `append_index_html_on_directories` is set to `false` in `ServeDir`. ## Breaking changes From 74b752f1766cfeb8679b450fb0c2e880b42fe722 Mon Sep 17 00:00:00 2001 From: Programatik Date: Thu, 19 Aug 2021 17:14:00 +0300 Subject: [PATCH 4/4] add link to changelog --- tower-http/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tower-http/CHANGELOG.md b/tower-http/CHANGELOG.md index fd40c8c6..25160fcf 100644 --- a/tower-http/CHANGELOG.md +++ b/tower-http/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- Fix a bug which happens when `append_index_html_on_directories` is set to `false` in `ServeDir`. +- Fix a [bug](https://github.com/tower-rs/tower-http/issues/121) which happens when `append_index_html_on_directories` is set to `false` in `ServeDir`. ## Breaking changes