Crates.io | httpcodec |
lib.rs | httpcodec |
version | 0.2.3 |
source | src |
created_at | 2018-04-20 17:07:58.897227 |
updated_at | 2019-08-18 10:56:13.946555 |
description | Encoders and decoders for HTTP/1.x messages based on bytecodec crate |
homepage | https://github.com/sile/httpcodec |
repository | https://github.com/sile/httpcodec |
max_upload_size | |
id | 61602 |
size | 86,982 |
Encoders and decoders for HTTP/1.x messages based on bytecodec crate.
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");