http_wire
A Rust library to serialize and parse HTTP/1.x requests and responses to/from their wire format (raw bytes).
Note: This crate only supports HTTP/1.0 and HTTP/1.1. HTTP/2 is not supported due to its binary framing, HPACK header compression, and multiplexed nature.
Usage
Add to your Cargo.toml:
[]
= "0.7"
Encoding
Use the [WireEncode] trait to serialize http::Request and http::Response values to their wire-format bytes.
Encoding is performed via direct serialization — no async runtime, no Tokio, no HTTP pipeline. The request/response line, headers, and body are written sequentially into a pre-allocated buffer. The body is collected using futures::executor::block_on, which requires no runtime for in-memory body types such as Full<Bytes> and Empty<Bytes>.
Encoding a request
use WireEncode;
use Request;
use Empty;
use Bytes;
let request = builder
.method
.uri
.header
.header
.body
.unwrap;
let bytes = request.encode.unwrap;
// b"GET /api/users HTTP/1.1\r\nhost: example.com\r\naccept: application/json\r\n\r\n"
Encoding a request with body
use WireEncode;
use Request;
use Full;
use Bytes;
let body = r#"{"name":"Alice"}"#;
let request = builder
.method
.uri
.header
.header
.header
.body
.unwrap;
let bytes = request.encode.unwrap;
Encoding a response
use WireEncode;
use Response;
use Full;
use Bytes;
let response = builder
.status
.header
.body
.unwrap;
let bytes = response.encode.unwrap;
// b"HTTP/1.1 200 OK\r\ncontent-type: application/json\r\n\r\n{\"status\":\"ok\"}"
Decoding
Use the [WireDecode] trait with [FullRequest] or [FullResponse] to parse raw bytes and determine complete message boundaries.
Decoding a request
use WireDecode;
use FullRequest;
let raw = b"GET /api/users HTTP/1.1\r\nHost: example.com\r\n\r\n";
let mut headers = ;
let = decode.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
Decoding a request (optimised — uninitialized headers)
For performance-critical code, FullRequest supports skipping the header buffer initialisation:
use WireDecode;
use FullRequest;
use MaybeUninit;
let raw = b"GET /api/users HTTP/1.1\r\nHost: example.com\r\n\r\n";
let mut headers = ;
let = decode_uninit.unwrap;
assert_eq!;
decode_uninitis only available forFullRequest.FullResponsedoes not support it because the underlyinghttparse::Responselacksparse_with_uninit_headers.
Decoding a response
use WireDecode;
use FullResponse;
let raw = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello";
let mut headers = ;
let = decode.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
Handling incomplete messages
Both decoders return structured errors rather than panicking on partial data:
use ;
use FullRequest;
// Incomplete headers
let raw = b"GET /api/users HTTP/1.1\r\nHost: example.com\r\n";
let mut headers = ;
assert!;
// Headers complete but body truncated
let raw = b"POST / HTTP/1.1\r\nContent-Length: 100\r\n\r\nshort";
let mut headers = ;
assert!;
Stream parsing
Decoders return the exact byte length of the complete message, making it straightforward to split a streaming buffer:
use WireDecode;
use FullRequest;
Transfer encodings
Both Content-Length and Transfer-Encoding: chunked are fully supported, including:
- Multiple chunks
- Chunk extensions (ignored)
- Trailer headers
- Case-insensitive
chunkeddetection - Multi-value
Transfer-Encodingheaders (e.g.gzip, chunked)
Status codes that never carry a body (1xx, 204, 304) are handled automatically by FullResponse.
Error handling
use ;
WireError variants:
| Variant | When |
|---|---|
Connection |
Body collection failed during encoding |
UnsupportedVersion |
HTTP version is not 1.0 or 1.1 |
PartialHead |
Headers section is incomplete |
IncompleteBody(n) |
Body is n bytes shorter than Content-Length |
InvalidChunkedBody |
Chunked encoding is malformed or incomplete |
HttparseError |
Header parsing failed (invalid characters, etc.) |
Features
- Direct serialization — no Tokio runtime required for encoding
- Zero-copy decoding — parsed fields borrow directly from the input buffer
- Full
Transfer-Encoding: chunkedsupport (encode and decode) - Case-insensitive header parsing
- HTTP/1.0 and HTTP/1.1 support
- Uninitialized header buffer optimisation for request decoding
License
MIT OR Apache-2.0