Unverified Commit 2efb6f79 authored by Niklas Adolfsson's avatar Niklas Adolfsson Committed by GitHub
Browse files

[http client]: add HTTPS support (#210)

* Revert "[http client]: switch to surf instead of hyper (#204)"

This reverts commit ce1625b7

.

* feat(http client): HTTPS support

* [deps]: hyper-rustls

* fix nit

* more ugly conditional compilation

* Update http-client/src/transport.rs

* Update http-client/src/transport.rs

* Update http-client/src/transport.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* Update http-client/src/transport.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* Update http-client/src/transport.rs

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>

* [http client]: make `tls` not optional.

* fix bad merge

* simplify imports

Co-authored-by: David's avatarDavid <dvdplm@gmail.com>
parent e2b62bab
......@@ -9,6 +9,8 @@ license = "MIT"
[dependencies]
async-trait = "0.1"
futures = "0.3"
hyper13-rustls = { package = "hyper-rustls", version = "0.21", optional = true }
hyper14-rustls = { package = "hyper-rustls", version = "0.22", optional = true }
hyper14 = { package = "hyper", version = "0.14", features = ["client", "http1", "http2", "tcp"], optional = true }
hyper13 = { package = "hyper", version = "0.13", optional = true }
jsonrpsee-types = { path = "../types", version = "0.2.0-alpha.2" }
......@@ -22,8 +24,8 @@ url = "2.2"
[features]
default = ["tokio1"]
tokio1 = ["hyper14", "jsonrpsee-utils/hyper14"]
tokio02 = ["hyper13", "jsonrpsee-utils/hyper13"]
tokio1 = ["hyper14", "hyper14-rustls", "jsonrpsee-utils/hyper14"]
tokio02 = ["hyper13", "hyper13-rustls", "jsonrpsee-utils/hyper13"]
[dev-dependencies]
jsonrpsee-test-utils = { path = "../test-utils" }
......
......@@ -28,9 +28,13 @@ compile_error!("feature `tokio1` or `tokio02` must be enabled for this crate");
#[cfg(all(feature = "tokio1", not(feature = "tokio02")))]
extern crate hyper14 as hyper;
#[cfg(all(feature = "tokio1", not(feature = "tokio02")))]
extern crate hyper14_rustls as hyper_rustls;
#[cfg(all(feature = "tokio02", not(feature = "tokio1")))]
extern crate hyper13 as hyper;
#[cfg(all(feature = "tokio02", not(feature = "tokio1")))]
extern crate hyper13_rustls as hyper_rustls;
mod client;
mod transport;
......
......@@ -6,6 +6,8 @@
// that we need to be guaranteed that hyper doesn't re-use an existing connection if we ever reset
// the JSON-RPC request id to a value that might have already been used.
use hyper::client::{Client, HttpConnector};
use hyper_rustls::HttpsConnector;
use jsonrpsee_types::{error::GenericTransportError, http::HttpConfig, jsonrpc};
use jsonrpsee_utils::http::hyper_helpers;
use thiserror::Error;
......@@ -17,8 +19,8 @@ const CONTENT_TYPE_JSON: &str = "application/json";
pub struct HttpTransportClient {
/// Target to connect to.
target: url::Url,
/// HTTP client,
client: hyper::Client<hyper::client::HttpConnector>,
/// HTTP client
client: Client<HttpsConnector<HttpConnector>>,
/// Configurable max request body size
config: HttpConfig,
}
......@@ -27,10 +29,15 @@ impl HttpTransportClient {
/// Initializes a new HTTP client.
pub fn new(target: impl AsRef<str>, config: HttpConfig) -> Result<Self, Error> {
let target = url::Url::parse(target.as_ref()).map_err(|e| Error::Url(format!("Invalid URL: {}", e)))?;
if target.scheme() == "http" {
Ok(HttpTransportClient { client: hyper::Client::new(), target, config })
if target.scheme() == "http" || target.scheme() == "https" {
#[cfg(feature = "tokio1")]
let connector = HttpsConnector::with_native_roots();
#[cfg(feature = "tokio02")]
let connector = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(connector);
Ok(HttpTransportClient { client, target, config })
} else {
Err(Error::Url("URL scheme not supported, expects 'http'".into()))
Err(Error::Url("URL scheme not supported, expects 'http' or 'https'".into()))
}
}
......@@ -50,7 +57,6 @@ impl HttpTransportClient {
.expect("URI and request headers are valid; qed");
let response = self.client.request(req).await.map_err(|e| Error::Http(Box::new(e)))?;
if response.status().is_success() {
Ok(response)
} else {
......
......@@ -199,6 +199,13 @@ async fn ws_more_request_than_buffer_should_not_deadlock() {
}
}
#[tokio::test]
async fn https_works() {
let client = HttpClient::new("https://kusama-rpc.polkadot.io", HttpConfig::default()).unwrap();
let response: String = client.request("system_chain", Params::None).await.unwrap();
assert_eq!(&response, "Kusama");
}
#[tokio::test]
async fn wss_works() {
let client = WsClient::new(WsConfig::with_url("wss://kusama-rpc.polkadot.io")).await.unwrap();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment