From 4c5dd0122457164475181b38fecf930709bbdbe8 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 16 Jul 2024 17:42:59 -0700 Subject: [PATCH] fix(router): ensure route-time locks --- Cargo.toml | 2 +- src/router.rs | 38 +++++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e35ffb6..d6d55b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ members = ["rossweisse"] [package] name = "windmark" -version = "0.3.10" +version = "0.3.11" authors = ["Fuwn "] edition = "2021" description = "An elegant and highly performant async Gemini server framework" diff --git a/src/router.rs b/src/router.rs index eefbf83..efe47ef 100644 --- a/src/router.rs +++ b/src/router.rs @@ -433,14 +433,15 @@ impl Router { module.on_pre_route(hook_context.clone()).await; } - for module in &mut *self.modules.lock().unwrap() { - module.on_pre_route(hook_context.clone()); + if let Ok(mut modules) = self.modules.lock() { + for module in &mut *modules { + module.on_pre_route(hook_context.clone()); + } } - (*self.pre_route_callback) - .lock() - .unwrap() - .call(hook_context.clone()); + if let Ok(mut callback) = self.pre_route_callback.lock() { + callback.call(hook_context.clone()); + } let mut content = if let Ok(ref route) = route { let footers_length = (*self.footers.lock().unwrap()).len(); @@ -451,11 +452,13 @@ impl Router { peer_certificate, ); - for partial_header in &mut *self.headers.lock().unwrap() { - header.push_str(&format!( - "{}\n", - partial_header.call(route_context.clone()), - )); + if let Ok(mut headers) = self.headers.lock() { + for partial_header in &mut *headers { + header.push_str(&format!( + "{}\n", + partial_header.call(route_context.clone()), + )); + } } for (i, partial_footer) in { @@ -493,14 +496,15 @@ impl Router { module.on_post_route(hook_context.clone()).await; } - for module in &mut *self.modules.lock().unwrap() { - module.on_post_route(hook_context.clone()); + if let Ok(mut modules) = self.modules.lock() { + for module in &mut *modules { + module.on_post_route(hook_context.clone()); + } } - (*self.post_route_callback) - .lock() - .unwrap() - .call(hook_context.clone(), &mut content); + if let Ok(mut callback) = self.post_route_callback.lock() { + callback.call(hook_context.clone(), &mut content); + } stream .write_all(