From 78ebc7b6d033e2b79db366626b16929b414c6580 Mon Sep 17 00:00:00 2001 From: Francesco Date: Thu, 9 Oct 2025 01:09:16 +0200 Subject: [PATCH] fix: send response to client --- src/http/http.c | 16 +++++++++------- src/main.c | 5 +++++ src/server/worker.c | 10 ++++------ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/http/http.c b/src/http/http.c index 2db46e1..00fe606 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -22,6 +22,7 @@ static cws_http_s *http_new() { request->http_version = string_new("", 16); request->location = string_new("", 128); + request->location_path = string_new("", 128); return request; } @@ -89,6 +90,7 @@ static size_t file_data(const char *path, char **data) { /* Retrieve file size */ fseek(file, 0, SEEK_END); const size_t content_length = ftell(file); + errno = 0; rewind(file); if (errno != 0) { fclose(file); @@ -198,16 +200,16 @@ cws_http_s *cws_http_parse(string_s *request_str) { return NULL; } - CWS_LOG_DEBUG("location: %s", pch); string_append(request->location, pch); - // TODO: fix www - request->location_path = string_format("%s/%s", "www", request->location->data); /* Adjust location path */ - if (strcmp(string_cstr(request->location), "/") == 0) { - string_append(request->location_path, "/index.html"); + string_append(request->location_path, "www/"); + CWS_LOG_DEBUG("location path: %s", request->location_path->data); + + if (strcmp(request->location->data, "/") == 0) { + string_append(request->location_path, "index.html"); } else { - string_append(request->location_path, string_cstr(request->location)); + string_append(request->location_path, request->location->data); } CWS_LOG_DEBUG("location path: %s", request->location_path->data); @@ -272,7 +274,7 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co char *status_code = http_status_string(status); size_t header_len = http_header_len(status_code, content_type, body_len_bytes); - size_t total_len = header_len + body_len_bytes; + size_t total_len = header_len + body_len_bytes + 1; *response = malloc(total_len); if (*response == NULL) { diff --git a/src/main.c b/src/main.c index 1643646..427cafc 100644 --- a/src/main.c +++ b/src/main.c @@ -19,6 +19,11 @@ int main(void) { return EXIT_FAILURE; } + if (sigaction(SIGTERM, &act, NULL)) { + CWS_LOG_ERROR("sigaction()"); + return EXIT_FAILURE; + } + cws_config_s *config = cws_config_init(); if (!config) { CWS_LOG_ERROR("Unable to read config file"); diff --git a/src/server/worker.c b/src/server/worker.c index 7584728..54bbf37 100644 --- a/src/server/worker.c +++ b/src/server/worker.c @@ -48,7 +48,7 @@ cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) { if (workers == NULL) { return NULL; } - memset(workers, 0, sizeof **workers * workers_num); + memset(workers, 0, workers_num * sizeof *workers); for (size_t i = 0; i < workers_num; ++i) { workers[i] = malloc(sizeof(cws_worker_s)); @@ -176,17 +176,15 @@ cws_server_ret cws_server_handle_client_data(int epfd, int client_fd) { } cws_http_s *request = cws_http_parse(data); - request->sockfd = client_fd; - string_free(data); - - // TODO: fix response - if (request == NULL) { cws_server_close_client(epfd, client_fd); return CWS_SERVER_HTTP_PARSE_ERROR; } + request->sockfd = client_fd; + + cws_http_send_response(request, HTTP_OK); cws_http_free(request);