Skip to content

Commit

Permalink
Fix EmbeddedFilesEndpoint not working for index.html in subdirect…
Browse files Browse the repository at this point in the history
…ories (#825)
  • Loading branch information
czy-29 authored Jul 27, 2024
1 parent 1c46633 commit ef43aae
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions poem/src/endpoint/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,29 @@ impl<E: RustEmbed + Send + Sync> Endpoint for EmbeddedFilesEndpoint<E> {
type Output = Response;

async fn call(&self, req: Request) -> Result<Self::Output, Error> {
let mut path = req
.uri()
.path()
.trim_start_matches('/')
.trim_end_matches('/')
.to_string();
if path.is_empty() {
path = "index.html".to_string();
let path = req.uri().path().trim_start_matches('/');
let original_path = req.original_uri().path();
let original_end_with_slash = original_path.ends_with('/');

use header::LOCATION;

if path.is_empty() && !original_end_with_slash {
Ok(Response::builder()
.status(StatusCode::FOUND)
.header(LOCATION, format!("{}/", original_path))
.finish())
} else if original_end_with_slash {
let path = format!("{}index.html", path);
EmbeddedFileEndpoint::<E>::new(&path).call(req).await
} else if E::get(path).is_some() {
EmbeddedFileEndpoint::<E>::new(path).call(req).await
} else if E::get(&format!("{}/index.html", path)).is_some() {
Ok(Response::builder()
.status(StatusCode::FOUND)
.header(LOCATION, format!("{}/", original_path))
.finish())
} else {
EmbeddedFileEndpoint::<E>::new(path).call(req).await
}
let path = path.as_ref();
EmbeddedFileEndpoint::<E>::new(path).call(req).await
}
}

0 comments on commit ef43aae

Please sign in to comment.