diff options
| author | Fuwn <[email protected]> | 2025-07-08 04:50:54 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-07-08 04:50:54 +0000 |
| commit | 1776a876c1b032a4c9fa111f2d55b0656096c4cc (patch) | |
| tree | d55a54b567d0bf4e365f93b6917440e718849e9e /src | |
| parent | feat(router): Add options system (diff) | |
| download | windmark-1776a876c1b032a4c9fa111f2d55b0656096c4cc.tar.xz windmark-1776a876c1b032a4c9fa111f2d55b0656096c4cc.zip | |
refactor(router): Smarter trailing slash clipper
Diffstat (limited to 'src')
| -rw-r--r-- | src/router.rs | 20 | ||||
| -rw-r--r-- | src/utilities.rs | 27 |
2 files changed, 12 insertions, 35 deletions
diff --git a/src/router.rs b/src/router.rs index 7584401..a82ea1e 100644 --- a/src/router.rs +++ b/src/router.rs @@ -51,7 +51,6 @@ use crate::{ module::{AsyncModule, Module}, response::Response, router_option::RouterOption, - utilities, }; macro_rules! block { @@ -421,13 +420,18 @@ impl Router { url.set_path("/"); } - let fixed_path = - if self.options.contains(&RouterOption::TrimTrailingSlashes) { - utilities::normalize_path_slashes(url.path()) - } else { - url.path().to_string() - }; - let route = &mut self.routes.at(&fixed_path); + let mut path = url.path().to_string(); + let mut route = self.routes.at(&path); + + if route.is_err() + && self.options.contains(&RouterOption::TrimTrailingSlashes) + && path.ends_with('/') + && path != "/" + { + path = path.trim_end_matches('/').to_string(); + route = self.routes.at(&path); + } + let peer_certificate = stream.ssl().peer_certificate(); let hook_context = HookContext::new( stream.get_ref().peer_addr(), diff --git a/src/utilities.rs b/src/utilities.rs index 663c26f..1eb4831 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -40,30 +40,3 @@ 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() - } -} |