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 20, 2020
1 parent 69133c8 commit 0fc5bc0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 23 additions & 12 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,52 @@ 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);

(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,
Err((Some(store), err)) => {
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<HttpConnector> {
fn default() -> Self {
Self::new()
Expand All @@ -73,7 +84,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 Down

0 comments on commit 0fc5bc0

Please sign in to comment.