Skip to content

Commit

Permalink
Fix path normalization for preceding slashes
Browse files Browse the repository at this point in the history
fixes #358
  • Loading branch information
jhoolmans committed Jun 9, 2023
1 parent d22f200 commit 4bce872
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions tower-http/src/normalize_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,15 @@ where
}

fn normalize_trailing_slash(uri: &mut Uri) {
if !uri.path().ends_with('/') {
if !uri.path().ends_with('/') && !uri.path().starts_with("//") {
return;
}

let new_path = uri.path().trim_end_matches('/');
let new_path = format!("/{}", uri.path().trim_matches('/'));

let mut parts = uri.clone().into_parts();

let new_path_and_query = if let Some(path_and_query) = &parts.path_and_query {
let new_path = if new_path.is_empty() {
"/"
} else {
new_path.into()
};

let new_path_and_query = if let Some(query) = path_and_query.query() {
Cow::Owned(format!("{}?{}", new_path, query))
} else {
Expand Down Expand Up @@ -218,4 +212,18 @@ mod tests {
normalize_trailing_slash(&mut uri);
assert_eq!(uri, "/?a=a");
}

#[test]
fn removes_multiple_preceding_slashes_even_with_query() {
let mut uri = "///foo//?a=a".parse::<Uri>().unwrap();
normalize_trailing_slash(&mut uri);
assert_eq!(uri, "/foo?a=a");
}

#[test]
fn removes_multiple_preceding_slashes() {
let mut uri = "///foo".parse::<Uri>().unwrap();
normalize_trailing_slash(&mut uri);
assert_eq!(uri, "/foo");
}
}

0 comments on commit 4bce872

Please sign in to comment.