@@ -108,21 +108,15 @@ where
108
108
}
109
109
110
110
fn normalize_trailing_slash ( uri : & mut Uri ) {
111
- if !uri. path ( ) . ends_with ( '/' ) {
111
+ if !uri. path ( ) . ends_with ( '/' ) && !uri . path ( ) . starts_with ( "//" ) {
112
112
return ;
113
113
}
114
114
115
- let new_path = uri. path ( ) . trim_end_matches ( '/' ) ;
115
+ let new_path = format ! ( "/{}" , uri. path( ) . trim_matches ( '/' ) ) ;
116
116
117
117
let mut parts = uri. clone ( ) . into_parts ( ) ;
118
118
119
119
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
-
126
120
let new_path_and_query = if let Some ( query) = path_and_query. query ( ) {
127
121
Cow :: Owned ( format ! ( "{}?{}" , new_path, query) )
128
122
} else {
@@ -218,4 +212,18 @@ mod tests {
218
212
normalize_trailing_slash ( & mut uri) ;
219
213
assert_eq ! ( uri, "/?a=a" ) ;
220
214
}
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
+ }
221
229
}
0 commit comments