From 26f290357d95f1b93e283efa84766f9bea5db545 Mon Sep 17 00:00:00 2001 From: Francesco Date: Mon, 28 Apr 2025 18:24:10 +0200 Subject: [PATCH] fix http resource not found --- include/server/server.h | 9 +++------ include/utils/hashmap.h | 2 +- src/http/http.c | 23 +++++++++++++++-------- src/main.c | 17 +++++++++-------- src/server/server.c | 6 ++++-- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/include/server/server.h b/include/server/server.h index 913021d..11509fd 100644 --- a/include/server/server.h +++ b/include/server/server.h @@ -15,6 +15,9 @@ /* Wait forever (epoll_wait()) */ #define CWS_SERVER_EPOLL_TIMEOUT -1 +/* Main server loop */ +extern volatile bool cws_server_run; + /** * @brief Runs the server * @@ -40,12 +43,6 @@ void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *host */ void cws_server_loop(int sockfd); -// @TODO -/** - * @brief Cleanup server's resources - */ -void cws_server_cleanup(); - /** * @brief Adds a file descriptor to the interest list * diff --git a/include/utils/hashmap.h b/include/utils/hashmap.h index a8f045d..e110ec1 100644 --- a/include/utils/hashmap.h +++ b/include/utils/hashmap.h @@ -8,7 +8,7 @@ #define CWS_HASHMAP_MAX_CLIENTS 1024 /** - * @brief Hash map struct + * @brief Client Hashmap struct * */ typedef struct cws_bucket_t { diff --git a/src/http/http.c b/src/http/http.c index 8008699..60a2994 100644 --- a/src/http/http.c +++ b/src/http/http.c @@ -33,7 +33,6 @@ cws_http *cws_http_parse(char *request_str, int sockfd) { strncpy(request->location, pch, CWS_HTTP_LOCATION_LEN); /* Parse location path */ - /* TODO: Prevent Path Traversal */ if (strcmp(request->location, "/") == 0) { snprintf(request->location_path, CWS_HTTP_LOCATION_PATH_LEN, "%s/index.html", CWS_WWW); } else { @@ -50,6 +49,9 @@ cws_http *cws_http_parse(char *request_str, int sockfd) { strncpy(request->http_version, pch, CWS_HTTP_VERSION_LEN); /* Parse other stuff... */ + /* Parse until a \r\n and store the header with its value + * into a hashmap + */ return request; } @@ -122,7 +124,6 @@ void cws_http_get_content_type(cws_http *request, char *content_type) { char ct[32]; /* TODO: Improve content_type (used to test) */ - if (strcmp(ptr, "html") == 0 || strcmp(ptr, "css") == 0 || strcmp(ptr, "javascript") == 0) { strncpy(ct, "text", sizeof ct); } @@ -153,11 +154,7 @@ void cws_http_send_not_implemented(cws_http *request) { } void cws_http_send_not_found(cws_http *request) { - const char response[1024] = - "HTTP/1.1 404 Not Found\r\n" - "Content-Type: text/html\r\n" - "Content-Length: 216\r\n" - "\r\n" + const char html_body[] = "\n" "\n" " 404 Not Found\n" @@ -166,7 +163,17 @@ void cws_http_send_not_found(cws_http *request) { "

404 Not Found.

\n" "\n" ""; - const size_t response_len = strlen(response); + int html_body_len = strlen(html_body); + + char response[1024]; + int response_len = snprintf(response, sizeof(response), + "HTTP/1.1 404 Not Found\r\n" + "Content-Type: text/html\r\n" + "Content-Length: %d\r\n" + "\r\n" + "%s", + html_body_len, html_body); + send(request->sockfd, response, response_len, 0); } diff --git a/src/main.c b/src/main.c index fe481eb..6b8bcec 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -7,27 +8,27 @@ #include "utils/config.h" void cws_signal_handler(int signo) { - /* TODO */ fprintf(stdout, BLUE "[server] Cleaning up resources...\n" RESET); - fprintf(stdout, BLUE "[server] Closing...\n" RESET); - _exit(1); + cws_server_run = false; } int main(int argc, char **argv) { + int ret; + cws_config *config = cws_config_init(); if (config == NULL) { fprintf(stderr, RED BOLD "[server] Unable to read config file\n" RESET); return 1; } - if (signal(SIGINT, cws_signal_handler) == SIG_ERR) { - fprintf(stderr, BOLD RED "[server] Unable to setup signal()\n" RESET); - return 1; - } + struct sigaction act = { + .sa_handler = cws_signal_handler + }; + ret = sigaction(SIGINT, &act, NULL); fprintf(stdout, BOLD GREEN "[server] Running cws on http://%s:%s...\n" RESET, config->host, config->port); - int ret = cws_server_start(config->host, config->port); + ret = cws_server_start(config->host, config->port); if (ret < 0) { fprintf(stderr, BOLD RED "[server] Unable to start web server\n" RESET); } diff --git a/src/server/server.c b/src/server/server.c index e73f4bd..34a6c00 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -16,6 +16,8 @@ #include "utils/hashmap.h" #include "utils/utils.h" +volatile bool cws_server_run = 1; + int cws_server_start(const char *hostname, const char *service) { struct addrinfo hints; struct addrinfo *res; @@ -83,7 +85,7 @@ void cws_server_loop(int sockfd) { struct epoll_event *revents = malloc(CWS_SERVER_EPOLL_MAXEVENTS * sizeof(struct epoll_event)); int client_fd; - while (1) { + while (cws_server_run) { int nfds = epoll_wait(epfd, revents, CWS_SERVER_EPOLL_MAXEVENTS, CWS_SERVER_EPOLL_TIMEOUT); for (int i = 0; i < nfds; ++i) { @@ -144,11 +146,11 @@ void cws_server_loop(int sockfd) { } /* Clean up everything */ - /* TODO: fix endless loop using cli args */ free(revents); close(epfd); cws_server_close_all_fds(clients); cws_hm_free(clients); + fprintf(stdout, BLUE "[server] Closing...\n" RESET); } void cws_epoll_add(int epfd, int sockfd, uint32_t events) {