Added support for Accept-Encoding

Signed-off-by: Alexandru Cihodaru <cihodar@amazon.com>
Signed-off-by: YUAN LYU <lyuyuan92@gmail.com>
This commit is contained in:
Alexandru Cihodaru
2020-09-03 18:23:24 +03:00
committed by Adrian Catangiu
parent 8a8d7bb5b1
commit 683b85d07e
5 changed files with 157 additions and 8 deletions

View File

@@ -84,6 +84,7 @@ pub struct ResponseHeaders {
content_type: MediaType,
server: String,
allow: Vec<Method>,
accept_encoding: bool,
}
impl Default for ResponseHeaders {
@@ -93,6 +94,7 @@ impl Default for ResponseHeaders {
content_type: Default::default(),
server: String::from("Firecracker API"),
allow: Vec::new(),
accept_encoding: false,
}
}
}
@@ -140,6 +142,13 @@ impl ResponseHeaders {
buf.write_all(&[COLON, SP])?;
buf.write_all(self.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])
@@ -159,6 +168,12 @@ impl ResponseHeaders {
pub fn set_content_type(&mut self, content_type: MediaType) {
self.content_type = content_type;
}
/// 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.
@@ -198,6 +213,11 @@ impl Response {
self.headers.set_content_type(content_type);
}
/// 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);
@@ -274,8 +294,9 @@ mod tests {
let body = "This is a test";
response.set_body(Body::new(body));
response.set_content_type(MediaType::PlainText);
response.set_encoding();
assert!(response.status() == StatusCode::OK);
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);
@@ -285,12 +306,13 @@ mod tests {
Server: Firecracker API\r\n\
Connection: keep-alive\r\n\
Content-Type: text/plain\r\n\
Content-Length: 14\r\n\r\n\
Content-Length: 14\r\n\
Accept-Encoding: identity\r\n\r\n\
This is a test";
let mut response_buf: [u8; 126] = [0; 126];
let mut response_buf: [u8; 153] = [0; 153];
assert!(response.write_all(&mut response_buf.as_mut()).is_ok());
assert!(response_buf.as_ref() == expected_response);
assert_eq!(response_buf.as_ref(), expected_response);
// Test response `Allow` header.
let mut response = Response::new(Version::Http10, StatusCode::OK);
@@ -320,7 +342,7 @@ mod tests {
response.set_content_type(MediaType::PlainText);
response.set_server(server);
assert!(response.status() == StatusCode::OK);
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);