If it's a directory, serve index.html instead (#1426)

This commit is contained in:
Bram Neijt 2021-04-07 21:47:15 +02:00 committed by GitHub
parent 341ac3bfbd
commit ba8939b240
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 1 deletions

View File

@ -107,9 +107,16 @@ async fn handle_request(req: Request<Body>, mut root: PathBuf) -> Result<Respons
return Ok(not_found()); return Ok(not_found());
} }
// Remove the trailing slash from the request path // Remove the first slash from the request path
// otherwise `PathBuf` will interpret it as an absolute path // otherwise `PathBuf` will interpret it as an absolute path
root.push(&decoded[1..]); root.push(&decoded[1..]);
let metadata = tokio::fs::metadata(root.as_path()).await?;
if metadata.is_dir() {
// if root is a directory, append index.html to try to read that instead
root.push("index.html");
};
let result = tokio::fs::read(&root).await; let result = tokio::fs::read(&root).await;
let contents = match result { let contents = match result {