mirror of
https://github.com/falk-werner/webfuse
synced 2024-10-27 20:34:10 +00:00
07e32757f8
* 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
243 lines
6.6 KiB
C
243 lines
6.6 KiB
C
#include "webfuse/adapter/impl/server_protocol.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <ctype.h>
|
|
#include <libwebsockets.h>
|
|
|
|
#include "webfuse/core/message.h"
|
|
#include "webfuse/core/util.h"
|
|
|
|
#include "webfuse/adapter/impl/credentials.h"
|
|
#include "webfuse/adapter/impl/jsonrpc/request.h"
|
|
|
|
static int wf_impl_server_protocol_callback(
|
|
struct lws * wsi,
|
|
enum lws_callback_reasons reason,
|
|
void * WF_UNUSED_PARAM(user),
|
|
void * in,
|
|
size_t len)
|
|
{
|
|
struct lws_protocols const * ws_protocol = lws_get_protocol(wsi);
|
|
if (NULL == ws_protocol)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
struct wf_server_protocol * protocol = ws_protocol->user;
|
|
|
|
wf_impl_timeout_manager_check(&protocol->timeout_manager);
|
|
struct wf_impl_session * session = wf_impl_session_manager_get(&protocol->session_manager, wsi);
|
|
|
|
switch (reason)
|
|
{
|
|
case LWS_CALLBACK_PROTOCOL_INIT:
|
|
protocol->is_operational = true;
|
|
break;
|
|
case LWS_CALLBACK_ESTABLISHED:
|
|
session = wf_impl_session_manager_add(
|
|
&protocol->session_manager,
|
|
wsi,
|
|
&protocol->authenticators,
|
|
&protocol->timeout_manager,
|
|
&protocol->server,
|
|
protocol->mount_point);
|
|
|
|
if (NULL != session)
|
|
{
|
|
wf_impl_session_authenticate(session, NULL);
|
|
}
|
|
break;
|
|
case LWS_CALLBACK_CLOSED:
|
|
wf_impl_session_manager_remove(&protocol->session_manager, wsi);
|
|
break;
|
|
case LWS_CALLBACK_SERVER_WRITEABLE:
|
|
if (NULL != session)
|
|
{
|
|
wf_impl_session_onwritable(session);
|
|
}
|
|
break;
|
|
case LWS_CALLBACK_RECEIVE:
|
|
if (NULL != session)
|
|
{
|
|
wf_impl_session_receive(session, in, len);
|
|
}
|
|
break;
|
|
case LWS_CALLBACK_RAW_RX_FILE:
|
|
if (NULL != session)
|
|
{
|
|
wf_impl_session_process_filesystem_request(session, wsi);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
struct wf_server_protocol * wf_impl_server_protocol_create(
|
|
char * mount_point)
|
|
{
|
|
struct wf_server_protocol * protocol = malloc(sizeof(struct wf_server_protocol));
|
|
if (NULL != protocol)
|
|
{
|
|
wf_impl_server_protocol_init(protocol, mount_point);
|
|
}
|
|
|
|
return protocol;
|
|
}
|
|
|
|
void wf_impl_server_protocol_dispose(
|
|
struct wf_server_protocol * protocol)
|
|
{
|
|
wf_impl_server_protocol_cleanup(protocol);
|
|
free(protocol);
|
|
}
|
|
|
|
void wf_impl_server_protocol_init_lws(
|
|
struct wf_server_protocol * protocol,
|
|
struct lws_protocols * lws_protocol)
|
|
{
|
|
lws_protocol->callback = &wf_impl_server_protocol_callback;
|
|
lws_protocol->per_session_data_size = 0;
|
|
lws_protocol->user = protocol;
|
|
}
|
|
|
|
static void wf_impl_server_protocol_authenticate(
|
|
struct wf_impl_jsonrpc_request * request,
|
|
char const * WF_UNUSED_PARAM(method_name),
|
|
json_t * params,
|
|
void * WF_UNUSED_PARAM(user_data))
|
|
{
|
|
bool result = false;
|
|
|
|
json_t * type_holder = json_array_get(params, 0);
|
|
json_t * creds_holder = json_array_get(params, 1);
|
|
|
|
if (json_is_string(type_holder) && json_is_object(creds_holder))
|
|
{
|
|
char const * type = json_string_value(type_holder);
|
|
struct wf_credentials creds;
|
|
|
|
wf_impl_credentials_init(&creds, type, creds_holder);
|
|
struct wf_impl_session * session = wf_impl_jsonrpc_request_get_userdata(request);
|
|
result = wf_impl_session_authenticate(session, &creds);
|
|
|
|
wf_impl_credentials_cleanup(&creds);
|
|
}
|
|
|
|
|
|
if (result)
|
|
{
|
|
json_t * result = json_object();
|
|
wf_impl_jsonrpc_respond(request, result);
|
|
}
|
|
else
|
|
{
|
|
wf_impl_jsonrpc_respond_error(request, WF_BAD_ACCESS_DENIED);
|
|
}
|
|
}
|
|
|
|
static bool wf_impl_server_protocol_check_name(char const * value)
|
|
{
|
|
while ('\0' != *value)
|
|
{
|
|
char const c = * value;
|
|
if (!isalpha(c) && !isdigit(c) && ('_' != c))
|
|
{
|
|
return false;
|
|
}
|
|
value++;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void wf_impl_server_protocol_add_filesystem(
|
|
struct wf_impl_jsonrpc_request * request,
|
|
char const * WF_UNUSED_PARAM(method_name),
|
|
json_t * params,
|
|
void * WF_UNUSED_PARAM(user_data))
|
|
{
|
|
struct wf_impl_session * session = wf_impl_jsonrpc_request_get_userdata(request);
|
|
wf_status status = (session->is_authenticated) ? WF_GOOD : WF_BAD_ACCESS_DENIED;
|
|
|
|
char const * name = NULL;
|
|
if (WF_GOOD == status)
|
|
{
|
|
json_t * name_holder = json_array_get(params, 0);
|
|
if (json_is_string(name_holder))
|
|
{
|
|
name = json_string_value(name_holder);
|
|
if (wf_impl_server_protocol_check_name(name))
|
|
{
|
|
bool const success = wf_impl_session_add_filesystem(session, name);
|
|
if (!success)
|
|
{
|
|
status = WF_BAD;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
status = WF_BAD_FORMAT;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
status = WF_BAD_FORMAT;
|
|
}
|
|
|
|
}
|
|
|
|
if (WF_GOOD == status)
|
|
{
|
|
json_t * result = json_object();
|
|
json_object_set_new(result, "id", json_string(name));
|
|
wf_impl_jsonrpc_respond(request, result);
|
|
}
|
|
else
|
|
{
|
|
wf_impl_jsonrpc_respond_error(request, status);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
void wf_impl_server_protocol_init(
|
|
struct wf_server_protocol * protocol,
|
|
char * mount_point)
|
|
{
|
|
protocol->mount_point = strdup(mount_point);
|
|
protocol->is_operational = false;
|
|
|
|
wf_impl_timeout_manager_init(&protocol->timeout_manager);
|
|
wf_impl_session_manager_init(&protocol->session_manager);
|
|
wf_impl_authenticators_init(&protocol->authenticators);
|
|
|
|
wf_impl_jsonrpc_server_init(&protocol->server);
|
|
wf_impl_jsonrpc_server_add(&protocol->server, "authenticate", &wf_impl_server_protocol_authenticate, protocol);
|
|
wf_impl_jsonrpc_server_add(&protocol->server, "add_filesystem", &wf_impl_server_protocol_add_filesystem, protocol);
|
|
}
|
|
|
|
void wf_impl_server_protocol_cleanup(
|
|
struct wf_server_protocol * protocol)
|
|
{
|
|
free(protocol->mount_point);
|
|
protocol->is_operational = false;
|
|
|
|
wf_impl_jsonrpc_server_cleanup(&protocol->server);
|
|
wf_impl_timeout_manager_cleanup(&protocol->timeout_manager);
|
|
wf_impl_authenticators_cleanup(&protocol->authenticators);
|
|
wf_impl_session_manager_cleanup(&protocol->session_manager);
|
|
}
|
|
|
|
void wf_impl_server_protocol_add_authenticator(
|
|
struct wf_server_protocol * protocol,
|
|
char const * type,
|
|
wf_authenticate_fn * authenticate,
|
|
void * user_data)
|
|
{
|
|
wf_impl_authenticators_add(&protocol->authenticators, type, authenticate, user_data);
|
|
}
|