diff options
| author | Fuwn <[email protected]> | 2025-07-08 04:09:36 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-07-08 04:09:36 +0000 |
| commit | 47df6e61ba4c63ef690081055ad551bf340d9c19 (patch) | |
| tree | 16a19278fe248c77a83531cb92d86790d26107ae /src | |
| parent | chore: Suggest stable Rust toolchain (diff) | |
| download | windmark-47df6e61ba4c63ef690081055ad551bf340d9c19.tar.xz windmark-47df6e61ba4c63ef690081055ad551bf340d9c19.zip | |
refactor(router): Manually implement trailing slash clipper
Diffstat (limited to 'src')
| -rw-r--r-- | src/router.rs | 9 | ||||
| -rw-r--r-- | src/utilities.rs | 27 |
2 files changed, 31 insertions, 5 deletions
diff --git a/src/router.rs b/src/router.rs index d999a32..a5c0833 100644 --- a/src/router.rs +++ b/src/router.rs @@ -49,6 +49,7 @@ use crate::{ }, module::{AsyncModule, Module}, response::Response, + utilities, }; macro_rules! block { @@ -384,7 +385,8 @@ impl Router { #[allow( clippy::too_many_lines, clippy::needless_pass_by_ref_mut, - clippy::significant_drop_in_scrutinee + clippy::significant_drop_in_scrutinee, + clippy::cognitive_complexity )] async fn handle( &mut self, @@ -418,10 +420,7 @@ impl Router { } let fixed_path = if self.fix_path { - self - .routes - .fix_path(url.path()) - .unwrap_or_else(|| url.path().to_string()) + utilities::normalize_path_slashes(url.path()) } else { url.path().to_string() }; diff --git a/src/utilities.rs b/src/utilities.rs index 1eb4831..663c26f 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -40,3 +40,30 @@ pub fn params_to_hashmap( .map(|(k, v)| (k.to_string(), v.to_string())) .collect() } + +/// Normalizes a path by removing all trailing slashes, unless it's the root +/// path "/". +/// +/// # Examples +/// +/// ```rust +/// assert_eq!( +/// windmark::utilities::normalize_path_slashes("/foo///"), +/// "/foo" +/// ); +/// assert_eq!(windmark::utilities::normalize_path_slashes("/foo/"), "/foo"); +/// assert_eq!(windmark::utilities::normalize_path_slashes("/foo"), "/foo"); +/// assert_eq!(windmark::utilities::normalize_path_slashes("/"), "/"); +/// ``` +#[must_use] +pub fn normalize_path_slashes(path: &str) -> String { + if path == "/" { + return "/".to_string(); + } + + if path.ends_with('/') { + path.trim_end_matches('/').to_string() + } else { + path.to_string() + } +} |