Skip to content

Commit

Permalink
Add support for HTTP HEAD requests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SeriousSamV committed Oct 31, 2024
1 parent 22fc78c commit 2cdbb60
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/tiny_http/tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/tiny_http/tiny_http_server_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions test/assert_tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2cdbb60

Please sign in to comment.