2019-03-26 22:04:53 +00:00
|
|
|
#include "webfuse/adapter/impl/session.h"
|
|
|
|
#include "webfuse/adapter/impl/authenticators.h"
|
|
|
|
#include "webfuse/core/message_queue.h"
|
|
|
|
#include "webfuse/core/message.h"
|
2020-02-16 20:03:17 +00:00
|
|
|
#include "webfuse/adapter/impl/mountpoint_factory.h"
|
|
|
|
#include "webfuse/adapter/impl/mountpoint.h"
|
2019-03-26 22:04:53 +00:00
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
#include "webfuse/core/container_of.h"
|
|
|
|
#include "webfuse/core/util.h"
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
#include "wf/jsonrpc/proxy.h"
|
|
|
|
#include "wf/jsonrpc/request.h"
|
|
|
|
#include "wf/jsonrpc/response.h"
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
#include <libwebsockets.h>
|
|
|
|
#include <stddef.h>
|
2019-04-17 20:51:16 +00:00
|
|
|
#include <stdlib.h>
|
2019-03-26 22:04:53 +00:00
|
|
|
|
2019-05-19 12:33:42 +00:00
|
|
|
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
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);
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
bool result = (session->is_authenticated || wf_jsonrpc_is_response(request)) && (NULL != session->wsi);
|
2019-04-01 20:15:12 +00:00
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
2019-04-17 20:51:16 +00:00
|
|
|
wf_slist_append(&session->messages, &message->item);
|
2019-04-01 20:15:12 +00:00
|
|
|
lws_callback_on_writable(session->wsi);
|
|
|
|
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wf_message_dispose(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
struct wf_impl_session * wf_impl_session_create(
|
2019-03-26 22:04:53 +00:00
|
|
|
struct lws * wsi,
|
|
|
|
struct wf_impl_authenticators * authenticators,
|
2020-02-29 20:06:40 +00:00
|
|
|
struct wf_timer_manager * timer_manager,
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_server * server,
|
2020-02-16 20:03:17 +00:00
|
|
|
struct wf_impl_mountpoint_factory * mountpoint_factory)
|
2019-04-17 20:51:16 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
struct wf_impl_session * session = malloc(sizeof(struct wf_impl_session));
|
|
|
|
if (NULL != session)
|
|
|
|
{
|
|
|
|
wf_slist_init(&session->filesystems);
|
|
|
|
|
|
|
|
session->wsi = wsi;
|
|
|
|
session->is_authenticated = false;
|
|
|
|
session->authenticators = authenticators;
|
|
|
|
session->server = server;
|
2020-02-16 20:03:17 +00:00
|
|
|
session->mountpoint_factory = mountpoint_factory;
|
2020-03-01 15:55:58 +00:00
|
|
|
session->rpc = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session);
|
2019-04-17 20:51:16 +00:00
|
|
|
wf_slist_init(&session->messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
2019-03-26 22:04:53 +00:00
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
static void wf_impl_session_dispose_filesystems(
|
|
|
|
struct wf_slist * filesystems)
|
|
|
|
{
|
2019-04-26 18:50:57 +00:00
|
|
|
struct wf_slist_item * item = wf_slist_first(filesystems);
|
2019-04-17 20:51:16 +00:00
|
|
|
while (NULL != item)
|
|
|
|
{
|
|
|
|
struct wf_slist_item * next = item->next;
|
2019-04-25 18:08:34 +00:00
|
|
|
struct wf_impl_filesystem * filesystem = wf_container_of(item, struct wf_impl_filesystem, item);
|
2019-04-17 20:51:16 +00:00
|
|
|
wf_impl_filesystem_dispose(filesystem);
|
|
|
|
|
|
|
|
item = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wf_impl_session_dispose(
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_session * session)
|
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_proxy_dispose(session->rpc);
|
2019-04-17 20:51:16 +00:00
|
|
|
wf_message_queue_cleanup(&session->messages);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
wf_impl_session_dispose_filesystems(&session->filesystems);
|
2019-04-17 20:51:16 +00:00
|
|
|
free(session);
|
|
|
|
}
|
2019-03-26 22:04:53 +00:00
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
bool wf_impl_session_authenticate(
|
2019-03-26 22:04:53 +00:00
|
|
|
struct wf_impl_session * session,
|
|
|
|
struct wf_credentials * creds)
|
|
|
|
{
|
|
|
|
session->is_authenticated = wf_impl_authenticators_authenticate(session->authenticators, creds);
|
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
return session->is_authenticated;
|
2019-03-26 22:04:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
bool wf_impl_session_add_filesystem(
|
|
|
|
struct wf_impl_session * session,
|
|
|
|
char const * name)
|
|
|
|
{
|
2020-02-16 20:03:17 +00:00
|
|
|
bool result;
|
|
|
|
|
|
|
|
struct wf_mountpoint * mountpoint = wf_impl_mountpoint_factory_create_mountpoint(session->mountpoint_factory, name);
|
|
|
|
result = (NULL != mountpoint);
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
struct wf_impl_filesystem * filesystem = wf_impl_filesystem_create(session, name, mountpoint);
|
|
|
|
wf_slist_append(&session->filesystems, &filesystem->item);
|
|
|
|
result = (NULL != filesystem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup on error
|
|
|
|
if (!result)
|
|
|
|
{
|
|
|
|
if (NULL != mountpoint)
|
|
|
|
{
|
|
|
|
wf_impl_mountpoint_dispose(mountpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2019-04-17 20:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-26 22:04:53 +00:00
|
|
|
void wf_impl_session_onwritable(
|
|
|
|
struct wf_impl_session * session)
|
|
|
|
{
|
2019-04-17 20:51:16 +00:00
|
|
|
if (!wf_slist_empty(&session->messages))
|
|
|
|
{
|
|
|
|
struct wf_slist_item * item = wf_slist_remove_first(&session->messages);
|
2019-04-25 18:08:34 +00:00
|
|
|
struct wf_message * message = wf_container_of(item, struct wf_message, item);
|
2019-03-26 22:04:53 +00:00
|
|
|
lws_write(session->wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
|
|
|
|
wf_message_dispose(message);
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
if (!wf_slist_empty(&session->messages))
|
2019-03-26 22:04:53 +00:00
|
|
|
{
|
|
|
|
lws_callback_on_writable(session->wsi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void wf_impl_session_receive(
|
|
|
|
struct wf_impl_session * session,
|
|
|
|
char const * data,
|
|
|
|
size_t length)
|
|
|
|
{
|
2019-04-01 20:15:12 +00:00
|
|
|
json_t * message = json_loadb(data, length, 0, NULL);
|
|
|
|
if (NULL != message)
|
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
if (wf_jsonrpc_is_response(message))
|
2019-04-01 20:15:12 +00:00
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_proxy_onresult(session->rpc, message);
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
2020-03-01 15:55:58 +00:00
|
|
|
else if (wf_jsonrpc_is_request(message))
|
2019-04-01 20:15:12 +00:00
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_server_process(session->server, message, &wf_impl_session_send, session);
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
}
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct wf_impl_filesystem * wf_impl_session_get_filesystem(
|
|
|
|
struct wf_impl_session * session,
|
|
|
|
struct lws * wsi)
|
|
|
|
{
|
|
|
|
struct wf_impl_filesystem * result = NULL;
|
|
|
|
|
2019-04-26 18:50:57 +00:00
|
|
|
struct wf_slist_item * item = wf_slist_first(&session->filesystems);
|
2019-04-17 20:51:16 +00:00
|
|
|
while (NULL != item)
|
|
|
|
{
|
|
|
|
struct wf_slist_item * next = item->next;
|
2019-04-26 18:50:57 +00:00
|
|
|
struct wf_impl_filesystem * filesystem = wf_container_of(item, struct wf_impl_filesystem, item);
|
2019-04-17 20:51:16 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|