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/session.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

198 lines
5.4 KiB
C

#include "webfuse/adapter/impl/session.h"
#include "webfuse/adapter/impl/authenticators.h"
#include "webfuse/core/message_queue.h"
#include "webfuse/core/message.h"
#include "webfuse/adapter/impl/jsonrpc/proxy.h"
#include "webfuse/adapter/impl/jsonrpc/request.h"
#include "webfuse/adapter/impl/jsonrpc/response.h"
#include "webfuse/core/container_of.h"
#include "webfuse/core/util.h"
#include <libwebsockets.h>
#include <stddef.h>
#include <stdlib.h>
#define WF_DEFAULT_TIMEOUT (10 * 1000)
static bool wf_impl_session_send(
json_t * request,
void * user_data)
{
struct wf_impl_session * session = user_data;
struct wf_message * message = wf_message_create(request);
bool result = (session->is_authenticated || wf_impl_jsonrpc_is_response(request)) && (NULL != session->wsi);
if (result)
{
wf_slist_append(&session->messages, &message->item);
lws_callback_on_writable(session->wsi);
result = true;
}
else
{
wf_message_dispose(message);
}
return result;
}
struct wf_impl_session * wf_impl_session_create(
struct lws * wsi,
struct wf_impl_authenticators * authenticators,
struct wf_impl_timeout_manager * timeout_manager,
struct wf_impl_jsonrpc_server * server,
char const * mount_point)
{
struct wf_impl_session * session = malloc(sizeof(struct wf_impl_session));
if (NULL != session)
{
wf_slist_init(&session->filesystems);
session->mount_point = strdup(mount_point);
session->wsi = wsi;
session->is_authenticated = false;
session->authenticators = authenticators;
session->server = server;
wf_impl_jsonrpc_proxy_init(&session->rpc, timeout_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session);
wf_slist_init(&session->messages);
}
return session;
}
static void wf_impl_session_dispose_filesystems(
struct wf_slist * filesystems)
{
struct wf_slist_item * item = wf_slist_first(filesystems);
while (NULL != item)
{
struct wf_slist_item * next = item->next;
struct wf_impl_filesystem * filesystem = wf_container_of(item, struct wf_impl_filesystem, item);
wf_impl_filesystem_dispose(filesystem);
item = next;
}
}
void wf_impl_session_dispose(
struct wf_impl_session * session)
{
wf_impl_jsonrpc_proxy_cleanup(&session->rpc);
wf_message_queue_cleanup(&session->messages);
wf_impl_session_dispose_filesystems(&session->filesystems);
session->is_authenticated = false;
session->wsi = NULL;
session->authenticators = NULL;
session->server = NULL;
free(session->mount_point);
free(session);
}
bool wf_impl_session_authenticate(
struct wf_impl_session * session,
struct wf_credentials * creds)
{
session->is_authenticated = wf_impl_authenticators_authenticate(session->authenticators, creds);
return session->is_authenticated;
}
bool wf_impl_session_add_filesystem(
struct wf_impl_session * session,
char const * name)
{
struct wf_impl_filesystem * filesystem = wf_impl_filesystem_create(session, name);
wf_slist_append(&session->filesystems, &filesystem->item);
return (NULL != filesystem);
}
void wf_impl_session_onwritable(
struct wf_impl_session * session)
{
if (!wf_slist_empty(&session->messages))
{
struct wf_slist_item * item = wf_slist_remove_first(&session->messages);
struct wf_message * message = wf_container_of(item, struct wf_message, item);
lws_write(session->wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
wf_message_dispose(message);
if (!wf_slist_empty(&session->messages))
{
lws_callback_on_writable(session->wsi);
}
}
}
void wf_impl_session_receive(
struct wf_impl_session * session,
char const * data,
size_t length)
{
json_t * message = json_loadb(data, length, 0, NULL);
if (NULL != message)
{
if (wf_impl_jsonrpc_is_response(message))
{
wf_impl_jsonrpc_proxy_onresult(&session->rpc, message);
}
else if (wf_impl_jsonrpc_is_request(message))
{
wf_impl_jsonrpc_server_process(session->server, message, &wf_impl_session_send, session);
}
json_decref(message);
}
}
static struct wf_impl_filesystem * wf_impl_session_get_filesystem(
struct wf_impl_session * session,
struct lws * wsi)
{
struct wf_impl_filesystem * result = NULL;
struct wf_slist_item * item = wf_slist_first(&session->filesystems);
while (NULL != item)
{
struct wf_slist_item * next = item->next;
struct wf_impl_filesystem * filesystem = wf_container_of(item, struct wf_impl_filesystem, item);
if (wsi == filesystem->wsi)
{
result = filesystem;
break;
}
item = next;
}
return result;
}
bool wf_impl_session_contains_wsi(
struct wf_impl_session * session,
struct lws * wsi)
{
bool const result = (NULL != wsi) && ((wsi == session->wsi) || (NULL != wf_impl_session_get_filesystem(session, wsi)));
return result;
}
void wf_impl_session_process_filesystem_request(
struct wf_impl_session * session,
struct lws * wsi)
{
struct wf_impl_filesystem * filesystem = wf_impl_session_get_filesystem(session, wsi);
if (NULL != filesystem)
{
wf_impl_filesystem_process_request(filesystem);
}
}