Skip to content

Commit

Permalink
Redo http transport
Browse files Browse the repository at this point in the history
The existing implementation has accumulated a lot of crust from initial
async support and use of hyper. Now that is using reqwest we can do many
things simpler.

Removed things that were needed for hyper but are handled already by
reqwest:
- setting the proxy
- setting the auth header
- setting the content type header
- setting content length header

Other changes:
- Move from struct with Future impl to BoxFuture. We were already using
BoxFuture internally anyawy.
- Log request and response as debug strings. This makes sure no control
characters like newlines end up in the output.
- Use from_utf8_lossy for the response log so that we can still
partially log non utf8 responses.
- Fix not handling batch responses according to the jsonrpc
specification which does not guarantee the ordering of the list.
- Add more context to error logs.
- Allow giving a custom client to the transport. This is useful for
example to set a timeout.
  • Loading branch information
vkgnosis authored and e00E committed Jun 4, 2021
1 parent 6d15edf commit bf7e55d
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 232 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pin-project = "1.0"
## HTTP
base64 = { version = "0.13", optional = true }
bytes = { version = "1.0", optional = true }
reqwest = { version = "0.11", optional = true, default-features = false }
reqwest = { version = "0.11", optional = true, default-features = false, features = ["json"] }
headers = { version = "0.3", optional = true }
## WS
# async-native-tls = { git = "https://github.com/async-email/async-native-tls.git", rev = "b5b5562d6cea77f913d4cbe448058c031833bf17", optional = true, default-features = false }
Expand Down
16 changes: 12 additions & 4 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use futures::{
Future,
};
use pin_project::pin_project;
use serde::de::DeserializeOwned;
use std::{marker::PhantomData, pin::Pin};

/// Takes any type which is deserializable from rpc::Value and such a value and
Expand Down Expand Up @@ -70,12 +71,19 @@ pub fn build_request(id: usize, method: &str, params: Vec<rpc::Value>) -> rpc::C
/// Parse bytes slice into JSON-RPC response.
/// It looks for arbitrary_precision feature as a temporary workaround for https://github.com/tomusdrw/rust-web3/issues/460.
pub fn to_response_from_slice(response: &[u8]) -> error::Result<rpc::Response> {
arbitrary_precision_deserialize_workaround(response).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))
}

/// Deserialize bytes into T.
/// It looks for arbitrary_precision feature as a temporary workaround for https://github.com/tomusdrw/rust-web3/issues/460.
pub fn arbitrary_precision_deserialize_workaround<T>(bytes: &[u8]) -> Result<T, serde_json::Error>
where
T: DeserializeOwned,
{
if cfg!(feature = "arbitrary_precision") {
let val: serde_json::Value =
serde_json::from_slice(response).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))?;
serde_json::from_value(val).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))
serde_json::from_value(serde_json::from_slice(bytes)?)
} else {
serde_json::from_slice(response).map_err(|e| Error::InvalidResponse(format!("{:?}", e)))
serde_json::from_slice(bytes)
}
}

Expand Down
Loading

0 comments on commit bf7e55d

Please sign in to comment.