2019-03-26 22:04:53 +00:00
|
|
|
#include "webfuse/adapter/server.h"
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
2019-03-23 21:53:14 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
#include "webfuse/adapter/impl/server_config.h"
|
|
|
|
#include "webfuse/adapter/impl/server_protocol.h"
|
2019-05-19 12:33:42 +00:00
|
|
|
#include "webfuse/core/lws_log.h"
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
#define WF_SERVER_PROTOCOL_COUNT 3
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_server
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_server_config config;
|
|
|
|
struct wf_server_protocol protocol;
|
|
|
|
struct lws_protocols ws_protocols[WF_SERVER_PROTOCOL_COUNT];
|
2019-02-05 23:58:51 +00:00
|
|
|
struct lws_context * context;
|
2019-02-09 02:08:02 +00:00
|
|
|
struct lws_http_mount mount;
|
|
|
|
struct lws_context_creation_info info;
|
2019-01-27 02:45:03 +00:00
|
|
|
};
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
static bool wf_impl_server_tls_enabled(
|
|
|
|
struct wf_server * server)
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
|
|
|
return ((server->config.key_path != NULL) && (server->config.cert_path != NULL));
|
|
|
|
}
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
static struct lws_context * wf_impl_server_context_create(
|
|
|
|
struct wf_server * server)
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2019-05-19 12:33:42 +00:00
|
|
|
wf_lwslog_disable();
|
2019-02-10 14:12:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
memset(server->ws_protocols, 0, sizeof(struct lws_protocols) * WF_SERVER_PROTOCOL_COUNT);
|
2019-02-05 23:58:51 +00:00
|
|
|
server->ws_protocols[0].name = "http";
|
|
|
|
server->ws_protocols[0].callback = lws_callback_http_dummy;
|
|
|
|
server->ws_protocols[1].name = "fs";
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_server_protocol_init_lws(&server->protocol, &server->ws_protocols[1]);
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2019-02-09 02:08:02 +00:00
|
|
|
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;
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
if (NULL == server->config.document_root)
|
|
|
|
{
|
|
|
|
// disable http
|
2019-02-09 02:08:02 +00:00
|
|
|
server->info.protocols = &server->ws_protocols[1];
|
|
|
|
server->info.mounts = NULL;
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
if (wf_impl_server_tls_enabled(server))
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2019-02-09 02:08:02 +00:00
|
|
|
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;
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 02:08:02 +00:00
|
|
|
struct lws_context * const context = lws_create_context(&server->info);
|
2019-02-05 23:58:51 +00:00
|
|
|
return context;
|
2019-01-27 02:45:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
static bool wf_impl_server_check_mountpoint(
|
|
|
|
struct wf_server_config * config)
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2019-03-23 21:53:14 +00:00
|
|
|
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)
|
2019-02-09 02:08:02 +00:00
|
|
|
{
|
2019-03-23 21:53:14 +00:00
|
|
|
result = (0 == mkdir(config->mount_point, 0755));
|
2019-02-09 02:08:02 +00:00
|
|
|
}
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_server * wf_impl_server_create(
|
|
|
|
struct wf_server_config * config)
|
2019-03-23 21:53:14 +00:00
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_server * server = NULL;
|
2019-03-23 21:53:14 +00:00
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
if (wf_impl_server_check_mountpoint(config))
|
2019-03-23 21:53:14 +00:00
|
|
|
{
|
2019-03-26 22:04:53 +00:00
|
|
|
server = malloc(sizeof(struct wf_server));
|
2019-03-23 21:53:14 +00:00
|
|
|
if (NULL != server)
|
2019-02-09 02:08:02 +00:00
|
|
|
{
|
2019-04-17 20:51:16 +00:00
|
|
|
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);
|
2019-03-23 21:53:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-05 23:58:51 +00:00
|
|
|
|
|
|
|
return server;
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
void wf_impl_server_dispose(
|
|
|
|
struct wf_server * server)
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2019-02-05 23:58:51 +00:00
|
|
|
lws_context_destroy(server->context);
|
2019-03-26 22:04:53 +00:00
|
|
|
wf_impl_server_protocol_cleanup(&server->protocol);
|
|
|
|
wf_impl_server_config_cleanup(&server->config);
|
2019-02-05 23:58:51 +00:00
|
|
|
free(server);
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 12:33:42 +00:00
|
|
|
bool wf_impl_server_is_operational(
|
|
|
|
struct wf_server * server)
|
|
|
|
{
|
|
|
|
return server->protocol.is_operational;
|
|
|
|
}
|
|
|
|
|
2019-04-26 18:51:24 +00:00
|
|
|
void wf_impl_server_service(
|
|
|
|
struct wf_server * server,
|
|
|
|
int timeout_ms)
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2019-04-26 18:51:24 +00:00
|
|
|
lws_service(server->context, timeout_ms);
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|