Add support for the HTTP DELETE method

Add a Delete variant to the Method enum to support DELETE requests.
Reject DELETE requests with a body (same as GET requests).

Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
This commit is contained in:
Ilias Stamatis
2026-04-01 16:42:58 +01:00
committed by Ilias Stamatis
parent 9228ffdcc5
commit 876f3feccc
2 changed files with 22 additions and 1 deletions

View File

@@ -244,6 +244,8 @@ pub enum Method {
Put,
/// PATCH Method.
Patch,
/// DELETE Method.
Delete,
}
impl Method {
@@ -259,6 +261,7 @@ impl Method {
b"GET" => Ok(Self::Get),
b"PUT" => Ok(Self::Put),
b"PATCH" => Ok(Self::Patch),
b"DELETE" => Ok(Self::Delete),
_ => Err(RequestError::InvalidHttpMethod("Unsupported HTTP method.")),
}
}
@@ -269,6 +272,7 @@ impl Method {
Self::Get => b"GET",
Self::Put => b"PUT",
Self::Patch => b"PATCH",
Self::Delete => b"DELETE",
}
}
@@ -278,6 +282,7 @@ impl Method {
Method::Get => "GET",
Method::Put => "PUT",
Method::Patch => "PATCH",
Method::Delete => "DELETE",
}
}
}
@@ -398,11 +403,13 @@ mod tests {
assert_eq!(Method::Get.raw(), b"GET");
assert_eq!(Method::Put.raw(), b"PUT");
assert_eq!(Method::Patch.raw(), b"PATCH");
assert_eq!(Method::Delete.raw(), b"DELETE");
// Tests for try_from
assert_eq!(Method::try_from(b"GET").unwrap(), Method::Get);
assert_eq!(Method::try_from(b"PUT").unwrap(), Method::Put);
assert_eq!(Method::try_from(b"PATCH").unwrap(), Method::Patch);
assert_eq!(Method::try_from(b"DELETE").unwrap(), Method::Delete);
assert_eq!(
Method::try_from(b"POST").unwrap_err(),
RequestError::InvalidHttpMethod("Unsupported HTTP method.")
@@ -587,5 +594,8 @@ mod tests {
let val = Method::Patch;
assert_eq!(val.to_str(), "PATCH");
let val = Method::Delete;
assert_eq!(val.to_str(), "DELETE");
}
}

View File

@@ -249,7 +249,9 @@ impl Request {
None
}
content_length => {
if request_line.method == Method::Get {
if request_line.method == Method::Get
|| request_line.method == Method::Delete
{
return Err(RequestError::InvalidRequest);
}
// Multiplication is safe because `CRLF_LEN` is a small constant.
@@ -483,6 +485,15 @@ mod tests {
RequestError::InvalidRequest
);
// Test for invalid Request (`DELETE` requests should have no body).
let request_bytes = b"DELETE /machine-config HTTP/1.1\r\n\
Content-Length: 13\r\n\
Content-Type: application/json\r\n\r\nwhatever body";
assert_eq!(
Request::try_from(request_bytes, None).unwrap_err(),
RequestError::InvalidRequest
);
// Test for request larger than maximum len provided.
let request_bytes = b"GET http://localhost/home HTTP/1.0\r\n\
Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT\r\n\r\n";