Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow construction with HttpConnector and default ClientConfig #107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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