Skip to content

Commit

Permalink
fs: Simplify maybe_redirect_or_append_path
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Feb 19, 2024
1 parent 853f3e9 commit d205b1a
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions tower-http/src/services/fs/serve_dir/open_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,21 @@ async fn maybe_redirect_or_append_path(
uri: &Uri,
append_index_html_on_directories: bool,
) -> Option<OpenFileOutput> {
if !uri.path().ends_with('/') {
if is_dir(path_to_file).await {
if append_index_html_on_directories {
let location =
HeaderValue::from_str(&append_slash_on_path(uri.clone()).to_string()).unwrap();
Some(OpenFileOutput::Redirect { location })
} else {
Some(OpenFileOutput::FileNotFound)
}
} else {
None
}
} else if is_dir(path_to_file).await {
if append_index_html_on_directories {
path_to_file.push("index.html");
None
} else {
Some(OpenFileOutput::FileNotFound)
}
} else {
if !is_dir(path_to_file).await {
return None;
}

if !append_index_html_on_directories {
return Some(OpenFileOutput::FileNotFound);
}

if uri.path().ends_with('/') {
path_to_file.push("index.html");
None
} else {
let location =
HeaderValue::from_str(&append_slash_on_path(uri.clone()).to_string()).unwrap();
Some(OpenFileOutput::Redirect { location })
}
}

Expand Down

0 comments on commit d205b1a

Please sign in to comment.