Skip to main content

Module interop

Module interop 

Source
Expand description

Interop with the http crate (feature http).

httpsd deliberately defines its own Request / Response / Method / StatusCode / Headers so the core stays dependency-free. When you want to bridge to the wider ecosystem (tower/hyper-style middleware, libraries that speak http types), enable the http feature and use the conversions here.

Conversions follow the usual Rust convention: infallible directions are From, fallible ones are TryFrom with HttpConvertError. The fallible direction is httpsd โ†’ http (a Method::Other token, header name, or target string might not be valid http data); the reverse is infallible (header values that are not UTF-8 are decoded lossily).

use httpsd::Response;

// Build a response with the `http` crate; convert into an httpsd `Response`
// (and, with the `router` feature, return `http::Response` from a handler
// directly โ€” it implements `IntoResponse`).
let h: http::Response<Vec<u8>> = http::Response::builder()
    .status(http::StatusCode::CREATED)
    .header("x-made-by", "http-crate")
    .body(b"hi".to_vec())
    .unwrap();
let resp: Response = h.into();
assert_eq!(resp.status_code().code(), 201);

// The reverse, plus `Request`/`Method`/`StatusCode`/`Headers`, all convert
// too: `let out: http::Response<Vec<u8>> = resp.try_into()?;`

Enumsยง

HttpConvertError
Error converting an httpsd value into the corresponding http crate type.