Compare commits
4 Commits
cebb05a834
...
8f47a0b666
| Author | SHA1 | Date | |
|---|---|---|---|
| 8f47a0b666 | |||
| 15d6ae6829 | |||
| 7f054bb02e | |||
| c88b0823fc |
@@ -4,6 +4,7 @@ host = "localhost"
|
||||
port = "3030"
|
||||
# Root folder in case there are no virtual hosts
|
||||
root = "www"
|
||||
workers = 6
|
||||
|
||||
[[virtual_hosts]]
|
||||
# "default" domain is required
|
||||
|
||||
@@ -19,6 +19,7 @@ typedef struct cws_config {
|
||||
char *host;
|
||||
char *port;
|
||||
char *root;
|
||||
int workers;
|
||||
cws_vhost_s *virtual_hosts;
|
||||
unsigned virtual_hosts_count;
|
||||
} cws_config_s;
|
||||
|
||||
@@ -13,17 +13,11 @@
|
||||
#define CWS_SERVER_BACKLOG 128
|
||||
|
||||
/* Max number of epoll events processed per iteration */
|
||||
#define CWS_SERVER_EPOLL_MAXEVENTS 64
|
||||
#define CWS_SERVER_EPOLL_MAXEVENTS 128
|
||||
|
||||
/* Blocking timeout for epoll_wait in ms */
|
||||
#define CWS_SERVER_EPOLL_TIMEOUT 3000
|
||||
|
||||
/* Maximum allowed HTTP request size */
|
||||
#define CWS_SERVER_MAX_REQUEST_SIZE (16 * 1024) /* 16KB */
|
||||
|
||||
/* Number of worker threads */
|
||||
#define CWS_WORKERS_NUM 6
|
||||
|
||||
/* Global flag used to stop server */
|
||||
extern volatile sig_atomic_t cws_server_run;
|
||||
|
||||
|
||||
+1
-1
@@ -21,6 +21,6 @@ deps = [libtomlc17, libmath, libmyclib]
|
||||
|
||||
add_global_arguments('-DUSE_COLORS', language: 'c')
|
||||
add_global_arguments('-DEVELOPER', language: 'c')
|
||||
add_global_arguments('-D_POSIX_C_SOURCE=200112L', language: 'c')
|
||||
add_global_arguments('-D_POSIX_C_SOURCE=200809L', language: 'c')
|
||||
|
||||
executable('cws', server, include_directories: incdir, dependencies: deps)
|
||||
|
||||
+11
-23
@@ -2,26 +2,11 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string.h>
|
||||
#include <tomlc17.h>
|
||||
|
||||
#include "utils/debug.h"
|
||||
|
||||
static char *cws_strdup(const char *str) {
|
||||
if (!str) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t len = strlen(str) + 1;
|
||||
char *copy = malloc(sizeof *copy * len);
|
||||
if (!copy) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(copy, str, len);
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
static bool parse_vhosts(cws_config_s *config, toml_result_t result) {
|
||||
toml_datum_t vhosts = toml_seek(result.toptab, "virtual_hosts");
|
||||
config->virtual_hosts_count = vhosts.u.arr.size;
|
||||
@@ -34,13 +19,13 @@ static bool parse_vhosts(cws_config_s *config, toml_result_t result) {
|
||||
cws_vhost_s *vh = &config->virtual_hosts[i];
|
||||
toml_datum_t elem = vhosts.u.arr.elem[i];
|
||||
toml_datum_t domain = toml_seek(elem, "domain");
|
||||
vh->domain = cws_strdup(domain.u.str.ptr);
|
||||
vh->domain = strdup(domain.u.str.ptr);
|
||||
if (!vh->domain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
toml_datum_t root = toml_seek(elem, "root");
|
||||
vh->root = cws_strdup(root.u.str.ptr);
|
||||
vh->root = strdup(root.u.str.ptr);
|
||||
if (!vh->root) {
|
||||
return false;
|
||||
}
|
||||
@@ -56,13 +41,13 @@ static bool parse_vhosts(cws_config_s *config, toml_result_t result) {
|
||||
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[i].status = cws_strdup(status.u.str.ptr);
|
||||
vh->error_pages[j].status = strdup(status.u.str.ptr);
|
||||
if (!vh->error_pages[i].status) {
|
||||
return false;
|
||||
}
|
||||
|
||||
toml_datum_t path = toml_seek(page, "path");
|
||||
vh->error_pages[i].path = cws_strdup(path.u.str.ptr);
|
||||
vh->error_pages[j].path = strdup(path.u.str.ptr);
|
||||
if (!vh->error_pages[i].path) {
|
||||
return false;
|
||||
}
|
||||
@@ -94,23 +79,26 @@ static bool parse_toml(cws_config_s *config) {
|
||||
}
|
||||
|
||||
toml_datum_t host = toml_seek(result.toptab, "server.host");
|
||||
config->host = cws_strdup(host.u.str.ptr);
|
||||
config->host = strdup(host.u.str.ptr);
|
||||
if (!config->host) {
|
||||
return false;
|
||||
}
|
||||
|
||||
toml_datum_t port = toml_seek(result.toptab, "server.port");
|
||||
config->port = cws_strdup(port.u.str.ptr);
|
||||
config->port = strdup(port.u.str.ptr);
|
||||
if (!config->port) {
|
||||
return false;
|
||||
}
|
||||
|
||||
toml_datum_t root = toml_seek(result.toptab, "server.root");
|
||||
config->root = cws_strdup(root.u.str.ptr);
|
||||
config->root = strdup(root.u.str.ptr);
|
||||
if (!config->root) {
|
||||
return false;
|
||||
}
|
||||
|
||||
toml_datum_t workers = toml_seek(result.toptab, "server.workers");
|
||||
config->workers = workers.u.int64;
|
||||
|
||||
parse_vhosts(config, result);
|
||||
|
||||
toml_free(result);
|
||||
|
||||
+4
-4
@@ -89,7 +89,7 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
server->workers = cws_worker_new(CWS_WORKERS_NUM, config);
|
||||
server->workers = cws_worker_new(config->workers, config);
|
||||
if (server->workers == NULL) {
|
||||
return CWS_WORKER_ERROR;
|
||||
}
|
||||
@@ -104,7 +104,7 @@ cws_return cws_server_start(cws_server_s *server) {
|
||||
size_t workers_index = 0;
|
||||
|
||||
while (cws_server_run) {
|
||||
int nfds = epoll_wait(server->epfd, events, 128, -1);
|
||||
int nfds = epoll_wait(server->epfd, events, CWS_SERVER_EPOLL_MAXEVENTS, CWS_SERVER_EPOLL_TIMEOUT);
|
||||
|
||||
if (nfds < 0) {
|
||||
continue;
|
||||
@@ -122,7 +122,7 @@ cws_return cws_server_start(cws_server_s *server) {
|
||||
|
||||
cws_fd_set_nonblocking(client_fd);
|
||||
cws_epoll_add(server->workers[workers_index]->epfd, client_fd);
|
||||
workers_index = (workers_index + 1) % CWS_WORKERS_NUM;
|
||||
workers_index = (workers_index + 1) % server->config->workers;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,6 +172,6 @@ void cws_server_shutdown(cws_server_s *server) {
|
||||
}
|
||||
|
||||
if (server->workers) {
|
||||
cws_worker_free(server->workers, CWS_WORKERS_NUM);
|
||||
cws_worker_free(server->workers, server->config->workers);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ static cws_vhost_s *get_vhost(cws_config_s *config, char *host) {
|
||||
for (unsigned i = 0; i < config->virtual_hosts_count; ++i) {
|
||||
cws_vhost_s *vh = config->virtual_hosts;
|
||||
if (!strcmp(vh[i].domain, host)) {
|
||||
return vh;
|
||||
return &vh[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,11 @@ int main(void) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (signal(SIGTERM, cws_signal_handler) == SIG_ERR) {
|
||||
cws_log_error("signal()");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
cws_config_s *config = cws_config_init();
|
||||
if (!config) {
|
||||
cws_log_error("Unable to parse config");
|
||||
|
||||
Reference in New Issue
Block a user