std.http¶
Real HTTP/1.1 client + server via hyper
1.x and hyper-util.
Supersedes the slice-7 minimal in-memory server (mty-runtime::http),
which is still re-exported for backwards compatibility with existing
runtime tests.
Client¶
In Rust:
pub async fn get(url: &str) -> Result<Response, HttpErr>;
pub async fn post(url: &str, body: Vec<u8>) -> Result<Response, HttpErr>;
Response carries status: u16, body: Vec<u8>, and the response
headers: Vec<(String, String)>. Response::body_str() is a
convenience for UTF-8 bodies.
Server¶
In Rust:
Handler is an Arc<dyn Fn(Request) -> impl Future<Output = Response>>.
The accept loop runs on a dedicated tokio task; the returned JoinHandle
can be abort()ed to shut down. Binding 127.0.0.1:0 lets the OS
pick a port — the returned SocketAddr carries the actual port.
Example: echo server¶
use sdust_stdlib::http::{serve, Handler, Request, Response};
use std::sync::Arc;
let h: Handler = Arc::new(|req: Request| Box::pin(async move {
Response {
status: 200,
body: req.body,
headers: vec![("content-type".into(), "text/plain".into())],
}
}));
let (addr, _task) = serve("127.0.0.1:0", h).await?;
println!("listening on {addr}");
Known limits (v0.2)¶
- HTTPS client: only
http://URLs are accepted;https://errors withHttpErr::Url. v0.3 will wirehyper-rustlsviastd.tls. - HTTP/2 server:
hypersupports HTTP/2, but the v0.2 server builder useshttp1::Builderonly. HTTP/2 + ALPN is a v0.3 task.
See also¶
std.tlsfor the TLS primitives.- STDLIB_V0_2_NOTES.md for the HTTPS + HTTP/2 roadmap.