Skip to content

Commit

Permalink
Allow construction with HttpConnector and default ClientConfig (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
inikulin committed Apr 17, 2020
1 parent 69133c8 commit 5a523eb
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use futures_util::FutureExt;
#[cfg(feature = "tokio-runtime")]
use hyper::client::connect::HttpConnector;
use hyper::{client::connect::Connection, service::Service, Uri};
use log::warn;
use rustls::ClientConfig;
use std::future::Future;
use std::pin::Pin;
Expand All @@ -11,7 +12,6 @@ use std::{fmt, io};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_rustls::TlsConnector;
use webpki::DNSNameRef;
use log::warn;

use crate::stream::MaybeHttpsStream;

Expand All @@ -24,41 +24,27 @@ pub struct HttpsConnector<T> {
tls_config: Arc<ClientConfig>,
}

#[cfg(all(any(feature = "rustls-native-certs", feature = "webpki-roots"), feature = "tokio-runtime"))]
#[cfg(all(
any(feature = "rustls-native-certs", feature = "webpki-roots"),
feature = "tokio-runtime"
))]
impl HttpsConnector<HttpConnector> {
/// Construct a new `HttpsConnector`.
///
/// Takes number of DNS worker threads.
pub fn new() -> Self {
let mut http = HttpConnector::new();

http.enforce_http(false);
let mut config = ClientConfig::new();
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
#[cfg(feature = "rustls-native-certs")]
{
config.root_store = match rustls_native_certs::load_native_certs() {
Ok(store) => store,
Err((Some(store), err)) => {
warn!("Could not load all certificates: {:?}", err);
store
}
Err((None, err)) => {
Err(err).expect("cannot access native cert store")
}
};
}
#[cfg(feature = "webpki-roots")]
{
config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
}
config.ct_logs = Some(&ct_logs::LOGS);
(http, config).into()

http.into()
}
}

#[cfg(all(any(feature = "rustls-native-certs", feature = "webpki-roots"), feature = "tokio-runtime"))]
#[cfg(all(
any(feature = "rustls-native-certs", feature = "webpki-roots"),
feature = "tokio-runtime"
))]
impl Default for HttpsConnector<HttpConnector> {
fn default() -> Self {
Self::new()
Expand All @@ -73,7 +59,7 @@ impl<T> fmt::Debug for HttpsConnector<T> {

impl<H, C> From<(H, C)> for HttpsConnector<H>
where
C: Into<Arc<ClientConfig>>
C: Into<Arc<ClientConfig>>,
{
fn from((http, cfg): (H, C)) -> Self {
HttpsConnector {
Expand All @@ -83,6 +69,40 @@ where
}
}

#[cfg(all(
any(feature = "rustls-native-certs", feature = "webpki-roots"),
feature = "tokio-runtime"
))]
impl<H> From<H> for HttpsConnector<H> {
fn from(http: H) -> Self {
let mut config = ClientConfig::new();
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
#[cfg(feature = "rustls-native-certs")]
{
config.root_store = match rustls_native_certs::load_native_certs() {
Ok(store) => store,
Err((Some(store), err)) => {
warn!("Could not load all certificates: {:?}", err);
store
}
Err((None, err)) => Err(err).expect("cannot access native cert store"),
};
}
#[cfg(feature = "webpki-roots")]
{
config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
}
config.ct_logs = Some(&ct_logs::LOGS);

HttpsConnector {
http,
tls_config: config.into(),
}
}
}

impl<T> Service<Uri> for HttpsConnector<T>
where
T: Service<Uri>,
Expand Down

0 comments on commit 5a523eb

Please sign in to comment.