From 3760d69ade15aa83859a15cdf7adb425f9381165 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 26 Apr 2024 23:21:28 +0200 Subject: [PATCH] Upgrade http from 0.2 to 1.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and related dependencies. --- Cargo.toml | 2 +- crates/ruma-client/Cargo.toml | 12 ++++--- crates/ruma-client/src/http_client/hyper.rs | 40 +++++++++++---------- crates/ruma-server-util/Cargo.toml | 2 +- examples/joke_bot/Cargo.toml | 8 +++-- examples/joke_bot/src/main.rs | 8 +++-- 6 files changed, 41 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1e6db8ce..36e3d2fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ assert_matches2 = "0.1.0" assign = "1.1.1" base64 = "0.21.0" criterion = "0.5.0" -http = "0.2.8" +http = "1.1.0" js_int = "0.2.2" maplit = "1.0.2" ruma-appservice-api = { version = "0.9.0", path = "crates/ruma-appservice-api" } diff --git a/crates/ruma-client/Cargo.toml b/crates/ruma-client/Cargo.toml index 880e91fc..1b279065 100644 --- a/crates/ruma-client/Cargo.toml +++ b/crates/ruma-client/Cargo.toml @@ -19,7 +19,7 @@ rustdoc-args = ["--cfg", "docsrs"] client-api = ["dep:as_variant", "dep:ruma-client-api"] # HTTP clients -hyper = ["dep:hyper"] +hyper = ["dep:http-body-util", "dep:hyper", "dep:hyper-util"] hyper-native-tls = ["hyper", "dep:hyper-tls"] hyper-rustls = ["hyper", "dep:hyper-rustls"] reqwest = ["dep:reqwest"] @@ -37,10 +37,12 @@ async-stream = "0.3.0" bytes = "1.0.1" futures-core = "0.3.8" http = { workspace = true } -hyper = { version = "0.14.2", optional = true, features = ["client", "http1", "http2", "tcp"] } -hyper-rustls = { version = "0.24.0", optional = true, default-features = false } -hyper-tls = { version = "0.5.0", optional = true } -reqwest = { version = "0.11.4", optional = true, default-features = false } +http-body-util = { version = "0.1.1", optional = true } +hyper = { version = "1.3.1", optional = true, features = ["client", "http1", "http2"] } +hyper-rustls = { version = "0.27.1", optional = true, default-features = false } +hyper-tls = { version = "0.6.0", optional = true } +hyper-util = { version = "0.1.3", optional = true, features = ["client-legacy", "http1", "http2", "tokio"] } +reqwest = { version = "0.12.4", optional = true, default-features = false } ruma-client-api = { workspace = true, optional = true, features = ["client"] } ruma-common = { workspace = true, features = ["api"] } serde_html_form = { workspace = true } diff --git a/crates/ruma-client/src/http_client/hyper.rs b/crates/ruma-client/src/http_client/hyper.rs index 55f497d7..5df1faf6 100644 --- a/crates/ruma-client/src/http_client/hyper.rs +++ b/crates/ruma-client/src/http_client/hyper.rs @@ -1,44 +1,46 @@ use bytes::{Bytes, BytesMut}; -use hyper::client::{connect::Connect, HttpConnector}; +use http_body_util::{BodyExt as _, Full}; +use hyper_util::{ + client::legacy::connect::{Connect, HttpConnector}, + rt::TokioExecutor, +}; use super::{DefaultConstructibleHttpClient, HttpClient}; -/// A basic hyper HTTP client. +/// A hyper HTTP client. /// -/// You basically never want this, since it doesn't support `https`. -pub type Hyper = hyper::Client; +/// The default connector is rarely useful, since it doesn't support `https`. +pub type Hyper = hyper_util::client::legacy::Client>; /// A hyper HTTP client using native-tls for TLS support. #[cfg(feature = "hyper-native-tls")] -pub type HyperNativeTls = hyper::Client>; +pub type HyperNativeTls = Hyper>; /// A hyper HTTP client using rustls for TLS support. /// -/// This client does not implement `DefaultConstructibleHttpClient`. To use it, you need to manually -/// construct +/// This client does not implement `DefaultConstructibleHttpClient`. +/// To use it, you need to manually create an instance. #[cfg(feature = "hyper-rustls")] -pub type HyperRustls = hyper::Client>; +pub type HyperRustls = Hyper>; -impl HttpClient for hyper::Client +impl HttpClient for Hyper where C: Connect + Clone + Send + Sync + 'static, { type RequestBody = BytesMut; type ResponseBody = Bytes; - type Error = hyper::Error; + type Error = Box; async fn send_http_request( &self, req: http::Request, - ) -> Result, hyper::Error> { - let (head, body) = self - .request(req.map(|body| hyper::body::Body::from(body.freeze()))) - .await? - .into_parts(); + ) -> Result, Self::Error> { + let (head, body) = + self.request(req.map(|body| Full::new(body.freeze()))).await?.into_parts(); // FIXME: Use aggregate instead of to_bytes once serde_json can parse from a reader at a // comparable speed as reading from a slice: https://github.com/serde-rs/json/issues/160 - let body = hyper::body::to_bytes(body).await?; + let body = body.collect().await?.to_bytes(); Ok(http::Response::from_parts(head, body)) } } @@ -46,13 +48,15 @@ where #[cfg(feature = "hyper")] impl DefaultConstructibleHttpClient for Hyper { fn default() -> Self { - hyper::Client::new() + hyper_util::client::legacy::Client::builder(TokioExecutor::new()) + .build(HttpConnector::new()) } } #[cfg(feature = "hyper-native-tls")] impl DefaultConstructibleHttpClient for HyperNativeTls { fn default() -> Self { - hyper::Client::builder().build(hyper_tls::HttpsConnector::new()) + hyper_util::client::legacy::Client::builder(TokioExecutor::new()) + .build(hyper_tls::HttpsConnector::new()) } } diff --git a/crates/ruma-server-util/Cargo.toml b/crates/ruma-server-util/Cargo.toml index de3f06ce..562394d1 100644 --- a/crates/ruma-server-util/Cargo.toml +++ b/crates/ruma-server-util/Cargo.toml @@ -16,7 +16,7 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [dependencies] -headers = "0.3" +headers = "0.4.0" ruma-common = { workspace = true } tracing = { workspace = true } yap = "0.11.0" diff --git a/examples/joke_bot/Cargo.toml b/examples/joke_bot/Cargo.toml index 901c0e10..3c8bcade 100644 --- a/examples/joke_bot/Cargo.toml +++ b/examples/joke_bot/Cargo.toml @@ -11,9 +11,11 @@ ruma = { version = "0.9.4", path = "../../crates/ruma", features = ["client-api- # ruma = { git = "https://github.com/ruma/ruma", rev = "f161c8117c706fc52089999e1f406cf34276ec9d", features = ["client-api-c", "client", "client-hyper-native-tls", "events"] } futures-util = { version = "0.3.21", default-features = false, features = ["std"] } -http = "0.2.2" -hyper = "0.14.2" -hyper-tls = "0.5.0" +http = "1.1.0" +http-body-util = "0.1.1" +hyper = "1.3.1" +hyper-tls = "0.6.0" +hyper-util = { version = "0.1.3", features = ["client-legacy", "http1", "http2", "tokio"] } serde_json = "1.0" tokio = { version = "1", features = ["full"] } tokio-stream = "0.1.7" diff --git a/examples/joke_bot/src/main.rs b/examples/joke_bot/src/main.rs index ad13785b..6e4ebdd5 100644 --- a/examples/joke_bot/src/main.rs +++ b/examples/joke_bot/src/main.rs @@ -1,6 +1,8 @@ use std::{error::Error, io, process::exit, time::Duration}; use futures_util::future::{join, join_all}; +use http_body_util::BodyExt as _; +use hyper_util::rt::TokioExecutor; use ruma::{ api::client::{ filter::FilterDefinition, membership::join_room_by_id, message::send_message_event, @@ -34,8 +36,8 @@ type MatrixClient = client::Client; async fn run() -> Result<(), Box> { let config = read_config().await.map_err(|e| format!("configuration in ./config is invalid: {e}"))?; - let http_client = - hyper::Client::builder().build::<_, hyper::Body>(hyper_tls::HttpsConnector::new()); + let http_client = hyper_util::client::legacy::Client::builder(TokioExecutor::new()) + .build(hyper_tls::HttpsConnector::new()); let matrix_client = if let Some(state) = read_state().await.ok().flatten() { ruma::Client::builder() .homeserver_url(config.homeserver.clone()) @@ -204,7 +206,7 @@ async fn get_joke(client: &HttpClient) -> Result> { let uri = "https://v2.jokeapi.dev/joke/Programming,Pun,Misc?safe-mode&type=single" .parse::()?; let rsp = client.get(uri).await?; - let bytes = hyper::body::to_bytes(rsp).await?; + let bytes = rsp.into_body().collect().await?.to_bytes(); let joke_obj = serde_json::from_slice::(&bytes) .map_err(|_| "invalid JSON returned from joke API")?; let joke = joke_obj["joke"].as_str().ok_or("joke field missing from joke API response")?;