Skip to content
This repository was archived by the owner on Apr 19, 2026. It is now read-only.

Commit d725e27

Browse files
authored
Merge pull request rustls#134 from djc/additive-features
Force backend choice through API rather than features
2 parents 15a46eb + fc60b8c commit d725e27

5 files changed

Lines changed: 46 additions & 53 deletions

File tree

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ tokio-runtime = ["hyper/runtime", "ct-logs"]
3535
[[example]]
3636
name = "client"
3737
path = "examples/client.rs"
38-
required-features = ["tokio-runtime"]
38+
required-features = ["native-tokio", "tokio-runtime"]
3939

4040
[[example]]
4141
name = "server"
4242
path = "examples/server.rs"
4343
required-features = ["tokio-runtime"]
44+
45+
[package.metadata.docs.rs]
46+
all-features = true
47+
rustdoc-args = ["--cfg", "docsrs"]

examples/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ async fn run_client() -> io::Result<()> {
5555
hyper_rustls::HttpsConnector::from((http, tls))
5656
}
5757
// Default HTTPS connector.
58-
None => hyper_rustls::HttpsConnector::new(),
58+
None => hyper_rustls::HttpsConnector::with_native_roots(),
5959
};
6060

6161
// Build the hyper client from the HTTPS connector.

examples/server.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
//! otherwise HTTP/1.1 will be used.
77
use async_stream::stream;
88
use core::task::{Context, Poll};
9-
use futures_util::{
10-
future::TryFutureExt,
11-
stream::Stream,
12-
};
9+
use futures_util::{future::TryFutureExt, stream::Stream};
1310
use hyper::service::{make_service_fn, service_fn};
1411
use hyper::{Body, Method, Request, Response, Server, StatusCode};
1512
use rustls::internal::pemfile;

src/connector.rs

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use futures_util::FutureExt;
22
#[cfg(feature = "tokio-runtime")]
33
use hyper::client::connect::HttpConnector;
44
use hyper::{client::connect::Connection, service::Service, Uri};
5+
use log::warn;
56
use rustls::ClientConfig;
67
use std::future::Future;
78
use std::pin::Pin;
@@ -11,7 +12,6 @@ use std::{fmt, io};
1112
use tokio::io::{AsyncRead, AsyncWrite};
1213
use tokio_rustls::TlsConnector;
1314
use webpki::DNSNameRef;
14-
use log::warn;
1515

1616
use crate::stream::MaybeHttpsStream;
1717

@@ -24,47 +24,48 @@ pub struct HttpsConnector<T> {
2424
tls_config: Arc<ClientConfig>,
2525
}
2626

27-
#[cfg(all(any(feature = "rustls-native-certs", feature = "webpki-roots"), feature = "tokio-runtime"))]
27+
#[cfg(all(
28+
any(feature = "rustls-native-certs", feature = "webpki-roots"),
29+
feature = "tokio-runtime"
30+
))]
2831
impl HttpsConnector<HttpConnector> {
29-
/// Construct a new `HttpsConnector`.
30-
///
31-
/// Takes number of DNS worker threads.
32-
pub fn new() -> Self {
33-
let mut http = HttpConnector::new();
34-
http.enforce_http(false);
32+
/// Construct a new `HttpsConnector` using the OS root store
33+
#[cfg(feature = "rustls-native-certs")]
34+
#[cfg_attr(docsrs, doc(cfg(feature = "rustls-native-certs")))]
35+
pub fn with_native_roots() -> Self {
3536
let mut config = ClientConfig::new();
36-
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
37-
#[cfg(feature = "rustls-native-certs")]
38-
{
39-
config.root_store = match rustls_native_certs::load_native_certs() {
40-
Ok(store) => store,
41-
Err((Some(store), err)) => {
42-
warn!("Could not load all certificates: {:?}", err);
43-
store
44-
}
45-
Err((None, err)) => {
46-
Err(err).expect("cannot access native cert store")
47-
}
48-
};
49-
}
50-
#[cfg(feature = "webpki-roots")]
51-
{
52-
config
53-
.root_store
54-
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
55-
}
56-
config.ct_logs = Some(&ct_logs::LOGS);
37+
config.root_store = match rustls_native_certs::load_native_certs() {
38+
Ok(store) => store,
39+
Err((Some(store), err)) => {
40+
warn!("Could not load all certificates: {:?}", err);
41+
store
42+
}
43+
Err((None, err)) => Err(err).expect("cannot access native cert store"),
44+
};
5745
if config.root_store.is_empty() {
5846
panic!("no CA certificates found");
5947
}
60-
(http, config).into()
48+
Self::build(config)
6149
}
62-
}
6350

64-
#[cfg(all(any(feature = "rustls-native-certs", feature = "webpki-roots"), feature = "tokio-runtime"))]
65-
impl Default for HttpsConnector<HttpConnector> {
66-
fn default() -> Self {
67-
Self::new()
51+
/// Construct a new `HttpsConnector` using the `webpki_roots`
52+
#[cfg(feature = "webpki-roots")]
53+
#[cfg_attr(docsrs, doc(cfg(feature = "webpki-roots")))]
54+
pub fn with_webpki_roots() -> Self {
55+
let mut config = ClientConfig::new();
56+
config
57+
.root_store
58+
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
59+
Self::build(config)
60+
}
61+
62+
fn build(mut config: ClientConfig) -> Self {
63+
let mut http = HttpConnector::new();
64+
http.enforce_http(false);
65+
66+
config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
67+
config.ct_logs = Some(&ct_logs::LOGS);
68+
(http, config).into()
6869
}
6970
}
7071

@@ -76,7 +77,7 @@ impl<T> fmt::Debug for HttpsConnector<T> {
7677

7778
impl<H, C> From<(H, C)> for HttpsConnector<H>
7879
where
79-
C: Into<Arc<ClientConfig>>
80+
C: Into<Arc<ClientConfig>>,
8081
{
8182
fn from((http, cfg): (H, C)) -> Self {
8283
HttpsConnector {

src/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
//! ## Example
66
//!
77
//! ```no_run
8-
//! # #[cfg(all(any(feature = "rustls-native-certs", feature = "webpki-roots"), feature = "tokio-runtime"))]
8+
//! # #[cfg(all(feature = "rustls-native-certs", feature = "tokio-runtime"))]
99
//! # fn main() {
1010
//! use hyper::{Body, Client, StatusCode, Uri};
1111
//!
1212
//! let mut rt = tokio::runtime::Runtime::new().unwrap();
1313
//! let url = ("https://hyper.rs").parse().unwrap();
14-
//! let https = hyper_rustls::HttpsConnector::new();
14+
//! let https = hyper_rustls::HttpsConnector::with_native_roots();
1515
//!
1616
//! let client: Client<_, hyper::Body> = Client::builder().build(https);
1717
//!
@@ -22,15 +22,6 @@
2222
//! # fn main() {}
2323
//! ```
2424
25-
#[cfg(all(
26-
feature = "tokio-runtime",
27-
any(not(feature = "rustls-native-certs"), feature = "webpki-roots"),
28-
any(not(feature = "webpki-roots"), feature = "rustls-native-certs")
29-
))]
30-
compile_error!(
31-
"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)"
32-
);
33-
3425
mod connector;
3526
mod stream;
3627

0 commit comments

Comments
 (0)