-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.rs
300 lines (290 loc) · 10.4 KB
/
request.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use crate::head::read_http_head;
use crate::http_error::HttpError;
use crate::rand::next_insecure_rand_u64;
use crate::{AsciiString, ContentType, HeaderList, RequestBody, Response};
use fixed_buffer::FixedBuf;
use futures_io::AsyncRead;
use std::collections::HashMap;
use std::fmt::Debug;
use std::net::SocketAddr;
use url::Url;
#[derive(Clone, Eq, PartialEq)]
pub struct Request {
pub id: u64,
pub remote_addr: SocketAddr,
pub method: String,
pub url: Url,
pub headers: HeaderList,
pub cookies: HashMap<String, String>,
pub content_type: ContentType,
pub expect_continue: bool,
pub chunked: bool,
pub gzip: bool,
pub content_length: Option<u64>,
pub body: RequestBody,
}
impl Request {
#[must_use]
pub fn body(&self) -> &RequestBody {
&self.body
}
#[must_use]
pub fn content_type(&self) -> &ContentType {
&self.content_type
}
#[must_use]
pub fn method(&self) -> &str {
&self.method
}
#[must_use]
pub fn url(&self) -> &Url {
&self.url
}
/// # Errors
/// Returns an error when the request body length is known and it is larger than `max_len`.
///
/// When the request body is not known, this returns `Response::get_body_and_reprocess(max_len)`.
/// The server then tries to read the request body.
/// If it reads more than `max_len` bytes, it stops and returns `413 Payload Too Large`.
pub fn recv_body(self, max_len: u64) -> Result<Request, Response> {
if self.body.len().unwrap_or(0) > max_len {
Err(Response::payload_too_large_413())
} else if self.body().is_pending() {
Err(Response::get_body_and_reprocess(max_len))
} else {
Ok(self)
}
}
/// Checks that the request body has type `application/x-www-form-urlencoded`
/// and deserializes it into type `T`.
///
/// # Errors
/// Returns an error when:
/// - the request has no body
/// - the request body was not received
/// - the request content type is not `application/x-www-form-urlencoded`
/// - we fail to parse the body as URL-encoded data
/// - we fail to deserialize the body into a `T`
///
/// # Panics
/// Panics when the request body was saved to a file and it fails to read the file.
#[cfg(feature = "urlencoded")]
pub fn urlencoded<T: serde::de::DeserializeOwned>(&self) -> Result<T, Response> {
use crate::util::escape_and_elide;
use std::io::Read;
if self.content_type != ContentType::FormUrlEncoded {
Err(Response::text(
400,
"expected x-www-form-urlencoded request body",
))
} else if self.body.is_pending() {
if self.body.len().is_some() {
Err(Response::payload_too_large_413())
} else {
Err(Response::length_required_411())
}
} else {
let mut buf = Vec::new();
if let Err(e) = self.body.reader()?.read_to_end(&mut buf) {
panic!("error reading body: {e}");
}
serde_urlencoded::from_bytes(&buf).map_err(|e| {
Response::text(
400,
format!(
"error processing form data: {}",
escape_and_elide(e.to_string().as_bytes(), 100)
),
)
})
}
}
/// # Errors
/// Returns an error when:
/// - the request has no body
/// - the request body was not received
/// - the request content type is not `application/json`
/// - we fail to parse the body as JSON data
/// - we fail to deserialize the body into a `T`
///
/// # Panics
/// Panics when the request body was saved to a file and it fails to read the file.
#[cfg(feature = "json")]
pub fn json<T: serde::de::DeserializeOwned>(&self) -> Result<T, Response> {
use serde_json::error::Category;
if self.content_type != ContentType::Json {
Err(Response::text(400, "expected json request body"))
} else if self.body.is_pending() {
if self.body.len().is_some() {
Err(Response::payload_too_large_413())
} else {
Err(Response::length_required_411())
}
} else {
serde_json::from_reader(self.body.reader()?).map_err(|e| match e.classify() {
Category::Eof => Response::text(400, "truncated json"),
Category::Io => panic!("error reading body: {e}"),
Category::Syntax => Response::text(400, format!("malformed json: {e}")),
Category::Data => Response::text(400, format!("unexpected json: {e}")),
})
}
}
/// Returns None when the request has no body.
///
/// # Errors
/// Returns an error when:
/// - the request body was not received
/// - the request content type is not `application/json`
/// - we fail to parse the body as JSON data
/// - we fail to deserialize the body into a `T`
///
/// # Panics
/// Panics when the request body was saved to a file and it fails to read the file.
#[cfg(feature = "json")]
pub fn opt_json<T: serde::de::DeserializeOwned>(&self) -> Result<Option<T>, Response> {
if self.body.is_empty().unwrap_or(false) {
Ok(None)
} else {
Ok(Some(self.json()?))
}
}
/// Parses the request URL and deserializes it into type `T`.
///
/// Treats a missing URL query string (`/foo`) as an empty query string (`/foo?`).
///
/// # Errors
/// Returns an error when
/// - the URL parameters are mal-formed
/// - we fail to deserialize the URL parameters into a `T`
#[cfg(feature = "urlencoded")]
pub fn parse_url<T: serde::de::DeserializeOwned>(&self) -> Result<T, Response> {
use crate::util::escape_and_elide;
let url_str = self.url.query().unwrap_or_default();
serde_urlencoded::from_str(url_str).map_err(|e| {
Response::text(
400,
format!(
"error processing url: {}",
escape_and_elide(e.to_string().as_bytes(), 100)
),
)
})
}
}
impl Debug for Request {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
let mut cookie_strings: Vec<String> = self
.cookies
.iter()
.map(|(name, value)| format!("{name}={value}"))
.collect();
cookie_strings.sort();
write!(
f,
"Request{{{}, method={}, path={:?}, headers={:?}, cookies={:?}, {:?}{}{}{}{}, {:?}}}",
self.remote_addr,
self.method(),
self.url().path(),
self.headers,
cookie_strings,
self.content_type(),
if self.expect_continue { ", expect" } else { "" },
if self.chunked { ", chunked" } else { "" },
if self.gzip { ", gzip" } else { "" },
if let Some(len) = &self.content_length {
format!(", {len}")
} else {
String::new()
},
*self.body()
)
}
}
/// # Errors
/// Returns an error when:
/// - the connection is closed
/// - we fail to read a full request
/// - we fail to parse the request
/// - the request uses an unsupported transfer encoding
/// - the request content-length is too long to fit in `u64`
#[allow(clippy::module_name_repetitions)]
pub async fn read_http_request<const BUF_SIZE: usize>(
remote_addr: SocketAddr,
buf: &mut FixedBuf<BUF_SIZE>,
reader: impl AsyncRead + Unpin,
) -> Result<Request, HttpError> {
//dbg!("read_http_request", &buf);
buf.shift();
let mut head = read_http_head(buf, reader).await?;
//dbg!(&head);
let content_type = head
.headers
.remove_only("content-type")
.map_or(ContentType::None, |s| ContentType::parse(s.as_str()));
let expect_continue = head
.headers
.remove_only("expect")
.map_or(false, |s| s.as_str() == "100-continue");
let (gzip, chunked) = {
let opt_ascii_string = head.headers.remove_only("transfer-encoding");
let mut iter = opt_ascii_string
.as_ref()
.map(AsciiString::as_str)
.unwrap_or_default()
.split(',')
.map(str::trim)
.filter(|s| !s.is_empty());
match (iter.next(), iter.next(), iter.next()) {
(Some("gzip"), Some("chunked"), None) => (true, true),
(Some("gzip"), None, None) => (true, false),
(Some("chunked"), None, None) => (false, true),
(None, None, None) => (false, false),
_ => return Err(HttpError::UnsupportedTransferEncoding),
}
};
let mut cookies = HashMap::new();
for header_value in head.headers.get_all("cookie") {
for cookie_str in header_value
.split(';')
.map(str::trim)
.filter(|s| !s.is_empty())
{
let mut parts = cookie_str.splitn(2, '=');
match (parts.next(), parts.next()) {
(Some(name), Some(value)) => {
cookies.insert(name.to_string(), value.to_string());
}
_ => return Err(HttpError::MalformedCookieHeader),
}
}
}
let content_length = if let Some(s) = head.headers.get_only("content-length") {
Some(s.parse().map_err(|_| HttpError::InvalidContentLength)?)
} else {
None
};
#[allow(clippy::match_same_arms)]
// https://datatracker.ietf.org/doc/html/rfc7230#section-3.3
let body = match (chunked, &content_length, head.method.as_str()) {
(true, _, _) => RequestBody::PendingUnknown,
(false, Some(0), _) => RequestBody::empty(),
(false, Some(len), _) => RequestBody::PendingKnown(*len),
(false, None, "POST" | "PUT") => RequestBody::PendingUnknown,
(false, None, _) if expect_continue || gzip => RequestBody::PendingUnknown,
(false, None, _) => RequestBody::empty(),
};
Ok(Request {
id: next_insecure_rand_u64(),
remote_addr,
method: head.method,
url: head.url,
headers: head.headers,
cookies,
content_type,
expect_continue,
chunked,
gzip,
content_length,
body,
})
}