Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit a7a9369

Browse files
committed
Upgrade most HTTP/Hyper crates and make mas-listener work
1 parent ba58195 commit a7a9369

File tree

10 files changed

+233
-131
lines changed

10 files changed

+233
-131
lines changed

Cargo.lock

Lines changed: 147 additions & 73 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ version = "0.1.80"
6868
[workspace.dependencies.anyhow]
6969
version = "1.0.86"
7070

71+
# HTTP router
72+
[workspace.dependencies.axum]
73+
version = "0.7.5"
74+
75+
# Extra utilities for Axum
76+
[workspace.dependencies.axum-extra]
77+
version = "0.9.3"
78+
features = ["cookie-private", "cookie-key-expansion", "typed-header"]
79+
7180
# UTF-8 paths
7281
[workspace.dependencies.camino]
7382
version = "1.1.7"
@@ -94,20 +103,25 @@ version = "0.3.9"
94103

95104
# HTTP request/response
96105
[workspace.dependencies.http]
97-
version = "0.2.12"
106+
version = "1.1.0"
98107

99108
# HTTP body trait
100109
[workspace.dependencies.http-body]
101-
version = "0.4.6"
110+
version = "1.0.0"
102111

103-
# HTTP client
112+
# HTTP client and server
104113
[workspace.dependencies.hyper]
105-
version = "0.14.29"
106-
features = ["client", "http1", "http2", "stream", "runtime"]
114+
version = "1.4.0"
115+
features = ["client", "http1", "http2"]
116+
117+
# Additional Hyper utilties
118+
[workspace.dependencies.hyper-util]
119+
version = "0.1.6"
120+
features = ["server", "server-auto", "service", "http1", "http2", "tokio"]
107121

108122
# Hyper Rustls support
109123
[workspace.dependencies.hyper-rustls]
110-
version = "0.25.0"
124+
version = "0.27.2"
111125
features = ["http1", "http2"]
112126
default-features = false
113127

crates/axum-utils/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ workspace = true
1313

1414
[dependencies]
1515
async-trait.workspace = true
16-
axum = { version = "0.6.20", features = ["headers"] }
17-
axum-extra = { version = "0.8.0", features = ["cookie-private", "cookie-key-expansion"] }
16+
axum.workspace = true
17+
axum-extra.workspace = true
1818
chrono.workspace = true
1919
data-encoding = "2.6.0"
2020
futures-util = "0.3.30"

crates/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ workspace = true
1313

1414
[dependencies]
1515
anyhow.workspace = true
16-
axum = "0.6.20"
16+
axum.workspace = true
1717
camino.workspace = true
1818
clap.workspace = true
1919
console = "0.15.8"

crates/handlers/Cargo.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ sentry = { version = "0.31.8", default-features = false }
3131
hyper.workspace = true
3232
tower.workspace = true
3333
tower-http = { version = "0.4.4", features = ["cors"] }
34-
axum = "0.6.20"
35-
axum-macros = "0.3.8"
36-
axum-extra = { version = "0.8.0", features = ["cookie-private"] }
34+
axum.workspace = true
35+
axum-macros = "0.4.1"
36+
axum-extra.workspace = true
3737

3838
async-graphql.workspace = true
3939

@@ -52,7 +52,12 @@ serde_urlencoded = "0.7.1"
5252
# Password hashing
5353
argon2 = { version = "0.5.3", features = ["password-hash", "std"] }
5454
bcrypt = "0.15.1"
55-
pbkdf2 = { version = "0.12.2", features = ["password-hash", "std", "simple", "parallel"] }
55+
pbkdf2 = { version = "0.12.2", features = [
56+
"password-hash",
57+
"std",
58+
"simple",
59+
"parallel",
60+
] }
5661
zeroize = "1.8.1"
5762

5863
# Various data types and utilities

crates/http/Cargo.toml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ tokio.workspace = true
4040

4141
[features]
4242
client = [
43-
"dep:mas-tower",
44-
"dep:rustls",
45-
"hyper/tcp",
46-
"dep:hyper-rustls",
47-
"dep:rustls-platform-verifier",
48-
"tower/limit",
49-
"tower-http/timeout",
50-
"tower-http/follow-redirect",
51-
"tower-http/set-header",
43+
"dep:mas-tower",
44+
"dep:rustls",
45+
"dep:hyper-rustls",
46+
"dep:rustls-platform-verifier",
47+
"tower/limit",
48+
"tower-http/timeout",
49+
"tower-http/follow-redirect",
50+
"tower-http/set-header",
5251
]

crates/listener/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ event-listener = "5.3.1"
1717
futures-util = "0.3.30"
1818
http-body.workspace = true
1919
hyper = { workspace = true, features = ["server"] }
20+
hyper-util.workspace = true
2021
libc = "0.2.155"
2122
pin-project-lite = "0.2.14"
2223
socket2 = "0.5.7"
2324
thiserror.workspace = true
2425
tokio.workspace = true
2526
tokio-rustls = "0.25.0"
26-
tower-http = { version = "0.4.4", features = ["add-extension"] }
27-
tower-service = "0.3.2"
27+
tower.workspace = true
28+
tower-http = { version = "0.5.2", features = ["add-extension"] }
2829
tracing.workspace = true
2930

3031
[dev-dependencies]

crates/listener/examples/demo/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ use std::{
2121
};
2222

2323
use anyhow::Context;
24-
use hyper::{service::service_fn, Request, Response};
24+
use hyper::{Request, Response};
2525
use mas_listener::{server::Server, shutdown::ShutdownStream, ConnectionInfo};
2626
use tokio::signal::unix::SignalKind;
2727
use tokio_rustls::rustls::{server::WebPkiClientVerifier, RootCertStore, ServerConfig};
28+
use tower::service_fn;
2829

2930
static CA_CERT_PEM: &[u8] = include_bytes!("./certs/ca.pem");
3031
static SERVER_CERT_PEM: &[u8] = include_bytes!("./certs/server.pem");
3132
static SERVER_KEY_PEM: &[u8] = include_bytes!("./certs/server-key.pem");
3233

33-
async fn handler(req: Request<hyper::Body>) -> Result<Response<String>, Infallible> {
34+
async fn handler(req: Request<hyper::body::Incoming>) -> Result<Response<String>, Infallible> {
3435
tracing::info!("Handling request");
3536
tokio::time::sleep(Duration::from_secs(3)).await;
3637
let info = req.extensions().get::<ConnectionInfo>().unwrap();

crates/listener/src/server.rs

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ use std::{
2222

2323
use event_listener::{Event, EventListener};
2424
use futures_util::{stream::SelectAll, Stream, StreamExt};
25-
use http_body::Body;
26-
use hyper::{body::HttpBody, server::conn::Connection, Request, Response};
25+
use hyper::{Request, Response};
26+
use hyper_util::{
27+
rt::{TokioExecutor, TokioIo},
28+
server::conn::auto::Connection,
29+
service::TowerToHyperService,
30+
};
2731
use pin_project_lite::pin_project;
2832
use thiserror::Error;
29-
use tokio::io::{AsyncRead, AsyncWrite};
3033
use tokio_rustls::rustls::ServerConfig;
34+
use tower::Service;
3135
use tower_http::add_extension::AddExtension;
32-
use tower_service::Service;
3336
use tracing::Instrument;
3437

3538
use crate::{
@@ -91,10 +94,10 @@ impl<S> Server<S> {
9194
/// Run a single server
9295
pub async fn run<B, SD>(self, shutdown: SD)
9396
where
94-
S: Service<Request<hyper::Body>, Response = Response<B>> + Clone + Send + 'static,
97+
S: Service<Request<hyper::body::Incoming>, Response = Response<B>> + Clone + Send + 'static,
9598
S::Future: Send + 'static,
9699
S::Error: std::error::Error + Send + Sync + 'static,
97-
B: Body + Send + 'static,
100+
B: http_body::Body + Send + 'static,
98101
B::Data: Send,
99102
B::Error: std::error::Error + Send + Sync + 'static,
100103
SD: Stream + Unpin,
@@ -173,15 +176,20 @@ async fn accept<S, B>(
173176
stream: UnixOrTcpConnection,
174177
service: S,
175178
) -> Result<
176-
Connection<MaybeTlsStream<Rewind<UnixOrTcpConnection>>, AddExtension<S, ConnectionInfo>>,
179+
Connection<
180+
'static,
181+
TokioIo<MaybeTlsStream<Rewind<UnixOrTcpConnection>>>,
182+
TowerToHyperService<AddExtension<S, ConnectionInfo>>,
183+
TokioExecutor,
184+
>,
177185
AcceptError,
178186
>
179187
where
180-
S: Service<Request<hyper::Body>, Response = Response<B>>,
188+
S: Service<Request<hyper::body::Incoming>, Response = Response<B>> + Send + Clone + 'static,
181189
S::Error: std::error::Error + Send + Sync + 'static,
182190
S::Future: Send + 'static,
183-
B: HttpBody + Send + 'static,
184-
B::Data: Send + 'static,
191+
B: http_body::Body + Send + 'static,
192+
B::Data: Send,
185193
B::Error: std::error::Error + Send + Sync + 'static,
186194
{
187195
let span = tracing::Span::current();
@@ -219,18 +227,17 @@ where
219227
net_peer_addr: peer_addr.into_net(),
220228
};
221229

222-
let service = AddExtension::new(service, info);
223-
224-
let conn = if is_h2 {
225-
hyper::server::conn::Http::new()
226-
.http2_only(true)
227-
.serve_connection(stream, service)
228-
} else {
229-
hyper::server::conn::Http::new()
230-
.http1_only(true)
231-
.http1_keep_alive(true)
232-
.serve_connection(stream, service)
233-
};
230+
let mut builder = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new());
231+
if is_h2 {
232+
builder = builder.http2_only();
233+
}
234+
builder.http1().keep_alive(true);
235+
236+
let service = TowerToHyperService::new(AddExtension::new(service, info));
237+
238+
let conn = builder
239+
.serve_connection(TokioIo::new(stream), service)
240+
.into_owned();
234241

235242
Ok(conn)
236243
})
@@ -270,18 +277,19 @@ impl<C> AbortableConnection<C> {
270277
}
271278
}
272279

273-
impl<T, S, B> Future for AbortableConnection<Connection<T, S>>
280+
impl<T, S, B> Future
281+
for AbortableConnection<Connection<'static, T, TowerToHyperService<S>, TokioExecutor>>
274282
where
275-
Connection<T, S>: Future,
276-
S: Service<Request<hyper::Body>, Response = Response<B>> + Send + 'static,
283+
Connection<'static, T, TowerToHyperService<S>, TokioExecutor>: Future,
284+
S: Service<Request<hyper::body::Incoming>, Response = Response<B>> + Send + Clone + 'static,
277285
S::Future: Send + 'static,
278286
S::Error: std::error::Error + Send + Sync,
279-
B: HttpBody + Send + 'static,
287+
T: hyper::rt::Read + hyper::rt::Write + Unpin,
288+
B: http_body::Body + Send + 'static,
280289
B::Data: Send,
281-
B::Error: std::error::Error + Send + Sync,
282-
T: AsyncRead + AsyncWrite + Unpin,
290+
B::Error: std::error::Error + Send + Sync + 'static,
283291
{
284-
type Output = <Connection<T, S> as Future>::Output;
292+
type Output = <Connection<'static, T, TowerToHyperService<S>, TokioExecutor> as Future>::Output;
285293

286294
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
287295
let mut this = self.project();
@@ -308,10 +316,10 @@ where
308316
#[allow(clippy::too_many_lines)]
309317
pub async fn run_servers<S, B, SD>(listeners: impl IntoIterator<Item = Server<S>>, mut shutdown: SD)
310318
where
311-
S: Service<Request<hyper::Body>, Response = Response<B>> + Clone + Send + 'static,
319+
S: Service<Request<hyper::body::Incoming>, Response = Response<B>> + Clone + Send + 'static,
312320
S::Future: Send + 'static,
313321
S::Error: std::error::Error + Send + Sync + 'static,
314-
B: Body + Send + 'static,
322+
B: http_body::Body + Send + 'static,
315323
B::Data: Send,
316324
B::Error: std::error::Error + Send + Sync + 'static,
317325
SD: Stream + Unpin,

crates/router/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ publish = false
1212
workspace = true
1313

1414
[dependencies]
15-
axum = { version = "0.6.20", default-features = false }
15+
axum.workspace = true
1616
serde.workspace = true
1717
serde_urlencoded = "0.7.1"
1818
url.workspace = true

0 commit comments

Comments
 (0)