From c343a62b08e03e357ed270041640b03f0fade974 Mon Sep 17 00:00:00 2001 From: "Antonio F. T" Date: Sun, 11 Feb 2024 19:58:22 +0100 Subject: [PATCH] Bump axum & allow for requests without an application/json header (#2) --- Cargo.lock | 153 +++++++++++++++++++++++++++++++--------- Cargo.toml | 5 +- src/gateway/endpoint.rs | 7 +- src/http.rs | 25 +++---- src/utils/axum_json.rs | 46 ++++++++++++ src/utils/mod.rs | 1 + 6 files changed, 187 insertions(+), 50 deletions(-) create mode 100644 src/utils/axum_json.rs diff --git a/Cargo.lock b/Cargo.lock index 402eee4..ae14c5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -119,18 +119,19 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "axum" -version = "0.6.20" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" +checksum = "1236b4b292f6c4d6dc34604bb5120d85c3fe1d1aa596bd5cc52ca054d13e7b9e" dependencies = [ "async-trait", "axum-core", - "bitflags 1.3.2", "bytes", "futures-util", - "http", - "http-body", - "hyper", + "http 1.0.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.1.0", + "hyper-util", "itoa", "matchit", "memchr", @@ -147,23 +148,28 @@ dependencies = [ "tower", "tower-layer", "tower-service", + "tracing", ] [[package]] name = "axum-core" -version = "0.3.4" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "759fa577a247914fd3f7f76d62972792636412fbfd634cd452f6a385a74d2d2c" +checksum = "a15c63fd72d41492dc4f497196f5da1fb04fb7529e631d73630d1b491e47a2e3" dependencies = [ "async-trait", "bytes", "futures-util", - "http", - "http-body", + "http 1.0.0", + "http-body 1.0.0", + "http-body-util", "mime", + "pin-project-lite", "rustversion", + "sync_wrapper", "tower-layer", "tower-service", + "tracing", ] [[package]] @@ -1070,7 +1076,7 @@ dependencies = [ "futures-timer", "futures-util", "hashers", - "http", + "http 0.2.11", "instant", "jsonwebtoken", "once_cell", @@ -1424,7 +1430,26 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 0.2.11", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "h2" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31d030e59af851932b72ceebadf4a2b5986dba4c3b99dd2493f8273a0f151943" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 1.0.0", "indexmap", "slab", "tokio", @@ -1506,6 +1531,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http-body" version = "0.4.5" @@ -1513,15 +1549,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" dependencies = [ "bytes", - "http", + "http 0.2.11", "pin-project-lite", ] [[package]] -name = "http-range-header" -version = "0.3.1" +name = "http-body" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" +dependencies = [ + "bytes", + "http 1.0.0", +] + +[[package]] +name = "http-body-util" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" +checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" +dependencies = [ + "bytes", + "futures-util", + "http 1.0.0", + "http-body 1.0.0", + "pin-project-lite", +] [[package]] name = "httparse" @@ -1545,9 +1598,9 @@ dependencies = [ "futures-channel", "futures-core", "futures-util", - "h2", - "http", - "http-body", + "h2 0.3.22", + "http 0.2.11", + "http-body 0.4.5", "httparse", "httpdate", "itoa", @@ -1559,6 +1612,25 @@ dependencies = [ "want", ] +[[package]] +name = "hyper" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5aa53871fc917b1a9ed87b683a5d86db645e23acb32c2e0785a353e522fb75" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "h2 0.4.2", + "http 1.0.0", + "http-body 1.0.0", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "tokio", +] + [[package]] name = "hyper-rustls" version = "0.24.2" @@ -1566,13 +1638,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", - "http", - "hyper", + "http 0.2.11", + "hyper 0.14.27", "rustls", "tokio", "tokio-rustls", ] +[[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.0.0", + "http-body 1.0.0", + "hyper 1.1.0", + "pin-project-lite", + "socket2 0.5.5", + "tokio", +] + [[package]] name = "iana-time-zone" version = "0.1.58" @@ -2015,6 +2103,7 @@ dependencies = [ "bech32 0.10.0-beta", "blake2", "bs58", + "bytes", "chrono", "ciborium", "crc16", @@ -2628,10 +2717,10 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "h2", - "http", - "http-body", - "hyper", + "h2 0.3.22", + "http 0.2.11", + "http-body 0.4.5", + "hyper 0.14.27", "hyper-rustls", "ipnet", "js-sys", @@ -3529,17 +3618,15 @@ dependencies = [ [[package]] name = "tower-http" -version = "0.4.4" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" +checksum = "0da193277a4e2c33e59e09b5861580c33dd0a637c3883d0fa74ba40c0374af2e" dependencies = [ "bitflags 2.4.1", "bytes", - "futures-core", - "futures-util", - "http", - "http-body", - "http-range-header", + "http 1.0.0", + "http-body 1.0.0", + "http-body-util", "pin-project-lite", "tower-layer", "tower-service", @@ -3644,7 +3731,7 @@ dependencies = [ "byteorder", "bytes", "data-encoding", - "http", + "http 0.2.11", "httparse", "log", "rand", diff --git a/Cargo.toml b/Cargo.toml index a62bb2b..aa86589 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.0.1" edition = "2021" [dependencies] -axum = "0.6.20" +axum = "0.7.4" chrono = "0.4.31" dotenvy = "0.15.7" ethers = "2.0.10" @@ -19,10 +19,11 @@ serde_json = "1.0.108" thiserror = "1.0.50" tokio = {version = "1", features = ["full"]} tokio-postgres = "0.7.10" -tower-http = { version = "0.4.4", features = ["cors"] } +tower-http = { version = "0.5.1", features = ["cors"] } tracing = "0.1.40" tracing-subscriber = { version = "0.3.18", features = ["env-filter"]} lazy_static = { version = "1.4.0", features = [] } +bytes = "1.5.0" # Multicoin encoding bs58 = "0.5.0" diff --git a/src/gateway/endpoint.rs b/src/gateway/endpoint.rs index 88f69c6..caad6c9 100644 --- a/src/gateway/endpoint.rs +++ b/src/gateway/endpoint.rs @@ -3,19 +3,20 @@ use std::sync::Arc; use axum::{ extract::State, response::{IntoResponse, Response}, - Json, }; use thiserror::Error; use crate::state::GlobalState; +use crate::utils; use super::{payload::ResolveCCIPPostPayload, response::GatewayResponse}; pub async fn route( - // Ommiting sender from path awaiting viem patch + // Omitting sender from path awaiting viem patch // Path(sender): Path, State(state): State>, - Json(request_payload): Json, + // custom less strict json implementation because viem makes the request wrong + utils::axum_json::Json(request_payload): utils::axum_json::Json, ) -> impl IntoResponse { handle(request_payload, state) .await diff --git a/src/http.rs b/src/http.rs index 6e7111b..fcc7198 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1,9 +1,10 @@ -use std::{env, net::SocketAddr, sync::Arc}; +use std::{env, sync::Arc}; use axum::{ routing::{get, post}, - Router, Server, + Router, }; +use tokio::net::TcpListener; use tower_http::cors::CorsLayer; use tracing::{debug, info}; @@ -25,19 +26,19 @@ pub async fn serve(state: GlobalState) { .with_state(Arc::new(state)) .layer(CorsLayer::very_permissive()); - let addr = SocketAddr::from(( - [0, 0, 0, 0], + let listener = TcpListener::bind(format!( + "0.0.0.0:{}", env::var("PORT") .unwrap_or("3000".to_string()) .parse::() - .expect("port should fit in u16"), - )); - debug!("Listening on {}", addr); - - Server::bind(&addr) - .serve(app.into_make_service()) - .await - .unwrap(); + .expect("port should fit in u16") + )) + .await + .unwrap(); + + debug!("Listening on {}", listener.local_addr().unwrap()); + + axum::serve(listener, app).await.unwrap(); } /// Self Endpoint on the Gateway diff --git a/src/utils/axum_json.rs b/src/utils/axum_json.rs new file mode 100644 index 0000000..76ed101 --- /dev/null +++ b/src/utils/axum_json.rs @@ -0,0 +1,46 @@ +use axum::async_trait; +use axum::extract::rejection::JsonRejection; +use axum::extract::{FromRequest, Request}; +use axum::response::{IntoResponse, Response}; +use serde::de::DeserializeOwned; +use serde::Serialize; + +pub struct Json(pub T); + +#[async_trait] +impl FromRequest for Json +where + T: DeserializeOwned, + S: Send + Sync, +{ + type Rejection = JsonRejection; + + async fn from_request(req: Request, state: &S) -> Result { + let bytes = bytes::Bytes::from_request(req, state).await?; + Self::from_bytes(&bytes) + } +} + +impl Json +where + T: DeserializeOwned, +{ + pub fn from_bytes(bytes: &[u8]) -> Result { + axum::extract::Json::from_bytes(bytes).map(|it| Json(it.0)) + } +} + +impl IntoResponse for Json +where + T: Serialize, +{ + fn into_response(self) -> Response { + axum::extract::Json::into_response(axum::extract::Json(self.0)) + } +} + +impl From for Json { + fn from(inner: T) -> Self { + Self(inner) + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 4dbc4a3..d22fea7 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,2 +1,3 @@ +pub mod axum_json; pub mod dns; pub mod sha256;