Skip to content

Commit 97bf959

Browse files
authoredJul 19, 2023
Fix path normalization for preceding slashes
1 parent 1cd1505 commit 97bf959

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed
 

‎tower-http/src/normalize_path.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,15 @@ where
108108
}
109109

110110
fn normalize_trailing_slash(uri: &mut Uri) {
111-
if !uri.path().ends_with('/') {
111+
if !uri.path().ends_with('/') && !uri.path().starts_with("//") {
112112
return;
113113
}
114114

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

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

119119
let new_path_and_query = if let Some(path_and_query) = &parts.path_and_query {
120-
let new_path = if new_path.is_empty() {
121-
"/"
122-
} else {
123-
new_path.into()
124-
};
125-
126120
let new_path_and_query = if let Some(query) = path_and_query.query() {
127121
Cow::Owned(format!("{}?{}", new_path, query))
128122
} else {
@@ -218,4 +212,18 @@ mod tests {
218212
normalize_trailing_slash(&mut uri);
219213
assert_eq!(uri, "/?a=a");
220214
}
215+
216+
#[test]
217+
fn removes_multiple_preceding_slashes_even_with_query() {
218+
let mut uri = "///foo//?a=a".parse::<Uri>().unwrap();
219+
normalize_trailing_slash(&mut uri);
220+
assert_eq!(uri, "/foo?a=a");
221+
}
222+
223+
#[test]
224+
fn removes_multiple_preceding_slashes() {
225+
let mut uri = "///foo".parse::<Uri>().unwrap();
226+
normalize_trailing_slash(&mut uri);
227+
assert_eq!(uri, "/foo");
228+
}
221229
}

0 commit comments

Comments
 (0)