Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for HTTP HEAD, POST, DELETE methods are needed to support RESTful API #1

Merged
merged 2 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.2.0

- support HEAD, POST, DELETE methods.

# v0.1.0

- micro-http v0.1.0 first release.
16 changes: 7 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "micro_http"
version = "0.1.0"
version = "0.2.0"
license = "Apache-2.0"
authors = ["Amazon Firecracker team <firecracker-devel@amazon.com>"]
edition = "2018"
Expand Down
2 changes: 1 addition & 1 deletion coverage_config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"coverage_score": 92.4, "exclude_path": "", "crate_features": ""}
{"coverage_score": 92.5, "exclude_path": "", "crate_features": ""}
32 changes: 31 additions & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,14 @@ impl Body {
pub enum Method {
/// GET Method.
Get,
/// HEAD Method.
Head,
/// POST Method.
Post,
/// PUT Method.
Put,
/// DELETE Method.
Delete,
/// PATCH Method.
Patch,
}
Expand All @@ -163,7 +169,10 @@ impl Method {
pub fn try_from(bytes: &[u8]) -> Result<Self, RequestError> {
match bytes {
b"GET" => Ok(Method::Get),
b"HEAD" => Ok(Method::Head),
b"POST" => Ok(Method::Post),
b"PUT" => Ok(Method::Put),
b"DELETE" => Ok(Method::Delete),
b"PATCH" => Ok(Method::Patch),
_ => Err(RequestError::InvalidHttpMethod("Unsupported HTTP method.")),
}
Expand All @@ -173,7 +182,10 @@ impl Method {
pub fn raw(self) -> &'static [u8] {
match self {
Method::Get => b"GET",
Method::Head => b"HEAD",
Method::Post => b"POST",
Method::Put => b"PUT",
Method::Delete => b"DELETE",
Method::Patch => b"PATCH",
}
}
Expand All @@ -182,7 +194,10 @@ impl Method {
pub fn to_str(self) -> &'static str {
match self {
Method::Get => "GET",
Method::Head => "HEAD",
Method::Post => "POST",
Method::Put => "PUT",
Method::Delete => "DELETE",
Method::Patch => "PATCH",
}
}
Expand Down Expand Up @@ -279,15 +294,21 @@ mod tests {
fn test_method() {
// Test for raw
assert_eq!(Method::Get.raw(), b"GET");
assert_eq!(Method::Head.raw(), b"HEAD");
assert_eq!(Method::Post.raw(), b"POST");
assert_eq!(Method::Put.raw(), b"PUT");
assert_eq!(Method::Delete.raw(), b"DELETE");
assert_eq!(Method::Patch.raw(), b"PATCH");

// Tests for try_from
assert_eq!(Method::try_from(b"GET").unwrap(), Method::Get);
assert_eq!(Method::try_from(b"HEAD").unwrap(), Method::Head);
assert_eq!(Method::try_from(b"POST").unwrap(), Method::Post);
assert_eq!(Method::try_from(b"PUT").unwrap(), Method::Put);
assert_eq!(Method::try_from(b"DELETE").unwrap(), Method::Delete);
assert_eq!(Method::try_from(b"PATCH").unwrap(), Method::Patch);
assert_eq!(
Method::try_from(b"POST").unwrap_err(),
Method::try_from(b"CONNECT").unwrap_err(),
RequestError::InvalidHttpMethod("Unsupported HTTP method.")
);
}
Expand Down Expand Up @@ -382,9 +403,18 @@ mod tests {
let val = Method::Get;
assert_eq!(val.to_str(), "GET");

let val = Method::Head;
assert_eq!(val.to_str(), "HEAD");

let val = Method::Post;
assert_eq!(val.to_str(), "POST");

let val = Method::Put;
assert_eq!(val.to_str(), "PUT");

let val = Method::Delete;
assert_eq!(val.to_str(), "DELETE");

let val = Method::Patch;
assert_eq!(val.to_str(), "PATCH");
}
Expand Down
2 changes: 1 addition & 1 deletion src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ mod tests {
);

// Test for invalid method.
let request_line = b"POST http://localhost/home HTTP/1.0";
let request_line = b"CONNECT http://localhost/home HTTP/1.0";
assert_eq!(
RequestLine::try_from(request_line).unwrap_err(),
RequestError::InvalidHttpMethod("Unsupported HTTP method.")
Expand Down
11 changes: 4 additions & 7 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,10 @@ impl<T: Read + Write> ClientConnection<T> {

// Send an error response for the request that gave us the error.
let mut error_response = Response::new(Version::Http11, StatusCode::BadRequest);
error_response.set_body(Body::new(
format!(
"{{ \"error\": \"{}\nAll previous unanswered requests will be dropped.\" }}",
inner.to_string()
)
.to_string(),
));
error_response.set_body(Body::new(format!(
"{{ \"error\": \"{}\nAll previous unanswered requests will be dropped.\" }}",
inner.to_string()
)));
self.connection.enqueue_response(error_response);
}
Err(ConnectionError::InvalidWrite) => {
Expand Down