From b6951c18912c82e7863ebd6c6c77b891ad9aea0c Mon Sep 17 00:00:00 2001 From: frosty Date: Sat, 8 Aug 2026 00:37:44 -0400 Subject: fix: centralise empty HTTP status responses --- src/http.c | 20 ++++++++++++++++++++ src/routing.c | 8 ++------ src/server.c | 9 +++------ 3 files changed, 25 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/http.c b/src/http.c index 891fca3..778bf51 100644 --- a/src/http.c +++ b/src/http.c @@ -48,6 +48,26 @@ static void build_cookie_headers(char *cookie_headers_buffer, } } +void send_status(const char *status_line) { + if (current_client_socket == -1) { + fprintf(stderr, "[ERROR] send_status: No client socket set. Cannot send response.\n"); + return; + } + + char http_response[BUFFER_SIZE]; + snprintf(http_response, sizeof(http_response), + "HTTP/1.1 %s\r\n" + "Content-Length: 0\r\n" + "Connection: close\r\n" + "\r\n", + status_line); + + if (send(current_client_socket, http_response, strlen(http_response), 0) < 0) { + perror("Error sending HTTP status"); + fprintf(stderr, "[ERROR] send_status: Failed to send HTTP status.\n"); + } +} + void send_response(const char *html) { if (current_client_socket == -1) { diff --git a/src/routing.c b/src/routing.c index 55a6599..00886c8 100644 --- a/src/routing.c +++ b/src/routing.c @@ -265,9 +265,7 @@ bool serve_static_file_with_mime(const char *request_path_relative_to_static, co request_path_relative_to_static[0] == '/') { fprintf(stderr, "[SECURITY] Attempted directory traversal: %s\n", request_path_relative_to_static); - const char *forbidden_response = - "HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n"; - send(current_client_socket, forbidden_response, strlen(forbidden_response), 0); + send_status("403 Forbidden"); return true; } @@ -289,9 +287,7 @@ bool serve_static_file_with_mime(const char *request_path_relative_to_static, co fprintf(stderr, "[ERROR] serve_static_file_with_mime: fstat failed for '%s'.\n", full_static_path); fclose(fp); - const char *server_error_response = - "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n"; - send(current_client_socket, server_error_response, strlen(server_error_response), 0); + send_status("500 Internal Server Error"); return true; } diff --git a/src/server.c b/src/server.c index 9f1ca2a..85ad61a 100644 --- a/src/server.c +++ b/src/server.c @@ -218,16 +218,14 @@ void handle_client_connection(int new_socket) { if (first_line_end == NULL) { fprintf(stderr, "[ERROR] handle_client_connection: Invalid HTTP request: No CRLF found.\n"); - const char *bad_request = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n"; - send(new_socket, bad_request, strlen(bad_request), 0); + send_status("400 Bad Request"); close(new_socket); return; } size_t request_line_len = first_line_end - buffer; if (request_line_len >= sizeof(request_line)) { fprintf(stderr, "[ERROR] handle_client_connection: Request line too long.\n"); - const char *bad_request = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n"; - send(new_socket, bad_request, strlen(bad_request), 0); + send_status("400 Bad Request"); close(new_socket); return; } @@ -239,8 +237,7 @@ void handle_client_connection(int new_socket) { if (requested_path == NULL) { fprintf(stderr, "[ERROR] handle_client_connection: Could not parse request path. Sending 400 Bad Request.\n"); - const char *bad_request = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n"; - send(new_socket, bad_request, strlen(bad_request), 0); + send_status("400 Bad Request"); close(new_socket); return; } -- cgit v1.3