#codec #http-request #http-1 #http-response #encoder #decoder #bytecodec

httpcodec

Encoders and decoders for HTTP/1.x messages based on bytecodec crate

15 releases

Uses old Rust 2015

0.2.3 Aug 18, 2019
0.2.2 May 19, 2018
0.1.10 May 2, 2018
0.1.6 Apr 30, 2018

#896 in Encoding

Download history 929/week @ 2024-08-04 898/week @ 2024-08-11 1064/week @ 2024-08-18 1248/week @ 2024-08-25 1127/week @ 2024-09-01 1043/week @ 2024-09-08 919/week @ 2024-09-15 1219/week @ 2024-09-22 975/week @ 2024-09-29 778/week @ 2024-10-06 1049/week @ 2024-10-13 1189/week @ 2024-10-20 1401/week @ 2024-10-27 1292/week @ 2024-11-03 1018/week @ 2024-11-10 1119/week @ 2024-11-17

4,901 downloads per month
Used in 13 crates

MIT license

85KB
2K SLoC

httpcodec

Documentation Build Status Code Coverage License: MIT

Encoders and decoders for HTTP/1.x messages based on bytecodec crate.

Documentation

Examples

Encodes a HTTP request message:

use bytecodec::Encode;
use bytecodec::bytes::BytesEncoder;
use bytecodec::io::IoEncodeExt;
use httpcodec::{BodyEncoder, HttpVersion, Method, Request, RequestEncoder, RequestTarget};

let request = Request::new(
    Method::new("GET").unwrap(),
    RequestTarget::new("/foo").unwrap(),
    HttpVersion::V1_1,
    b"barbaz",
);

let mut encoder = RequestEncoder::new(BodyEncoder::new(BytesEncoder::new()));
encoder.start_encoding(request).unwrap();

let mut buf = Vec::new();
encoder.encode_all(&mut buf).unwrap();
assert_eq!(buf, "GET /foo HTTP/1.1\r\nContent-Length: 6\r\n\r\nbarbaz".as_bytes());

Decodes a HTTP response message:

use bytecodec::bytes::RemainingBytesDecoder;
use bytecodec::io::IoDecodeExt;
use httpcodec::{BodyDecoder, HttpVersion, ResponseDecoder};

let mut decoder =
    ResponseDecoder::<BodyDecoder<RemainingBytesDecoder>>::default();

let input = b"HTTP/1.0 200 OK\r\nContent-Length: 6\r\n\r\nbarbaz";
let response = decoder.decode_exact(input.as_ref()).unwrap();

assert_eq!(response.http_version(), HttpVersion::V1_0);
assert_eq!(response.status_code().as_u16(), 200);
assert_eq!(response.reason_phrase().as_str(), "OK");
assert_eq!(
    response.header()
        .fields()
        .map(|f| (f.name().to_owned(), f.value().to_owned()))
        .collect::<Vec<_>>(),
    vec![("Content-Length".to_owned(), "6".to_owned())]
);
assert_eq!(response.body(), b"barbaz");

References

  • RFC 7230 Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing

Dependencies

~2MB
~44K SLoC