#[cfg(all(
any(target_os = "macos", target_os = "windows", target_os = "ios"),
feature = "tls"
))]
use std::convert::From as _;
#[cfg(all(
not(any(target_os = "macos", target_os = "windows", target_os = "ios")),
feature = "tls"
))]
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct Connector;
impl Connector {
pub fn builder() -> Builder {
Builder {}
}
}
#[derive(Debug)]
pub struct Builder {}
impl Builder {
#[cfg(feature = "tls")]
pub fn https(self) -> HttpsBuilder {
HttpsBuilder {
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
server_cert: None,
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
client_cert: None,
}
}
pub fn build(self) -> hyper_util::client::legacy::connect::HttpConnector {
hyper_util::client::legacy::connect::HttpConnector::new()
}
}
#[cfg(feature = "tls")]
#[derive(Debug)]
pub struct HttpsBuilder {
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
server_cert: Option<PathBuf>,
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
client_cert: Option<(PathBuf, PathBuf)>,
}
#[cfg(feature = "tls")]
impl HttpsBuilder {
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
pub fn pin_server_certificate<CA>(mut self, ca_certificate: CA) -> Self
where
CA: AsRef<Path>,
{
self.server_cert = Some(ca_certificate.as_ref().to_owned());
self
}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
pub fn client_authentication<K, C>(mut self, client_key: K, client_certificate: C) -> Self
where
K: AsRef<Path>,
C: AsRef<Path>,
{
self.client_cert = Some((
client_key.as_ref().to_owned(),
client_certificate.as_ref().to_owned(),
));
self
}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
pub fn build(
self,
) -> Result<
hyper_openssl::client::legacy::HttpsConnector<
hyper_util::client::legacy::connect::HttpConnector,
>,
openssl::error::ErrorStack,
> {
let mut ssl = openssl::ssl::SslConnector::builder(openssl::ssl::SslMethod::tls())?;
if let Some(ca_certificate) = self.server_cert {
ssl.set_ca_file(ca_certificate)?;
}
if let Some((client_key, client_certificate)) = self.client_cert {
ssl.set_private_key_file(client_key, openssl::ssl::SslFiletype::PEM)?;
ssl.set_certificate_chain_file(client_certificate)?;
ssl.check_private_key()?;
}
let mut connector = hyper_util::client::legacy::connect::HttpConnector::new();
connector.enforce_http(false);
hyper_openssl::client::legacy::HttpsConnector::<
hyper_util::client::legacy::connect::HttpConnector,
>::with_connector(connector, ssl)
}
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "ios"))]
pub fn build(
self,
) -> Result<
hyper_tls::HttpsConnector<hyper_util::client::legacy::connect::HttpConnector>,
native_tls::Error,
> {
let tls = native_tls::TlsConnector::new()?.into();
let mut connector = hyper_util::client::legacy::connect::HttpConnector::new();
connector.enforce_http(false);
let mut connector = hyper_tls::HttpsConnector::from((connector, tls));
connector.https_only(true);
Ok(connector)
}
}