Skip to content

Commit

Permalink
Optionally disable pulling in the tokio runtime
Browse files Browse the repository at this point in the history
Before this patch, hyper-rustls required pulling in the tokio runtime
due to it's use of `hyper::client::HttpConnector`. Unfortunately
Fuchsia does not support tokio, which prevents our use of this
library. This PR allows us to optionally disable pulling in tokio,
and instead swap in our own version of the hyper `HttpConnector`.
  • Loading branch information
erickt committed Mar 12, 2019
1 parent 8925936 commit ee70519
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ before_script:

script:
- cargo test
- cargo test --all-features
- cargo test --no-default-features
- bash -c 'if [[ "$TRAVIS_RUST_VERSION" == "$CLIPPY_RUST_VERSION" ]]; then
cargo clippy -- -D warnings;
fi'
20 changes: 17 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,29 @@ repository = "https://github.com/ctz/hyper-rustls"

[dependencies]
bytes = "0.4"
ct-logs = "0.5"
ct-logs = { version = "0.5", optional = true }
futures = "0.1.21"
hyper = "0.12.14"
hyper = { version = "0.12.14", default-features = false }
rustls = "0.15"
tokio-io = "0.1.1"
tokio-rustls = "0.9"
webpki = "0.19.0"
webpki-roots = "0.16"
webpki-roots = { version = "0.16", optional = true }

[dev-dependencies]
tokio = "0.1"
tokio-tcp = "0.1"

[features]
default = ["tokio-runtime"]
tokio-runtime = ["hyper/runtime", "ct-logs", "webpki-roots"]

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

[[example]]
name = "server"
path = "examples/server.rs"
required-features = ["tokio-runtime"]
7 changes: 5 additions & 2 deletions src/connector.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use ct_logs;
use futures::{Future, Poll};
use hyper::client::connect::{self, Connect};
#[cfg(feature = "tokio-runtime")]
use hyper::client::HttpConnector;
use rustls::{ClientConfig, Session};
use std::sync::Arc;
use std::{fmt, io};
use tokio_rustls::TlsConnector;
use webpki::{DNSName, DNSNameRef};
use webpki_roots;

use stream::MaybeHttpsStream;

Expand All @@ -18,11 +17,15 @@ pub struct HttpsConnector<T> {
tls_config: Arc<ClientConfig>,
}

#[cfg(feature = "tokio-runtime")]
impl HttpsConnector<HttpConnector> {
/// Construct a new `HttpsConnector`.
///
/// Takes number of DNS worker threads.
pub fn new(threads: usize) -> Self {
use ct_logs;
use webpki_roots;

let mut http = HttpConnector::new(threads);
http.enforce_http(false);
let mut config = ClientConfig::new();
Expand Down
28 changes: 16 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,36 @@
//! ## Example
//!
//! ```no_run
//! extern crate hyper;
//! extern crate hyper_rustls;
//! extern crate tokio;
//!
//! # #[cfg(feature = "tokio-runtime")]
//! # extern crate hyper;
//! #
//! # #[cfg(feature = "tokio-runtime")]
//! # fn main() {
//! use hyper::{Body, Client, StatusCode, Uri};
//!
//! fn main() {
//! let mut rt = tokio::runtime::Runtime::new().unwrap();
//! let url = ("https://hyper.rs").parse().unwrap();
//! let https = hyper_rustls::HttpsConnector::new(4);
//! let mut rt = tokio::runtime::Runtime::new().unwrap();
//! let url = ("https://hyper.rs").parse().unwrap();
//! let https = hyper_rustls::HttpsConnector::new(4);
//!
//! let client: Client<_, hyper::Body> = Client::builder().build(https);
//! let client: Client<_, hyper::Body> = Client::builder().build(https);
//!
//! let res = rt.block_on(client.get(url)).unwrap();
//! assert_eq!(res.status(), StatusCode::OK);
//! }
//! let res = rt.block_on(client.get(url)).unwrap();
//! assert_eq!(res.status(), StatusCode::OK);
//! # }
//! # #[cfg(not(feature = "tokio-runtime"))]
//! # fn main() {}
//! ```
extern crate bytes;
#[cfg(feature = "tokio-runtime")]
extern crate ct_logs;
extern crate futures;
extern crate hyper;
extern crate rustls;
extern crate tokio_io;
extern crate tokio_rustls;
extern crate webpki;
#[cfg(feature = "tokio-runtime")]
extern crate webpki_roots;

mod connector;
Expand Down

0 comments on commit ee70519

Please sign in to comment.