refactor(config): add default domain pointer

This commit is contained in:
2026-03-25 00:54:53 +01:00
parent b2b1de6495
commit 0de6dfda58
3 changed files with 31 additions and 18 deletions
+1
View File
@@ -22,6 +22,7 @@ typedef struct cws_config {
int workers;
cws_vhost_s *virtual_hosts;
unsigned virtual_hosts_count;
cws_vhost_s *default_vh;
} cws_config_s;
cws_config_s *cws_config_init(void);
+28 -15
View File
@@ -6,23 +6,44 @@
#include "utils/debug.h"
static bool is_default(const char *domain) {
if (!strcmp(domain, "default")) {
return true;
}
return false;
}
static bool parse_vhosts(cws_config_s *config, toml_result_t result) {
toml_datum_t vhosts = toml_seek(result.toptab, "virtual_hosts");
/* Retrieve virtual hosts counter */
config->virtual_hosts_count = vhosts.u.arr.size;
/* Allocate virtual hosts array */
config->virtual_hosts = malloc(sizeof *config->virtual_hosts * config->virtual_hosts_count);
if (!config->virtual_hosts) {
return false;
}
/* Iterate for each virtual host */
for (int i = 0; i < vhosts.u.arr.size; ++i) {
cws_vhost_s *vh = &config->virtual_hosts[i];
toml_datum_t elem = vhosts.u.arr.elem[i];
/* Retrieve vh's domain */
toml_datum_t domain = toml_seek(elem, "domain");
vh->domain = strdup(domain.u.str.ptr);
if (!vh->domain) {
return false;
}
/* Check if vh->domain is the default domain */
if (is_default(vh->domain)) {
config->default_vh = vh;
}
/* Retrieve vh's root folder */
toml_datum_t root = toml_seek(elem, "root");
vh->root = strdup(root.u.str.ptr);
if (!vh->root) {
@@ -32,13 +53,17 @@ static bool parse_vhosts(cws_config_s *config, toml_result_t result) {
/* Pages */
toml_datum_t pages = toml_seek(elem, "pages");
vh->error_pages_count = pages.u.arr.size;
/* Allocate error pages array */
vh->error_pages = malloc(sizeof *vh->error_pages * vh->error_pages_count);
if (!vh->error_pages) {
return false;
}
/* Iterate for each page */
for (int j = 0; j < pages.u.arr.size; ++j) {
toml_datum_t page = pages.u.arr.elem[i];
toml_datum_t status = toml_seek(page, "status");
vh->error_pages[j].status = strdup(status.u.str.ptr);
if (!vh->error_pages[i].status) {
@@ -56,17 +81,6 @@ static bool parse_vhosts(cws_config_s *config, toml_result_t result) {
return true;
}
static bool find_default(cws_config_s *config) {
for (unsigned i = 0; i < config->virtual_hosts_count; ++i) {
cws_vhost_s *vh = config->virtual_hosts;
if (!strcmp(vh[i].domain, "default")) {
return true;
}
}
return false;
}
static bool parse_toml(cws_config_s *config) {
const char *path = "config.toml";
@@ -98,15 +112,14 @@ static bool parse_toml(cws_config_s *config) {
toml_datum_t workers = toml_seek(result.toptab, "server.workers");
config->workers = workers.u.int64;
parse_vhosts(config, result);
bool ret = parse_vhosts(config, result);
toml_free(result);
return find_default(config);
return ret;
}
cws_config_s *cws_config_init(void) {
cws_config_s *config = malloc(sizeof *config);
cws_config_s *config = calloc(1, sizeof *config);
if (!config) {
return NULL;
}
+2 -3
View File
@@ -35,9 +35,8 @@ static cws_vhost_s *get_vhost(cws_config_s *config, char *host) {
}
}
/* Return first domain */
/* TODO: return default domain */
return &config->virtual_hosts[0];
/* Return default domain */
return config->default_vh;
}
static cws_return worker_handle_client_data(int epfd, int client_fd, cws_config_s *config) {