Skip to content

Commit

Permalink
retain trailing slashes in paths but leave matching trail-slash-insen…
Browse files Browse the repository at this point in the history
…sitive
  • Loading branch information
skirsdeda committed Jan 5, 2024
1 parent 16cf3c4 commit 98baa38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
5 changes: 4 additions & 1 deletion router/src/components/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,10 @@ fn create_routes(
}
let mut acc = Vec::new();
for original_path in expand_optionals(&route_def.path) {
let path = join_paths(base, &original_path);
// compat: trim_end_matches ensures that routes with trailing slash still match ones with no trailing slash
let path = join_paths(base, &original_path)
.trim_end_matches('/')
.to_string();
let pattern = if is_leaf {
path
} else {
Expand Down
24 changes: 21 additions & 3 deletions router/src/matching/resolve_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ fn has_scheme(path: &str) -> bool {

#[doc(hidden)]
fn normalize(path: &str, omit_slash: bool) -> Cow<'_, str> {
let s = path.trim_start_matches('/').trim_end_matches('/');
let s = path.trim_start_matches('/');
let trim_end = s
.chars()
.rev()
.take_while(|c| *c == '/')
.count()
.saturating_sub(1);
let s = &s[0..s.len() - trim_end];
if s.is_empty() || omit_slash || begins_with_query_or_hash(s) {
s.into()
} else {
Expand All @@ -70,9 +77,10 @@ fn begins_with_query_or_hash(text: &str) -> bool {
}

fn remove_wildcard(text: &str) -> String {
text.split_once('*')
.map(|(prefix, _)| prefix.trim_end_matches('/'))
text.rsplit_once('*')
.map(|(prefix, _)| prefix)
.unwrap_or(text)
.trim_end_matches('/')
.to_string()
}

Expand All @@ -83,4 +91,14 @@ mod tests {
fn normalize_query_string_with_opening_slash() {
assert_eq!(normalize("/?foo=bar", false), "?foo=bar");
}

#[test]
fn normalize_retain_trailing_slash() {
assert_eq!(normalize("foo/bar/", false), "/foo/bar/");
}

#[test]
fn normalize_dedup_trailing_slashes() {
assert_eq!(normalize("foo/bar/////", false), "/foo/bar/");
}
}

0 comments on commit 98baa38

Please sign in to comment.