refactoring server and add myclib

This commit is contained in:
2025-08-02 13:35:56 +02:00
parent fa964b2620
commit 2918aeadcb
14 changed files with 505 additions and 292 deletions

View File

@@ -191,11 +191,11 @@ void cws_http_send_response(cws_http *request, cws_http_status status) {
}
}
void cws_http_send_resource(cws_http *request) {
int cws_http_send_resource(cws_http *request) {
FILE *file = fopen(request->location_path, "rb");
if (file == NULL) {
cws_http_send_response(request, CWS_HTTP_NOT_FOUND);
return;
return -1;
}
/* Retrieve correct Content-Type */
@@ -215,7 +215,7 @@ void cws_http_send_resource(cws_http *request) {
if (file_data == NULL) {
fclose(file);
CWS_LOG_ERROR("Unable to allocate file data");
return;
return -1;
}
/* Read 1 byte until content_length from file and put in file_data */
@@ -225,7 +225,7 @@ void cws_http_send_resource(cws_http *request) {
if (read_bytes != content_length) {
free(file_data);
CWS_LOG_ERROR("Partial read from file");
return;
return -1;
}
char conn[32] = "close";
@@ -238,13 +238,16 @@ void cws_http_send_resource(cws_http *request) {
size_t response_len = cws_http_response_builder(&response, "HTTP/1.1", CWS_HTTP_OK, content_type, conn, file_data, content_length);
size_t bytes_sent = 0;
size_t sent;
do {
size_t sent = send(request->sockfd, response + bytes_sent, response_len, 0);
sent = send(request->sockfd, response + bytes_sent, response_len, 0);
bytes_sent += sent;
} while (bytes_sent < response_len);
} while (bytes_sent < response_len && sent != 0);
free(response);
free(file_data);
return 0;
}
int cws_http_get_content_type(cws_http *request, char *content_type) {

View File

@@ -1,3 +1,3 @@
server = files('main.c', 'server/server.c')
server += files('utils/utils.c', 'utils/hashmap.c', 'utils/config.c')
server += files('utils/utils.c', 'utils/config.c')
server += files('http/http.c')

View File

@@ -81,15 +81,12 @@ int cws_server_start(cws_config *config) {
}
int cws_server_loop(int sockfd, cws_config *config) {
struct sockaddr_storage their_sa;
socklen_t theirsa_size = sizeof their_sa;
int ret;
mcl_hashmap *clients = mcl_hm_init(my_int_hash_fn, my_int_equal_fn, NULL, my_int_free_fn);
mcl_hashmap *clients = mcl_hm_init(my_int_hash_fn, my_int_equal_fn, my_int_free_key_fn, my_str_free_fn);
if (!clients) {
return -1;
}
int ret = 0;
int epfd = epoll_create1(0);
ret = cws_fd_set_nonblocking(sockfd);
@@ -116,69 +113,20 @@ int cws_server_loop(int sockfd, cws_config *config) {
return -1;
}
int client_fd;
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) {
if (revents[i].data.fd == sockfd) {
/* New client */
char ip[INET_ADDRSTRLEN];
client_fd = cws_server_accept_client(sockfd, &their_sa, &theirsa_size);
if (client_fd < 0) {
continue;
ret = cws_server_handle_new_client(sockfd, epfd, clients);
if (ret < 0) {
CWS_LOG_DEBUG("handle new client error");
}
cws_utils_get_client_ip(&their_sa, ip);
CWS_LOG_INFO("Client (%s) connected", ip);
cws_fd_set_nonblocking(client_fd);
cws_epoll_add(epfd, client_fd, EPOLLIN);
mcl_hm_set(clients, &client_fd, &their_sa);
} else {
char data[4096] = {0};
char ip[INET_ADDRSTRLEN] = {0};
/* Incoming data */
client_fd = revents[i].data.fd;
const ssize_t bytes_read = recv(client_fd, data, sizeof data, 0);
/* Retrieve client ip */
mcl_bucket *client = mcl_hm_get(clients, &client_fd);
struct sockaddr_storage client_sas = *(struct sockaddr_storage *)client->value;
cws_utils_get_client_ip(&client_sas, ip);
if (bytes_read == 0) {
/* Client disconnected */
CWS_LOG_INFO("Client (%s) disconnected", ip);
cws_server_close_client(epfd, client_fd, clients);
continue;
ret = cws_server_handle_client_data(revents[i].data.fd, epfd, clients, config);
if (ret < 0) {
CWS_LOG_DEBUG("handle client data error");
}
if (bytes_read < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
/* Error during read, handle it (close client) */
CWS_LOG_INFO("Client (%s) disconnected (error)", ip);
cws_server_close_client(epfd, client_fd, clients);
}
continue;
}
/* Parse HTTP request */
cws_http *request = cws_http_parse(data, client_fd, config);
if (request == NULL) {
cws_server_close_client(epfd, client_fd, clients);
continue;
}
cws_http_send_resource(request);
cws_http_free(request);
/* Clear str */
memset(data, 0, sizeof data);
}
}
}
@@ -187,7 +135,90 @@ int cws_server_loop(int sockfd, cws_config *config) {
free(revents);
close(epfd);
mcl_hm_free(clients);
CWS_LOG_INFO("Closing...");
return 0;
}
int cws_server_handle_new_client(int sockfd, int epfd, mcl_hashmap *clients) {
struct sockaddr_storage their_sa;
socklen_t theirsa_size = sizeof their_sa;
char ip[INET_ADDRSTRLEN];
int client_fd = cws_server_accept_client(sockfd, &their_sa, &theirsa_size);
if (client_fd < 0) {
return -1;
}
cws_utils_get_client_ip(&their_sa, ip);
CWS_LOG_INFO("Client (%s) (fd: %d) connected", ip, client_fd);
cws_fd_set_nonblocking(client_fd);
cws_epoll_add(epfd, client_fd, EPOLLIN);
int *key = malloc(sizeof(int));
*key = client_fd;
struct sockaddr_storage *value = malloc(sizeof(struct sockaddr_storage));
*value = their_sa;
mcl_hm_set(clients, key, value);
return 0;
}
int cws_server_handle_client_data(int client_fd, int epfd, mcl_hashmap *clients, cws_config *config) {
char data[4096] = {0};
char ip[INET_ADDRSTRLEN] = {0};
/* Incoming data */
const ssize_t bytes_read = recv(client_fd, data, sizeof(data), 0);
/* Retrieve client ip */
int *client_fd_key = malloc(sizeof(int));
*client_fd_key = client_fd;
mcl_bucket *client = mcl_hm_get(clients, client_fd_key);
free(client_fd_key);
if (!client) {
CWS_LOG_ERROR("Client fd %d not found in hashmap", client_fd);
cws_epoll_del(epfd, client_fd);
close(client_fd);
return -1;
}
struct sockaddr_storage client_sas = *(struct sockaddr_storage *)client->value;
cws_utils_get_client_ip(&client_sas, ip);
if (bytes_read == 0) {
/* Client disconnected */
CWS_LOG_INFO("Client (%s) disconnected", ip);
cws_server_close_client(epfd, client_fd, clients);
return -1;
}
if (bytes_read < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
/* Error during read, handle it (close client) */
CWS_LOG_INFO("Client (%s) disconnected (error)", ip);
cws_server_close_client(epfd, client_fd, clients);
}
return -1;
}
/* Parse HTTP request */
cws_http *request = cws_http_parse(data, client_fd, config);
if (request == NULL) {
CWS_LOG_INFO("Client (%s) disconnected (request NULL)", ip);
cws_server_close_client(epfd, client_fd, clients);
return -1;
}
cws_http_send_resource(request);
cws_http_free(request);
return 0;
}
@@ -243,5 +274,9 @@ int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, sock
void cws_server_close_client(int epfd, int client_fd, mcl_hashmap *hashmap) {
cws_epoll_del(epfd, client_fd);
mcl_hm_remove(hashmap, &client_fd);
int *key_to_find = malloc(sizeof(int));
*key_to_find = client_fd;
mcl_hm_remove(hashmap, key_to_find);
free(key_to_find);
}

View File

@@ -1,215 +0,0 @@
#include "utils/hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
mcl_hashmap *mcl_hm_init(mcl_hash_fn *hash_fn, mcl_equal_fn *equal_fn, mcl_free_key_fn *free_key_fn, mcl_free_value_fn *free_value_fn) {
/* Allocate memory for hash map struct */
mcl_hashmap *hashmap = malloc(sizeof(mcl_hashmap));
if (hashmap == NULL) {
return NULL;
}
/* Initialize hash map with given parameters */
hashmap->hash_fn = hash_fn;
hashmap->equal_fn = equal_fn;
hashmap->free_key_fn = free_key_fn;
hashmap->free_value_fn = free_value_fn;
/* Clear all buckets in the map */
memset(hashmap->map, 0, sizeof(hashmap->map));
return hashmap;
}
void mcl_hm_free(mcl_hashmap *hashmap) {
if (hashmap == NULL) {
return;
}
/* Iterate through all buckets in the hash map */
for (size_t i = 0; i < MYCLIB_HASHMAP_SIZE; ++i) {
mcl_bucket *bucket = &hashmap->map[i];
/* Free the first bucket if it contains data */
if (bucket->key != NULL) {
if (hashmap->free_key_fn != NULL) {
hashmap->free_key_fn(bucket->key);
}
if (hashmap->free_value_fn != NULL) {
hashmap->free_value_fn(bucket->value);
}
}
/* Free all chained buckets */
bucket = bucket->next;
while (bucket != NULL) {
if (hashmap->free_key_fn != NULL) {
hashmap->free_key_fn(bucket->key);
}
if (hashmap->free_value_fn != NULL) {
hashmap->free_value_fn(bucket->value);
}
mcl_bucket *next = bucket->next;
free(bucket);
bucket = next;
}
}
/* Free the hash map structure itself */
free(hashmap);
}
bool mcl_hm_set(mcl_hashmap *hashmap, void *key, void *value) {
/* Validate input parameters */
if (hashmap == NULL || key == NULL) {
return false;
}
/* Calculate hash index for the key */
int index = hashmap->hash_fn(key) % MYCLIB_HASHMAP_SIZE;
mcl_bucket *bucket = &hashmap->map[index];
/* If bucket is empty, insert new key-value pair */
if (bucket->key == NULL) {
bucket->key = key;
bucket->value = value;
bucket->next = NULL;
return true;
}
/* Check if first bucket has the same key */
if (hashmap->equal_fn(bucket->key, key)) {
/* Update existing value, free old value if needed */
if (hashmap->free_value_fn != NULL && bucket->value != NULL) {
hashmap->free_value_fn(bucket->value);
}
bucket->value = value;
return true;
}
/* Search through the collision chain */
mcl_bucket *current = bucket->next;
while (current != NULL) {
if (hashmap->equal_fn(current->key, key)) {
/* Update existing value, free old value if needed */
if (hashmap->free_value_fn != NULL && current->value != NULL) {
hashmap->free_value_fn(current->value);
}
current->value = value;
return true;
}
current = current->next;
}
/* Key not found, create new bucket and add to chain */
mcl_bucket *new_bucket = malloc(sizeof(mcl_bucket));
if (new_bucket == NULL) {
return false; /* Memory allocation failed */
}
/* Initialize new bucket and insert at head of chain */
new_bucket->key = key;
new_bucket->value = value;
new_bucket->next = bucket->next;
bucket->next = new_bucket;
return true;
}
mcl_bucket *mcl_hm_get(mcl_hashmap *hashmap, void *key) {
/* Validate input parameters */
if (hashmap == NULL || key == NULL) {
return NULL;
}
/* Calculate hash index for the key */
int index = hashmap->hash_fn(key) % MYCLIB_HASHMAP_SIZE;
mcl_bucket *bucket = &hashmap->map[index];
/* Return NULL if bucket is empty */
if (bucket->key == NULL) {
return NULL;
}
/* Search through the collision chain */
while (bucket != NULL) {
if (hashmap->equal_fn(bucket->key, key)) {
return bucket; /* Key found */
}
bucket = bucket->next;
}
/* Key not found */
return NULL;
}
static void mcl_free_bucket_content(mcl_hashmap *hashmap, mcl_bucket *bucket) {
/* Free key if free function is provided */
if (hashmap->free_key_fn != NULL && bucket->key != NULL) {
hashmap->free_key_fn(bucket->key);
}
/* Free value if free function is provided */
if (hashmap->free_value_fn != NULL && bucket->value != NULL) {
hashmap->free_value_fn(bucket->value);
}
}
bool mcl_hm_remove(mcl_hashmap *hashmap, void *key) {
/* Validate input parameters */
if (hashmap == NULL || key == NULL) {
return false;
}
/* Calculate hash index for the key */
int index = hashmap->hash_fn(key) % MYCLIB_HASHMAP_SIZE;
mcl_bucket *bucket = &hashmap->map[index];
/* Return false if bucket is empty */
if (bucket->key == NULL) {
return false;
}
/* Check if first bucket contains the key to remove */
if (hashmap->equal_fn(bucket->key, key)) {
/* Free the content of the bucket */
mcl_free_bucket_content(hashmap, bucket);
if (bucket->next != NULL) {
/* Move next bucket's content to first bucket and free the next bucket */
mcl_bucket *to_free = bucket->next;
bucket->key = to_free->key;
bucket->value = to_free->value;
bucket->next = to_free->next;
free(to_free);
} else {
/* No next bucket, mark first bucket as empty */
bucket->key = NULL;
bucket->value = NULL;
bucket->next = NULL;
}
return true;
}
/* Search through the collision chain */
mcl_bucket *prev = bucket;
mcl_bucket *current = bucket->next;
while (current != NULL) {
if (hashmap->equal_fn(current->key, key)) {
/* Key found, free content and unlink bucket */
mcl_free_bucket_content(hashmap, current);
prev->next = current->next;
free(current);
return true;
}
prev = current;
current = current->next;
}
/* Key not found */
return false;
}

View File

@@ -97,7 +97,8 @@ bool my_int_equal_fn(const void *a, const void *b) {
return false;
}
void my_int_free_fn(void *value) {
int fd = *(int *)value;
void my_int_free_key_fn(void *key) {
int fd = *(int *)key;
close(fd);
free(key);
}