rust_wistia/
https.rs

1//! Module to resolve the HTTPS Connector / Client used to make requests,
2//! depending on which TLS implementation we want to use.
3//!
4//! For instance, when building for the `x86_64-unknown-linux-musl` target
5//! like we do for [AWS Lambda][] deployments, we want to prefer to use
6//! `hyper_rustls` - which uses a pure Rust implementation of TLS - over the
7//! native `hyper_tls` implementation, which uses OpenSSL. For that reason,
8//! using the connector from the `hyper_rustls` crate is actually the default.
9//!
10//! This can be controlled by the optional "features" enabled for this crate:
11//!     * `rust-tls`: enables the rust implementation of TLS (default)
12//!     * `native-tls`: enables the native implementation of TLS using OpenSSL
13//!
14//! [AWS Lambda]: https://docs.aws.amazon.com/sdk-for-rust/latest/dg/lambda.html
15//!
16use hyper::client::HttpConnector;
17use hyper::Client;
18
19#[cfg(feature = "rust-tls")]
20pub use hyper_rustls as tls;
21#[cfg(not(feature = "rust-tls"))]
22pub use hyper_tls as tls;
23#[cfg(feature = "rust-tls")]
24use rustls::ClientConfig;
25#[cfg(feature = "rust-tls")]
26use tls::ConfigBuilderExt;
27
28#[cfg(all(feature = "rust-tls", feature = "http2"))]
29pub fn get_https_client<T>() -> Client<tls::HttpsConnector<HttpConnector>, T>
30where
31    T: hyper::body::HttpBody + std::marker::Send,
32    <T as hyper::body::HttpBody>::Data: Send,
33{
34    // Prepare the HTTPS connector
35    let https_connector = tls::HttpsConnectorBuilder::new()
36        // .with_native_roots()
37        .with_tls_config(
38            ClientConfig::builder()
39                .with_safe_defaults()
40                .with_native_roots()
41                .with_no_client_auth(),
42        )
43        .https_only()
44        .enable_http2()
45        .build();
46
47    let mut builder = Client::builder();
48    builder.http2_only(true);
49
50    builder.build::<_, T>(https_connector)
51}
52
53#[cfg(all(feature = "rust-tls", not(feature = "http2")))]
54pub fn get_https_client<T>() -> Client<tls::HttpsConnector<HttpConnector>, T>
55where
56    T: hyper::body::HttpBody + std::marker::Send,
57    <T as hyper::body::HttpBody>::Data: Send,
58{
59    // Prepare the HTTPS connector
60    let https_connector = tls::HttpsConnectorBuilder::new()
61        // .with_native_roots()
62        .with_tls_config(
63            ClientConfig::builder()
64                .with_safe_defaults()
65                .with_native_roots()
66                .with_no_client_auth(),
67        )
68        .https_only()
69        .enable_http2()
70        .build();
71
72    Client::builder().build::<_, T>(https_connector)
73}
74
75#[cfg(not(feature = "rust-tls"))]
76pub fn get_https_client<T>() -> Client<tls::HttpsConnector<HttpConnector>, T>
77where
78    T: hyper::body::HttpBody + std::marker::Send,
79    <T as hyper::body::HttpBody>::Data: Send,
80{
81    // Prepare the HTTPS connector
82    let https_connector = tls::HttpsConnector::new();
83    Client::builder().build::<_, T>(https_connector)
84}