Skip to main content

tower_proxy/
client.rs

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