mirror of
https://github.com/opus-tango/http-server-in-c.git
synced 2026-03-19 19:52:51 +00:00
fix memory leaks and unecessary newlines in logs
This commit is contained in:
@@ -37,9 +37,10 @@ void* client_handler(void* args) {
|
||||
send(client, response->body, response->content_length, 0);
|
||||
send(client, "\r\n", 2, 0);
|
||||
|
||||
free(response);
|
||||
return NULL;
|
||||
free_http_response(response);
|
||||
free(headers);
|
||||
}
|
||||
free(buffer);
|
||||
free(args);
|
||||
return NULL;
|
||||
}
|
||||
@@ -216,6 +216,7 @@ char* response_headers_to_string(http_response* res) {
|
||||
strcat(response, "Content-Length: ");
|
||||
strcat(response, content_lenth_str);
|
||||
strcat(response, "\r\n\r\n");
|
||||
free(content_lenth_str);
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -226,7 +227,7 @@ char* get_header_value_request(http_request* req, char* key) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -68,11 +68,13 @@ void parse_http_request(char* request, int length, struct http_request* req) {
|
||||
req->method = DELETE;
|
||||
} else {
|
||||
log_message(LOG_ERROR, "Invalid packet (method), cannot parse");
|
||||
free(type);
|
||||
return;
|
||||
}
|
||||
req->method_str = (char*)malloc(type_end - type_start + 1);
|
||||
strncpy(req->method_str, type_start, type_end - type_start);
|
||||
req->method_str[type_end - type_start] = '\0';
|
||||
free(type);
|
||||
|
||||
// Extract URL
|
||||
char* url_start = type_end + 1;
|
||||
|
||||
@@ -37,6 +37,8 @@ void response_handle_get(http_request* req, http_response* res) {
|
||||
serve_404(res);
|
||||
return;
|
||||
}
|
||||
|
||||
free(file_path);
|
||||
}
|
||||
|
||||
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
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user