initial virtual hosts support
This commit is contained in:
@@ -48,6 +48,9 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
|
||||
}
|
||||
request->sockfd = sockfd;
|
||||
|
||||
/* Prepare the virtual_host struct */
|
||||
cws_virtual_host vhost;
|
||||
|
||||
char *request_str_cpy = strdup(mcl_string_cstr(request_str));
|
||||
if (!request_str_cpy) {
|
||||
free(request);
|
||||
@@ -89,7 +92,7 @@ cws_http *cws_http_parse(mcl_string *request_str, int sockfd, cws_config *config
|
||||
}
|
||||
// CWS_LOG_DEBUG("location: %s", pch);
|
||||
mcl_string_append(request->location, pch);
|
||||
mcl_string_append(request->location_path, config->www);
|
||||
// TODO: mcl_string_append(request->location_path, config->www);
|
||||
|
||||
/* Adjust location path */
|
||||
if (strcmp(mcl_string_cstr(request->location), "/") == 0) {
|
||||
|
||||
@@ -19,11 +19,16 @@ int main(void) {
|
||||
}
|
||||
|
||||
cws_config *config = cws_config_init();
|
||||
if (!config) {
|
||||
if (config == NULL) {
|
||||
CWS_LOG_ERROR("Unable to read config file");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
CWS_LOG_INFO("Virtual hosts count: %d", config->virtual_hosts_count);
|
||||
for (size_t i = 0; i < config->virtual_hosts_count; ++i) {
|
||||
CWS_LOG_DEBUG("%s (ssl: %d)", config->virtual_hosts[i].domain, config->virtual_hosts[i].ssl);
|
||||
}
|
||||
|
||||
CWS_LOG_INFO("Running cws on http://%s:%s...", config->hostname, config->port);
|
||||
int ret = cws_server_start(config);
|
||||
if (ret < 0) {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
#include "utils/config.h"
|
||||
|
||||
#include <cyaml/cyaml.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "utils/colors.h"
|
||||
|
||||
static const cyaml_config_t cyaml_config = {
|
||||
.log_fn = cyaml_log,
|
||||
@@ -8,15 +11,28 @@ static const cyaml_config_t cyaml_config = {
|
||||
.log_level = CYAML_LOG_WARNING,
|
||||
};
|
||||
|
||||
static const cyaml_schema_field_t top_mapping_schema[] = {CYAML_FIELD_STRING_PTR("hostname", CYAML_FLAG_POINTER, cws_config, hostname, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_STRING_PTR("port", CYAML_FLAG_POINTER, cws_config, port, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_STRING_PTR("www", CYAML_FLAG_POINTER, cws_config, www, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_STRING_PTR("cert", CYAML_FLAG_POINTER, cws_config, cert, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_STRING_PTR("key", CYAML_FLAG_POINTER, cws_config, key, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_END};
|
||||
static const cyaml_schema_field_t virtual_hosts_fields[] = {
|
||||
CYAML_FIELD_STRING_PTR("domain", CYAML_FLAG_POINTER, struct cws_virtual_host_t, domain, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_STRING_PTR("root", CYAML_FLAG_POINTER, struct cws_virtual_host_t, root, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_BOOL("ssl", CYAML_FLAG_DEFAULT, struct cws_virtual_host_t, ssl),
|
||||
CYAML_FIELD_STRING_PTR("cert", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_virtual_host_t, cert, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_STRING_PTR("key", CYAML_FLAG_POINTER | CYAML_FLAG_OPTIONAL, struct cws_virtual_host_t, key, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_END,
|
||||
};
|
||||
|
||||
static const cyaml_schema_value_t top_schema = {
|
||||
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, cws_config, top_mapping_schema),
|
||||
static cyaml_schema_value_t virtual_hosts_schema = {
|
||||
CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT, struct cws_virtual_host_t, virtual_hosts_fields),
|
||||
};
|
||||
|
||||
static const cyaml_schema_field_t top_schema_fields[] = {
|
||||
CYAML_FIELD_STRING_PTR("hostname", CYAML_FLAG_POINTER, struct cws_config_t, hostname, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_STRING_PTR("port", CYAML_FLAG_POINTER, struct cws_config_t, port, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_SEQUENCE("virtual_hosts", CYAML_FLAG_POINTER, struct cws_config_t, virtual_hosts, &virtual_hosts_schema, 0, CYAML_UNLIMITED),
|
||||
CYAML_FIELD_END,
|
||||
};
|
||||
|
||||
static cyaml_schema_value_t top_schema = {
|
||||
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER, struct cws_config_t, top_schema_fields),
|
||||
};
|
||||
|
||||
cws_config *cws_config_init(void) {
|
||||
@@ -25,6 +41,8 @@ cws_config *cws_config_init(void) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -34,10 +52,6 @@ cws_config *cws_config_init(void) {
|
||||
void cws_config_free(cws_config *config) {
|
||||
cyaml_err_t err = cyaml_free(&cyaml_config, &top_schema, config, 0);
|
||||
if (err != CYAML_OK) {
|
||||
/* Handle */
|
||||
/* TODO: Handle */
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
https://github.com/tlsa/libcyaml/blob/main/docs/guide.md
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user