Skip to content

Commit 10f6aa2

Browse files
committed
Only attempt directory fallback if index.html exists
This commit updates the logic for the directory fallback to only attempt it when 1) the request is for a directory, and 2) the directory contains an `index.html` file. Otherwise, it will attempt the static application fallback configured through the environment variable. Signed-off-by: Radu Matei <[email protected]>
1 parent 05bc8ea commit 10f6aa2

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,17 @@ fn serve(req: Request) -> Result<Response> {
8888
Ok(b) => Some(b),
8989
Err(e) => {
9090
// if the error is because the path points to a directory, attempt to read `index.html`
91-
// from the directory. This is because most static file generators will generate this
91+
// from the directory. This is because most static site generators will generate this
9292
// file structure (where `/about` should be mapped to `/about/index.html`).
9393
eprintln!("Cannot find file {path}. Attempting fallback.");
9494
// TODO: ideally, we would return better errors throughout the implementation.
95-
if e.to_string().contains("Is a directory") {
96-
let path = PathBuf::from(path).join(DIRECTORY_FALLBACK_PATH);
97-
let path = path.to_str().context("cannot convert path to string")?;
98-
eprintln!("Attempting fallback {path}");
99-
FileServer::read(path, &enc).ok()
95+
let directory_fallback = PathBuf::from(path).join(DIRECTORY_FALLBACK_PATH);
96+
if e.to_string().contains("Is a directory") && directory_fallback.exists() {
97+
let directory_fallback = directory_fallback
98+
.to_str()
99+
.context("cannot convert path to string")?;
100+
eprintln!("Attempting directory fallback {directory_fallback}");
101+
FileServer::read(directory_fallback, &enc).ok()
100102
} else {
101103
match std::env::var(FALLBACK_PATH_ENV) {
102104
// try to read the fallback path

0 commit comments

Comments
 (0)