From 34f72fd3277b93366f818d1089a1394ff9eef513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?19=E5=B9=B4=E6=A2=A6=E9=86=92?= <3949379+getong@users.noreply.github.com> Date: Sun, 14 Jul 2024 19:24:15 +0800 Subject: [PATCH 1/2] update hyper to version 1.0 --- Cargo.toml | 4 ++- src/transports/http.rs | 64 +++++++++++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b4fbd628..33d11189 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,7 +67,9 @@ hex-literal = "0.4" wasm-bindgen-test = "0.3.19" [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] -hyper = { version = "0.14", default-features = false, features = ["server"] } +hyper = { version = "1", default-features = false, features = ["server"] } +hyper-util = { version = "0.1", features = ["full"] } +http-body-util = "0.1" tokio = { version = "1.0", features = ["full"] } tokio-stream = { version = "0.1", features = ["net"] } diff --git a/src/transports/http.rs b/src/transports/http.rs index 252538e2..43445f06 100644 --- a/src/transports/http.rs +++ b/src/transports/http.rs @@ -193,6 +193,8 @@ fn id_of_output(output: &Output) -> Result { mod tests { use super::*; use crate::Error::Rpc; + use bytes::Bytes; + use http_body_util::{BodyExt, Full}; use jsonrpc_core::ErrorCode; use std::net::TcpListener; @@ -200,37 +202,43 @@ mod tests { Some(TcpListener::bind(("127.0.0.1", 0)).ok()?.local_addr().ok()?.port()) } - async fn server(req: hyper::Request) -> hyper::Result> { - use hyper::body::HttpBody; - + async fn server(req: hyper::Request) -> hyper::Result>> { let expected = r#"{"jsonrpc":"2.0","method":"eth_getAccounts","params":[],"id":0}"#; let response = r#"{"jsonrpc":"2.0","id":0,"result":"x"}"#; assert_eq!(req.method(), &hyper::Method::POST); assert_eq!(req.uri().path(), "/"); let mut content: Vec = vec![]; - let mut body = req.into_body(); - while let Some(Ok(chunk)) = body.data().await { - content.extend(&*chunk); - } + let body = req.into_body(); + let body = body.collect().await?.to_bytes().to_vec(); + content.extend(body); + assert_eq!(std::str::from_utf8(&*content), Ok(expected)); - Ok(hyper::Response::new(response.into())) + Ok(hyper::Response::new(Full::new(Bytes::from(response)))) } #[tokio::test] async fn should_make_a_request() { - use hyper::service::{make_service_fn, service_fn}; - + use hyper::service::service_fn; + use hyper_util::{ + rt::{TokioExecutor, TokioIo}, + server::conn::auto, + }; + use tokio::net::TcpListener; // given let addr = format!("127.0.0.1:{}", get_available_port().unwrap()); - // start server - let service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(server)) }); - let server = hyper::Server::bind(&addr.parse().unwrap()).serve(service); let addr_clone = addr.clone(); + // start server + let listener = TcpListener::bind(addr.clone()).await.unwrap(); tokio::spawn(async move { println!("Listening on http://{}", addr_clone); - server.await.unwrap(); + let (stream, _) = listener.accept().await.unwrap(); + let io = TokioIo::new(stream); + auto::Builder::new(TokioExecutor::new()) + .serve_connection(io, service_fn(server)) + .await + .unwrap(); }); // when @@ -245,9 +253,15 @@ mod tests { #[tokio::test] async fn catch_generic_json_error_for_batched_request() { - use hyper::service::{make_service_fn, service_fn}; - - async fn handler(_req: hyper::Request) -> hyper::Result> { + use http_body_util::Full; + use hyper::service::service_fn; + use hyper_util::{ + rt::{TokioExecutor, TokioIo}, + server::conn::auto, + }; + use tokio::net::TcpListener; + + async fn handler(_req: hyper::Request) -> hyper::Result>> { let response = r#"{ "jsonrpc":"2.0", "error":{ @@ -256,18 +270,22 @@ mod tests { }, "id":null }"#; - Ok(hyper::Response::::new(response.into())) + Ok(hyper::Response::new(Full::new(Bytes::from(response)))) } - // given + // Given let addr = format!("127.0.0.1:{}", get_available_port().unwrap()); - // start server - let service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(handler)) }); - let server = hyper::Server::bind(&addr.parse().unwrap()).serve(service); let addr_clone = addr.clone(); + // start server + let listener = TcpListener::bind(addr.clone()).await.unwrap(); tokio::spawn(async move { println!("Listening on http://{}", addr_clone); - server.await.unwrap(); + let (stream, _) = listener.accept().await.unwrap(); + let io = TokioIo::new(stream); + auto::Builder::new(TokioExecutor::new()) + .serve_connection(io, service_fn(handler)) + .await + .unwrap(); }); // when From 092fdd28e677af63216b720738d9a006a65f35be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?19=E5=B9=B4=E6=A2=A6=E9=86=92?= <3949379+getong@users.noreply.github.com> Date: Tue, 27 Aug 2024 23:47:21 +0800 Subject: [PATCH 2/2] update reqwest to 0.12 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 33d11189..cce5bff2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ once_cell = { version = "1.8.0", optional = true } ## HTTP base64 = { version = "0.22", optional = true } bytes = { version = "1.0", optional = true } -reqwest = { version = "0.11", optional = true, default-features = false, features = ["json"] } +reqwest = { version = "0.12", optional = true, default-features = false, features = ["json"] } headers = { version = "0.4", optional = true } ## WS # async-native-tls = { git = "https://github.com/async-email/async-native-tls.git", rev = "b5b5562d6cea77f913d4cbe448058c031833bf17", optional = true, default-features = false }