-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspan.rs
499 lines (426 loc) · 17.3 KB
/
span.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use std::ops::Range;
use bytes::Bytes;
use crate::{
helpers::get_span_range,
http::{
Body, BodyContent, Code, Header, HeaderName, HeaderValue, Method, Reason, Request,
RequestLine, Response, Status, Target,
},
json, ParseError, Span,
};
const MAX_HEADERS: usize = 128;
/// Parses an HTTP request.
pub fn parse_request(src: &[u8]) -> Result<Request, ParseError> {
parse_request_from_bytes(&Bytes::copy_from_slice(src), 0)
}
/// Parses an HTTP request from a `Bytes` buffer starting from the `offset`.
pub(crate) fn parse_request_from_bytes(src: &Bytes, offset: usize) -> Result<Request, ParseError> {
let mut headers = [httparse::EMPTY_HEADER; MAX_HEADERS];
let (method, path, head_end) = {
let mut request = httparse::Request::new(&mut headers);
let head_end = match request.parse(&src[offset..]) {
Ok(httparse::Status::Complete(head_end)) => head_end + offset,
Ok(httparse::Status::Partial) => {
return Err(ParseError(format!("incomplete request: {:?}", src)))
}
Err(err) => return Err(ParseError(err.to_string())),
};
let method = request
.method
.ok_or_else(|| ParseError("method missing from request".to_string()))?;
let path = request
.path
.ok_or_else(|| ParseError("path missing from request".to_string()))?;
(method, path, head_end)
};
let request_line_end = src[offset..]
.windows(2)
.position(|w| w == b"\r\n")
.expect("request line is terminated with CRLF");
let request_line_range = offset..offset + request_line_end + 2;
let headers = headers
.iter()
.take_while(|h| *h != &httparse::EMPTY_HEADER)
.map(|header| from_header(src, header))
.collect();
// httparse allocates a new buffer to store the method for performance reasons,
// so we have to search for the span in the source. This is quick as the method
// is at the front.
let method = src[offset..]
.windows(method.len())
.find(|w| *w == method.as_bytes())
.expect("method is present");
let mut request = Request {
span: Span::new_bytes(src.clone(), offset..head_end),
request: RequestLine {
span: Span::new_str(src.clone(), request_line_range),
method: Method(Span::new_str(src.clone(), get_span_range(src, method))),
target: Target(Span::new_from_str(src.clone(), path)),
},
headers,
body: None,
};
let body_len = request_body_len(&request)?;
if body_len > 0 {
let range = head_end..head_end + body_len;
if range.end > src.len() {
return Err(ParseError(format!(
"body range {}..{} exceeds source {}",
range.start,
range.end,
src.len()
)));
}
let content_type = request
.headers_with_name("Content-Type")
.next()
.map(|header| header.value.as_bytes())
.unwrap_or_default();
request.body = Some(parse_body(src, range.clone(), content_type)?);
request.span = Span::new_bytes(src.clone(), offset..range.end);
}
Ok(request)
}
/// Parses an HTTP response.
pub fn parse_response(src: &[u8]) -> Result<Response, ParseError> {
parse_response_from_bytes(&Bytes::copy_from_slice(src), 0)
}
/// Parses an HTTP response from a `Bytes` buffer starting from the `offset`.
pub(crate) fn parse_response_from_bytes(
src: &Bytes,
offset: usize,
) -> Result<Response, ParseError> {
let mut headers = [httparse::EMPTY_HEADER; MAX_HEADERS];
let (reason, code, head_end) = {
let mut response = httparse::Response::new(&mut headers);
let head_end = match response.parse(&src[offset..]) {
Ok(httparse::Status::Complete(head_end)) => head_end + offset,
Ok(httparse::Status::Partial) => {
return Err(ParseError(format!("incomplete response: {:?}", src)))
}
Err(err) => return Err(ParseError(err.to_string())),
};
let code = response
.code
.ok_or_else(|| ParseError("code missing from response".to_string()))
.map(|c| c.to_string())?;
let reason = response
.reason
.ok_or_else(|| ParseError("reason missing from response".to_string()))?;
(reason, code, head_end)
};
let status_line_end = src[offset..]
.windows(2)
.position(|w| w == b"\r\n")
.expect("status line is terminated with CRLF");
let status_line_range = offset..offset + status_line_end + 2;
let headers = headers
.iter()
.take_while(|h| *h != &httparse::EMPTY_HEADER)
.map(|header| from_header(src, header))
.collect();
// httparse doesn't preserve the response code span, so we find it.
let code = src[offset..]
.windows(3)
.find(|w| *w == code.as_bytes())
.expect("code is present");
let mut response = Response {
span: Span::new_bytes(src.clone(), offset..head_end),
status: Status {
span: Span::new_str(src.clone(), status_line_range),
code: Code(Span::new_str(src.clone(), get_span_range(src, code))),
reason: Reason(Span::new_from_str(src.clone(), reason)),
},
headers,
body: None,
};
let body_len = response_body_len(&response)?;
if body_len > 0 {
let range = head_end..head_end + body_len;
if range.end > src.len() {
return Err(ParseError(format!(
"body range {}..{} exceeds source {}",
range.start,
range.end,
src.len()
)));
}
let content_type = response
.headers_with_name("Content-Type")
.next()
.map(|header| header.value.as_bytes())
.unwrap_or_default();
response.body = Some(parse_body(src, range.clone(), content_type)?);
response.span = Span::new_bytes(src.clone(), offset..range.end);
}
Ok(response)
}
/// Converts a `httparse::Header` to a `Header`.
fn from_header(src: &Bytes, header: &httparse::Header) -> Header {
let name_range = get_span_range(src, header.name.as_bytes());
let value_range = get_span_range(src, header.value);
let crlf_idx = src[value_range.end..]
.windows(2)
.position(|b| b == b"\r\n")
.expect("CRLF is present in a valid header");
// Capture the entire header including trailing whitespace and the CRLF.
let header_range = name_range.start..value_range.end + crlf_idx + 2;
Header {
span: Span::new_bytes(src.clone(), header_range),
name: HeaderName(Span::new_str(src.clone(), name_range)),
value: HeaderValue(Span::new_bytes(src.clone(), value_range)),
}
}
/// Calculates the length of the request body according to RFC 9112, section 6.
fn request_body_len(request: &Request) -> Result<usize, ParseError> {
// The presence of a message body in a request is signaled by a Content-Length
// or Transfer-Encoding header field.
// If a message is received with both a Transfer-Encoding and a Content-Length header field,
// the Transfer-Encoding overrides the Content-Length
if request
.headers_with_name("Transfer-Encoding")
.next()
.is_some()
{
Err(ParseError(
"Transfer-Encoding not supported yet".to_string(),
))
} else if let Some(h) = request.headers_with_name("Content-Length").next() {
// If a valid Content-Length header field is present without Transfer-Encoding, its decimal value
// defines the expected message body length in octets.
std::str::from_utf8(h.value.0.as_bytes())?
.parse::<usize>()
.map_err(|err| ParseError(format!("failed to parse Content-Length value: {err}")))
} else {
// If this is a request message and none of the above are true, then the message body length is zero
Ok(0)
}
}
/// Calculates the length of the response body according to RFC 9112, section 6.
fn response_body_len(response: &Response) -> Result<usize, ParseError> {
// Any response to a HEAD request and any response with a 1xx (Informational), 204 (No Content), or 304 (Not Modified)
// status code is always terminated by the first empty line after the header fields, regardless of the header fields
// present in the message, and thus cannot contain a message body or trailer section.
match response
.status
.code
.as_str()
.parse::<usize>()
.expect("code is valid utf-8")
{
100..=199 | 204 | 304 => return Ok(0),
_ => {}
}
if response
.headers_with_name("Transfer-Encoding")
.next()
.is_some()
{
Err(ParseError(
"Transfer-Encoding not supported yet".to_string(),
))
} else if let Some(h) = response.headers_with_name("Content-Length").next() {
// If a valid Content-Length header field is present without Transfer-Encoding, its decimal value
// defines the expected message body length in octets.
std::str::from_utf8(h.value.0.as_bytes())?
.parse::<usize>()
.map_err(|err| ParseError(format!("failed to parse Content-Length value: {err}")))
} else {
// If this is a response message and none of the above are true, then there is no way to
// determine the length of the message body except by reading it until the connection is closed.
// We currently consider this an error because we have no outer context information.
Err(ParseError(
"A response with a body must contain either a Content-Length or Transfer-Encoding header".to_string(),
))
}
}
/// Parses a request or response message body.
///
/// # Arguments
///
/// * `src` - The source bytes.
/// * `range` - The range of the message body in the source bytes.
/// * `content_type` - The value of the Content-Type header.
fn parse_body(src: &Bytes, range: Range<usize>, content_type: &[u8]) -> Result<Body, ParseError> {
let span = Span::new_bytes(src.clone(), range.clone());
let content = if content_type.get(..16) == Some(b"application/json".as_slice()) {
let mut value = json::parse(span.data.clone())?;
value.offset(range.start);
BodyContent::Json(value)
} else {
BodyContent::Unknown(span.clone())
};
Ok(Body { span, content })
}
#[cfg(test)]
mod tests {
use crate::Spanned;
use super::*;
const TEST_REQUEST: &[u8] = b"\
GET /home.html HTTP/1.1\r\n\
Host: developer.mozilla.org\r\n\
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0\r\n\
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.\r\n\
Accept-Language: en-US,en;q=0.\r\n\
Accept-Encoding: gzip, deflate, b\r\n\
Referer: https://developer.mozilla.org/testpage.htm\r\n\
Connection: keep-alive\r\n\
Content-Length: 12\r\n\
Cache-Control: max-age=0\r\n\r\n\
Hello World!";
const TEST_RESPONSE: &[u8] = b"\
HTTP/1.1 200 OK\r\n\
Date: Mon, 27 Jul 2009 12:28:53 GMT\r\n\
Server: Apache/2.2.14 (Win32)\r\n\
Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT\r\n\
Content-Length: 52\r\n\
Content-Type: text/html\r\n\
Connection: Closed\r\n\r\n\
<html>\n\
<body>\n\
<h1>Hello, World!</h1>\n\
</body>\n\
</html>";
const TEST_REQUEST2: &[u8] = b"\
GET /info.html HTTP/1.1\r\n\
Host: tlsnotary.org\r\n\
User-Agent: client\r\n\
Content-Length: 4\r\n\r\n\
ping";
const TEST_RESPONSE2: &[u8] = b"\
HTTP/1.1 200 OK\r\n\
Server: server\r\n\
Content-Length: 4\r\n\
Content-Type: text/plain\r\n\
Connection: keep-alive\r\n\r\n\
pong";
const TEST_REQUEST_JSON: &[u8] = b"\
POST / HTTP/1.1\r\n\
Host: localhost\r\n\
Content-Type: application/json\r\n\
Content-Length: 14\r\n\r\n\
{\"foo\": \"bar\"}";
const TEST_RESPONSE_JSON: &[u8] = b"\
HTTP/1.1 200 OK\r\n\
Content-Type: application/json\r\n\
Content-Length: 14\r\n\r\n\
{\"foo\": \"bar\"}";
#[test]
fn test_parse_request() {
let req = parse_request(TEST_REQUEST).unwrap();
assert_eq!(req.span(), TEST_REQUEST);
assert_eq!(req.request.method.as_str(), "GET");
assert_eq!(
req.headers_with_name("Host").next().unwrap().value.span(),
b"developer.mozilla.org".as_slice()
);
assert_eq!(
req.headers_with_name("User-Agent")
.next()
.unwrap()
.value
.span(),
b"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0"
.as_slice()
);
assert_eq!(req.body.unwrap().span(), b"Hello World!".as_slice());
}
#[test]
fn test_parse_header_trailing_whitespace() {
let req = parse_request(b"GET / HTTP/1.1\r\nHost: example.com \r\n\r\n").unwrap();
let header = req.headers_with_name("Host").next().unwrap();
assert_eq!(header.span.as_bytes(), b"Host: example.com \r\n".as_slice());
}
#[test]
fn test_parse_response() {
let res = parse_response(TEST_RESPONSE).unwrap();
assert_eq!(res.span(), TEST_RESPONSE);
assert_eq!(res.status.code.as_str(), "200");
assert_eq!(res.status.reason.as_str(), "OK");
assert_eq!(
res.headers_with_name("Server").next().unwrap().value.span(),
b"Apache/2.2.14 (Win32)".as_slice()
);
assert_eq!(
res.headers_with_name("Connection")
.next()
.unwrap()
.value
.span(),
b"Closed".as_slice()
);
assert_eq!(
res.body.unwrap().span(),
b"<html>\n<body>\n<h1>Hello, World!</h1>\n</body>\n</html>".as_slice()
);
}
// Make sure the first request is not parsed.
#[test]
fn test_parse_request_from_bytes() {
let mut request = Vec::new();
request.extend(TEST_REQUEST2);
request.extend(TEST_REQUEST);
let request = Bytes::copy_from_slice(&request);
let req = parse_request_from_bytes(&request, TEST_REQUEST2.len()).unwrap();
assert_eq!(req.span(), TEST_REQUEST);
assert_eq!(req.request.method.as_str(), "GET");
assert_eq!(
req.headers_with_name("Host").next().unwrap().value.span(),
b"developer.mozilla.org".as_slice()
);
assert_eq!(
req.headers_with_name("User-Agent")
.next()
.unwrap()
.value
.span(),
b"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0"
.as_slice()
);
assert_eq!(req.body.unwrap().span(), b"Hello World!".as_slice());
}
// Make sure the first response is not parsed.
#[test]
fn test_parse_response_from_bytes() {
let mut response = Vec::new();
response.extend(TEST_RESPONSE2);
response.extend(TEST_RESPONSE);
let response = Bytes::copy_from_slice(&response);
let res = parse_response_from_bytes(&response, TEST_RESPONSE2.len()).unwrap();
assert_eq!(res.span(), TEST_RESPONSE);
assert_eq!(res.status.code.as_str(), "200");
assert_eq!(res.status.reason.as_str(), "OK");
assert_eq!(
res.headers_with_name("Server").next().unwrap().value.span(),
b"Apache/2.2.14 (Win32)".as_slice()
);
assert_eq!(
res.headers_with_name("Connection")
.next()
.unwrap()
.value
.span(),
b"Closed".as_slice()
);
assert_eq!(
res.body.unwrap().span(),
b"<html>\n<body>\n<h1>Hello, World!</h1>\n</body>\n</html>".as_slice()
);
}
#[test]
fn test_parse_request_json() {
let req = parse_request(TEST_REQUEST_JSON).unwrap();
let BodyContent::Json(value) = req.body.unwrap().content else {
panic!("body is not json");
};
assert_eq!(value.span(), "{\"foo\": \"bar\"}");
}
#[test]
fn test_parse_response_json() {
let res = parse_response(TEST_RESPONSE_JSON).unwrap();
let BodyContent::Json(value) = res.body.unwrap().content else {
panic!("body is not json");
};
assert_eq!(value.span(), "{\"foo\": \"bar\"}");
}
}