Skip to content

Commit

Permalink
Fix redirector in TLS example.
Browse files Browse the repository at this point in the history
Resolves #2795.
  • Loading branch information
SergioBenitez committed May 29, 2024
1 parent b12cc56 commit c303add
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions examples/tls/src/redirector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,47 @@ 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::<TlsConfig>().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<Rocket<Ignite>, Error> {
pub async fn try_launch(self, config: Config) -> Result<Rocket<Ignite>, 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 `<path..>` for each method.
let redirects = [Get, Put, Post, Delete, Options, Head, Trace, Connect, Patch]
.into_iter()
.map(|m| Route::new(m, "/<path..>", Self::redirect))
.collect::<Vec<_>>();

rocket::custom(config)
rocket::custom(redirector_config)
.manage(TlsConfig(config))
.mount("/", redirects)
.launch()
.await
Expand Down

0 comments on commit c303add

Please sign in to comment.