Compare commits
2 Commits
ca5adbea1a
...
cfaae5529e
| Author | SHA1 | Date | |
|---|---|---|---|
| cfaae5529e | |||
| 3e936767ce |
@@ -6,8 +6,8 @@ A minimal HTTP web server written in C.
|
|||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- [libcyaml](https://github.com/tlsa/libcyaml)
|
|
||||||
- myclib (on my profile)
|
- myclib (on my profile)
|
||||||
|
- [tomlc17](https://github.com/cktan/tomlc17)
|
||||||
- [doxygen](https://www.doxygen.nl/) (optional, for documentation only - requires `dot`)
|
- [doxygen](https://www.doxygen.nl/) (optional, for documentation only - requires `dot`)
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
|||||||
22
config.toml
Normal file
22
config.toml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# Default server values
|
||||||
|
[server]
|
||||||
|
host = "localhost"
|
||||||
|
port = "3030"
|
||||||
|
# Root folder in case there are no virtual hosts
|
||||||
|
root = "www"
|
||||||
|
|
||||||
|
[[virtual_hosts]]
|
||||||
|
domain = "localhost"
|
||||||
|
root = "www"
|
||||||
|
|
||||||
|
[[virtual_hosts.pages]]
|
||||||
|
status = "404"
|
||||||
|
path = "www/pages/404.html"
|
||||||
|
|
||||||
|
[[virtual_hosts.pages]]
|
||||||
|
status = "505"
|
||||||
|
path = "www/pages/505.html"
|
||||||
|
|
||||||
|
[[virtual_hosts]]
|
||||||
|
domain = "example.com"
|
||||||
|
root = "example_site"
|
||||||
17
config.yaml
17
config.yaml
@@ -1,17 +0,0 @@
|
|||||||
# Default hostname
|
|
||||||
hostname: localhost
|
|
||||||
|
|
||||||
# Running port
|
|
||||||
port: 3030
|
|
||||||
|
|
||||||
# Virtual hosts setup
|
|
||||||
virtual_hosts:
|
|
||||||
- domain: localhost
|
|
||||||
# Root folder
|
|
||||||
root: www
|
|
||||||
# Custom pages
|
|
||||||
error_pages:
|
|
||||||
- method: 404
|
|
||||||
path: pages/404.html
|
|
||||||
- method: 500
|
|
||||||
path: pages/500.html
|
|
||||||
@@ -3,21 +3,22 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct cws_error_page {
|
typedef struct cws_page {
|
||||||
unsigned method;
|
char *status;
|
||||||
const char *path;
|
char *path;
|
||||||
} cws_error_page;
|
} cws_page_s;
|
||||||
|
|
||||||
typedef struct cws_vhost {
|
typedef struct cws_vhost {
|
||||||
char *domain;
|
char *domain;
|
||||||
char *root;
|
char *root;
|
||||||
cws_error_page *error_pages;
|
cws_page_s *error_pages;
|
||||||
unsigned error_pages_count;
|
unsigned error_pages_count;
|
||||||
} cws_vhost_s;
|
} cws_vhost_s;
|
||||||
|
|
||||||
typedef struct cws_config {
|
typedef struct cws_config {
|
||||||
char *hostname;
|
char *host;
|
||||||
char *port;
|
char *port;
|
||||||
|
char *root;
|
||||||
cws_vhost_s *virtual_hosts;
|
cws_vhost_s *virtual_hosts;
|
||||||
unsigned virtual_hosts_count;
|
unsigned virtual_hosts_count;
|
||||||
} cws_config_s;
|
} cws_config_s;
|
||||||
|
|||||||
@@ -11,12 +11,11 @@ subdir('src')
|
|||||||
|
|
||||||
incdir = include_directories('include')
|
incdir = include_directories('include')
|
||||||
|
|
||||||
libyaml = dependency('yaml-0.1')
|
libtomlc17 = dependency('libtomlc17', required: true)
|
||||||
libcyaml = dependency('libcyaml')
|
|
||||||
libmath = cc.find_library('m', required: true)
|
libmath = cc.find_library('m', required: true)
|
||||||
libmyclib = cc.find_library('myclib', required: true)
|
libmyclib = cc.find_library('myclib', required: true)
|
||||||
|
|
||||||
deps = [libyaml, libcyaml, libmath, libmyclib]
|
deps = [libtomlc17, libmath, libmyclib]
|
||||||
|
|
||||||
add_global_arguments('-DUSE_COLORS', language: 'c')
|
add_global_arguments('-DUSE_COLORS', language: 'c')
|
||||||
add_global_arguments('-DEVELOPER', language: 'c')
|
add_global_arguments('-DEVELOPER', language: 'c')
|
||||||
|
|||||||
@@ -1,85 +1,160 @@
|
|||||||
#include "config/config.h"
|
#include "config/config.h"
|
||||||
|
|
||||||
#include <cyaml/cyaml.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <tomlc17.h>
|
||||||
|
|
||||||
#include "utils/debug.h"
|
#include "utils/debug.h"
|
||||||
|
|
||||||
static const cyaml_config_t cyaml_config = {
|
static char *cws_strdup(const char *str) {
|
||||||
.log_fn = cyaml_log,
|
if (!str) {
|
||||||
.mem_fn = cyaml_mem,
|
return NULL;
|
||||||
.log_level = CYAML_LOG_WARNING,
|
}
|
||||||
};
|
|
||||||
|
|
||||||
static const cyaml_schema_field_t error_page_fields[] = {
|
size_t len = strlen(str) + 1;
|
||||||
CYAML_FIELD_INT("method", CYAML_FLAG_DEFAULT, cws_error_page, method),
|
char *copy = malloc(sizeof *copy * len);
|
||||||
CYAML_FIELD_STRING_PTR("path", CYAML_FLAG_POINTER, cws_error_page, path, 0, CYAML_UNLIMITED),
|
if (!copy) {
|
||||||
CYAML_FIELD_END,
|
return NULL;
|
||||||
};
|
}
|
||||||
|
|
||||||
static cyaml_schema_value_t error_page_schema = {
|
memcpy(copy, str, len);
|
||||||
CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT, cws_error_page, error_page_fields),
|
|
||||||
};
|
|
||||||
|
|
||||||
static const cyaml_schema_field_t virtual_hosts_fields[] = {
|
return copy;
|
||||||
CYAML_FIELD_STRING_PTR("domain", CYAML_FLAG_POINTER, struct cws_vhost, domain, 0, CYAML_UNLIMITED),
|
}
|
||||||
CYAML_FIELD_STRING_PTR("root", CYAML_FLAG_POINTER, struct cws_vhost, root, 0, CYAML_UNLIMITED),
|
|
||||||
CYAML_FIELD_SEQUENCE("error_pages", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_vhost, error_pages,
|
|
||||||
&error_page_schema, 0, CYAML_UNLIMITED),
|
|
||||||
CYAML_FIELD_END,
|
|
||||||
};
|
|
||||||
|
|
||||||
static cyaml_schema_value_t virtual_hosts_schema = {
|
static bool parse_vhosts(cws_config_s *config, toml_result_t result) {
|
||||||
CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT, struct cws_vhost, virtual_hosts_fields),
|
toml_datum_t vhosts = toml_seek(result.toptab, "virtual_hosts");
|
||||||
};
|
config->virtual_hosts_count = vhosts.u.arr.size;
|
||||||
|
config->virtual_hosts = malloc(sizeof *config->virtual_hosts * config->virtual_hosts_count);
|
||||||
|
if (!config->virtual_hosts) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static const cyaml_schema_field_t top_schema_fields[] = {
|
for (int i = 0; i < vhosts.u.arr.size; ++i) {
|
||||||
CYAML_FIELD_STRING_PTR("hostname", CYAML_FLAG_POINTER, struct cws_config, hostname, 0, CYAML_UNLIMITED),
|
cws_vhost_s *vh = &config->virtual_hosts[i];
|
||||||
CYAML_FIELD_STRING_PTR("port", CYAML_FLAG_POINTER, struct cws_config, port, 0, CYAML_UNLIMITED),
|
toml_datum_t elem = vhosts.u.arr.elem[i];
|
||||||
CYAML_FIELD_SEQUENCE("virtual_hosts", CYAML_FLAG_POINTER, struct cws_config, virtual_hosts, &virtual_hosts_schema,
|
toml_datum_t domain = toml_seek(elem, "domain");
|
||||||
0, CYAML_UNLIMITED),
|
vh->domain = cws_strdup(domain.u.str.ptr);
|
||||||
CYAML_FIELD_END,
|
if (!vh->domain) {
|
||||||
};
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static cyaml_schema_value_t top_schema = {
|
toml_datum_t root = toml_seek(elem, "root");
|
||||||
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, struct cws_config, top_schema_fields),
|
vh->root = cws_strdup(root.u.str.ptr);
|
||||||
};
|
if (!vh->root) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Pages */
|
||||||
|
toml_datum_t pages = toml_seek(elem, "pages");
|
||||||
|
vh->error_pages_count = pages.u.arr.size;
|
||||||
|
vh->error_pages = malloc(sizeof *vh->error_pages * vh->error_pages_count);
|
||||||
|
if (!vh->error_pages) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
if (!vh->error_pages[i].path) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static bool find_default_hostname(cws_config_s *config) {
|
|
||||||
for (unsigned i = 0; i < config->virtual_hosts_count; ++i) {
|
|
||||||
if (strcmp(config->hostname, config->virtual_hosts[i].domain) == 0) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
static bool parse_toml(cws_config_s *config) {
|
||||||
|
const char *path = "config.toml";
|
||||||
|
|
||||||
|
toml_result_t result = toml_parse_file_ex(path);
|
||||||
|
if (!result.ok) {
|
||||||
|
cws_log_error("Unable to parse config.toml");
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toml_datum_t host = toml_seek(result.toptab, "server.host");
|
||||||
|
config->host = cws_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);
|
||||||
|
if (!config->port) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
toml_datum_t root = toml_seek(result.toptab, "server.root");
|
||||||
|
config->root = cws_strdup(root.u.str.ptr);
|
||||||
|
if (!config->root) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_vhosts(config, result);
|
||||||
|
|
||||||
|
toml_free(result);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
cws_config_s *cws_config_init(void) {
|
cws_config_s *cws_config_init(void) {
|
||||||
const char *path = "config.yaml";
|
cws_config_s *config = malloc(sizeof *config);
|
||||||
cws_config_s *config;
|
if (!config) {
|
||||||
|
|
||||||
cyaml_err_t err = cyaml_load_file(path, &cyaml_config, &top_schema, (cyaml_data_t **)&config, NULL);
|
|
||||||
if (err != CYAML_OK) {
|
|
||||||
cws_log_error("%s", cyaml_strerror(err));
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool found = find_default_hostname(config);
|
parse_toml(config);
|
||||||
if (!found) {
|
|
||||||
cws_log_error("Default hostname not found in config.yaml");
|
|
||||||
cws_config_free(config);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cws_config_free(cws_config_s *config) {
|
void cws_config_free(cws_config_s *config) {
|
||||||
cyaml_err_t err = cyaml_free(&cyaml_config, &top_schema, config, 0);
|
if (!config) {
|
||||||
if (err != CYAML_OK) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config->host) {
|
||||||
|
free(config->host);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config->port) {
|
||||||
|
free(config->port);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config->root) {
|
||||||
|
free(config->root);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < config->virtual_hosts_count; ++i) {
|
||||||
|
cws_vhost_s *vh = &config->virtual_hosts[i];
|
||||||
|
if (vh->domain) {
|
||||||
|
free(vh->domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vh->root) {
|
||||||
|
free(vh->root);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned j = 0; j < vh->error_pages_count; ++j) {
|
||||||
|
if (vh->error_pages[i].path) {
|
||||||
|
free(vh->error_pages[i].path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vh->error_pages[i].status) {
|
||||||
|
free(vh->error_pages[i].status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(config);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ static cws_return cws_server_setup_epoll(int server_fd, int *epfd_out) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
||||||
if (!config || !config->hostname || !config->port) {
|
if (!config || !config->host || !config->port) {
|
||||||
return CWS_CONFIG_ERROR;
|
return CWS_CONFIG_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,9 +49,9 @@ cws_return cws_server_setup(cws_server_s *server, cws_config_s *config) {
|
|||||||
|
|
||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
struct addrinfo *res;
|
struct addrinfo *res;
|
||||||
cws_server_setup_hints(&hints, config->hostname);
|
cws_server_setup_hints(&hints, config->host);
|
||||||
|
|
||||||
int status = getaddrinfo(config->hostname, config->port, &hints, &res);
|
int status = getaddrinfo(config->host, config->port, &hints, &res);
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
cws_log_error("getaddrinfo() error: %s", gai_strerror(status));
|
cws_log_error("getaddrinfo() error: %s", gai_strerror(status));
|
||||||
return CWS_GETADDRINFO_ERROR;
|
return CWS_GETADDRINFO_ERROR;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "utils/debug.h"
|
#include "utils/debug.h"
|
||||||
#include "utils/hash.h"
|
#include "utils/hash.h"
|
||||||
|
|
||||||
static cws_request_s *http_new(void) {
|
static cws_request_s *http_request_new(void) {
|
||||||
cws_request_s *request = malloc(sizeof(*request));
|
cws_request_s *request = malloc(sizeof(*request));
|
||||||
if (!request) {
|
if (!request) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -138,7 +138,7 @@ cws_request_s *cws_http_parse(string_s *request_str) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_request_s *request = http_new();
|
cws_request_s *request = http_request_new();
|
||||||
if (!request) {
|
if (!request) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ cws_response_s *cws_response_new(cws_http_status_e status) {
|
|||||||
resp->body_file = NULL;
|
resp->body_file = NULL;
|
||||||
resp->content_length = 0;
|
resp->content_length = 0;
|
||||||
|
|
||||||
|
/* TODO: get the value from connection */
|
||||||
cws_response_set_header(resp, "Connection", "close");
|
cws_response_set_header(resp, "Connection", "close");
|
||||||
|
|
||||||
return resp;
|
return resp;
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ int main(void) {
|
|||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_log_info("Running cws on http://%s:%s", config->hostname, config->port);
|
cws_log_info("Running cws on http://%s:%s", config->host, config->port);
|
||||||
ret = cws_server_start(&server);
|
ret = cws_server_start(&server);
|
||||||
if (ret != CWS_OK) {
|
if (ret != CWS_OK) {
|
||||||
cws_log_error("Unable to start web server: %s", cws_error_str(ret));
|
cws_log_error("Unable to start web server: %s", cws_error_str(ret));
|
||||||
|
|||||||
Reference in New Issue
Block a user