Merge pull request #3357 from zecakeh/use-axum

sdk: Replace hyper with axum for local servers
This commit is contained in:
Ivan Enderlin
2024-04-29 09:35:33 +02:00
committed by GitHub
5 changed files with 251 additions and 92 deletions

250
Cargo.lock generated
View File

@@ -426,13 +426,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf"
dependencies = [
"async-trait",
"axum-core",
"axum-core 0.3.4",
"bitflags 1.3.2",
"bytes",
"futures-util",
"http",
"http-body",
"hyper",
"http 0.2.11",
"http-body 0.4.6",
"hyper 0.14.28",
"itoa",
"matchit",
"memchr",
@@ -447,6 +447,40 @@ dependencies = [
"tower-service",
]
[[package]]
name = "axum"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1236b4b292f6c4d6dc34604bb5120d85c3fe1d1aa596bd5cc52ca054d13e7b9e"
dependencies = [
"async-trait",
"axum-core 0.4.3",
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.0",
"http-body-util",
"hyper 1.3.1",
"hyper-util",
"itoa",
"matchit",
"memchr",
"mime",
"percent-encoding",
"pin-project-lite",
"rustversion",
"serde",
"serde_json",
"serde_path_to_error",
"serde_urlencoded",
"sync_wrapper",
"tokio",
"tower",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "axum-core"
version = "0.3.4"
@@ -456,14 +490,35 @@ dependencies = [
"async-trait",
"bytes",
"futures-util",
"http",
"http-body",
"http 0.2.11",
"http-body 0.4.6",
"mime",
"rustversion",
"tower-layer",
"tower-service",
]
[[package]]
name = "axum-core"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3"
dependencies = [
"async-trait",
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.0",
"http-body-util",
"mime",
"pin-project-lite",
"rustversion",
"sync_wrapper",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "backoff"
version = "0.4.0"
@@ -1667,10 +1722,10 @@ name = "example-oidc-cli"
version = "0.1.0"
dependencies = [
"anyhow",
"axum 0.7.4",
"dirs",
"futures-util",
"http",
"hyper",
"http 1.1.0",
"matrix-sdk",
"matrix-sdk-ui",
"rand 0.8.5",
@@ -2187,7 +2242,26 @@ dependencies = [
"futures-core",
"futures-sink",
"futures-util",
"http",
"http 0.2.11",
"indexmap 2.2.2",
"slab",
"tokio",
"tokio-util",
"tracing",
]
[[package]]
name = "h2"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "816ec7294445779408f36fe57bc5b7fc1cf59664059096c65f905c1c61f58069"
dependencies = [
"bytes",
"fnv",
"futures-core",
"futures-sink",
"futures-util",
"http 1.1.0",
"indexmap 2.2.2",
"slab",
"tokio",
@@ -2239,7 +2313,7 @@ dependencies = [
"base64 0.21.7",
"bytes",
"headers-core",
"http",
"http 0.2.11",
"httpdate",
"mime",
"sha1",
@@ -2251,7 +2325,7 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429"
dependencies = [
"http",
"http 0.2.11",
]
[[package]]
@@ -2321,6 +2395,17 @@ dependencies = [
"itoa",
]
[[package]]
name = "http"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258"
dependencies = [
"bytes",
"fnv",
"itoa",
]
[[package]]
name = "http-body"
version = "0.4.6"
@@ -2328,7 +2413,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2"
dependencies = [
"bytes",
"http",
"http 0.2.11",
"pin-project-lite",
]
[[package]]
name = "http-body"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643"
dependencies = [
"bytes",
"http 1.1.0",
]
[[package]]
name = "http-body-util"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0475f8b2ac86659c21b64320d5d653f9efe42acd2a4e560073ec61a155a34f1d"
dependencies = [
"bytes",
"futures-core",
"http 1.1.0",
"http-body 1.0.0",
"pin-project-lite",
]
@@ -2348,7 +2456,7 @@ dependencies = [
"async-channel 1.9.0",
"base64 0.13.1",
"futures-lite",
"http",
"http 0.2.11",
"infer",
"pin-project-lite",
"rand 0.7.3",
@@ -2381,9 +2489,9 @@ dependencies = [
"futures-channel",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"h2 0.3.26",
"http 0.2.11",
"http-body 0.4.6",
"httparse",
"httpdate",
"itoa",
@@ -2395,6 +2503,26 @@ dependencies = [
"want",
]
[[package]]
name = "hyper"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d"
dependencies = [
"bytes",
"futures-channel",
"futures-util",
"h2 0.4.4",
"http 1.1.0",
"http-body 1.0.0",
"httparse",
"httpdate",
"itoa",
"pin-project-lite",
"smallvec",
"tokio",
]
[[package]]
name = "hyper-rustls"
version = "0.24.2"
@@ -2402,8 +2530,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590"
dependencies = [
"futures-util",
"http",
"hyper",
"http 0.2.11",
"hyper 0.14.28",
"rustls",
"tokio",
"tokio-rustls",
@@ -2415,7 +2543,7 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1"
dependencies = [
"hyper",
"hyper 0.14.28",
"pin-project-lite",
"tokio",
"tokio-io-timeout",
@@ -2428,12 +2556,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [
"bytes",
"hyper",
"hyper 0.14.28",
"native-tls",
"tokio",
"tokio-native-tls",
]
[[package]]
name = "hyper-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa"
dependencies = [
"bytes",
"futures-util",
"http 1.1.0",
"http-body 1.0.0",
"hyper 1.3.1",
"pin-project-lite",
"socket2",
"tokio",
]
[[package]]
name = "iana-time-zone"
version = "0.1.60"
@@ -2996,9 +3140,9 @@ dependencies = [
"bytes",
"futures-util",
"headers",
"http",
"http-body",
"hyper",
"http 0.2.11",
"http-body 0.4.6",
"hyper 0.14.28",
"opentelemetry",
"serde",
"serde_json",
@@ -3060,7 +3204,7 @@ dependencies = [
"form_urlencoded",
"futures-util",
"headers",
"http",
"http 0.2.11",
"language-tags",
"mas-http",
"mas-iana",
@@ -3130,6 +3274,7 @@ dependencies = [
"async-channel 2.1.1",
"async-stream",
"async-trait",
"axum 0.7.4",
"backoff",
"bytes",
"bytesize",
@@ -3145,8 +3290,7 @@ dependencies = [
"futures-executor",
"futures-util",
"gloo-timers",
"http",
"hyper",
"http 0.2.11",
"image",
"imbl",
"indexmap 2.2.2",
@@ -3201,7 +3345,7 @@ dependencies = [
"eyeball-im",
"futures-executor",
"futures-util",
"http",
"http 0.2.11",
"matrix-sdk-common",
"matrix-sdk-crypto",
"matrix-sdk-store-encryption",
@@ -3266,7 +3410,7 @@ dependencies = [
"futures-util",
"hkdf",
"hmac",
"http",
"http 0.2.11",
"indoc",
"itertools 0.12.1",
"js_option",
@@ -3303,7 +3447,7 @@ dependencies = [
"assert_matches2",
"futures-util",
"hmac",
"http",
"http 0.2.11",
"js_int",
"matrix-sdk-common",
"matrix-sdk-crypto",
@@ -3410,7 +3554,7 @@ dependencies = [
"futures",
"futures-core",
"futures-util",
"http",
"http 0.2.11",
"json-structural-diff",
"matrix-sdk",
"matrix-sdk-test",
@@ -3491,7 +3635,7 @@ version = "0.7.0"
dependencies = [
"ctor",
"getrandom 0.2.12",
"http",
"http 0.2.11",
"matrix-sdk-test-macros",
"once_cell",
"ruma",
@@ -3844,7 +3988,7 @@ source = "git+https://github.com/matrix-org/matrix-authentication-service?rev=09
dependencies = [
"chrono",
"data-encoding",
"http",
"http 0.2.11",
"language-tags",
"mas-iana",
"mas-jose",
@@ -3980,7 +4124,7 @@ checksum = "7cbfa5308166ca861434f0b0913569579b8e587430a3d6bcd7fd671921ec145a"
dependencies = [
"async-trait",
"bytes",
"http",
"http 0.2.11",
"opentelemetry",
"reqwest",
]
@@ -3993,7 +4137,7 @@ checksum = "1a016b8d9495c639af2145ac22387dcb88e44118e45320d9238fbf4e7889abcb"
dependencies = [
"async-trait",
"futures-core",
"http",
"http 0.2.11",
"opentelemetry",
"opentelemetry-http",
"opentelemetry-proto",
@@ -4875,10 +5019,10 @@ dependencies = [
"encoding_rs",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"hyper",
"h2 0.3.26",
"http 0.2.11",
"http-body 0.4.6",
"hyper 0.14.28",
"hyper-rustls",
"hyper-tls",
"ipnet",
@@ -5037,7 +5181,7 @@ dependencies = [
"assign",
"bytes",
"date_header",
"http",
"http 0.2.11",
"js_int",
"js_option",
"maplit",
@@ -5060,7 +5204,7 @@ dependencies = [
"bytes",
"form_urlencoded",
"getrandom 0.2.12",
"http",
"http 0.2.11",
"indexmap 2.2.2",
"js-sys",
"js_int",
@@ -5469,6 +5613,16 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_path_to_error"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af99884400da37c88f5e9146b7f1fd0fbcae8f6eec4e9da38b67d05486f814a6"
dependencies = [
"itoa",
"serde",
]
[[package]]
name = "serde_qs"
version = "0.8.5"
@@ -6160,13 +6314,13 @@ checksum = "76c4eb7a4e9ef9d4763600161f12f5070b92a578e1b634db88a6887844c91a13"
dependencies = [
"async-stream",
"async-trait",
"axum",
"axum 0.6.20",
"base64 0.21.7",
"bytes",
"h2",
"http",
"http-body",
"hyper",
"h2 0.3.26",
"http 0.2.11",
"http-body 0.4.6",
"hyper 0.14.28",
"hyper-timeout",
"percent-encoding",
"pin-project",
@@ -6209,8 +6363,8 @@ dependencies = [
"bytes",
"futures-core",
"futures-util",
"http",
"http-body",
"http 0.2.11",
"http-body 0.4.6",
"http-range-header",
"pin-project-lite",
"tower-layer",
@@ -7084,7 +7238,7 @@ dependencies = [
"futures",
"futures-timer",
"http-types",
"hyper",
"hyper 0.14.28",
"log",
"once_cell",
"regex",

View File

@@ -37,7 +37,7 @@ markdown = ["ruma/markdown"]
native-tls = ["reqwest/native-tls"]
rustls-tls = ["reqwest/rustls-tls"]
socks = ["reqwest/socks"]
sso-login = ["dep:hyper", "dep:rand", "dep:tower"]
sso-login = ["dep:axum", "dep:rand", "dep:tower"]
image-proc = ["dep:image"]
image-rayon = ["image-proc", "image?/rayon"]
@@ -70,6 +70,7 @@ as_variant = { workspace = true }
async-channel = "2.1.0"
async-stream = { workspace = true }
async-trait = { workspace = true }
axum = { version = "0.7.4", optional = true }
bytes = "1.1.0"
bytesize = "1.1"
cfg-vis = "0.3.0"
@@ -82,7 +83,6 @@ eyre = { version = "0.6.8", optional = true }
futures-core = { workspace = true }
futures-util = { workspace = true }
http = { workspace = true }
hyper = { version = "0.14.20", features = ["http1", "http2", "server"], optional = true }
imbl = { workspace = true, features = ["serde"] }
indexmap = "2.0.2"
js_int = "0.2.2"

View File

@@ -324,11 +324,15 @@ where
sync::{Arc, Mutex},
};
use http::{Method, StatusCode};
use hyper::{server::conn::AddrIncoming, service::service_fn};
use axum::{
http::{self, Method, StatusCode},
response::IntoResponse,
routing::any_service,
};
use rand::{thread_rng, Rng};
use serde::Deserialize;
use tokio::{net::TcpListener, sync::oneshot};
use tower::service_fn;
use tracing::debug;
use url::Url;
@@ -381,7 +385,7 @@ where
data_tx.send(query.login_token).unwrap();
}
Ok(http::Response::new(response.clone()))
Ok(response.clone())
};
let listener = {
@@ -411,24 +415,21 @@ where
}
};
let incoming = AddrIncoming::from_listener(listener).unwrap();
let server = hyper::Server::builder(incoming)
.serve(tower::make::Shared::new(service_fn(move |request| {
let handle_request = handle_request.clone();
async move {
match handle_request(request) {
Ok(res) => Ok::<_, Infallible>(res.map(hyper::Body::from)),
Err(status_code) => {
let mut res = http::Response::new(hyper::Body::default());
*res.status_mut() = status_code;
Ok(res)
}
}
let router = any_service(service_fn(move |request| {
let handle_request = handle_request.clone();
async move {
match handle_request(request) {
Ok(res) => Ok::<_, Infallible>(res.into_response()),
Err(status_code) => Ok(status_code.into_response()),
}
})))
}
}));
let server = axum::serve(listener, router)
.with_graceful_shutdown(async {
signal_rx.await.ok();
});
})
.into_future();
tokio::spawn(server);

View File

@@ -10,10 +10,10 @@ test = false
[dependencies]
anyhow = "1"
axum = "0.7.4"
dirs = "5.0.1"
futures-util = { version = "0.3.21", default-features = false }
http = { workspace = true }
hyper = { version = "0.14.20", features = ["http1", "http2", "server"] }
http = "1.1.0"
matrix-sdk-ui = { path = "../../crates/matrix-sdk-ui" }
rand = { workspace = true }
serde = { workspace = true }

View File

@@ -13,6 +13,8 @@
// limitations under the License.
use std::{
convert::Infallible,
future::IntoFuture,
io::{self, Write},
ops::Range,
path::{Path, PathBuf},
@@ -21,9 +23,9 @@ use std::{
};
use anyhow::{anyhow, bail};
use axum::{response::IntoResponse, routing::any_service};
use futures_util::StreamExt;
use http::{Method, StatusCode};
use hyper::{server::conn::AddrIncoming, service::service_fn, Body, Server};
use matrix_sdk::{
config::SyncSettings,
oidc::{
@@ -49,7 +51,7 @@ use matrix_sdk_ui::sync_service::SyncService;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serde::{Deserialize, Serialize};
use tokio::{fs, io::AsyncBufReadExt as _, net::TcpListener, sync::oneshot};
use tower::make::Shared;
use tower::service_fn;
use url::Url;
/// A command-line tool to demonstrate the steps requiring an interaction with
@@ -875,29 +877,31 @@ async fn spawn_local_server(
};
// Set up the server.
let incoming = AddrIncoming::from_listener(listener)?;
let server = Server::builder(incoming)
.serve(Shared::new(service_fn(move |request| {
let data_tx_mutex = data_tx_mutex.clone();
async move {
// Reject methods others than HEAD or GET.
if request.method() != Method::HEAD && request.method() != Method::GET {
return http::Response::builder().status(StatusCode::METHOD_NOT_ALLOWED).body(Body::default());
}
let router = any_service(service_fn(move |request: http::Request<_>| {
let data_tx_mutex = data_tx_mutex.clone();
async move {
// Reject methods others than HEAD or GET.
if request.method() != Method::HEAD && request.method() != Method::GET {
return Ok::<_, Infallible>(StatusCode::METHOD_NOT_ALLOWED.into_response());
}
// We only need to get the first response so we consume the transmitter the first time.
if let Some(data_tx) = data_tx_mutex.lock().unwrap().take() {
let query_string = request.uri().query().unwrap_or_default();
// We only need to get the first response so we consume the transmitter the
// first time.
if let Some(data_tx) = data_tx_mutex.lock().unwrap().take() {
let query_string = request.uri().query().unwrap_or_default();
data_tx.send(query_string.to_owned()).expect("The receiver is still alive");
}
data_tx.send(query_string.to_owned()).expect("The receiver is still alive");
}
Ok(http::Response::new(Body::from("The authorization step is complete. You can close this page and go back to the oidc-cli.")))
}
})))
.with_graceful_shutdown(async {
signal_rx.await.ok();
});
Ok("The authorization step is complete. You can close this page and go back to the oidc-cli.".into_response())
}
}));
let server = axum::serve(listener, router)
.with_graceful_shutdown(async {
signal_rx.await.ok();
})
.into_future();
tokio::spawn(server);