expression
Hyper HTTP/1 over an in-memory duplex stream
Hyper can run an HTTP/1 connection over a compatible byte transport. The handshake returns a request sender and a connection future that must be driven. This test connects two in-memory endpoints, sends a single request and bounds the entire scenario. It does not exercise TCP or TLS. In-memory fixture; no external connection. Expected stdout (not executed): 200 hello Common mistake: Awaiting a response without polling the connection future.
Formal Syntax & Structure
rust syntaxrust
http1::handshake(TokioIo::new(io)).awaitWorking Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { use hyper::{Request, Response, body::Bytes, service::service_fn}; use http_body_util::{Empty, Full, BodyExt}; use hyper_util::rt::TokioIo; use std::{convert::Infallible, time::Duration}; tokio::time::timeout(Duration::from_secs(3), async { let (client_io, server_io) = tokio::io::duplex(4096); let server = tokio::spawn(async move { hyper::server::conn::http1::Builder::new().keep_alive(false) .serve_connection(TokioIo::new(server_io), service_fn(|_: Request<hyper::body::Incoming>| async { Ok::<_, Infallible>(Response::new(Full::new(Bytes::from_static(b"hello")))) })).await.unwrap(); }); let (mut sender, conn) = hyper::client::conn::http1::handshake::<_, Empty<Bytes>>(TokioIo::new(client_io)).await.unwrap(); let driver = tokio::spawn(conn); let req = Request::builder().uri("/hello").header("host", "local.test").body(Empty::new()).unwrap(); let resp = sender.send_request(req).await.unwrap(); let status = resp.status().as_u16(); let body = resp.into_body().collect().await.unwrap().to_bytes(); println!("{status} {}", std::str::from_utf8(&body).unwrap()); drop(sender); driver.await.unwrap().unwrap(); server.await.unwrap(); }).await.unwrap();}