Skip to main content

Crate http_wire

Crate http_wire 

Source
Expand description

Serialize and parse HTTP/1.x requests and responses to/from wire format bytes.

This crate provides encoding and decoding for HTTP/1.0 and HTTP/1.1 messages.

§Implementation Details

Encoding is performed via direct serialization: the request/response line, headers, and body are written sequentially into a BytesMut buffer. No async runtime or HTTP pipeline is required.

The body is collected synchronously using futures::executor::block_on, which works without a Tokio runtime for any body type whose future completes without async I/O (e.g. http_body_util::Full, http_body_util::Empty).

§Encoding

Use the WireEncode trait to convert HTTP messages to their wire format:

use http_wire::WireEncode;
use http::Request;
use http_body_util::Full;
use bytes::Bytes;

let request = Request::builder()
    .method("POST")
    .uri("/api/users")
    .header("Host", "example.com")
    .header("Content-Type", "application/json")
    .body(Full::new(Bytes::from(r#"{"name":"John"}"#)))
    .unwrap();

let bytes = request.encode().unwrap();

§Decoding

Use the WireDecode trait with request::FullRequest or response::FullResponse to parse raw bytes and determine message boundaries.

Modules§

request
HTTP request encoding and decoding.
response
HTTP response encoding and decoding.

Structs§

Header
Represents a parsed header.

Enums§

WireError
Errors that can occur during HTTP wire format encoding or decoding.

Traits§

WireDecode
Decode HTTP messages from raw bytes.
WireEncode
Encode HTTP messages to their wire format bytes.