Skip to content

Commit

Permalink
Remove unnecessary CRLF from HTTP response body end
Browse files Browse the repository at this point in the history
Removed the extraneous CRLF at the end of the HTTP response body to ensure accurate Content-Length. Updated test cases to reflect the change in response body length from 48 to 46 bytes.
  • Loading branch information
SeriousSamV committed Nov 1, 2024
1 parent 12c6696 commit 035ed38
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 0 additions & 2 deletions src/tiny_http/tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ enum render_http_response_status render_http_response(
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

Expand Down
11 changes: 7 additions & 4 deletions test/assert_tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ void test_response_render_200_with_body(void) {
.reason_phrase = (uint8_t *) "OK",
.headers_cnt = 2,
.body = (uint8_t *) "{\n \"key1\": \"value1\",\n \"key2\": \"value2\"\n}",
.body_len = 48
.body_len = 46
};
response.headers = malloc(sizeof(struct http_header) * response.headers_cnt);
response.headers[0].name = "Content-Type";
response.headers[0].value = "application/json";
response.headers[1].name = "Content-Length";
response.headers[1].value = "48";
response.headers[1].value = "46";

uint8_t *response_octets = nullptr;
size_t response_octets_len = 0;
Expand All @@ -192,17 +192,20 @@ void test_response_render_200_with_body(void) {

const char *expected_response = "HTTP/1.0 200 OK\r\n"
"Content-Type: application/json\r\n"
"Content-Length: 48\r\n"
"Content-Length: 46\r\n"
"\r\n"
"{\n \"key1\": \"value1\",\n \"key2\": \"value2\"\n}";
const size_t expected_response_len = strlen(expected_response);
const size_t expected_response_len = strnlen(expected_response, 2000);
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);
}
}

int main() {
char *test = "hello\r\n";
assert(strnlen(test, 255) == 7);

test_request_parse_get_root_curl();
test_request_post_root_curl();
test_request_post_root_curl_with_wide_chars();
Expand Down

0 comments on commit 035ed38

Please sign in to comment.