Skip to content

Commit 7cc818c

Browse files
committed
Introduce dynamic TLS resolvers.
This commit introduces the ability to dynamically select a TLS configuration based on the client's TLS hello via the new `Resolver` trait. In support of this, it also makes the following changes: * Added `Authority::set_port()`. * `UdsListener` is now `UnixListener`. * `Bindable` removed in favor of new `Bind`. * All built-in listeners now implement `Bind<&Rocket>`. * `Connection` requires `AsyncRead + AsyncWrite`. * The `Debug` impl for `Endpoint` displays the underlying address. * `Listener` must be `Sized`. * The TLS listener was moved to `tls::TlsListener`. * The preview `quic` listener no longer implements `Listener`. * Added `TlsConfig::server_config()`. * Added `race` future helpers. * Added `Rocket::launch_with()`, `Rocket::bind_launch()`. * Added a default `client.pem` to the TLS example. * Various unnecessary listener `Config` structures removed. In addition, the testbench was revamped to support more scenarios. This resulted in the following issues being found and fixed: * Fix an issue where the logger would ignore color requests. * Clarified docs for `mtls::Certificate` guard. * Improved error messages on listener misconfiguration. Resolves #2730. Resolves #2363. Closes #2748. Closes #2683. Closes #2577.
1 parent 60f3cd5 commit 7cc818c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1627
-710
lines changed

contrib/dyn_templates/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@
117117
//! to an `Object` (a dictionary) value. The [`context!`] macro can be used to
118118
//! create inline `Serialize`-able context objects.
119119
//!
120+
//! [`Serialize`]: rocket::serde::Serialize
121+
//!
120122
//! ```rust
121123
//! # #[macro_use] extern crate rocket;
122124
//! use rocket::serde::Serialize;
@@ -165,7 +167,7 @@
165167
//! builds, template reloading is disabled to improve performance and cannot be
166168
//! enabled.
167169
//!
168-
//! [attached]: Rocket::attach()
170+
//! [attached]: rocket::Rocket::attach()
169171
//!
170172
//! ### Metadata and Rendering to `String`
171173
//!

contrib/dyn_templates/src/template.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,12 @@ impl Template {
140140
}
141141

142142
/// Render the template named `name` with the context `context`. The
143-
/// `context` is typically created using the [`context!`] macro, but it can
144-
/// be of any type that implements `Serialize`, such as `HashMap` or a
145-
/// custom `struct`.
143+
/// `context` is typically created using the [`context!()`](crate::context!)
144+
/// macro, but it can be of any type that implements `Serialize`, such as
145+
/// `HashMap` or a custom `struct`.
146146
///
147-
/// To render a template directly into a string, use [`Metadata::render()`].
147+
/// To render a template directly into a string, use
148+
/// [`Metadata::render()`](crate::Metadata::render()).
148149
///
149150
/// # Examples
150151
///
@@ -291,8 +292,8 @@ impl Sentinel for Template {
291292
/// A macro to easily create a template rendering context.
292293
///
293294
/// Invocations of this macro expand to a value of an anonymous type which
294-
/// implements [`serde::Serialize`]. Fields can be literal expressions or
295-
/// variables captured from a surrounding scope, as long as all fields implement
295+
/// implements [`Serialize`]. Fields can be literal expressions or variables
296+
/// captured from a surrounding scope, as long as all fields implement
296297
/// `Serialize`.
297298
///
298299
/// # Examples

contrib/sync_db_pools/lib/tests/shutdown.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#[cfg(all(feature = "diesel_sqlite_pool"))]
21
#[cfg(test)]
2+
#[cfg(all(feature = "diesel_sqlite_pool"))]
33
mod sqlite_shutdown_test {
44
use rocket::{async_test, Build, Rocket};
55
use rocket_sync_db_pools::database;

core/http/src/uri/authority.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<'a> Authority<'a> {
185185
self.host.from_cow_source(&self.source)
186186
}
187187

188-
/// Returns the port part of the authority URI, if there is one.
188+
/// Returns the `port` part of the authority URI, if there is one.
189189
///
190190
/// # Example
191191
///
@@ -206,6 +206,28 @@ impl<'a> Authority<'a> {
206206
pub fn port(&self) -> Option<u16> {
207207
self.port
208208
}
209+
210+
/// Set the `port` of the authority URI.
211+
///
212+
/// # Example
213+
///
214+
/// ```rust
215+
/// # #[macro_use] extern crate rocket;
216+
/// let mut uri = uri!("username:password@host:123");
217+
/// assert_eq!(uri.port(), Some(123));
218+
///
219+
/// uri.set_port(1024);
220+
/// assert_eq!(uri.port(), Some(1024));
221+
/// assert_eq!(uri, "username:password@host:1024");
222+
///
223+
/// uri.set_port(None);
224+
/// assert_eq!(uri.port(), None);
225+
/// assert_eq!(uri, "username:password@host");
226+
/// ```
227+
#[inline(always)]
228+
pub fn set_port<T: Into<Option<u16>>>(&mut self, port: T) {
229+
self.port = port.into();
230+
}
209231
}
210232

211233
impl_serde!(Authority<'a>, "an authority-form URI");

core/lib/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ ref-swap = "0.1.2"
6969
parking_lot = "0.12"
7070
ubyte = {version = "0.10.2", features = ["serde"] }
7171
serde = { version = "1.0", features = ["derive"] }
72-
figment = { version = "0.10.13", features = ["toml", "env"] }
72+
figment = { version = "0.10.17", features = ["toml", "env"] }
7373
rand = "0.8"
7474
either = "1"
7575
pin-project-lite = "0.2"
@@ -140,5 +140,5 @@ version_check = "0.9.1"
140140

141141
[dev-dependencies]
142142
tokio = { version = "1", features = ["macros", "io-std"] }
143-
figment = { version = "0.10", features = ["test"] }
143+
figment = { version = "0.10.17", features = ["test"] }
144144
pretty_assertions = "1"

core/lib/src/config/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ mod secret_key;
137137
#[cfg(unix)]
138138
pub use crate::shutdown::Sig;
139139

140-
#[cfg(unix)]
141-
pub use crate::listener::unix::UdsConfig;
142-
143140
#[cfg(feature = "secrets")]
144141
pub use secret_key::SecretKey;
145142

core/lib/src/error.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,17 @@ impl Error {
178178
self.mark_handled();
179179
match self.kind() {
180180
ErrorKind::Bind(ref a, ref e) => {
181-
match a {
182-
Some(a) => error!("Binding to {} failed.", a.primary().underline()),
183-
None => error!("Binding to network interface failed."),
184-
}
181+
if let Some(e) = e.downcast_ref::<Self>() {
182+
e.pretty_print()
183+
} else {
184+
match a {
185+
Some(a) => error!("Binding to {} failed.", a.primary().underline()),
186+
None => error!("Binding to network interface failed."),
187+
}
185188

186-
info_!("{}", e);
187-
"aborting due to bind error"
189+
info_!("{}", e);
190+
"aborting due to bind error"
191+
}
188192
}
189193
ErrorKind::Io(ref e) => {
190194
error!("Rocket failed to launch due to an I/O error.");

core/lib/src/listener/bind.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use crate::listener::{Endpoint, Listener};
2+
3+
pub trait Bind<T>: Listener + 'static {
4+
type Error: std::error::Error + Send + 'static;
5+
6+
#[crate::async_bound(Send)]
7+
async fn bind(to: T) -> Result<Self, Self::Error>;
8+
9+
fn bind_endpoint(to: &T) -> Result<Endpoint, Self::Error>;
10+
}

core/lib/src/listener/bindable.rs

-52
This file was deleted.

core/lib/src/listener/connection.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::io;
22
use std::borrow::Cow;
33

4+
use tokio::io::{AsyncRead, AsyncWrite};
45
use tokio_util::either::Either;
56

67
use super::Endpoint;
@@ -9,7 +10,7 @@ use super::Endpoint;
910
#[derive(Clone)]
1011
pub struct Certificates<'r>(Cow<'r, [der::CertificateDer<'r>]>);
1112

12-
pub trait Connection: Send + Unpin {
13+
pub trait Connection: AsyncRead + AsyncWrite + Send + Unpin {
1314
fn endpoint(&self) -> io::Result<Endpoint>;
1415

1516
/// DER-encoded X.509 certificate chain presented by the client, if any.

0 commit comments

Comments
 (0)