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

Remove dependencies on OpenSSL #4036

Merged
merged 3 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 5 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 @@ -47,12 +47,11 @@ pub fn http() -> (HttpApi, HttpWorker) {
requests: FnvHashMap::default(),
};

let tls = hyper_rustls::HttpsConnector::new(1);
bkchr marked this conversation as resolved.
Show resolved Hide resolved
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(tls),
bkchr marked this conversation as resolved.
Show resolved Hide resolved
requests: Vec::new(),
};

Expand Down Expand Up @@ -551,38 +550,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 +661,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