From 0fc5bc0e971cf4303c66add0babbbb27f18cc18b Mon Sep 17 00:00:00 2001 From: Ivan Nikulin Date: Sat, 18 Apr 2020 00:07:59 +0100 Subject: [PATCH] Allow construction with HttpConnector and default ClientConfig (closes #67) --- .azure-pipelines.yml | 2 +- src/connector.rs | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/.azure-pipelines.yml b/.azure-pipelines.yml index f2ba500..c006698 100644 --- a/.azure-pipelines.yml +++ b/.azure-pipelines.yml @@ -21,7 +21,7 @@ jobs: - template: admin/pipelines/cargo-steps.yml - job: MacOS pool: - vmImage: macOS-10.13 + vmImage: macOS-10.14 steps: - template: admin/pipelines/rustup.yml - template: admin/pipelines/cargo-steps.yml diff --git a/src/connector.rs b/src/connector.rs index c23144b..ef40061 100644 --- a/src/connector.rs +++ b/src/connector.rs @@ -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; @@ -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; @@ -24,17 +24,26 @@ pub struct HttpsConnector { tls_config: Arc, } -#[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 { /// Construct a new `HttpsConnector`. - /// - /// Takes number of DNS worker threads. pub fn new() -> Self { let mut http = HttpConnector::new(); + http.enforce_http(false); + + (http, Self::default_client_config()).into() + } + + /// Constructs default `ClientConfig` which later can be used for + /// construction of `HttpsConnector` with custom `HttpConnector`. + pub fn default_client_config() -> ClientConfig { let mut config = ClientConfig::new(); config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()]; - #[cfg(feature = "rustls-native-certs")] + #[cfg(feature = "rustls-native-certs")] { config.root_store = match rustls_native_certs::load_native_certs() { Ok(store) => store, @@ -42,23 +51,25 @@ impl HttpsConnector { warn!("Could not load all certificates: {:?}", err); store } - Err((None, err)) => { - Err(err).expect("cannot access native cert store") - } + Err((None, err)) => Err(err).expect("cannot access native cert store"), }; } - #[cfg(feature = "webpki-roots")] + #[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() + + config } } -#[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 { fn default() -> Self { Self::new() @@ -73,7 +84,7 @@ impl fmt::Debug for HttpsConnector { impl From<(H, C)> for HttpsConnector where - C: Into> + C: Into>, { fn from((http, cfg): (H, C)) -> Self { HttpsConnector {