From a505119efea17022f4bd62b1d2191f005b7e29ce Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Sat, 22 Jun 2024 12:18:16 +1200 Subject: [PATCH] upgrade to hyper 1.x --- Cargo.lock | 108 +++++++++++++++++++++++------------------------ Cargo.toml | 6 ++- src/lib.rs | 44 ++++++++----------- src/web/mod.rs | 9 +++- src/web/nip11.rs | 6 ++- 5 files changed, 85 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc15d89..33c4925 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -247,8 +247,10 @@ dependencies = [ "dashmap", "env_logger", "futures", + "http-body-util", "hyper", "hyper-tungstenite", + "hyper-util", "lazy_static", "log", "parking_lot", @@ -672,13 +674,36 @@ dependencies = [ ] [[package]] -name = "http-body" -version = "0.4.6" +name = "http" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" dependencies = [ "bytes", - "http", + "fnv", + "itoa", +] + +[[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.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793429d76616a256bcb62c2a2ec2bed781c8307e797e2598c50010f2bee2544f" +dependencies = [ + "bytes", + "futures-util", + "http 1.1.0", + "http-body", "pin-project-lite", ] @@ -702,40 +727,53 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "0.14.29" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f361cde2f109281a220d4307746cdfd5ee3f410da58a70377762396775634b33" +checksum = "fe575dd17d0862a9a33781c8c4696a55c320909004a67a00fb286ba8b1bc496d" dependencies = [ "bytes", "futures-channel", - "futures-core", "futures-util", - "http", + "http 1.1.0", "http-body", "httparse", "httpdate", "itoa", "pin-project-lite", - "socket2", + "smallvec", "tokio", - "tower-service", - "tracing", - "want", ] [[package]] name = "hyper-tungstenite" -version = "0.11.1" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cc7dcb1ab67cd336f468a12491765672e61a3b6b148634dbfe2fe8acd3fe7d9" +checksum = "096e0568151fedc7a77619c28f56a86b1cbae56aed4dfe443efbd739229c2733" dependencies = [ + "http-body-util", "hyper", + "hyper-util", "pin-project-lite", "tokio", "tokio-tungstenite", "tungstenite", ] +[[package]] +name = "hyper-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b875924a60b96e5d7b9ae7b066540b1dd1cbd90d1828f54c92e02a283351c56" +dependencies = [ + "bytes", + "futures-util", + "http 1.1.0", + "http-body", + "hyper", + "pin-project-lite", + "tokio", +] + [[package]] name = "idna" version = "0.5.0" @@ -1665,37 +1703,6 @@ dependencies = [ "winnow", ] -[[package]] -name = "tower-service" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" - -[[package]] -name = "tracing" -version = "0.1.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" -dependencies = [ - "pin-project-lite", - "tracing-core", -] - -[[package]] -name = "tracing-core" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" -dependencies = [ - "once_cell", -] - -[[package]] -name = "try-lock" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" - [[package]] name = "tungstenite" version = "0.20.1" @@ -1705,7 +1712,7 @@ dependencies = [ "byteorder", "bytes", "data-encoding", - "http", + "http 0.2.12", "httparse", "log", "rand 0.8.5", @@ -1777,15 +1784,6 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "want" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" -dependencies = [ - "try-lock", -] - [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 5f90227..31289ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,8 +12,10 @@ base64 = "0.22" dashmap = "5.5" env_logger = "0.11" futures = "0.3" -hyper = { version = "0.14", features = [ "http1", "server", "runtime", "stream" ] } -hyper-tungstenite = "0.11" +http-body-util = "0.1" +hyper = { version = "1.0.0", features = [ "http1", "server" ] } +hyper-tungstenite = "0.12" +hyper-util = "0.1" lazy_static = "1.4" log = "0.4" pocket-types = { git = "https://github.com/mikedilger/pocket", branch = "master" } diff --git a/src/lib.rs b/src/lib.rs index cf5966d..e5c214f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,12 +15,16 @@ use crate::globals::GLOBALS; use crate::ip::{HashedIp, HashedPeer, IpData, SessionExit}; use crate::reply::NostrReply; use futures::{sink::SinkExt, stream::StreamExt}; +use http_body_util::Full; +use hyper::body::{Bytes, Incoming}; +use hyper::server::conn::http1; use hyper::service::Service; use hyper::upgrade::Upgraded; use hyper::StatusCode; -use hyper::{Body, Request, Response}; +use hyper::{Request, Response}; use hyper_tungstenite::tungstenite; use hyper_tungstenite::WebSocketStream; +use hyper_util::rt::TokioIo; use pocket_db::Store; use pocket_types::{Id, OwnedFilter, Pubkey}; use speedy::{Readable, Writable}; @@ -33,7 +37,6 @@ use std::net::IpAddr; use std::path::Path; use std::pin::Pin; use std::sync::atomic::Ordering; -use std::task::{Context, Poll}; use std::time::Duration; use textnonce::TextNonce; use tokio::io::{AsyncRead, AsyncWrite}; @@ -49,25 +52,16 @@ impl FullStream for TlsStream> {} /// Serve a single network connection pub async fn serve(stream: Box, peer: HashedPeer) { - // Serve the network stream with our http server and our HttpService - let service = HttpService { peer }; + // Serve the network stream with our http server and our ChorusService + let service = ChorusService { peer }; - let mut http_server = hyper::server::conn::Http::new(); - http_server.http1_only(true); - http_server.http1_keep_alive(true); - let connection = http_server - .serve_connection(stream, service) - .with_upgrades(); + let io = hyper_util::rt::TokioIo::new(stream); - /* hyper 1 let mut http1builder = http1::Builder::new(); http1builder.half_close(true); http1builder.keep_alive(true); http1builder.header_read_timeout(Duration::from_secs(5)); - let connection = http1builder - .serve_connection(stream, service) - .with_upgrades(); - */ + let connection = http1builder.serve_connection(io, service).with_upgrades(); // If our service exits with an error, log the error if let Err(he) = connection.await { @@ -87,22 +81,18 @@ pub async fn serve(stream: Box, peer: HashedPeer) { } // This is our per-connection HTTP service -struct HttpService { +struct ChorusService { peer: HashedPeer, } -impl Service> for HttpService { - type Response = Response; +impl Service> for ChorusService { + type Response = Response>; type Error = Error; type Future = Pin> + Send>>; - fn poll_ready(&mut self, _: &mut Context) -> Poll> { - Poll::Ready(Ok(())) - } - // This is called for each HTTP request made by the client // NOTE: it is not called for each websocket message once upgraded. - fn call(&mut self, req: Request) -> Self::Future { + fn call(&self, req: Request) -> Self::Future { let mut peer = self.peer; // If chorus is behind a proxy that sets an "X-Real-Ip" header, we use @@ -125,8 +115,8 @@ impl Service> for HttpService { async fn handle_http_request( peer: HashedPeer, - mut request: Request, -) -> Result, Error> { + mut request: Request, +) -> Result>, Error> { let ua = match request.headers().get("user-agent") { Some(ua) => ua.to_str().unwrap_or("NON-UTF8-HEADER").to_owned(), None => "(no user-agent)".to_owned(), @@ -142,7 +132,7 @@ async fn handle_http_request( if *cur.value() >= max_conn { return Ok(Response::builder() .status(StatusCode::TOO_MANY_REQUESTS) - .body(Body::empty())?); + .body(Full::new(Bytes::new()))?); } } @@ -279,7 +269,7 @@ struct WebSocketService { pub peer: HashedPeer, pub subscriptions: HashMap>, pub buffer: Vec, - pub websocket: WebSocketStream, + pub websocket: WebSocketStream>, pub challenge: String, pub user: Option, pub error_punishment: f32, diff --git a/src/web/mod.rs b/src/web/mod.rs index cd088b2..8d8c68e 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -2,9 +2,14 @@ mod nip11; use crate::error::Error; use crate::ip::HashedPeer; -use hyper::{Body, Request, Response, StatusCode}; +use http_body_util::Full; +use hyper::body::{Bytes, Incoming}; +use hyper::{Request, Response, StatusCode}; -pub async fn serve_http(peer: HashedPeer, request: Request) -> Result, Error> { +pub async fn serve_http( + peer: HashedPeer, + request: Request, +) -> Result>, Error> { // check for Accept header of application/nostr+json if let Some(accept) = request.headers().get("Accept") { if let Ok(s) = accept.to_str() { diff --git a/src/web/nip11.rs b/src/web/nip11.rs index 36d5135..58bc4a4 100644 --- a/src/web/nip11.rs +++ b/src/web/nip11.rs @@ -2,9 +2,11 @@ use crate::config::Config; use crate::error::Error; use crate::globals::GLOBALS; use crate::ip::HashedPeer; -use hyper::{Body, Response, StatusCode}; +use http_body_util::Full; +use hyper::body::Bytes; +use hyper::{Response, StatusCode}; -pub async fn serve_nip11(peer: HashedPeer) -> Result, Error> { +pub async fn serve_nip11(peer: HashedPeer) -> Result>, Error> { log::debug!(target: "Client", "{}: sent NIP-11", peer); let rid = { let config = &*GLOBALS.config.read();