Skip to main content

max / makenotwork

994 B · 24 lines History Blame Raw
1 //! The rustls crypto provider, and the one place an HTTP client is built.
2 //!
3 //! reqwest is compiled `rustls-no-provider` to keep aws-lc-rs (a C backend) out
4 //! of the tree, which means `ClientBuilder::build` fails unless a provider has
5 //! been installed process-wide first. Every client therefore comes from
6 //! [`client`], so the two cannot get out of order.
7
8 use std::time::Duration;
9
10 /// Install ring as the process-wide rustls crypto provider. Idempotent (guarded
11 /// by a `Once`), so `main` and the client builder can both call it.
12 pub(crate) fn install_crypto_provider() {
13 static INSTALLED: std::sync::Once = std::sync::Once::new();
14 INSTALLED.call_once(|| {
15 let _ = rustls::crypto::ring::default_provider().install_default();
16 });
17 }
18
19 /// An HTTP client with a provider guaranteed to be installed.
20 pub(crate) fn client(timeout: Duration) -> reqwest::Result<reqwest::Client> {
21 install_crypto_provider();
22 reqwest::Client::builder().timeout(timeout).build()
23 }
24