reverse_proxy_service/
client.rs

1//! Includes helper functions to build [`Client`]s, and some re-exports from [`hyper::client`] or
2//! [`hyper_tls`].
3
4use hyper::body::HttpBody;
5pub use hyper::client::{Builder, Client};
6
7use hyper::client::connect::Connect;
8pub use hyper::client::connect::HttpConnector;
9
10#[cfg(feature = "https")]
11#[cfg_attr(docsrs, doc(cfg(feature = "https")))]
12pub use hyper_tls::HttpsConnector;
13
14#[cfg(feature = "__rustls")]
15#[cfg_attr(docsrs, doc(cfg(feature = "rustls")))]
16pub use hyper_rustls::HttpsConnector as RustlsConnector;
17
18#[cfg(feature = "nativetls")]
19#[cfg_attr(docsrs, doc(cfg(feature = "nativetls")))]
20pub use hyper_tls::HttpsConnector as NativeTlsConnector;
21
22/// Default [`Builder`].
23pub fn builder() -> Builder {
24    Builder::default()
25}
26
27/// Same as [`Client::new()`], except for the `B` parameter.
28pub fn http_default<B>() -> Client<HttpConnector, B>
29where
30    B: HttpBody + Send,
31    B::Data: Send,
32{
33    Builder::default().build_http()
34}
35
36/// Alias to [`nativetls_default()`].
37#[cfg(any(feature = "https", feature = "nativetls"))]
38#[cfg_attr(docsrs, doc(cfg(any(feature = "https", feature = "nativetls"))))]
39#[inline]
40pub fn https_default<B>() -> Client<NativeTlsConnector<HttpConnector>, B>
41where
42    B: HttpBody + Send,
43    B::Data: Send,
44{
45    nativetls_default()
46}
47
48/// With the default [`hyper_tls::HttpsConnector`].
49#[cfg(feature = "nativetls")]
50#[cfg_attr(docsrs, doc(cfg(feature = "nativetls")))]
51pub fn nativetls_default<B>() -> Client<NativeTlsConnector<HttpConnector>, B>
52where
53    B: HttpBody + Send,
54    B::Data: Send,
55{
56    Builder::default().build(NativeTlsConnector::new())
57}
58
59/// With the default [`hyper_rustls::HttpsConnector`].
60///
61/// The config is determined as follows. I think the cert root is similar to the `reqwest` crate.
62///
63/// 1. Cert roots
64///
65/// - if the feature `rustls-webpki-roots` is enabled, then use
66/// [`HttpsConnector::with_webpki_roots()`](hyper_rustls::HttpsConnector::with_webpki_roots());
67/// - if `rustls-webpki-roots` is disabled and `rustls-native-roots` enabled, then
68/// [`HttpsConnector::with_native_roots()`](hyper_rustls::HttpsConnector::with_native_roots());
69/// - otherwise compilation fails.
70///
71/// The feature `rustls` is equivalent to `rustls-webpki-roots`.
72///
73/// 2. Scheme
74///
75/// HTTPS only
76///
77/// 3. HTTP version
78///
79/// - if the feature `http1` is enabled, then call
80/// [`HttpsConnector::enable_http1()`](hyper_rustls::HttpsConnector::enable_http1());
81/// - if the feature `rustls-http2` is enabled, then call
82/// [`HttpsConnector::enable_http2()`](hyper_rustls::HttpsConnector::enable_http2()).
83///
84/// This is not exclusive: if both features are enabled, then both mehtods are called.
85///
86#[cfg(feature = "__rustls")]
87#[cfg_attr(docsrs, doc(cfg(feature = "rustls")))]
88pub fn rustls_default<B>() -> Client<RustlsConnector<HttpConnector>, B>
89where
90    B: HttpBody + Send,
91    B::Data: Send,
92{
93    let conn = hyper_rustls::HttpsConnectorBuilder::new();
94    #[cfg(feature = "rustls-webpki-roots")]
95    let conn = conn.with_webpki_roots();
96    #[cfg(all(not(feature = "rustls-webpki-roots"), feature = "rustls-native-roots"))]
97    let conn = conn.with_native_roots();
98    let conn = conn.https_only();
99    #[cfg(feature = "http1")]
100    let conn = conn.enable_http1();
101    #[cfg(feature = "rustls-http2")]
102    let conn = conn.enable_http2();
103    Builder::default().build(conn.build())
104}
105
106/// Default builder and given connector.
107pub fn with_connector_default<C, B>(conn: C) -> Client<C, B>
108where
109    C: Connect + Clone,
110    B: HttpBody + Send,
111    B::Data: Send,
112{
113    Builder::default().build(conn)
114}