Skip to content

Commit

Permalink
Ensure proper de-allocation of HTTP request in tests
Browse files Browse the repository at this point in the history
Convert const http_request pointers to mutable pointers to enable de-allocation. Introduce destroy_http_request function to free memory and add necessary documentation for the new function.
  • Loading branch information
SeriousSamV committed Oct 31, 2024
1 parent 8ff350d commit 0aa64a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/tiny_http/tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ void destroy_http_request(http_request *http_request) {
if (http_request->headers != nullptr) {
for (size_t i = 0; i < http_request->headers_cnt; i++) {
free(http_request->headers[i]);
http_request->headers = nullptr;
http_request->headers[i] = nullptr;
}
http_request->headers = nullptr;
http_request->headers_cnt = 0;
}
free(http_request->url);
Expand Down Expand Up @@ -232,6 +233,15 @@ enum parse_http_request_status parse_http_request_line_from_packet(
return PARSE_OK;
}


/**
* Parses an HTTP request from the given http packet in octets.
*
* @param http_packet A pointer to the HTTP packet to parse.
* @param http_packet_len The length of the HTTP packet.
*
* @return A pointer to the parsed HTTP request, or nullptr if parsing fails.
*/
http_request *parse_http_request(const uint8_t *const http_packet, const size_t http_packet_len) {
if (http_packet != nullptr && http_packet_len <= 5) {
fprintf(stderr, "cannot parse http request as it appears empty");
Expand Down
15 changes: 15 additions & 0 deletions src/tiny_http/tiny_http_server_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ typedef struct http_response {
size_t body_len;
} http_response;

/**
* Parses an HTTP request from the given http packet in octets.
*
* @param http_packet A pointer to the HTTP packet to parse.
* @param http_packet_len The length of the HTTP packet.
*
* @return A pointer to the parsed HTTP request, or nullptr if parsing fails.
*/
http_request *parse_http_request(const uint8_t *const http_packet, const size_t http_packet_len);

/**
* Frees the memory allocated for the given HTTP request and its components.
*
* @param http_request The pointer to the HTTP request to be destroyed.
*/
void destroy_http_request(http_request *http_request);

#endif //TINY_HTTP_SERVER_LIB_H
6 changes: 4 additions & 2 deletions test/assert_tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void test_request_parse_get_root_curl(void) {
"User-Agent: curl/8.7.1\r\n"
"Accept: */*\r\n"
"\r\n";
const http_request *http_req = parse_http_request(request, strlen((char *) request));
http_request *http_req = parse_http_request(request, strlen((char *) request));
assert(http_req != nullptr);
assert(http_req->method == GET);
assert(http_req->version == HTTP_1_0);
Expand All @@ -32,6 +32,7 @@ void test_request_parse_get_root_curl(void) {
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);
}

void test_request_post_root_curl(void) {
Expand All @@ -45,7 +46,7 @@ void test_request_post_root_curl(void) {
"Content-Length: 68\r\n"
"\r\n"
"{\n \"key1\": \"value1\",\n \"key2\": \"value2\",\n \"key3\": \"value3\"\n}";
const http_request *http_req = parse_http_request(request, strlen((char *) request));
http_request *http_req = parse_http_request(request, strlen((char *) request));
assert(http_req != nullptr);
assert(http_req->method == POST);
assert(http_req->version == HTTP_1_0);
Expand All @@ -66,6 +67,7 @@ void test_request_post_root_curl(void) {
assert(
strncmp((char *) http_req->body,
"{\n \"key1\": \"value1\",\n \"key2\": \"value2\",\n \"key3\": \"value3\"\n}", 68) == 0);
destroy_http_request(http_req);
}

int main() {
Expand Down

0 comments on commit 0aa64a9

Please sign in to comment.