From 1776a876c1b032a4c9fa111f2d55b0656096c4cc Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 8 Jul 2025 04:50:54 +0000 Subject: refactor(router): Smarter trailing slash clipper --- src/router.rs | 20 ++++++++++++-------- src/utilities.rs | 27 --------------------------- 2 files changed, 12 insertions(+), 35 deletions(-) (limited to 'src') 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() - } -} -- cgit v1.2.3