refactor: upgrade myclib dep

This commit is contained in:
2025-09-12 03:44:55 +02:00
parent 651b5e592b
commit fc21c740bd
8 changed files with 66 additions and 63 deletions

View File

@@ -8,6 +8,7 @@ A minimal web server. This is a personal project; it is not intended to be a pro
- libssl - libssl
- libcyaml - libcyaml
- libyaml - libyaml
- myclib (on my profile)
- [doxygen](https://www.doxygen.nl/) - [doxygen](https://www.doxygen.nl/)
- Optional, just to build the docs. It requires `dot`. - Optional, just to build the docs. It requires `dot`.

View File

@@ -1,10 +1,10 @@
#ifndef CWS_HTTP_H #ifndef CWS_HTTP_H
#define CWS_HTTP_H #define CWS_HTTP_H
#include <myclib/myhashmap.h>
#include <myclib/mystring.h>
#include <stddef.h> #include <stddef.h>
#include "myclib/hashmap/myhashmap.h"
#include "myclib/string/mystring.h"
#include "utils/config.h" #include "utils/config.h"
#define CWS_HTTP_HEADER_MAX 512 #define CWS_HTTP_HEADER_MAX 512
@@ -31,10 +31,10 @@ typedef enum cws_http_status_t {
typedef struct cws_http_t { typedef struct cws_http_t {
int sockfd; /**< Socket file descriptor */ int sockfd; /**< Socket file descriptor */
cws_http_method method; /**< HTTP request method */ cws_http_method method; /**< HTTP request method */
mcl_string *location; /**< Resource requested */ string_s *location; /**< Resource requested */
mcl_string *location_path; /**< Full resource path */ string_s *location_path; /**< Full resource path */
mcl_string *http_version; /**< HTTP version */ string_s *http_version; /**< HTTP version */
mcl_hashmap *headers; /**< Headers hash map */ hashmap_s *headers; /**< Headers hash map */
} cws_http; } cws_http;
/** /**
@@ -43,7 +43,7 @@ typedef struct cws_http_t {
* @param[in] request_str The http request sent to the server * @param[in] request_str The http request sent to the server
* @return Returns a http_t pointer to the request * @return Returns a http_t pointer to the request
*/ */
cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config); cws_http *cws_http_parse(string_s *request_str, int sockfd, cws_config *config);
int cws_http_get_content_type(cws_http *request, char *content_type); int cws_http_get_content_type(cws_http *request, char *content_type);

View File

@@ -1,11 +1,11 @@
#ifndef CWS_SERVER_H #ifndef CWS_SERVER_H
#define CWS_SERVER_H #define CWS_SERVER_H
#include <myclib/myhashmap.h>
#include <netdb.h> #include <netdb.h>
#include <signal.h> #include <signal.h>
#include <sys/socket.h> #include <sys/socket.h>
#include "myclib/hashmap/myhashmap.h"
#include "utils/config.h" #include "utils/config.h"
/* Clients max queue */ /* Clients max queue */
@@ -48,7 +48,7 @@ typedef enum cws_server_ret_t {
cws_server_ret cws_server_start(cws_config *config); cws_server_ret cws_server_start(cws_config *config);
cws_server_ret cws_server_loop(int server_fd, cws_config *config); cws_server_ret cws_server_loop(int server_fd, cws_config *config);
int cws_server_handle_new_client(int server_fd, mcl_hashmap *clients); int cws_server_handle_new_client(int server_fd, hashmap_s *clients);
int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size); int cws_server_accept_client(int server_fd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);
cws_server_ret cws_fd_set_nonblocking(int sockfd); cws_server_ret cws_fd_set_nonblocking(int sockfd);

View File

@@ -1,9 +1,9 @@
#ifndef CWS_WORKER_H #ifndef CWS_WORKER_H
#define CWS_WORKER_H #define CWS_WORKER_H
#include <myclib/myhashmap.h>
#include <pthread.h> #include <pthread.h>
#include "myclib/hashmap/myhashmap.h"
#include "server/server.h" #include "server/server.h"
typedef struct cws_worker_t { typedef struct cws_worker_t {
@@ -11,17 +11,17 @@ typedef struct cws_worker_t {
int pipefd[2]; int pipefd[2];
size_t clients_num; size_t clients_num;
pthread_t thread; pthread_t thread;
mcl_hashmap *clients; hashmap_s *clients;
cws_config *config; cws_config *config;
} cws_worker; } cws_worker;
cws_worker **cws_worker_init(size_t workers_num, mcl_hashmap *clients, cws_config *config); cws_worker **cws_worker_init(size_t workers_num, hashmap_s *clients, cws_config *config);
void cws_worker_free(cws_worker **workers, size_t workers_num); void cws_worker_free(cws_worker **workers, size_t workers_num);
void *cws_worker_loop(void *arg); void *cws_worker_loop(void *arg);
void cws_server_close_client(int epfd, int client_fd, mcl_hashmap *clients); void cws_server_close_client(int epfd, int client_fd, hashmap_s *clients);
cws_server_ret cws_epoll_add(int epfd, int sockfd, uint32_t events); cws_server_ret cws_epoll_add(int epfd, int sockfd, uint32_t events);
cws_server_ret cws_epoll_del(int epfd, int sockfd); cws_server_ret cws_epoll_del(int epfd, int sockfd);
cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, mcl_hashmap *clients, cws_config *config); cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, hashmap_s *clients, cws_config *config);
#endif #endif

View File

@@ -1,4 +1,4 @@
project('cws', 'c', version : '0.1.0') project('cws', 'c', version: '0.1.0')
cc = meson.get_compiler('c') cc = meson.get_compiler('c')
@@ -9,10 +9,12 @@ incdir = include_directories('include')
libssl = dependency('libssl') libssl = dependency('libssl')
libyaml = dependency('yaml-0.1') libyaml = dependency('yaml-0.1')
libcyaml = dependency('libcyaml') libcyaml = dependency('libcyaml')
libmath = cc.find_library('m', required : true) libmath = cc.find_library('m', required: true)
deps = [libssl, libyaml, libcyaml, libmath] libmyclib = cc.find_library('myclib', required: true)
add_global_arguments('-DUSE_COLORS', language : 'c') deps = [libssl, libyaml, libcyaml, libmath, libmyclib]
add_global_arguments('-DEVELOPER', language : 'c')
executable('cws', server + myclib, include_directories : incdir, dependencies : deps) add_global_arguments('-DUSE_COLORS', language: 'c')
add_global_arguments('-DEVELOPER', language: 'c')
executable('cws', server, include_directories: incdir, dependencies: deps)

View File

@@ -17,9 +17,9 @@ static void cws_http_init(cws_http **request) {
return; return;
} }
(*request)->http_version = mcl_string_new("", 16); (*request)->http_version = string_new("", 16);
(*request)->location = mcl_string_new("", 128); (*request)->location = string_new("", 128);
(*request)->location_path = mcl_string_new("", 512); (*request)->location_path = string_new("", 512);
} }
static int cws_http_parse_method(cws_http *request, const char *method) { static int cws_http_parse_method(cws_http *request, const char *method) {
@@ -36,7 +36,7 @@ static int cws_http_parse_method(cws_http *request, const char *method) {
return -1; return -1;
} }
cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config) { cws_http *cws_http_parse(string_s *request_str, int sockfd, cws_config *config) {
if (!request_str || !config) { if (!request_str || !config) {
return NULL; return NULL;
} }
@@ -49,9 +49,9 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
request->sockfd = sockfd; request->sockfd = sockfd;
/* Prepare the virtual_host struct */ /* Prepare the virtual_host struct */
cws_virtual_host vhost; // TODO: cws_virtual_host vhost;
char *request_str_cpy = strdup(mcl_string_cstr(request_str)); char *request_str_cpy = strdup(string_cstr(request_str));
if (!request_str_cpy) { if (!request_str_cpy) {
free(request); free(request);
@@ -91,14 +91,14 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
return NULL; return NULL;
} }
// CWS_LOG_DEBUG("location: %s", pch); // CWS_LOG_DEBUG("location: %s", pch);
mcl_string_append(request->location, pch); string_append(request->location, pch);
// TODO: mcl_string_append(request->location_path, config->www); // TODO: mcl_string_append(request->location_path, config->www);
/* Adjust location path */ /* Adjust location path */
if (strcmp(mcl_string_cstr(request->location), "/") == 0) { if (strcmp(string_cstr(request->location), "/") == 0) {
mcl_string_append(request->location_path, "/index.html"); string_append(request->location_path, "/index.html");
} else { } else {
mcl_string_append(request->location_path, mcl_string_cstr(request->location)); string_append(request->location_path, string_cstr(request->location));
} }
// CWS_LOG_DEBUG("location path: %s", mcl_string_cstr(request->location_path)); // CWS_LOG_DEBUG("location path: %s", mcl_string_cstr(request->location_path));
@@ -111,11 +111,11 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
return NULL; return NULL;
} }
// CWS_LOG_DEBUG("version: %s", pch); // CWS_LOG_DEBUG("version: %s", pch);
mcl_string_append(request->http_version, pch); string_append(request->http_version, pch);
/* Parse headers until a \r\n */ /* Parse headers until a \r\n */
request->headers = mcl_hm_init(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn, sizeof(char) * CWS_HTTP_HEADER_MAX, request->headers =
sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX); hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn, sizeof(char) * CWS_HTTP_HEADER_MAX, sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX);
char *header_colon; char *header_colon;
while (pch) { while (pch) {
/* Get header line */ /* Get header line */
@@ -141,7 +141,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
strncpy(hv, hvalue, sizeof(hv)); strncpy(hv, hvalue, sizeof(hv));
// CWS_LOG_DEBUG("hkey: %s -> %s", hk, hv); // CWS_LOG_DEBUG("hkey: %s -> %s", hk, hv);
mcl_hm_set(request->headers, hk, hv); hm_set(request->headers, hk, hv);
} }
/* TODO: Parse body */ /* TODO: Parse body */
@@ -219,7 +219,7 @@ int cws_http_send_resource(cws_http *request) {
/* keep-alive by default */ /* keep-alive by default */
int keepalive = 1; int keepalive = 1;
FILE *file = fopen(mcl_string_cstr(request->location_path), "rb"); FILE *file = fopen(string_cstr(request->location_path), "rb");
if (file == NULL) { if (file == NULL) {
cws_http_send_response(request, CWS_HTTP_NOT_FOUND); cws_http_send_response(request, CWS_HTTP_NOT_FOUND);
return 0; return 0;
@@ -260,12 +260,12 @@ int cws_http_send_resource(cws_http *request) {
/* Check for keep-alive */ /* Check for keep-alive */
char conn[32] = "keep-alive"; char conn[32] = "keep-alive";
mcl_bucket *connection = mcl_hm_get(request->headers, "Connection"); bucket_s *connection = hm_get(request->headers, "Connection");
if (connection && strcmp((char *)connection->value, "keep-alive") == 0) { if (connection && strcmp((char *)connection->value, "keep-alive") == 0) {
strcpy(conn, "keep-alive"); strcpy(conn, "keep-alive");
keepalive = 0; keepalive = 0;
} }
mcl_hm_free_bucket(connection); hm_free_bucket(connection);
char *response = NULL; char *response = NULL;
size_t response_len = cws_http_response_builder(&response, "HTTP/1.1", CWS_HTTP_OK, content_type, conn, file_data, content_length); size_t response_len = cws_http_response_builder(&response, "HTTP/1.1", CWS_HTTP_OK, content_type, conn, file_data, content_length);
@@ -295,7 +295,7 @@ int cws_http_send_resource(cws_http *request) {
} }
int cws_http_get_content_type(cws_http *request, char *content_type) { int cws_http_get_content_type(cws_http *request, char *content_type) {
char *ptr = strrchr(mcl_string_cstr(request->location_path), '.'); char *ptr = strrchr(string_cstr(request->location_path), '.');
if (ptr == NULL) { if (ptr == NULL) {
return -1; return -1;
} }
@@ -334,12 +334,12 @@ void cws_http_send_simple_html(cws_http *request, cws_http_status status, char *
size_t body_len = strlen(body); size_t body_len = strlen(body);
char conn[32] = "keep-alive"; char conn[32] = "keep-alive";
mcl_bucket *connection = mcl_hm_get(request->headers, "Connection"); bucket_s *connection = hm_get(request->headers, "Connection");
if (connection) { if (connection) {
strncpy(conn, (char *)connection->value, sizeof(conn) - 1); strncpy(conn, (char *)connection->value, sizeof(conn) - 1);
conn[sizeof(conn) - 1] = '\0'; conn[sizeof(conn) - 1] = '\0';
} }
mcl_hm_free_bucket(connection); hm_free_bucket(connection);
char *response = NULL; char *response = NULL;
size_t response_len = cws_http_response_builder(&response, "HTTP/1.1", status, "text/html", conn, body, body_len); size_t response_len = cws_http_response_builder(&response, "HTTP/1.1", status, "text/html", conn, body, body_len);
@@ -354,9 +354,9 @@ void cws_http_send_simple_html(cws_http *request, cws_http_status status, char *
} }
void cws_http_free(cws_http *request) { void cws_http_free(cws_http *request) {
mcl_hm_free(request->headers); hm_free(request->headers);
mcl_string_free(request->http_version); string_free(request->http_version);
mcl_string_free(request->location); string_free(request->location);
mcl_string_free(request->location_path); string_free(request->location_path);
free(request); free(request);
} }

View File

@@ -108,7 +108,7 @@ cws_server_ret cws_server_loop(int server_fd, cws_config *config) {
return ret; return ret;
} }
mcl_hashmap *clients = mcl_hm_init(my_int_hash_fn, my_int_equal_fn, my_int_free_key_fn, my_str_free_fn, sizeof(int), sizeof(char) * INET_ADDRSTRLEN); hashmap_s *clients = hm_new(my_int_hash_fn, my_int_equal_fn, my_int_free_key_fn, my_str_free_fn, sizeof(int), sizeof(char) * INET_ADDRSTRLEN);
if (clients == NULL) { if (clients == NULL) {
return CWS_SERVER_HASHMAP_INIT; return CWS_SERVER_HASHMAP_INIT;
} }
@@ -117,7 +117,7 @@ cws_server_ret cws_server_loop(int server_fd, cws_config *config) {
size_t workers_index = 0; size_t workers_index = 0;
cws_worker **workers = cws_worker_init(workers_num, clients, config); cws_worker **workers = cws_worker_init(workers_num, clients, config);
if (workers == NULL) { if (workers == NULL) {
mcl_hm_free(clients); hm_free(clients);
return CWS_SERVER_WORKER_ERROR; return CWS_SERVER_WORKER_ERROR;
} }
@@ -145,7 +145,7 @@ cws_server_ret cws_server_loop(int server_fd, cws_config *config) {
/* Add client to worker */ /* Add client to worker */
int random = 10; int random = 10;
mcl_hm_set(clients, &client_fd, &random); hm_set(clients, &client_fd, &random);
write(workers[workers_index]->pipefd[1], &client_fd, sizeof(int)); write(workers[workers_index]->pipefd[1], &client_fd, sizeof(int));
workers_index = (workers_index + 1) % workers_num; workers_index = (workers_index + 1) % workers_num;
} }
@@ -154,12 +154,12 @@ cws_server_ret cws_server_loop(int server_fd, cws_config *config) {
close(epfd); close(epfd);
cws_worker_free(workers, workers_num); cws_worker_free(workers, workers_num);
mcl_hm_free(clients); hm_free(clients);
return CWS_SERVER_OK; return CWS_SERVER_OK;
} }
int cws_server_handle_new_client(int server_fd, mcl_hashmap *clients) { int cws_server_handle_new_client(int server_fd, hashmap_s *clients) {
struct sockaddr_storage their_sa; struct sockaddr_storage their_sa;
socklen_t theirsa_size = sizeof their_sa; socklen_t theirsa_size = sizeof their_sa;
char ip[INET_ADDRSTRLEN]; char ip[INET_ADDRSTRLEN];

View File

@@ -34,7 +34,7 @@ static int cws_worker_setup_epoll(cws_worker *worker) {
return 0; return 0;
} }
cws_worker **cws_worker_init(size_t workers_num, mcl_hashmap *clients, cws_config *config) { cws_worker **cws_worker_init(size_t workers_num, hashmap_s *clients, cws_config *config) {
cws_worker **workers = malloc(sizeof(cws_worker) * workers_num); cws_worker **workers = malloc(sizeof(cws_worker) * workers_num);
if (workers == NULL) { if (workers == NULL) {
return NULL; return NULL;
@@ -117,12 +117,12 @@ void *cws_worker_loop(void *arg) {
return NULL; return NULL;
} }
void cws_server_close_client(int epfd, int client_fd, mcl_hashmap *clients) { void cws_server_close_client(int epfd, int client_fd, hashmap_s *clients) {
mcl_bucket *client = mcl_hm_get(clients, &client_fd); bucket_s *client = hm_get(clients, &client_fd);
if (client) { if (client) {
cws_epoll_del(epfd, client_fd); cws_epoll_del(epfd, client_fd);
mcl_hm_remove(clients, &client_fd); hm_remove(clients, &client_fd);
mcl_hm_free_bucket(client); hm_free_bucket(client);
} }
} }
@@ -151,12 +151,12 @@ cws_server_ret cws_epoll_del(int epfd, int sockfd) {
return CWS_SERVER_OK; return CWS_SERVER_OK;
} }
static size_t cws_read_data(int sockfd, mcl_string **str) { static size_t cws_read_data(int sockfd, string_s **str) {
size_t total_bytes = 0; size_t total_bytes = 0;
ssize_t bytes_read; ssize_t bytes_read;
if (*str == NULL) { if (*str == NULL) {
*str = mcl_string_new("", 4096); *str = string_new("", 4096);
} }
char tmp[4096]; char tmp[4096];
@@ -180,19 +180,19 @@ static size_t cws_read_data(int sockfd, mcl_string **str) {
} }
total_bytes += bytes_read; total_bytes += bytes_read;
mcl_string_append(*str, tmp); string_append(*str, tmp);
} }
return total_bytes; return total_bytes;
} }
cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, mcl_hashmap *clients, cws_config *config) { cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, hashmap_s *clients, cws_config *config) {
/* Read data from socket */ /* Read data from socket */
mcl_string *data = NULL; string_s *data = NULL;
size_t total_bytes = cws_read_data(client_fd, &data); size_t total_bytes = cws_read_data(client_fd, &data);
if (total_bytes <= 0) { if (total_bytes <= 0) {
if (data) { if (data) {
mcl_string_free(data); string_free(data);
} }
cws_server_close_client(epfd, client_fd, clients); cws_server_close_client(epfd, client_fd, clients);
@@ -201,7 +201,7 @@ cws_server_ret cws_server_handle_client_data(int epfd, int client_fd, mcl_hashma
/* Parse HTTP request */ /* Parse HTTP request */
cws_http *request = cws_http_parse(data, client_fd, config); cws_http *request = cws_http_parse(data, client_fd, config);
mcl_string_free(data); string_free(data);
if (request == NULL) { if (request == NULL) {
cws_server_close_client(epfd, client_fd, clients); cws_server_close_client(epfd, client_fd, clients);