Skip to content

Commit

Permalink
Add support for hyper-rustls
Browse files Browse the repository at this point in the history
  • Loading branch information
wezm committed Aug 18, 2019
1 parent 4374c9e commit 7663710
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ chttp = { version = "0.5.3", optional = true }
# hyper-client
hyper = { version = "0.12.32", optional = true, default-features = false }
hyper-tls = { version = "0.3.2", optional = true }
hyper-rustls = { version = "0.17.0", optional = true }
native-tls = { version = "0.2.2", optional = true }
rustls = { version = "0.16.0", optional = true }
runtime = { version = "0.3.0-alpha.6", optional = true }
runtime-raw = { version = "0.3.0-alpha.4", optional = true }
runtime-tokio = { version = "0.3.0-alpha.5", optional = true }
Expand Down
30 changes: 25 additions & 5 deletions src/http_client/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ use futures::future::FutureObj;
use futures::prelude::*;
use futures::task::SpawnError;
use hyper::client::connect as hyper_connect;
#[cfg(feature = "hyper-tls")]
use hyper_tls::HttpsConnector;
#[cfg(feature = "hyper-rustls")]
use hyper_rustls::HttpsConnector;
#[cfg(feature = "hyper-rustls")]
use rustls::ClientConfig;
#[cfg(feature = "hyper-tls")]
use native_tls::TlsConnector;
use runtime::net::TcpStream;
use runtime_raw::Runtime;
Expand All @@ -27,11 +33,7 @@ pub struct HyperClient {
impl HyperClient {
/// Create a new instance.
pub(crate) fn new() -> Self {
// Create a TLS decoder, TCP stream, and combine them into a `Connector` to be passed to
// Hyper.
let tcp_connector = RuntimeTcpConnector::new();
let tls_connector = TlsConnector::new().unwrap();
let https = HttpsConnector::from((tcp_connector, tls_connector));
let https = Self::build_https_connector();

// Create the Hyper client with the `Connector`, and make sure we use `runtime-tokio` to
// spawn futures. Unfortunately, if futures are spawned onto `runtime-native`, we get weird
Expand All @@ -45,6 +47,24 @@ impl HyperClient {
client: Arc::new(client),
}
}

#[cfg(feature = "hyper-tls")]
fn build_https_connector() -> HttpsConnector<RuntimeTcpConnector> {
// Create a TLS decoder, TCP stream, and combine them into a `Connector` to be passed to
// Hyper.
let tcp_connector = RuntimeTcpConnector::new();
let tls_connector = TlsConnector::new().unwrap();
HttpsConnector::from((tcp_connector, tls_connector))
}

#[cfg(feature = "hyper-rustls")]
pub(crate) fn build_https_connector() -> HttpsConnector<RuntimeTcpConnector> {
// Create a TLS configuration, TCP stream, and combine them into a `Connector` to be passed
// to Hyper.
let tcp_connector = RuntimeTcpConnector::new();
let client_config = ClientConfig::new();
HttpsConnector::from((tcp_connector, client_config))
}
}

impl Clone for HyperClient {
Expand Down

0 comments on commit 7663710

Please sign in to comment.