Require the native-tls feature to supply a preconfigured tls (#814)

This commit is contained in:
Sean McArthur
2020-02-21 12:41:13 -08:00
committed by GitHub
parent 9ab8ab945c
commit 2e06108f70
5 changed files with 55 additions and 27 deletions

View File

@@ -1,4 +1,7 @@
#[cfg(feature = "__tls")]
#[cfg(any(
feature = "native-tls",
feature = "rustls-tls",
))]
use std::any::Any;
use std::convert::TryInto;
use std::net::IpAddr;
@@ -199,9 +202,9 @@ impl ClientBuilder {
config.nodelay,
)?
},
#[cfg(feature = "default-tls")]
TlsBackend::BuiltDefault(conn) => {
Connector::from_built_default(
#[cfg(feature = "native-tls")]
TlsBackend::BuiltNativeTls(conn) => {
Connector::from_built_default_tls(
conn,
proxies.clone(),
user_agent(&config.headers),
@@ -251,6 +254,10 @@ impl ClientBuilder {
config.nodelay,
)?
},
#[cfg(any(
feature = "native-tls",
feature = "rustls-tls",
))]
TlsBackend::UnknownPreconfigured => {
return Err(crate::error::builder(
"Unknown TLS backend passed to `use_preconfigured_tls`"
@@ -742,14 +749,22 @@ impl ClientBuilder {
///
/// If the passed `Any` argument is not a TLS backend that reqwest
/// understands, the `ClientBuilder` will error when calling `build`.
#[cfg(feature = "__tls")]
///
/// # Optional
///
/// This requires one of the optional features `native-tls` or
/// `rustls-tls` to be enabled.
#[cfg(any(
feature = "native-tls",
feature = "rustls-tls",
))]
pub fn use_preconfigured_tls(mut self, tls: impl Any) -> ClientBuilder {
let mut tls = Some(tls);
#[cfg(feature = "default-tls")]
#[cfg(feature = "native-tls")]
{
if let Some(conn) = (&mut tls as &mut dyn Any).downcast_mut::<Option<native_tls_crate::TlsConnector>>() {
let tls = conn.take().expect("is definitely Some");
let tls = crate::tls::TlsBackend::BuiltDefault(tls);
let tls = crate::tls::TlsBackend::BuiltNativeTls(tls);
self.config.tls = tls;
return self;
}

View File

@@ -1,3 +1,7 @@
#[cfg(any(
feature = "native-tls",
feature = "rustls-tls",
))]
use std::any::Any;
use std::convert::TryInto;
use std::fmt;
@@ -455,7 +459,15 @@ impl ClientBuilder {
///
/// If the passed `Any` argument is not a TLS backend that reqwest
/// understands, the `ClientBuilder` will error when calling `build`.
#[cfg(feature = "__tls")]
///
/// # Optional
///
/// This requires one of the optional features `native-tls` or
/// `rustls-tls` to be enabled.
#[cfg(any(
feature = "native-tls",
feature = "rustls-tls",
))]
pub fn use_preconfigured_tls(self, tls: impl Any) -> ClientBuilder {
self.with_inner(move |inner| inner.use_preconfigured_tls(tls))
}

View File

@@ -92,23 +92,17 @@ impl Connector {
T: Into<Option<IpAddr>>,
{
let tls = tls.build().map_err(crate::error::builder)?;
let mut http = http_connector()?;
http.set_local_address(local_addr.into());
http.enforce_http(false);
Ok(Connector {
inner: Inner::DefaultTls(http, tls),
Self::from_built_default_tls(
tls,
proxies,
verbose: verbose::OFF,
timeout: None,
nodelay,
user_agent,
})
local_addr,
nodelay,
)
}
#[cfg(feature = "default-tls")]
pub(crate) fn from_built_default<T> (
pub(crate) fn from_built_default_tls<T> (
tls: TlsConnector,
proxies: Arc<Vec<Proxy>>,
user_agent: Option<HeaderValue>,
@@ -117,7 +111,6 @@ impl Connector {
where
T: Into<Option<IpAddr>>,
{
let mut http = http_connector()?;
http.set_local_address(local_addr.into());
http.enforce_http(false);

View File

@@ -269,12 +269,16 @@ impl fmt::Debug for Identity {
pub(crate) enum TlsBackend {
#[cfg(feature = "default-tls")]
Default,
#[cfg(feature = "default-tls")]
BuiltDefault(native_tls_crate::TlsConnector),
#[cfg(feature = "native-tls")]
BuiltNativeTls(native_tls_crate::TlsConnector),
#[cfg(feature = "rustls-tls")]
Rustls,
#[cfg(feature = "rustls-tls")]
BuiltRustls(rustls::ClientConfig),
#[cfg(any(
feature = "native-tls",
feature = "rustls-tls",
))]
UnknownPreconfigured,
}
@@ -283,12 +287,16 @@ impl fmt::Debug for TlsBackend {
match self {
#[cfg(feature = "default-tls")]
TlsBackend::Default => write!(f, "Default"),
#[cfg(feature = "default-tls")]
TlsBackend::BuiltDefault(_) => write!(f, "BuiltDefault"),
#[cfg(feature = "native-tls")]
TlsBackend::BuiltNativeTls(_) => write!(f, "BuiltNativeTls"),
#[cfg(feature = "rustls-tls")]
TlsBackend::Rustls => write!(f, "Rustls"),
#[cfg(feature = "rustls-tls")]
TlsBackend::BuiltRustls(_) => write!(f, "BuiltRustls"),
#[cfg(any(
feature = "native-tls",
feature = "rustls-tls",
))]
TlsBackend::UnknownPreconfigured => write!(f, "UnknownPreconfigured"),
}
}

View File

@@ -134,7 +134,7 @@ async fn body_pipe_response() {
assert_eq!(res2.status(), reqwest::StatusCode::OK);
}
#[cfg(feature = "__tls")]
#[cfg(any(feature = "native-tls", feature = "rustls-tls",))]
#[test]
fn use_preconfigured_tls_with_bogus_backend() {
struct DefinitelyNotTls;
@@ -145,7 +145,7 @@ fn use_preconfigured_tls_with_bogus_backend() {
.expect_err("definitely is not TLS");
}
#[cfg(feature = "default-tls")]
#[cfg(feature = "native-tls")]
#[test]
fn use_preconfigured_native_tls_default() {
extern crate native_tls_crate;