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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::error::GenericTransportError;
use futures_util::stream::StreamExt;
pub async fn read_body(
headers: &hyper::HeaderMap,
mut body: hyper::Body,
max_request_body_size: u32,
) -> Result<(Vec<u8>, bool), GenericTransportError<hyper::Error>> {
let body_size = read_header_content_length(headers).unwrap_or(0);
if body_size > max_request_body_size {
return Err(GenericTransportError::TooLarge);
}
let first_chunk =
body.next().await.ok_or(GenericTransportError::Malformed)?.map_err(GenericTransportError::Inner)?;
if first_chunk.len() > max_request_body_size as usize {
return Err(GenericTransportError::TooLarge);
}
let first_non_whitespace = first_chunk.iter().find(|byte| !byte.is_ascii_whitespace());
let single = match first_non_whitespace {
Some(b'{') => true,
Some(b'[') => false,
_ => return Err(GenericTransportError::Malformed),
};
let mut received_data = Vec::with_capacity(body_size as usize);
received_data.extend_from_slice(&first_chunk);
while let Some(chunk) = body.next().await {
let chunk = chunk.map_err(GenericTransportError::Inner)?;
let body_length = chunk.len() + received_data.len();
if body_length > max_request_body_size as usize {
return Err(GenericTransportError::TooLarge);
}
received_data.extend_from_slice(&chunk);
}
Ok((received_data, single))
}
fn read_header_content_length(headers: &hyper::header::HeaderMap) -> Option<u32> {
let length = read_header_value(headers, "content-length")?;
length.parse::<u32>().ok()
}
pub fn read_header_value<'a>(headers: &'a hyper::header::HeaderMap, header_name: &str) -> Option<&'a str> {
let mut values = headers.get_all(header_name).iter();
let val = values.next()?;
if values.next().is_none() {
val.to_str().ok()
} else {
None
}
}
pub fn read_header_values<'a>(
headers: &'a hyper::header::HeaderMap,
header_name: &str,
) -> hyper::header::GetAll<'a, hyper::header::HeaderValue> {
headers.get_all(header_name)
}
pub fn get_cors_request_headers<'a>(headers: &'a hyper::header::HeaderMap) -> impl Iterator<Item = &str> {
const ACCESS_CONTROL_REQUEST_HEADERS: &str = "access-control-request-headers";
read_header_values(headers, ACCESS_CONTROL_REQUEST_HEADERS)
.iter()
.filter_map(|val| val.to_str().ok())
.flat_map(|val| val.split(','))
.map(|s| s.trim())
}
#[cfg(test)]
mod tests {
use super::{get_cors_request_headers, read_body, read_header_content_length};
#[tokio::test]
async fn body_to_bytes_size_limit_works() {
let headers = hyper::header::HeaderMap::new();
let body = hyper::Body::from(vec![0; 128]);
assert!(read_body(&headers, body, 127).await.is_err());
}
#[test]
fn read_content_length_works() {
let mut headers = hyper::header::HeaderMap::new();
headers.insert(hyper::header::CONTENT_LENGTH, "177".parse().unwrap());
assert_eq!(read_header_content_length(&headers), Some(177));
headers.append(hyper::header::CONTENT_LENGTH, "999".parse().unwrap());
assert_eq!(read_header_content_length(&headers), None);
}
#[test]
fn read_content_length_too_big_value() {
let mut headers = hyper::header::HeaderMap::new();
headers.insert(hyper::header::CONTENT_LENGTH, "18446744073709551616".parse().unwrap());
assert_eq!(read_header_content_length(&headers), None);
}
#[test]
fn get_cors_headers_works() {
let mut headers = hyper::header::HeaderMap::new();
headers.insert(hyper::header::ACCESS_CONTROL_REQUEST_HEADERS, "Content-Type,x-requested-with".parse().unwrap());
let values: Vec<&str> = get_cors_request_headers(&headers).collect();
assert_eq!(values, vec!["Content-Type", "x-requested-with"]);
headers.insert(
hyper::header::ACCESS_CONTROL_REQUEST_HEADERS,
"Content-Type, x-requested-with ".parse().unwrap(),
);
let values: Vec<&str> = get_cors_request_headers(&headers).collect();
assert_eq!(values, vec!["Content-Type", "x-requested-with"]);
}
}