From b12cfd2c1705c3b5fd6aa43729577e9585cc6d0f Mon Sep 17 00:00:00 2001 From: Nayan <33187059+GShadow5@users.noreply.github.com> Date: Thu, 8 May 2025 17:37:23 -0400 Subject: [PATCH] fix string and pointer bugs to actually send 404 --- client_handler.c | 4 ++-- http_stuff.c | 1 + request_handler.c | 13 +++++++------ request_handler.h | 2 +- response_builder.c | 1 + 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/client_handler.c b/client_handler.c index b3cb8bc..6b34768 100644 --- a/client_handler.c +++ b/client_handler.c @@ -28,11 +28,11 @@ void* client_handler(void* args) { buffer[bytes_read] = '\0'; // Allocate space for response - char* response = (char*)malloc(BUFFER_SIZE); + char* response = NULL; size_t response_length = BUFFER_SIZE; // Handle request - handle_request(buffer, bytes_read, response, &response_length); + handle_request(buffer, bytes_read, &response, &response_length); // Send response send(client, response, response_length, 0); diff --git a/http_stuff.c b/http_stuff.c index 6f3665f..6d78318 100644 --- a/http_stuff.c +++ b/http_stuff.c @@ -144,6 +144,7 @@ char* response_to_string(http_response* res) { 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, ": "); diff --git a/request_handler.c b/request_handler.c index 5f25650..cee8107 100644 --- a/request_handler.c +++ b/request_handler.c @@ -3,7 +3,7 @@ void parse_http_request(char* request, int length, struct http_request* req); void debug_print_request(char* request); -void handle_request(char* request, int length, char* response, +void handle_request(char* request, int length, char** response, size_t* response_length) { // Terminate request with EOF so strtok stops at end of string request[length] = EOF; @@ -13,12 +13,12 @@ void handle_request(char* request, int length, char* response, // debug_print_request(request); - printf("parsing request ---------\n"); + // printf("parsing request ---------\n"); // Parse request into struct http_request* req = create_http_request(); parse_http_request(request, length, req); - print_http_request(req); - printf("---------\n"); + // print_http_request(req); + // printf("---------\n"); // request_info_print(req); // free_http_request(req); @@ -31,8 +31,9 @@ void handle_request(char* request, int length, char* response, // Convert response to string char* response_string = response_to_string(res); // Copy string to response - response = (char*)malloc(strlen(response_string) + 1); - strcpy(response, response_string); + char* responsestr = (char*)malloc(strlen(response_string) + 1); + strcpy(responsestr, response_string); + *response = responsestr; // Set response length *response_length = strlen(response_string); // Free response diff --git a/request_handler.h b/request_handler.h index 8f9d0c1..df9d2e1 100644 --- a/request_handler.h +++ b/request_handler.h @@ -8,7 +8,7 @@ #include "http_stuff.h" #include "response_builder.h" -void handle_request(char* request, int length, char* response, +void handle_request(char* request, int length, char** response, size_t* response_length); #endif \ No newline at end of file diff --git a/response_builder.c b/response_builder.c index c508baa..ce8f843 100644 --- a/response_builder.c +++ b/response_builder.c @@ -9,6 +9,7 @@ void response_handle_get(http_request* req, http_response* res) { printf("%s\n", file_path); serve_404(res); + return; // Determine the file type char* ptr = file_path + strlen(file_path) - 1;