Skip to content

Commit

Permalink
Make TLS an optional feature of simple-request
Browse files Browse the repository at this point in the history
Removes 14 crates from the tree when compiling the message-queue client.

Also performs a non-intrusive cargo update.
  • Loading branch information
kayabaNerve committed Nov 15, 2023
1 parent 96f1d26 commit d25e3d8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 61 deletions.
77 changes: 40 additions & 37 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 coins/bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ frost = { package = "modular-frost", path = "../../crypto/frost", version = "0.8
hex = { version = "0.4", default-features = false, optional = true }
serde = { version = "1", default-features = false, features = ["derive"], optional = true }
serde_json = { version = "1", default-features = false, optional = true }
simple-request = { path = "../../common/request", version = "0.1", default-features = false, features = ["basic-auth"], optional = true }
simple-request = { path = "../../common/request", version = "0.1", default-features = false, features = ["tls", "basic-auth"], optional = true }

[dev-dependencies]
secp256k1 = { version = "0.28", default-features = false, features = ["std"] }
Expand Down
2 changes: 1 addition & 1 deletion coins/monero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ base58-monero = { version = "2", default-features = false, features = ["check"]

# Used for the provided HTTP RPC
digest_auth = { version = "0.3", default-features = false, optional = true }
simple-request = { path = "../../common/request", version = "0.1", default-features = false, optional = true }
simple-request = { path = "../../common/request", version = "0.1", default-features = false, features = ["tls"], optional = true }
tokio = { version = "1", default-features = false, optional = true }

[build-dependencies]
Expand Down
5 changes: 4 additions & 1 deletion common/request/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
# Deprecated here means to enable deprecated warnings, not to restore deprecated APIs
hyper = { version = "0.14", default-features = false, features = ["http1", "tcp", "client", "backports", "deprecated"] }
hyper-rustls = { version = "0.24", default-features = false, features = ["http1", "native-tokio"] }
tokio = { version = "1", default-features = false }

hyper-rustls = { version = "0.24", default-features = false, features = ["http1", "native-tokio"], optional = true }

zeroize = { version = "1", optional = true }
base64ct = { version = "1", features = ["alloc"], optional = true }

[features]
tls = ["hyper-rustls"]
basic-auth = ["zeroize", "base64ct"]
default = ["tls"]
47 changes: 26 additions & 21 deletions common/request/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;

use tokio::sync::Mutex;

#[cfg(feature = "tls")]
use hyper_rustls::{HttpsConnectorBuilder, HttpsConnector};
use hyper::{
Uri,
Expand All @@ -26,18 +27,19 @@ pub enum Error {
InvalidUri,
MissingHost,
InconsistentHost,
SslError(Box<dyn Send + Sync + std::error::Error>),
ConnectionError(Box<dyn Send + Sync + std::error::Error>),
Hyper(hyper::Error),
}

#[cfg(not(feature = "tls"))]
type Connector = HttpConnector;
#[cfg(feature = "tls")]
type Connector = HttpsConnector<HttpConnector>;

#[derive(Clone, Debug)]
enum Connection {
ConnectionPool(hyper::Client<HttpsConnector<HttpConnector>>),
Connection {
https_builder: HttpsConnector<HttpConnector>,
host: Uri,
connection: Arc<Mutex<Option<SendRequest<Body>>>>,
},
ConnectionPool(hyper::Client<Connector>),
Connection { connector: Connector, host: Uri, connection: Arc<Mutex<Option<SendRequest<Body>>>> },
}

#[derive(Clone, Debug)]
Expand All @@ -46,24 +48,25 @@ pub struct Client {
}

impl Client {
fn https_builder() -> HttpsConnector<HttpConnector> {
HttpsConnectorBuilder::new().with_native_roots().https_or_http().enable_http1().build()
fn connector() -> Connector {
#[cfg(feature = "tls")]
let res =
HttpsConnectorBuilder::new().with_native_roots().https_or_http().enable_http1().build();
#[cfg(not(feature = "tls"))]
let res = HttpConnector::new();
res
}

pub fn with_connection_pool() -> Client {
Client {
connection: Connection::ConnectionPool(hyper::Client::builder().build(Self::https_builder())),
connection: Connection::ConnectionPool(hyper::Client::builder().build(Self::connector())),
}
}

pub fn without_connection_pool(host: String) -> Result<Client, Error> {
Ok(Client {
connection: Connection::Connection {
https_builder: HttpsConnectorBuilder::new()
.with_native_roots()
.https_or_http()
.enable_http1()
.build(),
connector: Self::connector(),
host: {
let uri: Uri = host.parse().map_err(|_| Error::InvalidUri)?;
if uri.host().is_none() {
Expand Down Expand Up @@ -110,16 +113,18 @@ impl Client {

Ok(Response(match &self.connection {
Connection::ConnectionPool(client) => client.request(request).await.map_err(Error::Hyper)?,
Connection::Connection { https_builder, host, connection } => {
Connection::Connection { connector, host, connection } => {
let mut connection_lock = connection.lock().await;

// If there's not a connection...
if connection_lock.is_none() {
let (requester, connection) = hyper::client::conn::http1::handshake(
https_builder.clone().call(host.clone()).await.map_err(Error::SslError)?,
)
.await
.map_err(Error::Hyper)?;
let call_res = connector.clone().call(host.clone()).await;
#[cfg(not(feature = "tls"))]
let call_res = call_res.map_err(|e| Error::ConnectionError(format!("{e:?}").into()));
#[cfg(feature = "tls")]
let call_res = call_res.map_err(Error::ConnectionError);
let (requester, connection) =
hyper::client::conn::http1::handshake(call_res?).await.map_err(Error::Hyper)?;
// This will die when we drop the requester, so we don't need to track an AbortHandle for
// it
tokio::spawn(connection);
Expand Down

0 comments on commit d25e3d8

Please sign in to comment.