1
0
mirror of https://github.com/falk-werner/webfuse synced 2025-06-13 12:54:15 +00:00
falk-werner_webfuse/lib/webfuse/adapter/impl/session.c

195 lines
5.2 KiB
C
Raw Normal View History

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"
#include "webfuse/adapter/impl/jsonrpc/proxy.h"
#include "webfuse/adapter/impl/jsonrpc/request.h"
#include "webfuse/adapter/impl/jsonrpc/response.h"
2019-03-26 22:04:53 +00:00
2019-04-13 19:44:53 +00:00
#include "webfuse/core/container_of.h"
#include "webfuse/core/util.h"
2019-03-26 22:04:53 +00:00
#include <libwebsockets.h>
#include <stddef.h>
2019-04-09 20:54:41 +00:00
#include <stdlib.h>
2019-03-26 22:04:53 +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);
bool result = (session->is_authenticated || wf_impl_jsonrpc_is_response(request)) && (NULL != session->wsi);
if (result)
{
wf_message_queue_push(&session->queue, message);
lws_callback_on_writable(session->wsi);
result = true;
}
else
{
wf_message_dispose(message);
}
return result;
}
2019-04-09 20:54:41 +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,
struct wf_impl_timeout_manager * timeout_manager,
2019-04-02 21:00:03 +00:00
struct wf_impl_jsonrpc_server * server,
2019-04-13 19:44:53 +00:00
char const * mount_point)
2019-04-09 20:54:41 +00:00
{
struct wf_impl_session * session = malloc(sizeof(struct wf_impl_session));
if (NULL != session)
2019-04-02 21:00:03 +00:00
{
2019-04-16 20:09:35 +00:00
wf_slist_init(&session->filesystems);
2019-04-09 20:54:41 +00:00
2019-04-13 19:44:53 +00:00
session->mount_point = strdup(mount_point);
2019-04-09 20:54:41 +00:00
session->wsi = wsi;
session->is_authenticated = false;
session->authenticators = authenticators;
session->server = server;
wf_impl_jsonrpc_proxy_init(&session->rpc, timeout_manager, &wf_impl_session_send, session);
wf_message_queue_init(&session->queue);
2019-04-02 21:00:03 +00:00
}
2019-04-09 20:54:41 +00:00
return session;
}
2019-03-26 22:04:53 +00:00
2019-04-16 20:09:35 +00:00
static void wf_impl_session_dispose_filesystems(
struct wf_slist * filesystems)
2019-04-13 19:44:53 +00:00
{
2019-04-16 20:09:35 +00:00
struct wf_slist_item * item = filesystems->first;
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;
}
2019-04-13 19:44:53 +00:00
}
2019-04-09 20:54:41 +00:00
void wf_impl_session_dispose(
2019-03-26 22:04:53 +00:00
struct wf_impl_session * session)
{
2019-04-16 20:09:35 +00:00
wf_impl_session_dispose_filesystems(&session->filesystems);
2019-04-02 21:00:03 +00:00
2019-04-09 20:54:41 +00:00
wf_impl_jsonrpc_proxy_cleanup(&session->rpc);
wf_message_queue_cleanup(&session->queue);
session->is_authenticated = false;
session->wsi = NULL;
session->authenticators = NULL;
session->server = NULL;
2019-04-13 19:44:53 +00:00
free(session->mount_point);
2019-04-09 20:54:41 +00:00
free(session);
}
2019-03-26 22:04:53 +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);
return session->is_authenticated;
2019-03-26 22:04:53 +00:00
}
2019-04-13 19:44:53 +00:00
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);
2019-04-16 20:09:35 +00:00
wf_slist_append(&session->filesystems, &filesystem->item);
2019-04-13 19:44:53 +00:00
return (NULL != filesystem);
}
2019-03-26 22:04:53 +00:00
void wf_impl_session_onwritable(
struct wf_impl_session * session)
{
if (!wf_message_queue_empty(&session->queue))
{
struct wf_message * message = wf_message_queue_pop(&session->queue);
lws_write(session->wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
wf_message_dispose(message);
if (!wf_message_queue_empty(&session->queue))
{
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);
}
2019-04-13 19:44:53 +00:00
}
static struct wf_impl_filesystem * wf_impl_session_get_filesystem(
struct wf_impl_session * session,
struct lws * wsi)
{
2019-04-16 20:09:35 +00:00
struct wf_impl_filesystem * result = NULL;
struct wf_slist_item * item = session->filesystems.first;
while (NULL != item)
2019-04-13 19:44:53 +00:00
{
2019-04-16 20:09:35 +00:00
struct wf_slist_item * next = item->next;
struct wf_impl_filesystem * filesystem = WF_CONTAINER_OF(session->filesystems.first, struct wf_impl_filesystem, item);
2019-04-13 19:44:53 +00:00
if (wsi == filesystem->wsi)
{
2019-04-16 20:09:35 +00:00
result = filesystem;
2019-04-13 19:44:53 +00:00
break;
}
2019-04-16 20:09:35 +00:00
item = next;
2019-04-13 19:44:53 +00:00
}
2019-04-16 20:09:35 +00:00
return result;
2019-04-13 19:44:53 +00:00
}
bool wf_impl_session_contains_wsi(
2019-04-16 20:09:35 +00:00
struct wf_impl_session * session,
struct lws * wsi)
2019-04-13 19:44:53 +00:00
{
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);
}
}