Skip to content

Commit 2c73f72

Browse files
committed
Update http and try to figure out some hyper changes
> The free functions `hyper::body::to_bytes` and `aggregate` have been removed. Similar functionality is on `http_body_util::BodyExt::collect`. (0888623d) > > https://github.com/hyperium/hyper/blob/master/CHANGELOG.md#v100-rc1-2022-10-25 > remove higher-level `hyper::Client` ([#2941](hyperium/hyper#2941)) (bb3af17c) > > https://github.com/hyperium/hyper/blob/master/CHANGELOG.md#v100-rc1-2022-10-25 > remove client::connect module ([#2949](hyperium/hyper#2949)) (5e206883) > > > https://github.com/hyperium/hyper/blob/master/CHANGELOG.md#v100-rc1-2022-10-25 > remove `server::conn::{Http, Connection}` types ([#3013](hyperium/hyper#3013)) (0766d3f7, closes #3012) > > https://github.com/hyperium/hyper/blob/master/CHANGELOG.md#v100-rc1-2022-10-25 > The `poll_ready` method has been removed. (fee7d361) > > https://github.com/hyperium/hyper/blob/master/CHANGELOG.md#v100-rc1-2022-10-25 > replace IO traits with hyper::rt ones ([#3230](hyperium/hyper#3230)) (f9f65b7a, closes #3110) > > https://github.com/hyperium/hyper/blob/master/CHANGELOG.md#v100-rc1-2022-10-25
1 parent 2f8c31f commit 2c73f72

File tree

4 files changed

+62
-244
lines changed

4 files changed

+62
-244
lines changed

Cargo.toml

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ license = "Apache-2.0"
99
[dependencies]
1010
anyhow = "1.0.70"
1111
base64 = "0.21.0"
12+
bytes = "1.10.0"
1213
doc-comment = "0.3.3"
1314
ed25519-dalek = "2.0.0"
1415
futures = "0.3.28"
1516
futures-util = "0.3.28"
16-
http = "0.2.9"
17-
hyper = { version = "0.14.25", features = ["client", "http1", "server", "stream"] }
18-
hyper-rustls = "0.24.0"
17+
http = "1.2.0"
18+
http-body-util = "0.1.2"
19+
hyper = { version = "1.6.0", features = ["client", "http1", "server"] }
20+
hyper-rustls = "0.27.5"
21+
hyper-util = { version = "0.1.10", features = ["server"] }
1922
log = "0.4.17"
2023
rand = "0.8.5"
2124
serde = { version = "1.0.159", features = ["derive"] }
2225
serde_json = "1.0.95"
2326
signed-json = { git = "https://github.com/erikjohnston/rust-signed-json.git" }
2427
tokio = { version = "1.27.0", features = ["macros"] }
25-
tokio-rustls = "0.24.0"
28+
tokio-rustls = "0.26.1"
2629
hickory-resolver = "0.24.2"
2730
url = "2.5.4"

src/client.rs

+36-32
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,36 @@ use std::sync::Arc;
66
use anyhow::{bail, format_err, Context, Error};
77
use base64::prelude::BASE64_STANDARD_NO_PAD;
88
use base64::Engine;
9+
use bytes::Bytes;
910
use ed25519_dalek::SigningKey;
1011
use http::header::{AUTHORIZATION, CONTENT_TYPE};
1112
use http::request::{Builder, Parts};
1213
use http::{HeaderValue, Uri};
13-
use hyper::body::{to_bytes, HttpBody};
14-
use hyper::client::connect::Connect;
15-
use hyper::{Body, Client, Request, Response};
14+
use http_body_util::{BodyExt, Full};
15+
use hyper::{Request, Response};
16+
use hyper_util::client::legacy::connect::Connect;
17+
use hyper_util::client::legacy::Client;
18+
use hyper_util::rt::TokioExecutor;
1619
use serde::Serialize;
1720
use serde_json::value::RawValue;
1821
use signed_json::signed::Wrap;
1922
use signed_json::{Canonical, CanonicalWrapper, Signed};
2023

2124
use crate::server_resolver::{handle_delegated_server, MatrixConnector};
2225

23-
/// A [`hyper::Client`] that routes `matrix://` (Synapse <1.87.0rc1 (2023-06-27)) and
24-
/// `matrix-federation://` (Synapse >=1.87.0rc1 (2023-06-27)) URIs correctly, but does
25-
/// not sign the requests.
26+
/// A [`hyper_util::client::legacy::Client`] that routes `matrix://` (Synapse <1.87.0rc1
27+
/// (2023-06-27)) and `matrix-federation://` (Synapse >=1.87.0rc1 (2023-06-27)) URIs
28+
/// correctly, but does not sign the requests.
2629
///
2730
/// Either use [`SigningFederationClient`] if you want requests to be automatically
2831
/// signed, or [`sign_and_build_json_request`] to sign the requests.
2932
#[derive(Debug, Clone)]
3033
pub struct FederationClient {
31-
pub client: hyper::Client<MatrixConnector>,
34+
pub client: Client<MatrixConnector, Full<Bytes>>,
3235
}
3336

3437
impl FederationClient {
35-
pub fn new(client: hyper::Client<MatrixConnector>) -> Self {
38+
pub fn new(client: Client<MatrixConnector, Full<Bytes>>) -> Self {
3639
FederationClient { client }
3740
}
3841

@@ -41,11 +44,14 @@ impl FederationClient {
4144
let connector = MatrixConnector::with_default_resolver()?;
4245

4346
Ok(FederationClient {
44-
client: Client::builder().build(connector),
47+
client: Client::builder(TokioExecutor::new()).build(connector),
4548
})
4649
}
4750

48-
pub async fn request(&self, mut req: Request<Body>) -> Result<Response<Body>, Error> {
51+
pub async fn request(
52+
&self,
53+
mut req: Request<Full<Bytes>>,
54+
) -> Result<Response<Full<Bytes>>, Error> {
4955
req = handle_delegated_server(&self.client, req).await?;
5056

5157
Ok(self.client.request(req).await?)
@@ -79,7 +85,7 @@ impl SigningFederationClient<MatrixConnector> {
7985
let connector = MatrixConnector::with_default_resolver()?;
8086

8187
Ok(SigningFederationClient {
82-
client: Client::builder().build(connector),
88+
client: Client::builder(TokioExecutor::new()).build(connector),
8389
server_name: server_name.to_string(),
8490
key_id: key_id.to_string(),
8591
secret_key: Arc::new(secret_key),
@@ -114,8 +120,8 @@ where
114120
/// Make a GET request to the given URI.
115121
///
116122
/// Will sign the request if the URI has a `matrix` scheme.
117-
pub async fn get(&self, uri: Uri) -> Result<Response<Body>, Error> {
118-
let body = Body::default();
123+
pub async fn get(&self, uri: Uri) -> Result<Response<Full<Bytes>>, Error> {
124+
let body = Full::new(Bytes::new());
119125

120126
let mut req = Request::new(body);
121127
*req.uri_mut() = uri;
@@ -126,7 +132,10 @@ where
126132
///
127133
/// For `matrix://` or `matrix-federation://` URIs the request body must be JSON (if
128134
/// not empty) and the request will be signed.
129-
pub async fn request(&self, mut req: Request<Body>) -> Result<Response<Body>, Error> {
135+
pub async fn request(
136+
&self,
137+
mut req: Request<Full<Bytes>>,
138+
) -> Result<Response<Full<Bytes>>, Error> {
130139
req = handle_delegated_server(&self.client, req).await?;
131140

132141
// Return-early and make a normal request if the URI scheme is not `matrix://`
@@ -136,19 +145,14 @@ where
136145
_ => return Ok(self.client.request(req).await?),
137146
}
138147

139-
if !req.body().is_end_stream()
140-
&& req.headers().get(CONTENT_TYPE)
141-
!= Some(&HeaderValue::from_static("application/json"))
142-
{
148+
if req.headers().get(CONTENT_TYPE) != Some(&HeaderValue::from_static("application/json")) {
143149
bail!("Request has a non-JSON body")
144150
}
145151

146152
let (mut parts, body) = req.into_parts();
147153

148-
let content = if body.is_end_stream() {
149-
None
150-
} else {
151-
let bytes = to_bytes(body).await?;
154+
let content = {
155+
let bytes = BodyExt::collect(body).await.unwrap().to_bytes();
152156
let json_string = String::from_utf8(bytes.to_vec())?;
153157
Some(RawValue::from_string(json_string)?)
154158
};
@@ -167,7 +171,7 @@ where
167171
let new_body = if let Some(raw_value) = content {
168172
raw_value.to_string().into()
169173
} else {
170-
Body::default()
174+
Full::new(Bytes::new())
171175
};
172176

173177
let new_req = Request::from_parts(parts, new_body);
@@ -237,7 +241,7 @@ pub fn sign_and_build_json_request<T: serde::Serialize>(
237241
secret_key: &SigningKey,
238242
mut request_builder: Builder,
239243
content: Option<T>,
240-
) -> Result<Request<Body>, Error> {
244+
) -> Result<Request<Full<Bytes>>, Error> {
241245
let uri = request_builder
242246
.uri_ref()
243247
.ok_or_else(|| format_err!("URI must be set"))?;
@@ -276,9 +280,9 @@ pub fn sign_and_build_json_request<T: serde::Serialize>(
276280
let header_value = header_string.try_into()?;
277281

278282
let body = if let Some(c) = canonical_content {
279-
Body::from(c.into_canonical())
283+
Full::new(Bytes::from(c.into_canonical()))
280284
} else {
281-
Body::default()
285+
Full::new(Bytes::new())
282286
};
283287

284288
request_builder
@@ -327,7 +331,7 @@ pub trait SignedRequestBuilderExt {
327331
server_name: &str,
328332
key_id: &str,
329333
secret_key: &SigningKey,
330-
) -> Result<Request<Body>, Error>;
334+
) -> Result<Request<Full<Bytes>>, Error>;
331335

332336
/// Sign and build the request with the given JSON body.
333337
fn signed_json<T: Serialize>(
@@ -336,7 +340,7 @@ pub trait SignedRequestBuilderExt {
336340
key_id: &str,
337341
secret_key: &SigningKey,
338342
content: T,
339-
) -> Result<Request<Body>, Error>;
343+
) -> Result<Request<Full<Bytes>>, Error>;
340344

341345
/// Sign and build the request with optional JSON body.
342346
fn signed_json_opt<T: Serialize>(
@@ -345,7 +349,7 @@ pub trait SignedRequestBuilderExt {
345349
key_id: &str,
346350
secret_key: &SigningKey,
347351
content: Option<T>,
348-
) -> Result<Request<Body>, Error>;
352+
) -> Result<Request<Full<Bytes>>, Error>;
349353
}
350354

351355
impl SignedRequestBuilderExt for Builder {
@@ -354,7 +358,7 @@ impl SignedRequestBuilderExt for Builder {
354358
server_name: &str,
355359
key_id: &str,
356360
secret_key: &SigningKey,
357-
) -> Result<Request<Body>, Error> {
361+
) -> Result<Request<Full<Bytes>>, Error> {
358362
sign_and_build_json_request::<()>(server_name, key_id, secret_key, self, None)
359363
}
360364

@@ -364,7 +368,7 @@ impl SignedRequestBuilderExt for Builder {
364368
key_id: &str,
365369
secret_key: &SigningKey,
366370
content: T,
367-
) -> Result<Request<Body>, Error> {
371+
) -> Result<Request<Full<Bytes>>, Error> {
368372
sign_and_build_json_request(server_name, key_id, secret_key, self, Some(content))
369373
}
370374

@@ -374,7 +378,7 @@ impl SignedRequestBuilderExt for Builder {
374378
key_id: &str,
375379
secret_key: &SigningKey,
376380
content: Option<T>,
377-
) -> Result<Request<Body>, Error> {
381+
) -> Result<Request<Full<Bytes>>, Error> {
378382
if let Some(content) = content {
379383
self.signed_json(server_name, key_id, secret_key, content)
380384
} else {

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
//!
2929
//! # [`FederationClient`]
3030
//!
31-
//! The [`FederationClient`] is just a standard [`hyper::Client`] with a
32-
//! [`MatrixConnector`] that can route `matrix://` and `matrix-federation://` URIs, but
33-
//! does *not* sign the requests automatically:
31+
//! The [`FederationClient`] is just a standard [`hyper_util::client::legacy::Client`]
32+
//! with a [`MatrixConnector`] that can route `matrix://` and `matrix-federation://`
33+
//! URIs, but does *not* sign the requests automatically:
3434
//!
3535
//! ```no_run
3636
//! # use matrix_hyper_federation_client::FederationClient;

0 commit comments

Comments
 (0)