fix memory leaks and unecessary newlines in logs

This commit is contained in:
Nayan
2025-05-08 22:51:56 -04:00
parent 63a016cf36
commit 613e87ad86
4 changed files with 10 additions and 4 deletions

View File

@@ -37,9 +37,10 @@ void* client_handler(void* args) {
send(client, response->body, response->content_length, 0); send(client, response->body, response->content_length, 0);
send(client, "\r\n", 2, 0); send(client, "\r\n", 2, 0);
free(response); free_http_response(response);
return NULL; free(headers);
} }
free(buffer); free(buffer);
free(args);
return NULL; return NULL;
} }

View File

@@ -216,6 +216,7 @@ char* response_headers_to_string(http_response* res) {
strcat(response, "Content-Length: "); strcat(response, "Content-Length: ");
strcat(response, content_lenth_str); strcat(response, content_lenth_str);
strcat(response, "\r\n\r\n"); strcat(response, "\r\n\r\n");
free(content_lenth_str);
return response; return response;
} }
@@ -226,7 +227,7 @@ char* get_header_value_request(http_request* req, char* key) {
return req->headers[i].value; return req->headers[i].value;
} }
} }
log_message(LOG_WARN, "Header %s not found\n", key); log_message(LOG_WARN, "Header %s not found", key);
return NULL; return NULL;
} }

View File

@@ -68,11 +68,13 @@ void parse_http_request(char* request, int length, struct http_request* req) {
req->method = DELETE; req->method = DELETE;
} else { } else {
log_message(LOG_ERROR, "Invalid packet (method), cannot parse"); log_message(LOG_ERROR, "Invalid packet (method), cannot parse");
free(type);
return; return;
} }
req->method_str = (char*)malloc(type_end - type_start + 1); req->method_str = (char*)malloc(type_end - type_start + 1);
strncpy(req->method_str, type_start, type_end - type_start); strncpy(req->method_str, type_start, type_end - type_start);
req->method_str[type_end - type_start] = '\0'; req->method_str[type_end - type_start] = '\0';
free(type);
// Extract URL // Extract URL
char* url_start = type_end + 1; char* url_start = type_end + 1;

View File

@@ -37,6 +37,8 @@ void response_handle_get(http_request* req, http_response* res) {
serve_404(res); serve_404(res);
return; return;
} }
free(file_path);
} }
void response_handle_post(http_request* req, http_response* res) { void response_handle_post(http_request* req, http_response* res) {
@@ -162,7 +164,7 @@ void response_build_static_file(char* file_path, content_type content_type,
// Set content type string // Set content type string
res->content_type = content_type_str; res->content_type = content_type_str;
log_message(LOG_INFO, "Serving %s\n", file_path); log_message(LOG_INFO, "Serving %s", file_path);
} }
void serve_404(http_response* res) { void serve_404(http_response* res) {