Upgrade http from 0.2 to 1.0

… and related dependencies.
This commit is contained in:
Jonas Platte 2024-04-26 23:21:28 +02:00
parent 7b373b78bb
commit 3760d69ade
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
6 changed files with 41 additions and 31 deletions

View File

@ -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" }

View File

@ -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 }

View File

@ -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<HttpConnector>;
/// The default connector is rarely useful, since it doesn't support `https`.
pub type Hyper<C = HttpConnector> = hyper_util::client::legacy::Client<C, Full<Bytes>>;
/// A hyper HTTP client using native-tls for TLS support.
#[cfg(feature = "hyper-native-tls")]
pub type HyperNativeTls = hyper::Client<hyper_tls::HttpsConnector<HttpConnector>>;
pub type HyperNativeTls = Hyper<hyper_tls::HttpsConnector<HttpConnector>>;
/// 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<hyper_rustls::HttpsConnector<HttpConnector>>;
pub type HyperRustls = Hyper<hyper_rustls::HttpsConnector<HttpConnector>>;
impl<C> HttpClient for hyper::Client<C>
impl<C> HttpClient for Hyper<C>
where
C: Connect + Clone + Send + Sync + 'static,
{
type RequestBody = BytesMut;
type ResponseBody = Bytes;
type Error = hyper::Error;
type Error = Box<dyn std::error::Error + Send + Sync>;
async fn send_http_request(
&self,
req: http::Request<BytesMut>,
) -> Result<http::Response<Bytes>, hyper::Error> {
let (head, body) = self
.request(req.map(|body| hyper::body::Body::from(body.freeze())))
.await?
.into_parts();
) -> Result<http::Response<Bytes>, 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())
}
}

View File

@ -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"

View File

@ -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"

View File

@ -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<client::http_client::HyperNativeTls>;
async fn run() -> Result<(), Box<dyn Error>> {
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<String, Box<dyn Error>> {
let uri = "https://v2.jokeapi.dev/joke/Programming,Pun,Misc?safe-mode&type=single"
.parse::<hyper::Uri>()?;
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::<JsonValue>(&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")?;