-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathresponse.rs
553 lines (478 loc) · 19.4 KB
/
response.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use std::io::{Error as WriteError, Write};
use crate::ascii::{COLON, CR, LF, SP};
use crate::common::{Body, Version};
use crate::headers::{Header, MediaType};
use crate::Method;
/// Wrapper over a response status code.
///
/// The status code is defined as specified in the
/// [RFC](https://tools.ietf.org/html/rfc7231#section-6).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StatusCode {
/// 100, Continue
Continue,
/// 200, OK
OK,
/// 204, No Content
NoContent,
/// 400, Bad Request
BadRequest,
/// 401, Unauthorized
Unauthorized,
/// 404, Not Found
NotFound,
/// 405, Method Not Allowed
MethodNotAllowed,
/// 413, Payload Too Large
PayloadTooLarge,
/// 500, Internal Server Error
InternalServerError,
/// 501, Not Implemented
NotImplemented,
/// 503, Service Unavailable
ServiceUnavailable,
}
impl StatusCode {
/// Returns the status code as bytes.
pub fn raw(self) -> &'static [u8; 3] {
match self {
Self::Continue => b"100",
Self::OK => b"200",
Self::NoContent => b"204",
Self::BadRequest => b"400",
Self::Unauthorized => b"401",
Self::NotFound => b"404",
Self::MethodNotAllowed => b"405",
Self::PayloadTooLarge => b"413",
Self::InternalServerError => b"500",
Self::NotImplemented => b"501",
Self::ServiceUnavailable => b"503",
}
}
}
#[derive(Debug, PartialEq)]
struct StatusLine {
http_version: Version,
status_code: StatusCode,
}
impl StatusLine {
fn new(http_version: Version, status_code: StatusCode) -> Self {
Self {
http_version,
status_code,
}
}
fn write_all<T: Write>(&self, mut buf: T) -> Result<(), WriteError> {
buf.write_all(self.http_version.raw())?;
buf.write_all(&[SP])?;
buf.write_all(self.status_code.raw())?;
buf.write_all(&[SP, CR, LF])?;
Ok(())
}
}
/// Wrapper over the list of headers associated with a HTTP Response.
/// When creating a ResponseHeaders object, the content type is initialized to `text/plain`.
/// The content type can be updated with a call to `set_content_type`.
#[derive(Debug, PartialEq, Eq)]
pub struct ResponseHeaders {
content_length: Option<i32>,
content_type: MediaType,
deprecation: bool,
server: String,
allow: Vec<Method>,
accept_encoding: bool,
}
impl Default for ResponseHeaders {
fn default() -> Self {
Self {
content_length: Default::default(),
content_type: Default::default(),
deprecation: false,
server: String::from("Firecracker API"),
allow: Vec::new(),
accept_encoding: false,
}
}
}
impl ResponseHeaders {
// The logic pertaining to `Allow` header writing.
fn write_allow_header<T: Write>(&self, buf: &mut T) -> Result<(), WriteError> {
if self.allow.is_empty() {
return Ok(());
}
buf.write_all(b"Allow: ")?;
let delimitator = b", ";
for (idx, method) in self.allow.iter().enumerate() {
buf.write_all(method.raw())?;
// We check above that `self.allow` is not empty.
if idx < self.allow.len() - 1 {
buf.write_all(delimitator)?;
}
}
buf.write_all(&[CR, LF])
}
// The logic pertaining to `Deprecation` header writing.
fn write_deprecation_header<T: Write>(&self, buf: &mut T) -> Result<(), WriteError> {
if !self.deprecation {
return Ok(());
}
buf.write_all(b"Deprecation: true")?;
buf.write_all(&[CR, LF])
}
/// Writes the headers to `buf` using the HTTP specification.
pub fn write_all<T: Write>(&self, buf: &mut T) -> Result<(), WriteError> {
buf.write_all(Header::Server.raw())?;
buf.write_all(&[COLON, SP])?;
buf.write_all(self.server.as_bytes())?;
buf.write_all(&[CR, LF])?;
buf.write_all(b"Connection: keep-alive")?;
buf.write_all(&[CR, LF])?;
self.write_allow_header(buf)?;
self.write_deprecation_header(buf)?;
if let Some(content_length) = self.content_length {
buf.write_all(Header::ContentType.raw())?;
buf.write_all(&[COLON, SP])?;
buf.write_all(self.content_type.as_str().as_bytes())?;
buf.write_all(&[CR, LF])?;
buf.write_all(Header::ContentLength.raw())?;
buf.write_all(&[COLON, SP])?;
buf.write_all(content_length.to_string().as_bytes())?;
buf.write_all(&[CR, LF])?;
if self.accept_encoding {
buf.write_all(Header::AcceptEncoding.raw())?;
buf.write_all(&[COLON, SP])?;
buf.write_all(b"identity")?;
buf.write_all(&[CR, LF])?;
}
}
buf.write_all(&[CR, LF])
}
/// Sets the content length to be written in the HTTP response.
pub fn set_content_length(&mut self, content_length: Option<i32>) {
self.content_length = content_length;
}
/// Sets the HTTP response header server.
pub fn set_server(&mut self, server: &str) {
self.server = String::from(server);
}
/// Sets the content type to be written in the HTTP response.
pub fn set_content_type(&mut self, content_type: MediaType) {
self.content_type = content_type;
}
/// Sets the `Deprecation` header to be written in the HTTP response.
/// https://tools.ietf.org/id/draft-dalal-deprecation-header-03.html
#[allow(unused)]
pub fn set_deprecation(&mut self) {
self.deprecation = true;
}
/// Sets the encoding type to be written in the HTTP response.
#[allow(unused)]
pub fn set_encoding(&mut self) {
self.accept_encoding = true;
}
}
/// Wrapper over an HTTP Response.
///
/// The Response is created using a `Version` and a `StatusCode`. When creating a Response object,
/// the body is initialized to `None` and the header is initialized with the `default` value. The body
/// can be updated with a call to `set_body`. The header can be updated with `set_content_type` and
/// `set_server`.
#[derive(Debug, PartialEq)]
pub struct Response {
status_line: StatusLine,
headers: ResponseHeaders,
body: Option<Body>,
}
impl Response {
/// Creates a new HTTP `Response` with an empty body.
///
/// Although there are several cases where Content-Length field must not
/// be sent, micro-http omits Content-Length field when the response
/// status code is 1XX or 204. If needed, users can remove it by calling
/// `set_content_length(None)`.
///
/// https://datatracker.ietf.org/doc/html/rfc9110#name-content-length
/// > A server MAY send a Content-Length header field in a response to a
/// > HEAD request (Section 9.3.2); a server MUST NOT send Content-Length
/// > in such a response unless its field value equals the decimal number
/// > of octets that would have been sent in the content of a response if
/// > the same request had used the GET method.
/// >
/// > A server MAY send a Content-Length header field in a 304 (Not
/// > Modified) response to a conditional GET request (Section 15.4.5); a
/// > server MUST NOT send Content-Length in such a response unless its
/// > field value equals the decimal number of octets that would have been
/// > sent in the content of a 200 (OK) response to the same request.
/// >
/// > A server MUST NOT send a Content-Length header field in any response
/// > with a status code of 1xx (Informational) or 204 (No Content). A
/// > server MUST NOT send a Content-Length header field in any 2xx
/// > (Successful) response to a CONNECT request (Section 9.3.6).
pub fn new(http_version: Version, status_code: StatusCode) -> Self {
Self {
status_line: StatusLine::new(http_version, status_code),
headers: ResponseHeaders {
content_length: match status_code {
StatusCode::Continue | StatusCode::NoContent => None,
_ => Some(0),
},
..Default::default()
},
body: Default::default(),
}
}
/// Updates the body of the `Response`.
///
/// This function has side effects because it also updates the headers:
/// - `ContentLength`: this is set to the length of the specified body.
pub fn set_body(&mut self, body: Body) {
self.headers.set_content_length(Some(body.len() as i32));
self.body = Some(body);
}
/// Updates the content length of the `Response`.
///
/// It is recommended to use this method only when removing Content-Length
/// field if the response status is not 1XX or 204.
pub fn set_content_length(&mut self, content_length: Option<i32>) {
self.headers.set_content_length(content_length);
}
/// Updates the content type of the `Response`.
pub fn set_content_type(&mut self, content_type: MediaType) {
self.headers.set_content_type(content_type);
}
/// Marks the `Response` as deprecated.
pub fn set_deprecation(&mut self) {
self.headers.set_deprecation();
}
/// Updates the encoding type of `Response`.
pub fn set_encoding(&mut self) {
self.headers.set_encoding();
}
/// Sets the HTTP response server.
pub fn set_server(&mut self, server: &str) {
self.headers.set_server(server);
}
/// Sets the HTTP allowed methods.
pub fn set_allow(&mut self, methods: Vec<Method>) {
self.headers.allow = methods;
}
/// Allows a specific HTTP method.
pub fn allow_method(&mut self, method: Method) {
self.headers.allow.push(method);
}
fn write_body<T: Write>(&self, mut buf: T) -> Result<(), WriteError> {
if let Some(ref body) = self.body {
buf.write_all(body.raw())?;
}
Ok(())
}
/// Writes the content of the `Response` to the specified `buf`.
///
/// # Errors
/// Returns an error when the buffer is not large enough.
pub fn write_all<T: Write>(&self, mut buf: &mut T) -> Result<(), WriteError> {
self.status_line.write_all(&mut buf)?;
self.headers.write_all(&mut buf)?;
self.write_body(&mut buf)?;
Ok(())
}
/// Returns the Status Code of the Response.
pub fn status(&self) -> StatusCode {
self.status_line.status_code
}
/// Returns the Body of the response. If the response does not have a body,
/// it returns None.
pub fn body(&self) -> Option<Body> {
self.body.clone()
}
/// Returns the Content Length of the response.
pub fn content_length(&self) -> i32 {
self.headers.content_length.unwrap_or(0)
}
/// Returns the Content Type of the response.
pub fn content_type(&self) -> MediaType {
self.headers.content_type
}
/// Returns the deprecation status of the response.
pub fn deprecation(&self) -> bool {
self.headers.deprecation
}
/// Returns the HTTP Version of the response.
pub fn http_version(&self) -> Version {
self.status_line.http_version
}
/// Returns the allowed HTTP methods.
pub fn allow(&self) -> Vec<Method> {
self.headers.allow.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_write_response() {
let mut response = Response::new(Version::Http10, StatusCode::OK);
let body = "This is a test";
response.set_body(Body::new(body));
response.set_content_type(MediaType::PlainText);
response.set_encoding();
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.body().unwrap(), Body::new(body));
assert_eq!(response.http_version(), Version::Http10);
assert_eq!(response.content_length(), 14);
assert_eq!(response.content_type(), MediaType::PlainText);
let expected_response: &'static [u8] = b"HTTP/1.0 200 \r\n\
Server: Firecracker API\r\n\
Connection: keep-alive\r\n\
Content-Type: text/plain\r\n\
Content-Length: 14\r\n\
Accept-Encoding: identity\r\n\r\n\
This is a test";
let mut response_buf: [u8; 153] = [0; 153];
assert!(response.write_all(&mut response_buf.as_mut()).is_ok());
assert_eq!(response_buf.as_ref(), expected_response);
// Test response `Allow` header.
let mut response = Response::new(Version::Http10, StatusCode::OK);
let allowed_methods = vec![Method::Get, Method::Patch, Method::Put];
response.set_allow(allowed_methods.clone());
assert_eq!(response.allow(), allowed_methods);
let expected_response: &'static [u8] = b"HTTP/1.0 200 \r\n\
Server: Firecracker API\r\n\
Connection: keep-alive\r\n\
Allow: GET, PATCH, PUT\r\n\
Content-Type: application/json\r\n\
Content-Length: 0\r\n\r\n";
let mut response_buf: [u8; 141] = [0; 141];
assert!(response.write_all(&mut response_buf.as_mut()).is_ok());
assert_eq!(response_buf.as_ref(), expected_response);
// Test write failed.
let mut response_buf: [u8; 1] = [0; 1];
assert!(response.write_all(&mut response_buf.as_mut()).is_err());
}
#[test]
fn test_set_server() {
let mut response = Response::new(Version::Http10, StatusCode::OK);
let body = "This is a test";
let server = "rust-vmm API";
response.set_body(Body::new(body));
response.set_content_type(MediaType::PlainText);
response.set_server(server);
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.body().unwrap(), Body::new(body));
assert_eq!(response.http_version(), Version::Http10);
assert_eq!(response.content_length(), 14);
assert_eq!(response.content_type(), MediaType::PlainText);
let expected_response = format!(
"HTTP/1.0 200 \r\n\
Server: {}\r\n\
Connection: keep-alive\r\n\
Content-Type: text/plain\r\n\
Content-Length: 14\r\n\r\n\
This is a test",
server
);
let mut response_buf: [u8; 123] = [0; 123];
assert!(response.write_all(&mut response_buf.as_mut()).is_ok());
assert!(response_buf.as_ref() == expected_response.as_bytes());
}
#[test]
fn test_status_code() {
assert_eq!(StatusCode::Continue.raw(), b"100");
assert_eq!(StatusCode::OK.raw(), b"200");
assert_eq!(StatusCode::NoContent.raw(), b"204");
assert_eq!(StatusCode::BadRequest.raw(), b"400");
assert_eq!(StatusCode::Unauthorized.raw(), b"401");
assert_eq!(StatusCode::NotFound.raw(), b"404");
assert_eq!(StatusCode::MethodNotAllowed.raw(), b"405");
assert_eq!(StatusCode::PayloadTooLarge.raw(), b"413");
assert_eq!(StatusCode::InternalServerError.raw(), b"500");
assert_eq!(StatusCode::NotImplemented.raw(), b"501");
assert_eq!(StatusCode::ServiceUnavailable.raw(), b"503");
}
#[test]
fn test_allow_method() {
let mut response = Response::new(Version::Http10, StatusCode::MethodNotAllowed);
response.allow_method(Method::Get);
response.allow_method(Method::Put);
assert_eq!(response.allow(), vec![Method::Get, Method::Put]);
}
#[test]
fn test_deprecation() {
// Test a deprecated response with body.
let mut response = Response::new(Version::Http10, StatusCode::OK);
let body = "This is a test";
response.set_body(Body::new(body));
response.set_content_type(MediaType::PlainText);
response.set_encoding();
response.set_deprecation();
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.body().unwrap(), Body::new(body));
assert_eq!(response.http_version(), Version::Http10);
assert_eq!(response.content_length(), 14);
assert_eq!(response.content_type(), MediaType::PlainText);
assert!(response.deprecation());
let expected_response: &'static [u8] = b"HTTP/1.0 200 \r\n\
Server: Firecracker API\r\n\
Connection: keep-alive\r\n\
Deprecation: true\r\n\
Content-Type: text/plain\r\n\
Content-Length: 14\r\n\
Accept-Encoding: identity\r\n\r\n\
This is a test";
let mut response_buf: [u8; 172] = [0; 172];
assert!(response.write_all(&mut response_buf.as_mut()).is_ok());
assert_eq!(response_buf.as_ref(), expected_response);
// Test a deprecated response without a body.
let mut response = Response::new(Version::Http10, StatusCode::NoContent);
response.set_deprecation();
assert_eq!(response.status(), StatusCode::NoContent);
assert_eq!(response.http_version(), Version::Http10);
assert!(response.deprecation());
let expected_response: &'static [u8] = b"HTTP/1.0 204 \r\n\
Server: Firecracker API\r\n\
Connection: keep-alive\r\n\
Deprecation: true\r\n\r\n";
let mut response_buf: [u8; 85] = [0; 85];
assert!(response.write_all(&mut response_buf.as_mut()).is_ok());
assert_eq!(response_buf.as_ref(), expected_response);
}
#[test]
fn test_equal() {
let response = Response::new(Version::Http10, StatusCode::MethodNotAllowed);
let another_response = Response::new(Version::Http10, StatusCode::MethodNotAllowed);
assert_eq!(response, another_response);
let response = Response::new(Version::Http10, StatusCode::OK);
let another_response = Response::new(Version::Http10, StatusCode::BadRequest);
assert_ne!(response, another_response);
}
#[test]
fn test_content_length() {
// If the status code is 1XX or 204, Content-Length field must not exist.
let response = Response::new(Version::Http10, StatusCode::Continue);
let expected_response: &'static [u8] = b"HTTP/1.0 100 \r\n\
Server: Firecracker API\r\n\
Connection: keep-alive\r\n\r\n";
let mut response_buf: [u8; 66] = [0; 66];
assert!(response.write_all(&mut response_buf.as_mut()).is_ok());
assert_eq!(response_buf.as_ref(), expected_response);
let response = Response::new(Version::Http10, StatusCode::NoContent);
let expected_response: &'static [u8] = b"HTTP/1.0 204 \r\n\
Server: Firecracker API\r\n\
Connection: keep-alive\r\n\r\n";
let mut response_buf: [u8; 66] = [0; 66];
assert!(response.write_all(&mut response_buf.as_mut()).is_ok());
assert_eq!(response_buf.as_ref(), expected_response);
// If not 1XX or 204, Content-Length field must exist even if the body isn't set.
let response = Response::new(Version::Http10, StatusCode::OK);
let expected_response: &'static [u8] = b"HTTP/1.0 200 \r\n\
Server: Firecracker API\r\n\
Connection: keep-alive\r\n\
Content-Type: application/json\r\n\
Content-Length: 0\r\n\r\n";
let mut response_buf: [u8; 117] = [0; 117];
assert!(response.write_all(&mut response_buf.as_mut()).is_ok());
assert_eq!(response_buf.as_ref(), expected_response);
}
}