Skip to content

Commit

Permalink
Implement full HTTP response rendering and add memory deallocation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SeriousSamV committed Nov 1, 2024
1 parent b831336 commit 12c6696
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/tiny_http/tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,44 @@ enum render_http_response_status render_http_response(
octets_written += 2;
// endregion status line

// region headers
// <header name>: <header value><CR><LF>
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
// <body octets><CR><LF>
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;
Expand Down
7 changes: 5 additions & 2 deletions test/assert_tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 12c6696

Please sign in to comment.