diff --git a/src/tiny_http/tiny_http_server_lib.c b/src/tiny_http/tiny_http_server_lib.c index a4b1417..4c0be8a 100644 --- a/src/tiny_http/tiny_http_server_lib.c +++ b/src/tiny_http/tiny_http_server_lib.c @@ -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); @@ -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"); diff --git a/src/tiny_http/tiny_http_server_lib.h b/src/tiny_http/tiny_http_server_lib.h index 5d8d0ea..01e6390 100644 --- a/src/tiny_http/tiny_http_server_lib.h +++ b/src/tiny_http/tiny_http_server_lib.h @@ -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 diff --git a/test/assert_tiny_http_server_lib.c b/test/assert_tiny_http_server_lib.c index 30bd4a4..0380c9b 100644 --- a/test/assert_tiny_http_server_lib.c +++ b/test/assert_tiny_http_server_lib.c @@ -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); @@ -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) { @@ -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); @@ -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() {