1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! What rho's clients for URLs someone else chose share: `tools::web`'s
//! fetches and the daemon's `rho/forge/fetch`. Each follows no redirect on
//! its own, so a 3xx can't carry a request to a host its caller never
//! checked, and all use one TLS configuration ([`TLS_CONFIG`]).
use std::sync::LazyLock;
use rustls_platform_verifier::BuilderVerifierExt;
/// One rustls client config for the process.
///
/// Building it loads the system trust store, which costs a few milliseconds,
/// and a content fetch needs its own client to pin the connection. Sharing the
/// config keeps that client build in the microseconds. `None` means TLS is
/// unavailable, and reqwest then reports the same failure it would have
/// reported for a config it built itself.
static TLS_CONFIG: LazyLock<Option<rustls::ClientConfig>> = LazyLock::new(rustls_client_config);
fn rustls_client_config() -> Option<rustls::ClientConfig> {
rho_providers::ensure_rustls_ring_provider();
let mut config = rustls::ClientConfig::builder()
.with_platform_verifier()
.ok()?
.with_no_client_auth();
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
Some(config)
}
/// A client builder that follows no redirect, with [`TLS_CONFIG`]: each
/// caller checks a redirect's URL itself, and sets its own timeouts and
/// proxies.
pub(crate) fn no_redirect_builder() -> reqwest::ClientBuilder {
let builder = crate::reqwest_client_builder().redirect(reqwest::redirect::Policy::none());
match TLS_CONFIG.as_ref() {
Some(tls) => builder.tls_backend_preconfigured(tls.clone()),
None => builder,
}
}