fix string and pointer bugs to actually send 404

This commit is contained in:
Nayan
2025-05-08 17:37:23 -04:00
parent ddf6f8812f
commit b12cfd2c17
5 changed files with 12 additions and 9 deletions

View File

@@ -28,11 +28,11 @@ void* client_handler(void* args) {
buffer[bytes_read] = '\0'; buffer[bytes_read] = '\0';
// Allocate space for response // Allocate space for response
char* response = (char*)malloc(BUFFER_SIZE); char* response = NULL;
size_t response_length = BUFFER_SIZE; size_t response_length = BUFFER_SIZE;
// Handle request // Handle request
handle_request(buffer, bytes_read, response, &response_length); handle_request(buffer, bytes_read, &response, &response_length);
// Send response // Send response
send(client, response, response_length, 0); send(client, response, response_length, 0);

View File

@@ -144,6 +144,7 @@ char* response_to_string(http_response* res) {
char* response = (char*)malloc(total_length); char* response = (char*)malloc(total_length);
strcpy(response, res->status_line); strcpy(response, res->status_line);
strcat(response, "\r\n");
for (int i = 0; i < res->num_headers; i++) { for (int i = 0; i < res->num_headers; i++) {
strcat(response, res->headers[i].key); strcat(response, res->headers[i].key);
strcat(response, ": "); strcat(response, ": ");

View File

@@ -3,7 +3,7 @@
void parse_http_request(char* request, int length, struct http_request* req); void parse_http_request(char* request, int length, struct http_request* req);
void debug_print_request(char* request); 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) { size_t* response_length) {
// Terminate request with EOF so strtok stops at end of string // Terminate request with EOF so strtok stops at end of string
request[length] = EOF; request[length] = EOF;
@@ -13,12 +13,12 @@ void handle_request(char* request, int length, char* response,
// debug_print_request(request); // debug_print_request(request);
printf("parsing request ---------\n"); // printf("parsing request ---------\n");
// Parse request into struct // Parse request into struct
http_request* req = create_http_request(); http_request* req = create_http_request();
parse_http_request(request, length, req); parse_http_request(request, length, req);
print_http_request(req); // print_http_request(req);
printf("---------\n"); // printf("---------\n");
// request_info_print(req); // request_info_print(req);
// free_http_request(req); // free_http_request(req);
@@ -31,8 +31,9 @@ void handle_request(char* request, int length, char* response,
// Convert response to string // Convert response to string
char* response_string = response_to_string(res); char* response_string = response_to_string(res);
// Copy string to response // Copy string to response
response = (char*)malloc(strlen(response_string) + 1); char* responsestr = (char*)malloc(strlen(response_string) + 1);
strcpy(response, response_string); strcpy(responsestr, response_string);
*response = responsestr;
// Set response length // Set response length
*response_length = strlen(response_string); *response_length = strlen(response_string);
// Free response // Free response

View File

@@ -8,7 +8,7 @@
#include "http_stuff.h" #include "http_stuff.h"
#include "response_builder.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); size_t* response_length);
#endif #endif

View File

@@ -9,6 +9,7 @@ void response_handle_get(http_request* req, http_response* res) {
printf("%s\n", file_path); printf("%s\n", file_path);
serve_404(res); serve_404(res);
return;
// Determine the file type // Determine the file type
char* ptr = file_path + strlen(file_path) - 1; char* ptr = file_path + strlen(file_path) - 1;