mirror of
https://github.com/falk-werner/webfused
synced 2024-10-27 20:44:08 +00:00
read server config
This commit is contained in:
parent
d8670b742c
commit
389c27c7eb
@ -7,6 +7,15 @@
|
||||
#define WFD_CONFIG_DEFAULT_PORT (8080)
|
||||
#define WFD_CONFIG_DEFAULT_VHOSTNAME ("localhost")
|
||||
|
||||
struct wfd_config
|
||||
{
|
||||
char * vhost_name;
|
||||
char * server_cert;
|
||||
char * server_key;
|
||||
char * server_doc_root;
|
||||
int port;
|
||||
};
|
||||
|
||||
extern struct wfd_config *
|
||||
wfd_config_create(void)
|
||||
{
|
||||
@ -28,7 +37,6 @@ void wfd_config_dispose(
|
||||
free(config->server_key);
|
||||
free(config->server_doc_root);
|
||||
free(config);
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
@ -38,6 +46,14 @@ wfd_config_get_server_port(
|
||||
return config->port;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_set_server_port(
|
||||
struct wfd_config * config,
|
||||
int port)
|
||||
{
|
||||
config->port = port;
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_server_vhostname(
|
||||
struct wfd_config * config)
|
||||
@ -45,6 +61,23 @@ wfd_config_get_server_vhostname(
|
||||
return config->vhost_name;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_set_server_vhostname(
|
||||
struct wfd_config * config,
|
||||
char const * vhost_name)
|
||||
{
|
||||
free(config->vhost_name);
|
||||
config->vhost_name = strdup(vhost_name);
|
||||
}
|
||||
|
||||
bool
|
||||
wfd_config_is_server_tls_enabled(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return ((NULL != config->server_key)
|
||||
&& (NULL != config->server_cert));
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_server_cert(
|
||||
struct wfd_config * config)
|
||||
@ -52,6 +85,15 @@ wfd_config_get_server_cert(
|
||||
return config->server_cert;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_set_server_cert(
|
||||
struct wfd_config * config,
|
||||
char const * cert)
|
||||
{
|
||||
free(config->server_cert);
|
||||
config->server_cert = strdup(cert);
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_server_key(
|
||||
struct wfd_config * config)
|
||||
@ -59,6 +101,15 @@ wfd_config_get_server_key(
|
||||
return config->server_key;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_set_server_key(
|
||||
struct wfd_config * config,
|
||||
char const * key)
|
||||
{
|
||||
free(config->server_key);
|
||||
config->server_key = strdup(key);
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_server_document_root(
|
||||
struct wfd_config * config)
|
||||
@ -66,6 +117,15 @@ wfd_config_get_server_document_root(
|
||||
return config->server_doc_root;
|
||||
}
|
||||
|
||||
void
|
||||
wfd_config_set_server_document_root(
|
||||
struct wfd_config * config,
|
||||
char const * document_root)
|
||||
{
|
||||
free(config->server_doc_root);
|
||||
config->server_doc_root = strdup(document_root);
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_auth_provider(
|
||||
struct wfd_config * config)
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define WFD_CONFIG_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
@ -31,6 +32,10 @@ extern char const *
|
||||
wfd_config_get_server_cert(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern bool
|
||||
wfd_config_is_server_tls_enabled(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern char const *
|
||||
wfd_config_get_server_key(
|
||||
struct wfd_config * config);
|
||||
|
@ -6,18 +6,34 @@ extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfd_config
|
||||
{
|
||||
char * vhost_name;
|
||||
char * server_cert;
|
||||
char * server_key;
|
||||
char * server_doc_root;
|
||||
int port;
|
||||
};
|
||||
|
||||
extern struct wfd_config *
|
||||
wfd_config_create(void);
|
||||
|
||||
extern void
|
||||
wfd_config_set_server_vhostname(
|
||||
struct wfd_config * config,
|
||||
char const * vhost_name);
|
||||
|
||||
extern void
|
||||
wfd_config_set_server_port(
|
||||
struct wfd_config * config,
|
||||
int port);
|
||||
|
||||
extern void
|
||||
wfd_config_set_server_cert(
|
||||
struct wfd_config * config,
|
||||
char const * cert);
|
||||
|
||||
extern void
|
||||
wfd_config_set_server_key(
|
||||
struct wfd_config * config,
|
||||
char const * key);
|
||||
|
||||
extern void
|
||||
wfd_config_set_server_document_root(
|
||||
struct wfd_config * config,
|
||||
char const * document_root);
|
||||
|
||||
#ifdef _cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -56,12 +56,57 @@ wfd_config_check_version(
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
wfd_config_read_server(
|
||||
config_t * config,
|
||||
struct wfd_config * result)
|
||||
{
|
||||
char const * vhost_name;
|
||||
int rc = config_lookup_string(config, "server.vhost_name", &vhost_name);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_vhostname(result, vhost_name);
|
||||
}
|
||||
|
||||
int port;
|
||||
rc = config_lookup_int(config, "server.port", &port);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_port(result, port);
|
||||
}
|
||||
|
||||
char const * cert;
|
||||
rc = config_lookup_string(config, "server.tls.certificate", &cert);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_cert(result, cert);
|
||||
}
|
||||
|
||||
char const * key;
|
||||
rc = config_lookup_string(config, "server.tls.key", &key);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_key(result, key);
|
||||
}
|
||||
|
||||
char const * doc_root;
|
||||
rc = config_lookup_string(config, "server.document_root", &doc_root);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
wfd_config_set_server_document_root(result, doc_root);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static struct wfd_config *
|
||||
wfd_config_load(config_t * config)
|
||||
{
|
||||
struct wfd_config * result = wfd_config_create();
|
||||
|
||||
bool success = wfd_config_check_version(config);
|
||||
bool success = wfd_config_check_version(config)
|
||||
&& wfd_config_read_server(config, result)
|
||||
;
|
||||
|
||||
if (!success)
|
||||
{
|
||||
|
@ -118,3 +118,164 @@ TEST(config, valid_newer_minor)
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(config, default_values)
|
||||
{
|
||||
MockLogger logger;
|
||||
EXPECT_CALL(logger, log(_, _, _)).Times(0);
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
char const minimal[] = "version = { major = 1, minor = 0 }\n";
|
||||
struct wfd_config * config = wfd_config_load_string(minimal);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
ASSERT_EQ(8080, wfd_config_get_server_port(config));
|
||||
ASSERT_STREQ("localhost", wfd_config_get_server_vhostname(config));
|
||||
ASSERT_EQ(nullptr, wfd_config_get_server_cert(config));
|
||||
ASSERT_EQ(nullptr, wfd_config_get_server_key(config));
|
||||
ASSERT_EQ(nullptr, wfd_config_get_server_document_root(config));
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(config, vhost_name)
|
||||
{
|
||||
MockLogger logger;
|
||||
EXPECT_CALL(logger, log(_, _, _)).Times(0);
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
"server:\n"
|
||||
"{\n"
|
||||
" vhost_name = \"some.host\"\n"
|
||||
"}\n"
|
||||
;
|
||||
struct wfd_config * config = wfd_config_load_string(config_text);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
ASSERT_STREQ("some.host", wfd_config_get_server_vhostname(config));
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(config, port)
|
||||
{
|
||||
MockLogger logger;
|
||||
EXPECT_CALL(logger, log(_, _, _)).Times(0);
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
"server:\n"
|
||||
"{\n"
|
||||
" port = 54321\n"
|
||||
"}\n"
|
||||
;
|
||||
struct wfd_config * config = wfd_config_load_string(config_text);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
ASSERT_EQ(54321, wfd_config_get_server_port(config));
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(config, tls)
|
||||
{
|
||||
MockLogger logger;
|
||||
EXPECT_CALL(logger, log(_, _, _)).Times(0);
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
"server:\n"
|
||||
"{\n"
|
||||
" tls:\n"
|
||||
" {\n"
|
||||
" certificate = \"/path/to/cert.pem\"\n"
|
||||
" key = \"/path/to/key.pem\"\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
;
|
||||
struct wfd_config * config = wfd_config_load_string(config_text);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
ASSERT_TRUE(wfd_config_is_server_tls_enabled(config));
|
||||
ASSERT_STREQ("/path/to/cert.pem", wfd_config_get_server_cert(config));
|
||||
ASSERT_STREQ("/path/to/key.pem", wfd_config_get_server_key(config));
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(config, tls_certificate)
|
||||
{
|
||||
MockLogger logger;
|
||||
EXPECT_CALL(logger, log(_, _, _)).Times(0);
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
"server:\n"
|
||||
"{\n"
|
||||
" tls:\n"
|
||||
" {\n"
|
||||
" certificate = \"/path/to/cert.pem\"\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
;
|
||||
struct wfd_config * config = wfd_config_load_string(config_text);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
ASSERT_FALSE(wfd_config_is_server_tls_enabled(config));
|
||||
ASSERT_STREQ("/path/to/cert.pem", wfd_config_get_server_cert(config));
|
||||
ASSERT_EQ(nullptr, wfd_config_get_server_key(config));
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(config, tls_key)
|
||||
{
|
||||
MockLogger logger;
|
||||
EXPECT_CALL(logger, log(_, _, _)).Times(0);
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
"server:\n"
|
||||
"{\n"
|
||||
" tls:\n"
|
||||
" {\n"
|
||||
" key = \"/path/to/key.pem\"\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
;
|
||||
struct wfd_config * config = wfd_config_load_string(config_text);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
ASSERT_FALSE(wfd_config_is_server_tls_enabled(config));
|
||||
ASSERT_EQ(nullptr, wfd_config_get_server_cert(config));
|
||||
ASSERT_STREQ("/path/to/key.pem", wfd_config_get_server_key(config));
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(config, document_root)
|
||||
{
|
||||
MockLogger logger;
|
||||
EXPECT_CALL(logger, log(_, _, _)).Times(0);
|
||||
EXPECT_CALL(logger, onclose()).Times(1);
|
||||
|
||||
char const config_text[] =
|
||||
"version = { major = 1, minor = 0 }\n"
|
||||
"server:\n"
|
||||
"{\n"
|
||||
" document_root = \"/var/www\"\n"
|
||||
"}\n"
|
||||
;
|
||||
struct wfd_config * config = wfd_config_load_string(config_text);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
ASSERT_STREQ("/var/www", wfd_config_get_server_document_root(config));
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user