changes to send headers and body separately which fixes images not sending

This commit is contained in:
Nayan
2025-05-08 18:32:40 -04:00
parent b12cfd2c17
commit 4e8e22ec61
7 changed files with 80 additions and 51 deletions

View File

@@ -138,7 +138,7 @@ char* response_to_string(http_response* res) {
total_length += strlen("Content-Length: ");
total_length += strlen(content_lenth_str);
total_length += len_crlf;
total_length += strlen(res->body);
total_length += res->content_length;
total_length += len_crlf;
total_length += 1; // For null terminator
@@ -161,6 +161,48 @@ char* response_to_string(http_response* res) {
return response;
}
char* response_headers_to_string(http_response* res) {
// Define lengths
int total_length = 0;
int len_newline = strlen("\r\n");
int len_crlf = strlen("\r\n\r\n");
char* content_lenth_str = (char*)malloc(10);
sprintf(content_lenth_str, "%zu", res->content_length);
// Calculate total length
total_length += strlen(res->status_line);
total_length += len_newline;
for (int i = 0; i < res->num_headers; i++) {
total_length += strlen(res->headers[i].key);
total_length += strlen(": ");
total_length += strlen(res->headers[i].value);
total_length += len_newline;
}
total_length += strlen(res->content_type);
total_length += len_newline;
total_length += strlen("Content-Length: ");
total_length += strlen(content_lenth_str);
total_length += len_crlf;
total_length += 1; // For null terminator
char* response = (char*)malloc(total_length);
strcpy(response, res->status_line);
strcat(response, "\r\n");
for (int i = 0; i < res->num_headers; i++) {
strcat(response, res->headers[i].key);
strcat(response, ": ");
strcat(response, res->headers[i].value);
strcat(response, "\r\n");
}
strcat(response, res->content_type);
strcat(response, "\r\n");
strcat(response, "Content-Length: ");
strcat(response, content_lenth_str);
strcat(response, "\r\n\r\n");
return response;
}
char* get_header_value_request(http_request* req, char* key) {
// printf("Getting header value for key: %s\n", key);
for (int i = 0; i < req->num_headers; i++) {