🏗️ Elegant & Highly Performant Async Gemini Server Framework for the Modern Age
async framework gemini-protocol protocol gemini rust

refactor(router): Manually implement trailing slash clipper

fuwn.net 47df6e61 1b511ad3

verified
+31 -5
+4 -5
src/router.rs
··· 49 49 }, 50 50 module::{AsyncModule, Module}, 51 51 response::Response, 52 + utilities, 52 53 }; 53 54 54 55 macro_rules! block { ··· 384 385 #[allow( 385 386 clippy::too_many_lines, 386 387 clippy::needless_pass_by_ref_mut, 387 - clippy::significant_drop_in_scrutinee 388 + clippy::significant_drop_in_scrutinee, 389 + clippy::cognitive_complexity 388 390 )] 389 391 async fn handle( 390 392 &mut self, ··· 418 420 } 419 421 420 422 let fixed_path = if self.fix_path { 421 - self 422 - .routes 423 - .fix_path(url.path()) 424 - .unwrap_or_else(|| url.path().to_string()) 423 + utilities::normalize_path_slashes(url.path()) 425 424 } else { 426 425 url.path().to_string() 427 426 };
+27
src/utilities.rs
··· 40 40 .map(|(k, v)| (k.to_string(), v.to_string())) 41 41 .collect() 42 42 } 43 + 44 + /// Normalizes a path by removing all trailing slashes, unless it's the root 45 + /// path "/". 46 + /// 47 + /// # Examples 48 + /// 49 + /// ```rust 50 + /// assert_eq!( 51 + /// windmark::utilities::normalize_path_slashes("/foo///"), 52 + /// "/foo" 53 + /// ); 54 + /// assert_eq!(windmark::utilities::normalize_path_slashes("/foo/"), "/foo"); 55 + /// assert_eq!(windmark::utilities::normalize_path_slashes("/foo"), "/foo"); 56 + /// assert_eq!(windmark::utilities::normalize_path_slashes("/"), "/"); 57 + /// ``` 58 + #[must_use] 59 + pub fn normalize_path_slashes(path: &str) -> String { 60 + if path == "/" { 61 + return "/".to_string(); 62 + } 63 + 64 + if path.ends_with('/') { 65 + path.trim_end_matches('/').to_string() 66 + } else { 67 + path.to_string() 68 + } 69 + }