use std::collections::HashMap;
use std::fmt::Debug;
use webparse::http::http2::Decoder;
use algorithm::buf::{BinaryMut, Bt, BtMut};
use webparse::{Helper, Request, Url, Version};
#[derive(Debug)]
pub enum Pay<T>
where
T: Bt,
{
Data(T),
}
pub trait Test {
fn serialize1<B: Bt + BtMut>(&self, buf: &mut B);
}
impl Test for &'static str {
fn serialize1<B: Bt + BtMut>(&self, buf: &mut B) {
buf.put_slice(self.as_bytes());
}
}
extern crate webparse;
fn hexstr_to_vec(s: &str) -> Vec<u8> {
let mut result = vec![];
let bytes = s.as_bytes();
let mut val = 0;
let mut is_first = true;
for b in bytes {
if b != &b' ' {
if is_first {
val = u8::from_str_radix(std::str::from_utf8(&[*b]).unwrap(), 16).unwrap();
is_first = false
} else {
val =
val * 16 + u8::from_str_radix(std::str::from_utf8(&[*b]).unwrap(), 16).unwrap();
result.push(val);
val = 0;
is_first = true;
}
}
}
result
}
fn hex_debug_print(val: &[u8]) {
for v in val {
print!(
"{}{} ",
String::from_utf8_lossy(&vec![Helper::to_hex(v / 16)]),
String::from_utf8_lossy(&vec![Helper::to_hex(v % 16)])
);
}
println!();
}
fn debug_request_parse_full_http2() {
let http2: Vec<u8> = vec![
80, 82, 73, 32, 42, 32, 72, 84, 84, 80, 47, 50, 46, 48, 13, 10, 13, 10, 83, 77, 13, 10, 13,
10, 0, 0, 18, 4, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 100, 0, 4, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0,
0, 4, 8, 0, 0, 0, 0, 0, 1, 255, 0, 1, 0, 0, 55, 1, 5, 0, 0, 0, 1, 131, 132, 134, 65, 143,
11, 226, 92, 46, 60, 184, 93, 125, 112, 178, 205, 199, 128, 240, 63, 122, 136, 37, 182, 80,
195, 171, 186, 210, 224, 83, 3, 42, 47, 42, 64, 136, 37, 168, 73, 233, 91, 169, 125, 127,
137, 37, 168, 73, 233, 91, 184, 232, 180, 191, 0, 0, 25, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 83, 69, 84, 84, 73, 78, 71, 83, 32, 101, 120, 112, 101, 99, 116, 101, 100,
];
let mut req = Request::new();
let _size = req.parse(&http2).unwrap();
println!("req = {:?}", req);
}
fn debug_request_parse() {
let mut request = Request::new();
request.headers_mut().insert("Connection", "ok");
let _xx = request.headers().is_keep_alive();
return;
let _bytes = [
80, 79, 83, 84, 32, 47, 112, 111, 115, 116, 32, 72, 84, 84, 80, 47, 49, 46, 49, 13, 10, 72,
111, 115, 116, 58, 32, 49, 57, 50, 46, 49, 54, 56, 46, 49, 55, 57, 46, 49, 51, 51, 58, 56,
48, 56, 48, 13, 10, 85, 115, 101, 114, 45, 65, 103, 101, 110, 116, 58, 32, 99, 117, 114,
108, 47, 55, 46, 55, 52, 46, 48, 13, 10, 65, 99, 99, 101, 112, 116, 58, 32, 42, 47, 42, 13,
10, 67, 111, 110, 110, 101, 99, 116, 105, 111, 110, 58, 32, 85, 112, 103, 114, 97, 100,
101, 44, 32, 72, 84, 84, 80, 50, 45, 83, 101, 116, 116, 105, 110, 103, 115, 13, 10, 85,
112, 103, 114, 97, 100, 101, 58, 32, 104, 50, 99, 13, 10, 72, 84, 84, 80, 50, 45, 83, 101,
116, 116, 105, 110, 103, 115, 58, 32, 65, 65, 77, 65, 65, 65, 66, 107, 65, 65, 81, 67, 65,
65, 65, 65, 65, 65, 73, 65, 65, 65, 65, 65, 13, 10, 99, 117, 115, 116, 111, 109, 45, 107,
101, 121, 58, 99, 117, 115, 116, 111, 109, 45, 118, 97, 108, 117, 101, 13, 10, 67, 111,
110, 116, 101, 110, 116, 45, 76, 101, 110, 103, 116, 104, 58, 32, 50, 49, 13, 10, 67, 111,
110, 116, 101, 110, 116, 45, 84, 121, 112, 101, 58, 32, 97, 112, 112, 108, 105, 99, 97,
116, 105, 111, 110, 47, 120, 45, 119, 119, 119, 45, 102, 111, 114, 109, 45, 117, 114, 108,
101, 110, 99, 111, 100, 101, 100, 13, 10, 13, 10, 97, 97, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
];
let _result =
request.parse(b"GET //:///// HTTP/1.1\r\nHost: Upgrade, HTTP2-Settings \r\n\r\naaa");
println!("result = {:?}", request);
println!("is_partial = {}", request.is_partial());
println!("body len = {}", request.get_body_len());
println!("host len = {:?}", request.get_host());
println!("host len = {:?}", request.get_connect_url());
println!(
"http data = {}",
String::from_utf8_lossy(&request.http1_data().unwrap())
);
assert_eq!(
String::from_utf8_lossy(&request.http1_data().unwrap()).as_bytes(),
b"GET //:///// HTTP/1.1\r\nHost: \r\n\r\n"
);
let x = &request.headers()["Host"];
if x == &"foo" {
println!("111");
}
if &"foo" == x {
println!("111");
}
}
fn main() {
let mut req = crate::Request::new();
let ret = req.parse(b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r");
assert!(ret.err().unwrap().is_partial());
let buf = b"GET /index.html HTTP/1.1\r\nHost: example.domain\r\n\r\n";
let ret = req.parse(buf).unwrap();
assert!(ret == buf.len());
assert!(req.is_complete());
debug_request_parse();
println!("finish");
if true {
return;
}
let url: Result<Url, webparse::WebError> =
Url::try_from("https://%4811:!%2011@www.baidu.com:88/path?aaa=222");
println!("value = {:?}", url);
println!("value = {}", url.ok().unwrap());
let url = Url::try_from("/path?qqq=222");
println!("value = {:?}", url);
println!("value = {}", url.ok().unwrap());
println!("decode = {:?}", Url::url_decode("%48%211111"));
println!("decode = {:?}", Url::url_decode("%48%21111%1"));
println!("decode = {:?}", Url::url_decode("%48%21111%4j"));
let value = Url::try_from("https://11:11@www.baidu.com/path").unwrap();
println!("value = {}", value);
let req = Request::builder()
.version(Version::Http2)
.method("GET")
.url("/")
.header(":scheme", "http")
.header(":authority", "www.example.com");
{
}
let _rrr = req.body(()).unwrap();
let mut xx = HashMap::<(u32, u8), u8>::new();
xx.insert((48, 5), 6);
println!("aaa {:?}", xx.get(&(48, 5)));
}