fix http resource not found

This commit is contained in:
2025-04-28 18:24:10 +02:00
parent b9dd1c4007
commit 26f290357d
5 changed files with 32 additions and 25 deletions

View File

@@ -15,6 +15,9 @@
/* Wait forever (epoll_wait()) */ /* Wait forever (epoll_wait()) */
#define CWS_SERVER_EPOLL_TIMEOUT -1 #define CWS_SERVER_EPOLL_TIMEOUT -1
/* Main server loop */
extern volatile bool cws_server_run;
/** /**
* @brief Runs the server * @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); 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 * @brief Adds a file descriptor to the interest list
* *

View File

@@ -8,7 +8,7 @@
#define CWS_HASHMAP_MAX_CLIENTS 1024 #define CWS_HASHMAP_MAX_CLIENTS 1024
/** /**
* @brief Hash map struct * @brief Client Hashmap struct
* *
*/ */
typedef struct cws_bucket_t { typedef struct cws_bucket_t {

View File

@@ -33,7 +33,6 @@ cws_http *cws_http_parse(char *request_str, int sockfd) {
strncpy(request->location, pch, CWS_HTTP_LOCATION_LEN); strncpy(request->location, pch, CWS_HTTP_LOCATION_LEN);
/* Parse location path */ /* Parse location path */
/* TODO: Prevent Path Traversal */
if (strcmp(request->location, "/") == 0) { if (strcmp(request->location, "/") == 0) {
snprintf(request->location_path, CWS_HTTP_LOCATION_PATH_LEN, "%s/index.html", CWS_WWW); snprintf(request->location_path, CWS_HTTP_LOCATION_PATH_LEN, "%s/index.html", CWS_WWW);
} else { } else {
@@ -50,6 +49,9 @@ cws_http *cws_http_parse(char *request_str, int sockfd) {
strncpy(request->http_version, pch, CWS_HTTP_VERSION_LEN); strncpy(request->http_version, pch, CWS_HTTP_VERSION_LEN);
/* Parse other stuff... */ /* Parse other stuff... */
/* Parse until a \r\n and store the header with its value
* into a hashmap
*/
return request; return request;
} }
@@ -122,7 +124,6 @@ void cws_http_get_content_type(cws_http *request, char *content_type) {
char ct[32]; char ct[32];
/* TODO: Improve content_type (used to test) */ /* TODO: Improve content_type (used to test) */
if (strcmp(ptr, "html") == 0 || strcmp(ptr, "css") == 0 || strcmp(ptr, "javascript") == 0) { if (strcmp(ptr, "html") == 0 || strcmp(ptr, "css") == 0 || strcmp(ptr, "javascript") == 0) {
strncpy(ct, "text", sizeof ct); 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) { void cws_http_send_not_found(cws_http *request) {
const char response[1024] = const char html_body[] =
"HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/html\r\n"
"Content-Length: 216\r\n"
"\r\n"
"<html>\n" "<html>\n"
"<head>\n" "<head>\n"
" <title>404 Not Found</title>\n" " <title>404 Not Found</title>\n"
@@ -166,7 +163,17 @@ void cws_http_send_not_found(cws_http *request) {
"<p>404 Not Found.</p>\n" "<p>404 Not Found.</p>\n"
"</body>\n" "</body>\n"
"</html>"; "</html>";
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); send(request->sockfd, response, response_len, 0);
} }

View File

@@ -1,4 +1,5 @@
#include <signal.h> #include <signal.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@@ -7,27 +8,27 @@
#include "utils/config.h" #include "utils/config.h"
void cws_signal_handler(int signo) { void cws_signal_handler(int signo) {
/* TODO */
fprintf(stdout, BLUE "[server] Cleaning up resources...\n" RESET); fprintf(stdout, BLUE "[server] Cleaning up resources...\n" RESET);
fprintf(stdout, BLUE "[server] Closing...\n" RESET); cws_server_run = false;
_exit(1);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int ret;
cws_config *config = cws_config_init(); cws_config *config = cws_config_init();
if (config == NULL) { if (config == NULL) {
fprintf(stderr, RED BOLD "[server] Unable to read config file\n" RESET); fprintf(stderr, RED BOLD "[server] Unable to read config file\n" RESET);
return 1; return 1;
} }
if (signal(SIGINT, cws_signal_handler) == SIG_ERR) { struct sigaction act = {
fprintf(stderr, BOLD RED "[server] Unable to setup signal()\n" RESET); .sa_handler = cws_signal_handler
return 1; };
} ret = sigaction(SIGINT, &act, NULL);
fprintf(stdout, BOLD GREEN "[server] Running cws on http://%s:%s...\n" RESET, config->host, config->port); 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) { if (ret < 0) {
fprintf(stderr, BOLD RED "[server] Unable to start web server\n" RESET); fprintf(stderr, BOLD RED "[server] Unable to start web server\n" RESET);
} }

View File

@@ -16,6 +16,8 @@
#include "utils/hashmap.h" #include "utils/hashmap.h"
#include "utils/utils.h" #include "utils/utils.h"
volatile bool cws_server_run = 1;
int cws_server_start(const char *hostname, const char *service) { int cws_server_start(const char *hostname, const char *service) {
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *res; 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)); struct epoll_event *revents = malloc(CWS_SERVER_EPOLL_MAXEVENTS * sizeof(struct epoll_event));
int client_fd; int client_fd;
while (1) { while (cws_server_run) {
int nfds = epoll_wait(epfd, revents, CWS_SERVER_EPOLL_MAXEVENTS, CWS_SERVER_EPOLL_TIMEOUT); int nfds = epoll_wait(epfd, revents, CWS_SERVER_EPOLL_MAXEVENTS, CWS_SERVER_EPOLL_TIMEOUT);
for (int i = 0; i < nfds; ++i) { for (int i = 0; i < nfds; ++i) {
@@ -144,11 +146,11 @@ void cws_server_loop(int sockfd) {
} }
/* Clean up everything */ /* Clean up everything */
/* TODO: fix endless loop using cli args */
free(revents); free(revents);
close(epfd); close(epfd);
cws_server_close_all_fds(clients); cws_server_close_all_fds(clients);
cws_hm_free(clients); cws_hm_free(clients);
fprintf(stdout, BLUE "[server] Closing...\n" RESET);
} }
void cws_epoll_add(int epfd, int sockfd, uint32_t events) { void cws_epoll_add(int epfd, int sockfd, uint32_t events) {