1 个不稳定版本
使用旧 Rust 2015
0.2.0 | 2015年8月28日 |
---|
#1955 在 解析器实现
185KB
7.5K SLoC
nahpack
nahpack 是一个 RFC 7541 HPACK 编码和解码库。
它可以压缩、解压缩并验证包含在 HEADERS、PUSH PROMISE 和 CONTINUATION 帧中的 HTTP/2 "头部块"。
它与提交到 hpack-test-case 存储库 的实现完全兼容。
用法
此示例解码了在 HPACK RFC 中给出的 C.3 案例。
use nahpack::{decode_block, HpackContext, Headers};
let mut context = HpackContext::new();
{
// first request
let block = b"\x82\x86\x84A\x0fwww.example.com";
let mut headers = Headers::new(block);
match decode_block(&mut context, &mut headers) {
Ok(_) => { },
Err(_) => { }
}
}
{
// second request
let block = b"\x82\x86\x84\xbe\x58\x08no-cache";
let mut headers = Headers::new(block);
match decode_block(&mut context, &mut headers) {
Ok(_) => { },
Err(_) => { }
}
}
{
// third request
let block = b"\x82\x87\x85\xbf@\x88%\xa8I\xe9[\xa9}\x7f\x89%\xa8I\xe9[\xb8\xe8\xb4\xbf";
let mut headers = Headers::new(block);
match decode_block(&mut context, &mut headers) {
Ok(_) => { },
Err(_) => { }
}
}
此示例实现了 HpackEncodable
结构的编码。
use nahpack::{encode_block, HpackEncodable, HpackContext};
struct Header {
name: &'static [u8],
value: &'static [u8],
is_compressable: bool,
}
impl HpackEncodable for Header {
fn name(&self) -> &'static [u8] { self.name }
fn value(&self) -> &'static [u8] { self.value }
fn is_compressable(&self) -> bool { self.is_compressable.clone() }
}
let headers = vec![
Header { name: b":method", value: b"GET", is_compressable: true },
Header { name: b":scheme", value: b"http", is_compressable: true },
Header { name: b":path", value: b"/", is_compressable: true },
Header { name: b":authority", value: b"www.example.com", is_compressable: true },
];
let mut context = HpackContext::new();
let mut writer: Vec<u8> = Vec::new();
let num_bytes = encode_block(&mut writer, &headers, &mut context, 0);
其他
nahpack 根据 Mozilla Public License, Version 2.0 许可。
作者的存储库是 https://github.com/ijl/nahpack,存储库的提交由以下 [email protected] 的密钥签名
-----BEGIN PG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