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()) */
#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
*

View File

@@ -8,7 +8,7 @@
#define CWS_HASHMAP_MAX_CLIENTS 1024
/**
* @brief Hash map struct
* @brief Client Hashmap struct
*
*/
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);
/* 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[] =
"<html>\n"
"<head>\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"
"</body>\n"
"</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);
}

View File

@@ -1,4 +1,5 @@
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
@@ -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);
}

View File

@@ -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) {