Require the native-tls feature to supply a preconfigured tls (#814)
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
#[cfg(feature = "__tls")]
|
#[cfg(any(
|
||||||
|
feature = "native-tls",
|
||||||
|
feature = "rustls-tls",
|
||||||
|
))]
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
@@ -199,9 +202,9 @@ impl ClientBuilder {
|
|||||||
config.nodelay,
|
config.nodelay,
|
||||||
)?
|
)?
|
||||||
},
|
},
|
||||||
#[cfg(feature = "default-tls")]
|
#[cfg(feature = "native-tls")]
|
||||||
TlsBackend::BuiltDefault(conn) => {
|
TlsBackend::BuiltNativeTls(conn) => {
|
||||||
Connector::from_built_default(
|
Connector::from_built_default_tls(
|
||||||
conn,
|
conn,
|
||||||
proxies.clone(),
|
proxies.clone(),
|
||||||
user_agent(&config.headers),
|
user_agent(&config.headers),
|
||||||
@@ -251,6 +254,10 @@ impl ClientBuilder {
|
|||||||
config.nodelay,
|
config.nodelay,
|
||||||
)?
|
)?
|
||||||
},
|
},
|
||||||
|
#[cfg(any(
|
||||||
|
feature = "native-tls",
|
||||||
|
feature = "rustls-tls",
|
||||||
|
))]
|
||||||
TlsBackend::UnknownPreconfigured => {
|
TlsBackend::UnknownPreconfigured => {
|
||||||
return Err(crate::error::builder(
|
return Err(crate::error::builder(
|
||||||
"Unknown TLS backend passed to `use_preconfigured_tls`"
|
"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
|
/// If the passed `Any` argument is not a TLS backend that reqwest
|
||||||
/// understands, the `ClientBuilder` will error when calling `build`.
|
/// 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 {
|
pub fn use_preconfigured_tls(mut self, tls: impl Any) -> ClientBuilder {
|
||||||
let mut tls = Some(tls);
|
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>>() {
|
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 = conn.take().expect("is definitely Some");
|
||||||
let tls = crate::tls::TlsBackend::BuiltDefault(tls);
|
let tls = crate::tls::TlsBackend::BuiltNativeTls(tls);
|
||||||
self.config.tls = tls;
|
self.config.tls = tls;
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
#[cfg(any(
|
||||||
|
feature = "native-tls",
|
||||||
|
feature = "rustls-tls",
|
||||||
|
))]
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
@@ -455,7 +459,15 @@ impl ClientBuilder {
|
|||||||
///
|
///
|
||||||
/// If the passed `Any` argument is not a TLS backend that reqwest
|
/// If the passed `Any` argument is not a TLS backend that reqwest
|
||||||
/// understands, the `ClientBuilder` will error when calling `build`.
|
/// 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 {
|
pub fn use_preconfigured_tls(self, tls: impl Any) -> ClientBuilder {
|
||||||
self.with_inner(move |inner| inner.use_preconfigured_tls(tls))
|
self.with_inner(move |inner| inner.use_preconfigured_tls(tls))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,23 +92,17 @@ impl Connector {
|
|||||||
T: Into<Option<IpAddr>>,
|
T: Into<Option<IpAddr>>,
|
||||||
{
|
{
|
||||||
let tls = tls.build().map_err(crate::error::builder)?;
|
let tls = tls.build().map_err(crate::error::builder)?;
|
||||||
|
Self::from_built_default_tls(
|
||||||
let mut http = http_connector()?;
|
tls,
|
||||||
http.set_local_address(local_addr.into());
|
|
||||||
http.enforce_http(false);
|
|
||||||
|
|
||||||
Ok(Connector {
|
|
||||||
inner: Inner::DefaultTls(http, tls),
|
|
||||||
proxies,
|
proxies,
|
||||||
verbose: verbose::OFF,
|
|
||||||
timeout: None,
|
|
||||||
nodelay,
|
|
||||||
user_agent,
|
user_agent,
|
||||||
})
|
local_addr,
|
||||||
|
nodelay,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "default-tls")]
|
#[cfg(feature = "default-tls")]
|
||||||
pub(crate) fn from_built_default<T> (
|
pub(crate) fn from_built_default_tls<T> (
|
||||||
tls: TlsConnector,
|
tls: TlsConnector,
|
||||||
proxies: Arc<Vec<Proxy>>,
|
proxies: Arc<Vec<Proxy>>,
|
||||||
user_agent: Option<HeaderValue>,
|
user_agent: Option<HeaderValue>,
|
||||||
@@ -117,7 +111,6 @@ impl Connector {
|
|||||||
where
|
where
|
||||||
T: Into<Option<IpAddr>>,
|
T: Into<Option<IpAddr>>,
|
||||||
{
|
{
|
||||||
|
|
||||||
let mut http = http_connector()?;
|
let mut http = http_connector()?;
|
||||||
http.set_local_address(local_addr.into());
|
http.set_local_address(local_addr.into());
|
||||||
http.enforce_http(false);
|
http.enforce_http(false);
|
||||||
|
|||||||
16
src/tls.rs
16
src/tls.rs
@@ -269,12 +269,16 @@ impl fmt::Debug for Identity {
|
|||||||
pub(crate) enum TlsBackend {
|
pub(crate) enum TlsBackend {
|
||||||
#[cfg(feature = "default-tls")]
|
#[cfg(feature = "default-tls")]
|
||||||
Default,
|
Default,
|
||||||
#[cfg(feature = "default-tls")]
|
#[cfg(feature = "native-tls")]
|
||||||
BuiltDefault(native_tls_crate::TlsConnector),
|
BuiltNativeTls(native_tls_crate::TlsConnector),
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
Rustls,
|
Rustls,
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
BuiltRustls(rustls::ClientConfig),
|
BuiltRustls(rustls::ClientConfig),
|
||||||
|
#[cfg(any(
|
||||||
|
feature = "native-tls",
|
||||||
|
feature = "rustls-tls",
|
||||||
|
))]
|
||||||
UnknownPreconfigured,
|
UnknownPreconfigured,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,12 +287,16 @@ impl fmt::Debug for TlsBackend {
|
|||||||
match self {
|
match self {
|
||||||
#[cfg(feature = "default-tls")]
|
#[cfg(feature = "default-tls")]
|
||||||
TlsBackend::Default => write!(f, "Default"),
|
TlsBackend::Default => write!(f, "Default"),
|
||||||
#[cfg(feature = "default-tls")]
|
#[cfg(feature = "native-tls")]
|
||||||
TlsBackend::BuiltDefault(_) => write!(f, "BuiltDefault"),
|
TlsBackend::BuiltNativeTls(_) => write!(f, "BuiltNativeTls"),
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
TlsBackend::Rustls => write!(f, "Rustls"),
|
TlsBackend::Rustls => write!(f, "Rustls"),
|
||||||
#[cfg(feature = "rustls-tls")]
|
#[cfg(feature = "rustls-tls")]
|
||||||
TlsBackend::BuiltRustls(_) => write!(f, "BuiltRustls"),
|
TlsBackend::BuiltRustls(_) => write!(f, "BuiltRustls"),
|
||||||
|
#[cfg(any(
|
||||||
|
feature = "native-tls",
|
||||||
|
feature = "rustls-tls",
|
||||||
|
))]
|
||||||
TlsBackend::UnknownPreconfigured => write!(f, "UnknownPreconfigured"),
|
TlsBackend::UnknownPreconfigured => write!(f, "UnknownPreconfigured"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ async fn body_pipe_response() {
|
|||||||
assert_eq!(res2.status(), reqwest::StatusCode::OK);
|
assert_eq!(res2.status(), reqwest::StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "__tls")]
|
#[cfg(any(feature = "native-tls", feature = "rustls-tls",))]
|
||||||
#[test]
|
#[test]
|
||||||
fn use_preconfigured_tls_with_bogus_backend() {
|
fn use_preconfigured_tls_with_bogus_backend() {
|
||||||
struct DefinitelyNotTls;
|
struct DefinitelyNotTls;
|
||||||
@@ -145,7 +145,7 @@ fn use_preconfigured_tls_with_bogus_backend() {
|
|||||||
.expect_err("definitely is not TLS");
|
.expect_err("definitely is not TLS");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "default-tls")]
|
#[cfg(feature = "native-tls")]
|
||||||
#[test]
|
#[test]
|
||||||
fn use_preconfigured_native_tls_default() {
|
fn use_preconfigured_native_tls_default() {
|
||||||
extern crate native_tls_crate;
|
extern crate native_tls_crate;
|
||||||
|
|||||||
Reference in New Issue
Block a user