From 2cdbb60c7859a9a2bc5a420f3931c57c645d168d Mon Sep 17 00:00:00 2001 From: Samuel Vishesh Paul Date: Fri, 1 Nov 2024 00:25:17 +0530 Subject: [PATCH] Add support for HTTP HEAD requests This update introduces parsing for HTTP HEAD requests. A new test case `test_request_parse_head` ensures the functionality is verified. The HTTP method enumeration is adjusted to include HEAD. --- src/tiny_http/tiny_http_server_lib.c | 4 ++++ src/tiny_http/tiny_http_server_lib.h | 3 ++- test/assert_tiny_http_server_lib.c | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/tiny_http/tiny_http_server_lib.c b/src/tiny_http/tiny_http_server_lib.c index 4c0be8a..df0f96c 100644 --- a/src/tiny_http/tiny_http_server_lib.c +++ b/src/tiny_http/tiny_http_server_lib.c @@ -184,6 +184,10 @@ enum parse_http_request_status parse_http_request_line_from_packet( *ptr += 5; // "POST" - 5 start_uri = 5; request->method = POST; + } else if (strncmp((char *) http_packet, "HEAD", 4) == 0) { + *ptr += 5; // "HEAD" - 5 + start_uri = 5; + request->method = HEAD; } else { fprintf(stderr, "right now, only HTTP GET and POST verbs are supported"); fflush(stderr); diff --git a/src/tiny_http/tiny_http_server_lib.h b/src/tiny_http/tiny_http_server_lib.h index 01e6390..d1afcbf 100644 --- a/src/tiny_http/tiny_http_server_lib.h +++ b/src/tiny_http/tiny_http_server_lib.h @@ -17,7 +17,8 @@ typedef enum http_version { typedef enum http_method { GET = 1, - POST = 2, + HEAD = 2, + POST = 3, } http_method; typedef struct http_header { diff --git a/test/assert_tiny_http_server_lib.c b/test/assert_tiny_http_server_lib.c index ed1b93b..7c3e8f1 100644 --- a/test/assert_tiny_http_server_lib.c +++ b/test/assert_tiny_http_server_lib.c @@ -110,10 +110,34 @@ void test_request_post_root_curl_with_wide_chars(void) { destroy_http_request(http_req); } +void test_request_parse_head(void) { + const uint8_t request[] = "HEAD /test HTTP/1.0\r\n" + "Host: localhost:8085\r\n" + "User-Agent: custom-agent/1.0\r\n" + "Accept: */*\r\n" + "\r\n"; + http_request *http_req = parse_http_request(request, strlen((char *) request)); + assert(http_req != nullptr); + assert(http_req->method == HEAD); + assert(http_req->version == HTTP_1_0); + assert(strncmp(http_req->url, "/test", 255) == 0); + assert(http_req->headers_cnt == 3); + assert(strncmp(http_req->headers[0]->name, "Host", 255) == 0); + assert(strncmp(http_req->headers[0]->value, "localhost:8085", 255) == 0); + assert(strncmp(http_req->headers[1]->name, "User-Agent", 255) == 0); + assert(strncmp(http_req->headers[1]->value, "custom-agent/1.0", 255) == 0); + assert(strncmp(http_req->headers[2]->name, "Accept", 255) == 0); + assert(strncmp(http_req->headers[2]->value, "*/*", 255) == 0); + assert(http_req->body == nullptr); + assert(http_req->body_len == 0); + destroy_http_request(http_req); +} + int main() { test_request_parse_get_root_curl(); test_request_post_root_curl(); test_request_post_root_curl_with_wide_chars(); + test_request_parse_head(); return EXIT_SUCCESS; }