-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacket_http_example_2.rs
39 lines (30 loc) · 990 Bytes
/
packet_http_example_2.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use jbytes_derive::{BorrowByteEncode, BorrowByteDecode};
use jbytes::prelude::*;
#[derive(Debug, Default, PartialEq, Eq, BorrowByteEncode, BorrowByteDecode)]
pub struct Http<'a> {
#[jbytes(linend=b"\x20")]
pub method: &'a str,
#[jbytes(linend=b"\x20")]
pub uri: &'a str,
#[jbytes(linend=b"\r\n")]
pub version: &'a str,
#[jbytes(try_count=20)]
pub headers: Vec<HttpHeader<'a>>,
}
#[derive(Debug, Default, PartialEq, Eq, BorrowByteEncode, BorrowByteDecode)]
pub struct HttpHeader<'a> {
#[jbytes(linend=b": ")]
pub key: &'a str,
#[jbytes(linend=b"\r\n")]
pub value: &'a str,
}
fn main() -> JResult<()> {
// decode
let data = b"GET http://www.jankincai.com/ HTTP/1.1\r\nHost: www.jankincai.com\r\nAccept-Encoding: gzip, deflate\r\n";
let bytes = Bytes::new(data);
let value: Http = jbytes::decode_borrow(&bytes)?;
println!("{value:?}");
// encode
assert_eq!(*jbytes::encode_borrow(value)?, data);
Ok(())
}