-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_example.rs
32 lines (25 loc) · 948 Bytes
/
http_example.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
use std::collections::HashMap;
use jppe::{BorrowByteDecode, BorrowByteEncode};
use jppe_derive::{BorrowByteEncode, BorrowByteDecode};
#[derive(Debug, Default, PartialEq, Eq, BorrowByteEncode, BorrowByteDecode)]
pub struct Http<'a> {
#[jppe(linend=b"\x20")]
pub method: &'a str,
#[jppe(linend=b"\x20")]
pub uri: &'a str,
#[jppe(linend=b"\r\n")]
pub http: &'a str,
#[jppe(linend=b"\r\n")]
pub headers: HashMap<&'a str, &'a str>,
}
fn main() {
let input = b"GET http://www.jankincai.com/ HTTP/1.1\r\nHost: www.jankincai.com\r\nAccept-Encoding: gzip, deflate\r\n";
let (input_remain, value) = Http::decode(input, None, None).unwrap();
println!("{value:?} {input_remain:?}");
// encode
let mut buf = vec![];
value.encode(&mut buf, None, None);
// The headers hashmap is out of order and cannot be compared.
// assert_eq!(buf, input);
assert_eq!(input_remain.is_empty(), true);
}