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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::convert::TryFrom;
use std::io::{Error, Result, ErrorKind};
use bytes::Buf;
use httparse::{Status, Request};
use https::header::{HeaderName, HeaderValue, HeaderMap};
use tcp::{Socket, SocketHandle};
use crate::utils::DEFAULT_SUPPORT_HTTP_VERSION;
use tcp::connect::TcpSocket;
///
/// 默认读取Http请求的字节长度
///
pub const DEFAULT_READ_READY_HTTP_REQUEST_BYTE_LEN: usize = 0;
///
/// 上行请求头
///
pub struct UpStreamHeader;
unsafe impl Send for UpStreamHeader {}
unsafe impl Sync for UpStreamHeader {}
impl UpStreamHeader {
/// 读请求,并解析报文头
pub async fn read_header<'h, 'b, S>(handle: SocketHandle<S>,
buf: &'b [u8],
req: &mut Request<'h, 'b>,
headers: &mut HeaderMap) -> Result<Option<usize>>
where 'b: 'h,
S: Socket {
match req.parse(buf) {
Err(e) => {
//解析Http头错误
handle.close(Err(Error::new(ErrorKind::Other,
format!("Http server parse header failed, token: {:?}, remote: {:?}, local: {:?}, reason: parse request header error, buf_len: {:?}, buf: {:?}, reason: {:?}",
handle.get_token(),
handle.get_remote(),
handle.get_local(),
buf.len(),
buf,
e))));
return Err(Error::new(ErrorKind::Other,
format!("Http server parse header failed, token: {:?}, remote: {:?}, local: {:?}, reason: parse request header error, buf_len: {:?}, buf: {:?}",
handle.get_token(),
handle.get_remote(),
handle.get_local(),
buf.len(),
buf)));
},
Ok(ref status) if status.is_partial() => {
//部分头数据已到达
match req.version {
Some(ver) if ver != DEFAULT_SUPPORT_HTTP_VERSION => {
//不合法的Http版本号
handle.close(Err(Error::new(ErrorKind::Other,
format!("Http server parse header failed, token: {:?}, remote: {:?}, local: {:?}, version: {}, reason: not support http version",
handle.get_token(),
handle.get_remote(),
handle.get_local(),
ver))));
return Err(Error::new(ErrorKind::Other,
format!("Http server parse header failed, token: {:?}, remote: {:?}, local: {:?}, version: {}, reason: not support http version",
handle.get_token(),
handle.get_remote(),
handle.get_local(),
ver)));
},
_ => {
//头数据不完整,则继续读
return Ok(None);
}
}
},
Ok(status) => {
//全部头数据已到达,则继续读取,并解析体数据
if let Status::Complete(len) = status {
if let Some(bin) = unsafe { (&mut *handle.get_read_buffer().get()) } {
let _ = bin.copy_to_bytes(len); //消耗请求头的数据
}
if let Err(e) = fill_headers(headers, req) {
handle.close(Err(e));
return Err(Error::new(ErrorKind::Other,
format!("Http server fill header failed, token: {:?}, remote: {:?}, local: {:?}, reason: fill headers error",
handle.get_token(),
handle.get_remote(),
handle.get_local())));
}
return Ok(Some(len));
}
Err(Error::new(ErrorKind::Other,
format!("Http server parse header failed, token: {:?}, remote: {:?}, local: {:?}, reason: unknown error",
handle.get_token(),
handle.get_remote(),
handle.get_local())))
},
}
}
}
/// 将报文中的Http头填充到头映射表中
pub fn fill_headers<'h, 'b>(headers: &mut HeaderMap,
req: & mut Request<'h, 'b>) -> Result<usize> {
let mut count = 0;
for header in req.headers.iter() {
match HeaderName::try_from(header.name) {
Err(e) => {
return Err(Error::new(ErrorKind::InvalidData, e));
},
Ok(key) => {
match HeaderValue::from_bytes(header.value) {
Err(e) => {
return Err(Error::new(ErrorKind::InvalidData, e));
},
Ok(value) => {
//构建值成功,则写入头信息表
headers.insert(key, value);
count += 1;
},
}
},
}
}
Ok(count)
}