From c303add07fe15520654aa388e6f387979704b1d5 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Wed, 29 May 2024 18:07:15 -0500 Subject: [PATCH] Fix redirector in TLS example. Resolves #2795. --- examples/tls/src/redirector.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/examples/tls/src/redirector.rs b/examples/tls/src/redirector.rs index 0aafddf9ed..fb51d7400c 100644 --- a/examples/tls/src/redirector.rs +++ b/examples/tls/src/redirector.rs @@ -11,26 +11,38 @@ pub struct Redirector { pub port: u16 } +/// Managed state for the TLS server's configuration. +struct TlsConfig(Config); + impl Redirector { // Route function that gets call on every single request. fn redirect<'r>(req: &'r Request, _: Data<'r>) -> route::BoxFuture<'r> { - // FIXME: Check the host against a whitelist! + // TODO: Check the host against a whitelist! + let config = req.rocket().state::().expect("managed config"); if let Some(host) = req.host() { - let https_uri = format!("https://{}{}", host, req.uri()); - route::Outcome::from(req, Redirect::permanent(https_uri)).pin() + let domain = host.domain(); + let https_uri = match dbg!(config.0.port) { + 443 => format!("https://{domain}{}", req.uri()), + port => format!("https://{domain}:{port}{}", req.uri()), + }; + + route::Outcome::from(req, Redirect::temporary(https_uri)).pin() } else { route::Outcome::from(req, Status::BadRequest).pin() } } // Launch an instance of Rocket than handles redirection on `self.port`. - pub async fn try_launch(self, mut config: Config) -> Result, Error> { + pub async fn try_launch(self, config: Config) -> Result, Error> { use rocket::http::Method::*; // Adjust config for redirector: disable TLS, set port, disable logging. - config.tls = None; - config.port = self.port; - config.log_level = LogLevel::Critical; + let redirector_config = Config { + tls: None, + port: self.port, + log_level: LogLevel::Critical, + ..config.clone() + }; // Build a vector of routes to `redirect` on `` for each method. let redirects = [Get, Put, Post, Delete, Options, Head, Trace, Connect, Patch] @@ -38,7 +50,8 @@ impl Redirector { .map(|m| Route::new(m, "/", Self::redirect)) .collect::>(); - rocket::custom(config) + rocket::custom(redirector_config) + .manage(TlsConfig(config)) .mount("/", redirects) .launch() .await