1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-09-28 17:20:46 +00:00
falk-werner_webfuse-provider/lib/webfuse/adapter/impl/server.c
Falk Werner 07e32757f8
chore(webfuse) Increase test coverage (#34)
* removes unnecessary code

* adds test of wf_status

* adds tests of wf_message

* adds tests of wf_message_queue

* changed branch of coverage badge to display correct results

* moves core tests into separate subdirectory

* increases coverage of timer test

* moves adapter specific tests into separate directory

* moves provider specific tests into separate directory

* adds tests of jsonrpc utilities

* adds tests of jsonrpc request

* adds test of jsonrpc response

* adds tests of jsonrpc server

* adds tests of jsonrpc proxy

* adds integration test (found some issues)

* disables problematic tests

* fixes resource leak: pending timer after cleanup proxy

* fixes order of cleanup to prevent processing pending requests after filesystem shut down

* fixes some memcheck and helgrind errors: initialization of lws_log; setup of client and server

* disabled a test

* fixes error in msleep utility

* fixes deadlock at IntegrationTest using valgrind

* removes unit test code from coverage report

* adds some integration tests

* makes badge show coverage of master

* fixes some coding style issues

* fixes eary trigger of is_connected (provider)

* fixes read error in 32 bit environments\n\ninode is always 64 bit, but variadic wf_impl_jsonrpc_proxy_invoke expects int
2019-05-19 14:33:42 +02:00

140 lines
3.7 KiB
C

#include "webfuse/adapter/server.h"
#include <stdlib.h>
#include <stdbool.h>
#include <libwebsockets.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "webfuse/adapter/impl/server_config.h"
#include "webfuse/adapter/impl/server_protocol.h"
#include "webfuse/core/lws_log.h"
#define WF_SERVER_PROTOCOL_COUNT 3
struct wf_server
{
struct wf_server_config config;
struct wf_server_protocol protocol;
struct lws_protocols ws_protocols[WF_SERVER_PROTOCOL_COUNT];
struct lws_context * context;
struct lws_http_mount mount;
struct lws_context_creation_info info;
};
static bool wf_impl_server_tls_enabled(
struct wf_server * server)
{
return ((server->config.key_path != NULL) && (server->config.cert_path != NULL));
}
static struct lws_context * wf_impl_server_context_create(
struct wf_server * server)
{
wf_lwslog_disable();
memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WF_SERVER_PROTOCOL_COUNT);
server->ws_protocols[0].name = "http";
server->ws_protocols[0].callback = lws_callback_http_dummy;
server->ws_protocols[1].name = "fs";
wf_impl_server_protocol_init_lws(&server->protocol, &server->ws_protocols[1]);
memset(&server->mount, 0, sizeof(struct lws_http_mount));
server->mount.mount_next = NULL,
server->mount.mountpoint = "/",
server->mount.origin = server->config.document_root,
server->mount.def = "index.html",
server->mount.origin_protocol = LWSMPRO_FILE,
server->mount.mountpoint_len = 1,
memset(&server->info, 0, sizeof(struct lws_context_creation_info));
server->info.port = server->config.port;
server->info.mounts = &server->mount;
server->info.protocols = server->ws_protocols;
server->info.vhost_name = server->config.vhost_name;
server->info.ws_ping_pong_interval = 10;
server->info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
if (NULL == server->config.document_root)
{
// disable http
server->info.protocols = &server->ws_protocols[1];
server->info.mounts = NULL;
}
if (wf_impl_server_tls_enabled(server))
{
server->info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
server->info.ssl_cert_filepath = server->config.cert_path;
server->info.ssl_private_key_filepath = server->config.key_path;
}
struct lws_context * const context = lws_create_context(&server->info);
return context;
}
static bool wf_impl_server_check_mountpoint(
struct wf_server_config * config)
{
bool result = false;
if (NULL != config->mount_point)
{
struct stat info;
int const rc = stat(config->mount_point, &info);
result = ((0 == rc) && (S_ISDIR(info.st_mode)));
if (!result)
{
result = (0 == mkdir(config->mount_point, 0755));
}
}
return result;
}
struct wf_server * wf_impl_server_create(
struct wf_server_config * config)
{
struct wf_server * server = NULL;
if (wf_impl_server_check_mountpoint(config))
{
server = malloc(sizeof(struct wf_server));
if (NULL != server)
{
wf_impl_server_protocol_init(&server->protocol, config->mount_point);
wf_impl_server_config_clone(config, &server->config);
wf_impl_authenticators_move(&server->config.authenticators, &server->protocol.authenticators);
server->context = wf_impl_server_context_create(server);
}
}
return server;
}
void wf_impl_server_dispose(
struct wf_server * server)
{
lws_context_destroy(server->context);
wf_impl_server_protocol_cleanup(&server->protocol);
wf_impl_server_config_cleanup(&server->config);
free(server);
}
bool wf_impl_server_is_operational(
struct wf_server * server)
{
return server->protocol.is_operational;
}
void wf_impl_server_service(
struct wf_server * server,
int timeout_ms)
{
lws_service(server->context, timeout_ms);
}