Unverified Commit 11d40103 authored by Niklas Adolfsson's avatar Niklas Adolfsson Committed by GitHub
Browse files

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

This reverts commit ce1625b7.
parent 523489e4
......@@ -54,7 +54,6 @@ jobs:
profile: minimal
toolchain: stable
override: true
target: wasm32-unknown-unknown
- name: Cache Dependencies & Build Outputs
uses: actions/cache@v2.1.4
......@@ -71,17 +70,11 @@ jobs:
command: check
args: --benches --tests
- name: Cargo check HTTP client with hyper
- name: Cargo check all-features
uses: actions-rs/cargo@v1
with:
command: check
args: --manifest-path http-client/Cargo.toml --no-default-features --features hyper-client
- name: Cargo check HTTP client for WASM.
uses: actions-rs/cargo@v1
with:
command: check
args: --manifest-path http-client/Cargo.toml --no-default-features --features wasm-client --target wasm32-unknown-unknown
args: --benches --tests --all-features
tests:
name: Run tests
......
......@@ -8,21 +8,16 @@ license = "MIT"
[dependencies]
futures = "0.3"
surf = { version = "2.1", default-features = false, optional = true }
hyper = { version = "0.14", features = ["stream", "client", "server", "http1", "http2", "tcp"] }
jsonrpsee-types = { path = "../types", version = "0.1" }
jsonrpsee-utils = { path = "../utils", version = "0.1" }
log = "0.4"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
unicase = "2.6"
url = "2.2"
[dev-dependencies]
jsonrpsee-test-utils = { path = "../test-utils" }
tokio = { version = "1.0", features = ["net", "rt-multi-thread", "macros"] }
[features]
default = ["surf/curl-client"]
curl-client = ["surf/curl-client"]
hyper-client = ["surf/hyper-client"]
wasm-client = ["surf/wasm-client"]
middleware-logger = ["surf/middleware-logger"]
......@@ -6,9 +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 futures::prelude::*;
use jsonrpsee_types::{error::GenericTransportError, http::HttpConfig, jsonrpc};
use surf::http::{mime::JSON, Url};
use jsonrpsee_utils::http::hyper_helpers;
use thiserror::Error;
const CONTENT_TYPE_JSON: &str = "application/json";
......@@ -17,9 +16,9 @@ const CONTENT_TYPE_JSON: &str = "application/json";
#[derive(Debug, Clone)]
pub struct HttpTransportClient {
/// Target to connect to.
target: Url,
target: url::Url,
/// HTTP client,
client: surf::Client,
client: hyper::Client<hyper::client::HttpConnector>,
/// Configurable max request body size
config: HttpConfig,
}
......@@ -27,16 +26,16 @@ pub struct HttpTransportClient {
impl HttpTransportClient {
/// Initializes a new HTTP client.
pub fn new(target: impl AsRef<str>, config: HttpConfig) -> Result<Self, Error> {
let target = Url::parse(target.as_ref()).map_err(|e| Error::Url(format!("Invalid URL: {}", e)))?;
let target = url::Url::parse(target.as_ref()).map_err(|e| Error::Url(format!("Invalid URL: {}", e)))?;
if target.scheme() == "http" {
Ok(HttpTransportClient { client: surf::Client::new(), target, config })
Ok(HttpTransportClient { client: hyper::Client::new(), target, config })
} else {
Err(Error::Url("URL scheme not supported, expects 'http'".into()))
}
}
/// Send request.
async fn send_request(&self, request: jsonrpc::Request) -> Result<surf::Response, Error> {
async fn send_request(&self, request: jsonrpc::Request) -> Result<hyper::Response<hyper::Body>, Error> {
let body = jsonrpc::to_vec(&request).map_err(Error::Serialization)?;
log::debug!("send: {}", request);
......@@ -44,10 +43,14 @@ impl HttpTransportClient {
return Err(Error::RequestTooLarge);
}
let request =
surf::post(&self.target).body(body).header("accept", CONTENT_TYPE_JSON).content_type(JSON).build();
let req = hyper::Request::post(self.target.as_str())
.header(hyper::header::CONTENT_TYPE, hyper::header::HeaderValue::from_static(CONTENT_TYPE_JSON))
.header(hyper::header::ACCEPT, hyper::header::HeaderValue::from_static(CONTENT_TYPE_JSON))
.body(From::from(body))
.expect("URI and request headers are valid; qed");
let response = self.client.request(req).await.map_err(|e| Error::Http(Box::new(e)))?;
let response = self.client.send(request).await.unwrap();
if response.status().is_success() {
Ok(response)
} else {
......@@ -66,22 +69,13 @@ impl HttpTransportClient {
&self,
request: jsonrpc::Request,
) -> Result<jsonrpc::Response, Error> {
let mut response = self.send_request(request).await.map_err(|e| Error::Http(Box::new(e)))?;
let length = response.len().unwrap_or(0);
if length > self.config.max_request_body_size as usize {
return Err(Error::RequestTooLarge.into());
}
let mut buffer = Vec::with_capacity(length);
let reader = response.take_body().into_reader();
let mut take = reader.take(self.config.max_request_body_size as u64);
take.read_to_end(&mut buffer).await.map_err(|e| Error::Http(Box::new(e)))?;
let response = self.send_request(request).await?;
let (parts, body) = response.into_parts();
let body = hyper_helpers::read_response_to_body(&parts.headers, body, self.config).await?;
let response: jsonrpc::Response = jsonrpc::from_slice(&buffer).map_err(Error::ParseError)?;
// Note that we don't check the Content-Type of the request. This is deemed
// unnecessary, as a parsing error while happen anyway.
let response: jsonrpc::Response = jsonrpc::from_slice(&body).map_err(Error::ParseError)?;
log::debug!("recv: {}", jsonrpc::to_string(&response).expect("request valid JSON; qed"));
Ok(response)
}
......
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