Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Remove dependencies on OpenSSL (#4036)
Browse files Browse the repository at this point in the history
* Remove dependency on hyper-tls

This removes one of the dependencies on native-tls, and thus on OpenSSL.
I will remove the other in a separate commit.

* Remove the `HttpClient` enum

It only had one variant.

* Apply suggestions from code review

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
  • Loading branch information
2 people authored and bkchr committed Nov 8, 2019
1 parent 6fb3758 commit 62e9e85
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
32 changes: 27 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/offchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ keystore = { package = "substrate-keystore", path = "../keystore" }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
hyper = "0.12.35"
hyper-tls = "0.3.2"
hyper-rustls = "0.17.1"

[dev-dependencies]
env_logger = "0.7.0"
Expand Down
37 changes: 4 additions & 33 deletions core/offchain/src/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::api::timestamp;
use bytes::Buf as _;
use fnv::FnvHashMap;
use futures::{prelude::*, channel::mpsc, compat::Compat01As03};
use log::{warn, error};
use log::error;
use primitives::offchain::{HttpRequestId, Timestamp, HttpRequestStatus, HttpError};
use std::{fmt, io::Read as _, mem, pin::Pin, task::Context, task::Poll};

Expand All @@ -50,9 +50,7 @@ pub fn http() -> (HttpApi, HttpWorker) {
let engine = HttpWorker {
to_api,
from_api,
// TODO: don't unwrap; we should fall back to the HttpConnector if we fail to create the
// Https one; there doesn't seem to be any built-in way to do this
http_client: HyperClient::new(),
http_client: hyper::Client::builder().build(hyper_rustls::HttpsConnector::new(1)),
requests: Vec::new(),
};

Expand Down Expand Up @@ -551,38 +549,14 @@ enum WorkerToApi {
},
}

/// Wraps around a `hyper::Client` with either TLS enabled or disabled.
enum HyperClient {
/// Everything is ok and HTTPS is available.
Https(hyper::Client<hyper_tls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>),
/// We failed to initialize HTTPS and therefore only allow HTTP.
Http(hyper::Client<hyper::client::HttpConnector, hyper::Body>),
}

impl HyperClient {
/// Creates new hyper client.
///
/// By default we will try to initialize the `HttpsConnector`,
/// If that's not possible we'll fall back to `HttpConnector`.
pub fn new() -> Self {
match hyper_tls::HttpsConnector::new(1) {
Ok(tls) => HyperClient::Https(hyper::Client::builder().build(tls)),
Err(e) => {
warn!("Unable to initialize TLS client. Falling back to HTTP-only: {:?}", e);
HyperClient::Http(hyper::Client::new())
},
}
}
}

/// Must be continuously polled for the [`HttpApi`] to properly work.
pub struct HttpWorker {
/// Used to sends messages to the `HttpApi`.
to_api: mpsc::UnboundedSender<WorkerToApi>,
/// Used to receive messages from the `HttpApi`.
from_api: mpsc::UnboundedReceiver<ApiToWorker>,
/// The engine that runs HTTP requests.
http_client: HyperClient,
http_client: hyper::Client<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body>,
/// HTTP requests that are being worked on by the engine.
requests: Vec<(HttpRequestId, HttpWorkerRequest)>,
}
Expand Down Expand Up @@ -686,10 +660,7 @@ impl Future for HttpWorker {
Poll::Pending => {},
Poll::Ready(None) => return Poll::Ready(()), // stops the worker
Poll::Ready(Some(ApiToWorker::Dispatch { id, request })) => {
let future = Compat01As03::new(match me.http_client {
HyperClient::Http(ref mut c) => c.request(request),
HyperClient::Https(ref mut c) => c.request(request),
});
let future = Compat01As03::new(me.http_client.request(request));
debug_assert!(me.requests.iter().all(|(i, _)| *i != id));
me.requests.push((id, HttpWorkerRequest::Dispatched(future)));
cx.waker().wake_by_ref(); // reschedule the task to poll the request
Expand Down

0 comments on commit 62e9e85

Please sign in to comment.