Skip to content

Commit

Permalink
Force backend choice through API rather than features
Browse files Browse the repository at this point in the history
As discussed in #125.
  • Loading branch information
djc committed Nov 28, 2020
1 parent 6ace7ca commit fc60b8c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 47 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ tokio-runtime = ["hyper/full", "ct-logs"]
[[example]]
name = "client"
path = "examples/client.rs"
required-features = ["tokio-runtime"]
required-features = ["native-tokio", "tokio-runtime"]

[[example]]
name = "server"
path = "examples/server.rs"
required-features = ["tokio-runtime"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
2 changes: 1 addition & 1 deletion examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async fn run_client() -> io::Result<()> {
hyper_rustls::HttpsConnector::from((http, tls))
}
// Default HTTPS connector.
None => hyper_rustls::HttpsConnector::new(),
None => hyper_rustls::HttpsConnector::with_native_roots(),
};

// Build the hyper client from the HTTPS connector.
Expand Down
65 changes: 31 additions & 34 deletions src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,43 @@ pub struct HttpsConnector<T> {
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);
/// Construct a new `HttpsConnector` using the OS root store
#[cfg(feature = "rustls-native-certs")]
#[cfg_attr(docsrs, doc(cfg(feature = "rustls-native-certs")))]
pub fn with_native_roots() -> 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);
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"),
};
if config.root_store.is_empty() {
panic!("no CA certificates found");
}
(http, config).into()
Self::build(config)
}
}

#[cfg(all(
any(feature = "rustls-native-certs", feature = "webpki-roots"),
feature = "tokio-runtime"
))]
impl Default for HttpsConnector<HttpConnector> {
fn default() -> Self {
Self::new()
/// Construct a new `HttpsConnector` using the `webpki_roots`
#[cfg(feature = "webpki-roots")]
#[cfg_attr(docsrs, doc(cfg(feature = "webpki-roots")))]
pub fn with_webpki_roots() -> Self {
let mut config = ClientConfig::new();
config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
Self::build(config)
}

fn build(mut config: ClientConfig) -> Self {
let mut http = HttpConnector::new();
http.enforce_http(false);

config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
config.ct_logs = Some(&ct_logs::LOGS);
(http, config).into()
}
}

Expand Down
13 changes: 2 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
//! ## Example
//!
//! ```no_run
//! # #[cfg(all(any(feature = "rustls-native-certs", feature = "webpki-roots"), feature = "tokio-runtime"))]
//! # #[cfg(all(feature = "rustls-native-certs", feature = "tokio-runtime"))]
//! # fn main() {
//! use hyper::{Body, Client, StatusCode, Uri};
//!
//! let mut rt = tokio::runtime::Runtime::new().unwrap();
//! let url = ("https://hyper.rs").parse().unwrap();
//! let https = hyper_rustls::HttpsConnector::new();
//! let https = hyper_rustls::HttpsConnector::with_native_roots();
//!
//! let client: Client<_, hyper::Body> = Client::builder().build(https);
//!
Expand All @@ -22,15 +22,6 @@
//! # fn main() {}
//! ```
#[cfg(all(
feature = "tokio-runtime",
any(not(feature = "rustls-native-certs"), feature = "webpki-roots"),
any(not(feature = "webpki-roots"), feature = "rustls-native-certs")
))]
compile_error!(
"Must enable exactly one of rustls-native-certs (default) or webpki-roots with tokio-runtime! (note: use `default-features = false' in a binary crate for one or other)"
);

mod connector;
mod stream;

Expand Down

0 comments on commit fc60b8c

Please sign in to comment.