From 11bff07e8ff3b0aba1dfcf82e4a62d18055d5cc8 Mon Sep 17 00:00:00 2001 From: Gohlub <62673775+Gohlub@users.noreply.github.com> Date: Wed, 4 Dec 2024 23:26:31 -0500 Subject: [PATCH 1/2] Multiple HTTP Bindings function added --- src/http/server.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/http/server.rs b/src/http/server.rs index cac699e..ab6c769 100644 --- a/src/http/server.rs +++ b/src/http/server.rs @@ -1048,6 +1048,48 @@ impl HttpServer { pub fn get_ws_channels(&self) -> HashMap> { self.ws_channels.clone() } + + /// Register multiple paths with the HTTP server using the same configuration. + /// Uses the security setting from HttpBindingConfig if secure is None, + /// forces secure binding if Some(true), or forces non-secure binding if Some(false). + /// All paths must be bound successfully, or none will be bound. If any path + /// fails to bind, all previously bound paths will be unbound before returning + /// the error. + pub fn bind_multiple_http_paths>( + &mut self, + paths: Vec, + config: HttpBindingConfig, + secure: Option, + ) -> Result<(), HttpServerError> { + let mut bound_paths = Vec::new(); + + for path in paths { + let path_str = path.into(); + let result = match secure { + Some(true) => self.secure_bind_http_path(path_str.clone()), + Some(false) => self.bind_http_path(path_str.clone(), config.clone()), + None => { + if config.secure_subdomain { + self.secure_bind_http_path(path_str.clone()) + } else { + self.bind_http_path(path_str.clone(), config.clone()) + } + } + }; + match result { + Ok(_) => bound_paths.push(path_str), + Err(e) => { + // If binding fails, unbind all previously bound paths + for bound_path in bound_paths { + let _ = self.unbind_http_path(&bound_path); + } + return Err(e); + } + } + } + + Ok(()) + } } /// Send an HTTP response to an incoming HTTP request ([`HttpServerRequest::Http`]). From d5ebd84cc714734aa2137ed4d34f17cf17ad6214 Mon Sep 17 00:00:00 2001 From: Gohlub <62673775+Gohlub@users.noreply.github.com> Date: Thu, 5 Dec 2024 14:17:53 -0500 Subject: [PATCH 2/2] amend the binding function --- src/http/server.rs | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/http/server.rs b/src/http/server.rs index ab6c769..b636451 100644 --- a/src/http/server.rs +++ b/src/http/server.rs @@ -1050,8 +1050,7 @@ impl HttpServer { } /// Register multiple paths with the HTTP server using the same configuration. - /// Uses the security setting from HttpBindingConfig if secure is None, - /// forces secure binding if Some(true), or forces non-secure binding if Some(false). + /// The security setting is determined by the `secure_subdomain` field in `HttpBindingConfig`. /// All paths must be bound successfully, or none will be bound. If any path /// fails to bind, all previously bound paths will be unbound before returning /// the error. @@ -1059,27 +1058,21 @@ impl HttpServer { &mut self, paths: Vec, config: HttpBindingConfig, - secure: Option, ) -> Result<(), HttpServerError> { let mut bound_paths = Vec::new(); - + for path in paths { let path_str = path.into(); - let result = match secure { - Some(true) => self.secure_bind_http_path(path_str.clone()), - Some(false) => self.bind_http_path(path_str.clone(), config.clone()), - None => { - if config.secure_subdomain { - self.secure_bind_http_path(path_str.clone()) - } else { - self.bind_http_path(path_str.clone(), config.clone()) - } - } + let result = match config.secure_subdomain { + true => self.secure_bind_http_path(path_str.clone()), + false => self.bind_http_path(path_str.clone(), config.clone()), }; + match result { + // If binding succeeds, add the path to the list of bound paths Ok(_) => bound_paths.push(path_str), + // If binding fails, unbind all previously bound paths Err(e) => { - // If binding fails, unbind all previously bound paths for bound_path in bound_paths { let _ = self.unbind_http_path(&bound_path); }