From 12c669602f13a031380519a5d7cefa536a49ce8e Mon Sep 17 00:00:00 2001 From: Samuel Vishesh Paul Date: Fri, 1 Nov 2024 14:31:11 +0530 Subject: [PATCH] Implement full HTTP response rendering and add memory deallocation Enhanced the HTTP response rendering to include headers and body octets. Updated test cases to free allocated memory for response octets, ensuring no memory leaks. --- src/tiny_http/tiny_http_server_lib.c | 36 ++++++++++++++++++++++++++++ test/assert_tiny_http_server_lib.c | 7 ++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/tiny_http/tiny_http_server_lib.c b/src/tiny_http/tiny_http_server_lib.c index 8cd7ca6..e78b2d3 100644 --- a/src/tiny_http/tiny_http_server_lib.c +++ b/src/tiny_http/tiny_http_server_lib.c @@ -113,8 +113,44 @@ enum render_http_response_status render_http_response( octets_written += 2; // endregion status line + // region headers + //
:
if (http_response->headers != nullptr && http_response->headers_cnt > 0) { + for (size_t i = 0; i < http_response->headers_cnt; i++) { + const size_t header_name_len = strnlen(http_response->headers[i].name, 2000); + strncpy( + (char *) *out_response_octets + octets_written, + http_response->headers[i].name, + header_name_len); + octets_written += header_name_len; + strcpy((char *) *out_response_octets + octets_written, ": "); + octets_written += 2; + + const size_t header_value_len = strnlen(http_response->headers[i].value, 2000); + strncpy( + (char *) *out_response_octets + octets_written, + http_response->headers[i].value, + header_value_len); + octets_written += header_value_len; + strcpy((char *) *out_response_octets + octets_written, "\r\n"); + octets_written += 2; + } + + strncpy((char *) *out_response_octets + octets_written, "\r\n", 2); + octets_written += 2; + } + // endregion headers + + // region body + // + if (http_response->body != nullptr && http_response->body_len > 0) { + strncpy((char *) *out_response_octets + octets_written, (const char *) http_response->body, + http_response->body_len); + octets_written += http_response->body_len; + strcpy((char *) *out_response_octets + octets_written, "\r\n"); + octets_written += 2; } + // endregion body *out_response_len = octets_written; return RENDER_OK; diff --git a/test/assert_tiny_http_server_lib.c b/test/assert_tiny_http_server_lib.c index cd394c7..c273ff0 100644 --- a/test/assert_tiny_http_server_lib.c +++ b/test/assert_tiny_http_server_lib.c @@ -146,6 +146,7 @@ void test_response_render_200_no_body(void) { assert(response_code == RENDER_OK); assert(response_octets_len > 0); assert(strncmp((char *) response_octets, "HTTP/1.0 200 OK\r\n", 32) == 0); + free(response_octets); } void test_response_render_404_no_body(void) { @@ -161,6 +162,7 @@ void test_response_render_404_no_body(void) { assert(response_code == RENDER_OK); assert(response_octets_len > 0); assert(strncmp((char *) response_octets, "HTTP/1.0 404 Not Found\r\n", 32) == 0); + free(response_octets); } void test_response_render_200_with_body(void) { @@ -193,8 +195,9 @@ void test_response_render_200_with_body(void) { "Content-Length: 48\r\n" "\r\n" "{\n \"key1\": \"value1\",\n \"key2\": \"value2\"\n}"; - assert(strncmp((char *)response_octets, expected_response, response_octets_len) == 0); - assert(strnlen((char *) response_octets, response_octets_len + 8) == response_octets_len); + const size_t expected_response_len = strlen(expected_response); + assert(strncmp((char *)response_octets, expected_response, expected_response_len) == 0); + assert(strnlen((char *) response_octets, expected_response_len) == response_octets_len); free(response_octets); } }