feat(authentication): provide an authentication mechanism (#19)
* moves server into session * renames jsonrpc server to jsonrpc proxy * moves server into session * renames jsonrpc server to jsonrpc proxy * adds json rpc server * removes obsolete proxy from protocol * changes interface of jsonrpc_proxy_onresult to accept previously parsed messages * adds infrastructure to process incoming requests; fixes invalid read of ill formatted responses * adds description of authentication request * adds authentication request * adds userdb for authentication purposes * removes debug code: console.log() * fixes cmake build error (missing openssl symbols) * fixes typo * replaces ASCII art by UML diagram * renames BAD_NOACCESS to BAD_ACCESS_DENIED * fixes style * adds docu of authentication * ignored false positives of flawfinder * fixes style issues * fixes javascript style issuespull/27/head
parent
1d413456a2
commit
87f34fa768
After Width: | Height: | Size: 37 KiB |
@ -0,0 +1,28 @@
|
|||||||
|
@startuml
|
||||||
|
participant "Filesystem Provider\n(e.g. Webbrowser)" as provider
|
||||||
|
participant "webfuse\ndaemon" as daemon
|
||||||
|
actor "user" as user
|
||||||
|
|
||||||
|
group directory listing fails without authentication
|
||||||
|
user -> daemon : ls
|
||||||
|
daemon -> daemon : is_authenticated
|
||||||
|
daemon -->x user : error: no entry
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
group authenticate
|
||||||
|
provider -> daemon: authenticate(type, credentials)
|
||||||
|
daemon -> daemon: get_authenticator(type)
|
||||||
|
daemon -> daemon: check(credentials)
|
||||||
|
daemon --> provider: result
|
||||||
|
end
|
||||||
|
|
||||||
|
group directory listing succeeds after authentication
|
||||||
|
user -> daemon : ls
|
||||||
|
daemon -> daemon : is_authenticated
|
||||||
|
daemon -> provider : readdir
|
||||||
|
provider --> daemon : readdir_resp
|
||||||
|
daemon --> user : [., ..]
|
||||||
|
end
|
||||||
|
|
||||||
|
@enduml
|
After Width: | Height: | Size: 30 KiB |
@ -0,0 +1,30 @@
|
|||||||
|
@startuml
|
||||||
|
participant "Filesystem Provider\n(e.g. Webbrowser)" as provider
|
||||||
|
participant "webfuse\ndaemon" as daemon
|
||||||
|
actor "user" as user
|
||||||
|
|
||||||
|
group startup
|
||||||
|
daemon -> daemon : fuse_mount
|
||||||
|
daemon -> daemon : start ws
|
||||||
|
end
|
||||||
|
...
|
||||||
|
|
||||||
|
group connect
|
||||||
|
provider -> daemon : connect
|
||||||
|
opt authentication
|
||||||
|
provider -> daemon: authenticate
|
||||||
|
daemon -> daemon: check credentials
|
||||||
|
daemon --> provider: okay
|
||||||
|
end
|
||||||
|
end
|
||||||
|
...
|
||||||
|
|
||||||
|
group directory listing
|
||||||
|
user -> daemon : ls
|
||||||
|
daemon -> provider : readdir
|
||||||
|
provider --> daemon : readdir_resp
|
||||||
|
daemon --> user : [., ..]
|
||||||
|
end
|
||||||
|
...
|
||||||
|
|
||||||
|
@enduml
|
@ -1,31 +1,40 @@
|
|||||||
#ifndef WF_ADAPTER_IMPL_JSONRPC_METHOD_H
|
#ifndef WF_ADAPTER_IMPL_JSONRPC_METHOD_H
|
||||||
#define WF_ADAPTER_IMPL_JSONRPC_METHOD_H
|
#define WF_ADAPTER_IMPL_JSONRPC_METHOD_H
|
||||||
|
|
||||||
#ifndef __cplusplus
|
|
||||||
#include <stdbool.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <jansson.h>
|
#include <jansson.h>
|
||||||
#include "webfuse/core/status.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef bool wf_impl_jsonrpc_method_invoke_fn(
|
struct wf_impl_jsonrpc_request;
|
||||||
void * user_data,
|
|
||||||
struct json_t const * method_call);
|
|
||||||
|
|
||||||
typedef void wf_impl_jsonrpc_method_finished_fn(
|
typedef void wf_impl_jsonrpc_method_invoke_fn(
|
||||||
void * user_data,
|
struct wf_impl_jsonrpc_request * request,
|
||||||
wf_status status,
|
char const * method_name,
|
||||||
struct json_t const * result);
|
json_t * params,
|
||||||
|
void * user_data);
|
||||||
|
|
||||||
|
struct wf_impl_jsonrpc_method
|
||||||
|
{
|
||||||
|
struct wf_impl_jsonrpc_method * next;
|
||||||
|
char * name;
|
||||||
|
wf_impl_jsonrpc_method_invoke_fn * invoke;
|
||||||
|
void * user_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create(
|
||||||
|
char const * method_name,
|
||||||
|
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
||||||
|
void * user_data);
|
||||||
|
|
||||||
|
extern void wf_impl_jsonrpc_method_dispose(
|
||||||
|
struct wf_impl_jsonrpc_method * method);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
#ifndef WF_ADAPTER_IMPL_JSONRPC_METHOD_INTERN_H
|
|
||||||
#define WF_ADAPTER_IMPL_JSONRPC_METHOD_INTERN_H
|
|
||||||
|
|
||||||
#include "webfuse/adapter/impl/jsonrpc/method.h"
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct wf_impl_jsonrpc_method
|
|
||||||
{
|
|
||||||
struct wf_impl_jsonrpc_method * next;
|
|
||||||
char * name;
|
|
||||||
wf_impl_jsonrpc_method_invoke_fn * invoke;
|
|
||||||
void * user_data;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create(
|
|
||||||
char const * name,
|
|
||||||
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
|
||||||
void * user_data);
|
|
||||||
|
|
||||||
extern void wf_impl_jsonrpc_method_dispose(
|
|
||||||
struct wf_impl_jsonrpc_method * method);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
@ -0,0 +1,181 @@
|
|||||||
|
#include "webfuse/adapter/impl/jsonrpc/proxy.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "webfuse/adapter/impl/jsonrpc/response.h"
|
||||||
|
|
||||||
|
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
||||||
|
|
||||||
|
static void wf_impl_jsonrpc_proxy_timeout(
|
||||||
|
struct wf_impl_timer * timer)
|
||||||
|
{
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy = timer->user_data;
|
||||||
|
|
||||||
|
if (proxy->request.is_pending)
|
||||||
|
{
|
||||||
|
wf_impl_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||||
|
void * user_data = proxy->request.user_data;
|
||||||
|
|
||||||
|
proxy->request.is_pending = false;
|
||||||
|
proxy->request.id = 0;
|
||||||
|
proxy->request.user_data = NULL;
|
||||||
|
proxy->request.finished = NULL;
|
||||||
|
wf_impl_timer_cancel(&proxy->request.timer);
|
||||||
|
|
||||||
|
finished(user_data, WF_BAD_TIMEOUT, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static json_t * wf_impl_jsonrpc_request_create(
|
||||||
|
char const * method,
|
||||||
|
int id,
|
||||||
|
char const * param_info,
|
||||||
|
va_list args)
|
||||||
|
{
|
||||||
|
json_t * request = json_object();
|
||||||
|
json_object_set_new(request, "method", json_string(method));
|
||||||
|
json_t * params = json_array();
|
||||||
|
|
||||||
|
for (char const * param_type = param_info; '\0' != *param_type; param_type++)
|
||||||
|
{
|
||||||
|
switch(*param_type)
|
||||||
|
{
|
||||||
|
case 's':
|
||||||
|
{
|
||||||
|
char const * const value = va_arg(args, char const *);
|
||||||
|
json_array_append_new(params, json_string(value));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'i':
|
||||||
|
{
|
||||||
|
int const value = va_arg(args, int);
|
||||||
|
json_array_append_new(params, json_integer(value));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
json_object_set_new(request, "params", params);
|
||||||
|
if (0 != id)
|
||||||
|
{
|
||||||
|
json_object_set_new(request, "id", json_integer(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wf_impl_jsonrpc_proxy_init(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy,
|
||||||
|
struct wf_impl_timeout_manager * timeout_manager,
|
||||||
|
wf_impl_jsonrpc_send_fn * send,
|
||||||
|
void * user_data)
|
||||||
|
{
|
||||||
|
proxy->send = send;
|
||||||
|
proxy->user_data = user_data;
|
||||||
|
proxy->request.is_pending = false;
|
||||||
|
|
||||||
|
wf_impl_timer_init(&proxy->request.timer, timeout_manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wf_impl_jsonrpc_proxy_cleanup(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy)
|
||||||
|
{
|
||||||
|
wf_impl_timer_cleanup(&proxy->request.timer);
|
||||||
|
|
||||||
|
if (proxy->request.is_pending)
|
||||||
|
{
|
||||||
|
proxy->request.finished(proxy->request.user_data, WF_BAD, NULL);
|
||||||
|
proxy->request.is_pending = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wf_impl_jsonrpc_proxy_invoke(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy,
|
||||||
|
wf_impl_jsonrpc_proxy_finished_fn * finished,
|
||||||
|
void * user_data,
|
||||||
|
char const * method_name,
|
||||||
|
char const * param_info,
|
||||||
|
...
|
||||||
|
)
|
||||||
|
{
|
||||||
|
if (!proxy->request.is_pending)
|
||||||
|
{
|
||||||
|
proxy->request.is_pending = true;
|
||||||
|
proxy->request.finished = finished;
|
||||||
|
proxy->request.user_data = user_data;
|
||||||
|
proxy->request.id = 42;
|
||||||
|
wf_impl_timer_start(&proxy->request.timer, wf_impl_timepoint_in_msec(WF_DEFAULT_TIMEOUT),
|
||||||
|
&wf_impl_jsonrpc_proxy_timeout, proxy);
|
||||||
|
|
||||||
|
va_list args;
|
||||||
|
va_start(args, param_info);
|
||||||
|
json_t * request = wf_impl_jsonrpc_request_create(method_name, proxy->request.id, param_info, args);
|
||||||
|
va_end(args);
|
||||||
|
if (NULL != request)
|
||||||
|
{
|
||||||
|
if (!proxy->send(request, proxy->user_data))
|
||||||
|
{
|
||||||
|
proxy->request.is_pending = false;
|
||||||
|
proxy->request.finished = NULL;
|
||||||
|
proxy->request.user_data = NULL;
|
||||||
|
proxy->request.id = 0;
|
||||||
|
wf_impl_timer_cancel(&proxy->request.timer);
|
||||||
|
|
||||||
|
finished(user_data, WF_BAD, NULL);
|
||||||
|
}
|
||||||
|
json_decref(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
finished(user_data, WF_BAD_BUSY, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern void wf_impl_jsonrpc_proxy_notify(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy,
|
||||||
|
char const * method_name,
|
||||||
|
char const * param_info,
|
||||||
|
...
|
||||||
|
)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, param_info);
|
||||||
|
json_t * request = wf_impl_jsonrpc_request_create(method_name, 0, param_info, args);
|
||||||
|
va_end(args);
|
||||||
|
if (NULL != request)
|
||||||
|
{
|
||||||
|
proxy->send(request, proxy->user_data);
|
||||||
|
json_decref(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void wf_impl_jsonrpc_proxy_onresult(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy,
|
||||||
|
json_t * message)
|
||||||
|
{
|
||||||
|
struct wf_impl_jsonrpc_response response;
|
||||||
|
wf_impl_jsonrpc_response_init(&response, message);
|
||||||
|
|
||||||
|
if ((proxy->request.is_pending) && (response.id == proxy->request.id))
|
||||||
|
{
|
||||||
|
wf_impl_jsonrpc_proxy_finished_fn * finished = proxy->request.finished;
|
||||||
|
void * user_data = proxy->request.user_data;
|
||||||
|
|
||||||
|
proxy->request.is_pending = false;
|
||||||
|
proxy->request.id = 0;
|
||||||
|
proxy->request.user_data = NULL;
|
||||||
|
proxy->request.finished = NULL;
|
||||||
|
wf_impl_timer_cancel(&proxy->request.timer);
|
||||||
|
|
||||||
|
finished(user_data, response.status, response.result);
|
||||||
|
}
|
||||||
|
|
||||||
|
wf_impl_jsonrpc_response_cleanup(&response);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,80 @@
|
|||||||
|
#ifndef WF_ADAPTER_IMPL_JSONRPC_PROXY_H
|
||||||
|
#define WF_ADAPTER_IMPL_JSONRPC_PROXY_H
|
||||||
|
|
||||||
|
#ifndef __cplusplus
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#else
|
||||||
|
#include <cstdarg>
|
||||||
|
#include <cstddef>
|
||||||
|
using std::size_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <jansson.h>
|
||||||
|
#include "webfuse/adapter/impl/jsonrpc/send_fn.h"
|
||||||
|
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||||
|
#include "webfuse/adapter/impl/time/timer.h"
|
||||||
|
#include "webfuse/core/status.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef void wf_impl_jsonrpc_proxy_finished_fn(
|
||||||
|
void * user_data,
|
||||||
|
wf_status status,
|
||||||
|
struct json_t const * result);
|
||||||
|
|
||||||
|
|
||||||
|
struct wf_impl_jsonrpc_request
|
||||||
|
{
|
||||||
|
bool is_pending;
|
||||||
|
wf_impl_jsonrpc_proxy_finished_fn * finished;
|
||||||
|
void * user_data;
|
||||||
|
int id;
|
||||||
|
struct wf_impl_timer timer;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct wf_impl_jsonrpc_proxy
|
||||||
|
{
|
||||||
|
struct wf_impl_jsonrpc_request request;
|
||||||
|
wf_impl_jsonrpc_send_fn * send;
|
||||||
|
void * user_data;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern void wf_impl_jsonrpc_proxy_init(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy,
|
||||||
|
struct wf_impl_timeout_manager * manager,
|
||||||
|
wf_impl_jsonrpc_send_fn * send,
|
||||||
|
void * user_data);
|
||||||
|
|
||||||
|
extern void wf_impl_jsonrpc_proxy_cleanup(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy);
|
||||||
|
|
||||||
|
extern void wf_impl_jsonrpc_proxy_invoke(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy,
|
||||||
|
wf_impl_jsonrpc_proxy_finished_fn * finished,
|
||||||
|
void * user_data,
|
||||||
|
char const * method_name,
|
||||||
|
char const * param_info,
|
||||||
|
...
|
||||||
|
);
|
||||||
|
|
||||||
|
extern void wf_impl_jsonrpc_proxy_notify(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy,
|
||||||
|
char const * method_name,
|
||||||
|
char const * param_info,
|
||||||
|
...
|
||||||
|
);
|
||||||
|
|
||||||
|
extern void wf_impl_jsonrpc_proxy_onresult(
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy,
|
||||||
|
json_t * message);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -1,44 +1,82 @@
|
|||||||
#include "webfuse/adapter/impl/jsonrpc/request.h"
|
#include "webfuse/adapter/impl/jsonrpc/request.h"
|
||||||
|
#include "webfuse/core/status_intern.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
json_t * wf_impl_jsonrpc_request_create(
|
struct wf_impl_jsonrpc_request
|
||||||
char const * method,
|
|
||||||
int id,
|
|
||||||
char const * param_info,
|
|
||||||
va_list args)
|
|
||||||
{
|
{
|
||||||
json_t * request = json_object();
|
int id;
|
||||||
json_object_set_new(request, "method", json_string(method));
|
wf_impl_jsonrpc_send_fn * send;
|
||||||
json_t * params = json_array();
|
void * user_data;
|
||||||
|
};
|
||||||
|
|
||||||
for (char const * param_type = param_info; '\0' != *param_type; param_type++)
|
bool wf_impl_jsonrpc_is_request(
|
||||||
{
|
json_t * message)
|
||||||
switch(*param_type)
|
{
|
||||||
{
|
json_t * id = json_object_get(message, "id");
|
||||||
case 's':
|
json_t * method = json_object_get(message, "method");
|
||||||
{
|
json_t * params = json_object_get(message, "params");
|
||||||
char const * const value = va_arg(args, char const *);
|
|
||||||
json_array_append_new(params, json_string(value));
|
return (json_is_integer(id) && json_is_string(method) &&
|
||||||
}
|
(json_is_array(params) || json_is_object(params)));
|
||||||
break;
|
}
|
||||||
case 'i':
|
|
||||||
{
|
|
||||||
int const value = va_arg(args, int);
|
|
||||||
json_array_append_new(params, json_integer(value));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
json_object_set_new(request, "params", params);
|
struct wf_impl_jsonrpc_request * wf_impl_jsonrpc_request_create(
|
||||||
if (0 != id)
|
int id,
|
||||||
|
wf_impl_jsonrpc_send_fn * send,
|
||||||
|
void * user_data)
|
||||||
|
{
|
||||||
|
struct wf_impl_jsonrpc_request * request = malloc(sizeof(struct wf_impl_jsonrpc_request));
|
||||||
|
if (NULL != request)
|
||||||
{
|
{
|
||||||
json_object_set_new(request, "id", json_integer(id));
|
request->id = id;
|
||||||
|
request->send = send;
|
||||||
|
request->user_data = user_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wf_impl_jsonrpc_request_dispose(
|
||||||
|
struct wf_impl_jsonrpc_request * request)
|
||||||
|
{
|
||||||
|
free(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
void * wf_impl_jsonrpc_request_get_userdata(
|
||||||
|
struct wf_impl_jsonrpc_request * request)
|
||||||
|
{
|
||||||
|
return request->user_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void wf_impl_jsonrpc_respond(
|
||||||
|
struct wf_impl_jsonrpc_request * request,
|
||||||
|
json_t * result)
|
||||||
|
{
|
||||||
|
json_t * response = json_object();
|
||||||
|
json_object_set_new(response, "result", result);
|
||||||
|
json_object_set_new(response, "id", json_integer(request->id));
|
||||||
|
|
||||||
|
request->send(response, request->user_data);
|
||||||
|
json_decref(response);
|
||||||
|
wf_impl_jsonrpc_request_dispose(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wf_impl_jsonrpc_respond_error(
|
||||||
|
struct wf_impl_jsonrpc_request * request,
|
||||||
|
wf_status status)
|
||||||
|
{
|
||||||
|
json_t * err = json_object();
|
||||||
|
json_object_set_new(err, "code", json_integer(status));
|
||||||
|
json_object_set_new(err, "message", json_string(wf_status_tostring(status)));
|
||||||
|
|
||||||
|
json_t * response = json_object();
|
||||||
|
json_object_set_new(response, "error", err);
|
||||||
|
json_object_set_new(response, "id", json_integer(request->id));
|
||||||
|
|
||||||
|
request->send(response, request->user_data);
|
||||||
|
json_decref(response);
|
||||||
|
wf_impl_jsonrpc_request_dispose(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef WF_ADAPTER_IMPL_JSONRPC_SEND_FN_H
|
||||||
|
#define WF_ADAPTER_IMPL_JSONRPC_SEND_FN_H
|
||||||
|
|
||||||
|
#ifndef __cplusplus
|
||||||
|
#include <stdbool.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <jansson.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef bool wf_impl_jsonrpc_send_fn(
|
||||||
|
json_t * request,
|
||||||
|
void * user_data);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
@ -1,186 +1,95 @@
|
|||||||
#include "webfuse/adapter/impl/jsonrpc/server.h"
|
#include "webfuse/adapter/impl/jsonrpc/server.h"
|
||||||
#include <string.h>
|
#include "webfuse/adapter/impl/jsonrpc/method.h"
|
||||||
|
|
||||||
#include "webfuse/adapter/impl/jsonrpc/method_intern.h"
|
|
||||||
#include "webfuse/adapter/impl/jsonrpc/request.h"
|
#include "webfuse/adapter/impl/jsonrpc/request.h"
|
||||||
#include "webfuse/adapter/impl/jsonrpc/response.h"
|
#include "webfuse/core/util.h"
|
||||||
|
|
||||||
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
|
||||||
|
|
||||||
static struct wf_impl_jsonrpc_method const * wf_impl_jsonrpc_server_getmethod(
|
|
||||||
struct wf_impl_jsonrpc_server * server,
|
|
||||||
char const * name)
|
|
||||||
{
|
|
||||||
struct wf_impl_jsonrpc_method * method = server->methods;
|
|
||||||
while ((NULL != method) && (0 == strcmp(name, method->name)))
|
|
||||||
{
|
|
||||||
method = method->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return method;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void wf_impl_jsonrpc_server_timeout(
|
|
||||||
struct wf_impl_timer * timer)
|
|
||||||
{
|
|
||||||
struct wf_impl_jsonrpc_server * server = timer->user_data;
|
|
||||||
|
|
||||||
if (server->request.is_pending)
|
|
||||||
{
|
|
||||||
wf_impl_jsonrpc_method_finished_fn * finished = server->request.finished;
|
|
||||||
void * user_data = server->request.user_data;
|
|
||||||
|
|
||||||
server->request.is_pending = false;
|
|
||||||
server->request.id = 0;
|
|
||||||
server->request.user_data = NULL;
|
|
||||||
server->request.finished = NULL;
|
|
||||||
wf_impl_timer_cancel(&server->request.timer);
|
|
||||||
|
|
||||||
finished(user_data, WF_BAD_TIMEOUT, NULL);
|
#include <string.h>
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void wf_impl_jsonrpc_server_init(
|
void wf_impl_jsonrpc_server_init(
|
||||||
struct wf_impl_jsonrpc_server * server,
|
struct wf_impl_jsonrpc_server * server)
|
||||||
struct wf_impl_timeout_manager * timeout_manager)
|
|
||||||
{
|
{
|
||||||
server->methods = NULL;
|
server->methods = NULL;
|
||||||
server->request.is_pending = false;
|
|
||||||
|
|
||||||
wf_impl_timer_init(&server->request.timer, timeout_manager);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wf_impl_jsonrpc_server_cleanup(
|
void wf_impl_jsonrpc_server_cleanup(
|
||||||
struct wf_impl_jsonrpc_server * server)
|
struct wf_impl_jsonrpc_server * server)
|
||||||
{
|
{
|
||||||
wf_impl_timer_cleanup(&server->request.timer);
|
struct wf_impl_jsonrpc_method * current = server->methods;
|
||||||
|
while (NULL != current)
|
||||||
if (server->request.is_pending)
|
|
||||||
{
|
|
||||||
server->request.finished(server->request.user_data, WF_BAD, NULL);
|
|
||||||
server->request.is_pending = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct wf_impl_jsonrpc_method * method = server->methods;
|
|
||||||
while (NULL != method)
|
|
||||||
{
|
{
|
||||||
struct wf_impl_jsonrpc_method * next = method->next;
|
struct wf_impl_jsonrpc_method * next = current->next;
|
||||||
method->next = NULL;
|
wf_impl_jsonrpc_method_dispose(current);
|
||||||
wf_impl_jsonrpc_method_dispose(method);
|
current = next;
|
||||||
method = next;
|
|
||||||
}
|
}
|
||||||
server->methods = NULL;
|
server->methods = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wf_impl_jsonrpc_server_add(
|
void wf_impl_jsonrpc_server_add(
|
||||||
struct wf_impl_jsonrpc_server * server,
|
struct wf_impl_jsonrpc_server * server,
|
||||||
char const * name,
|
char const * method_name,
|
||||||
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
||||||
void * user_data)
|
void * user_data)
|
||||||
{
|
{
|
||||||
struct wf_impl_jsonrpc_method * method = wf_impl_jsonrpc_method_create(name, invoke, user_data);
|
struct wf_impl_jsonrpc_method * method = wf_impl_jsonrpc_method_create(method_name, invoke, user_data);
|
||||||
method->next = server->methods;
|
method->next = server->methods;
|
||||||
server->methods = method;
|
server->methods = method;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wf_impl_jsonrpc_server_invoke(
|
static void wf_impl_jsonrpc_server_invalid_method_invoke(
|
||||||
struct wf_impl_jsonrpc_server * server,
|
struct wf_impl_jsonrpc_request * request,
|
||||||
wf_impl_jsonrpc_method_finished_fn * finished,
|
char const * WF_UNUSED_PARAM(method_name),
|
||||||
void * user_data,
|
json_t * WF_UNUSED_PARAM(params),
|
||||||
char const * method_name,
|
void * WF_UNUSED_PARAM(user_data))
|
||||||
char const * param_info,
|
|
||||||
...
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
if (!server->request.is_pending)
|
wf_impl_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED);
|
||||||
{
|
|
||||||
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_getmethod(server, method_name);
|
|
||||||
if (NULL != method)
|
|
||||||
{
|
|
||||||
server->request.is_pending = true;
|
|
||||||
server->request.finished = finished;
|
|
||||||
server->request.user_data = user_data;
|
|
||||||
server->request.id = 42;
|
|
||||||
wf_impl_timer_start(&server->request.timer, wf_impl_timepoint_in_msec(WF_DEFAULT_TIMEOUT),
|
|
||||||
&wf_impl_jsonrpc_server_timeout, server);
|
|
||||||
|
|
||||||
va_list args;
|
|
||||||
va_start(args, param_info);
|
|
||||||
json_t * request = wf_impl_jsonrpc_request_create(method_name, server->request.id, param_info, args);
|
|
||||||
va_end(args);
|
|
||||||
if (NULL != request)
|
|
||||||
{
|
|
||||||
if (!method->invoke(method->user_data, request))
|
|
||||||
{
|
|
||||||
server->request.is_pending = false;
|
|
||||||
server->request.finished = NULL;
|
|
||||||
server->request.user_data = NULL;
|
|
||||||
server->request.id = 0;
|
|
||||||
wf_impl_timer_cancel(&server->request.timer);
|
|
||||||
|
|
||||||
finished(user_data, WF_BAD, NULL);
|
|
||||||
}
|
|
||||||
json_decref(request);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
finished(user_data, WF_BAD_NOTIMPLEMENTED, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
finished(user_data, WF_BAD_BUSY, NULL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void wf_impl_jsonrpc_server_notify(
|
static struct wf_impl_jsonrpc_method const wf_impl_jsonrpc_server_invalid_method =
|
||||||
|
{
|
||||||
|
.next = NULL,
|
||||||
|
.name = "<invalid>",
|
||||||
|
.invoke = &wf_impl_jsonrpc_server_invalid_method_invoke,
|
||||||
|
.user_data = NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct wf_impl_jsonrpc_method const * wf_impl_jsonrpc_server_get_method(
|
||||||
struct wf_impl_jsonrpc_server * server,
|
struct wf_impl_jsonrpc_server * server,
|
||||||
char const * method_name,
|
char const * method_name)
|
||||||
char const * param_info,
|
|
||||||
...
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_getmethod(server, method_name);
|
struct wf_impl_jsonrpc_method const * current = server->methods;
|
||||||
if (NULL != method)
|
while (NULL != current)
|
||||||
{
|
{
|
||||||
|
if (0 == strcmp(method_name, current->name))
|
||||||
va_list args;
|
|
||||||
va_start(args, param_info);
|
|
||||||
json_t * request = wf_impl_jsonrpc_request_create(method_name, 0, param_info, args);
|
|
||||||
va_end(args);
|
|
||||||
if (NULL != request)
|
|
||||||
{
|
{
|
||||||
method->invoke(method->user_data, request);
|
return current;
|
||||||
json_decref(request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return &wf_impl_jsonrpc_server_invalid_method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wf_impl_jsonrpc_server_process(
|
||||||
void wf_impl_jsonrpc_server_onresult(
|
|
||||||
struct wf_impl_jsonrpc_server * server,
|
struct wf_impl_jsonrpc_server * server,
|
||||||
char const * message,
|
json_t * request_data,
|
||||||
size_t length)
|
wf_impl_jsonrpc_send_fn * send,
|
||||||
|
void * user_data)
|
||||||
{
|
{
|
||||||
struct wf_impl_jsonrpc_response response;
|
json_t * method_holder = json_object_get(request_data, "method");
|
||||||
wf_impl_jsonrpc_response_init(&response, message, length);
|
json_t * params = json_object_get(request_data, "params");
|
||||||
|
json_t * id_holder = json_object_get(request_data, "id");
|
||||||
|
|
||||||
if ((server->request.is_pending) && (response.id == server->request.id))
|
if (json_is_string(method_holder) &&
|
||||||
|
(json_is_array(params) || (json_is_object(params))) &&
|
||||||
|
json_is_integer(id_holder))
|
||||||
{
|
{
|
||||||
wf_impl_jsonrpc_method_finished_fn * finished = server->request.finished;
|
char const * method_name = json_string_value(method_holder);
|
||||||
void * user_data = server->request.user_data;
|
int id = json_integer_value(id_holder);
|
||||||
|
struct wf_impl_jsonrpc_request * request = wf_impl_jsonrpc_request_create(id, send, user_data);
|
||||||
server->request.is_pending = false;
|
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_get_method(server, method_name);
|
||||||
server->request.id = 0;
|
|
||||||
server->request.user_data = NULL;
|
|
||||||
server->request.finished = NULL;
|
|
||||||
wf_impl_timer_cancel(&server->request.timer);
|
|
||||||
|
|
||||||
finished(user_data, response.status, response.result);
|
method->invoke(request, method_name, params, method->user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
wf_impl_jsonrpc_response_cleanup(&response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
#include "webfuse/adapter/impl/operations.h"
|
||||||
|
#include "webfuse/adapter/impl/session_manager.h"
|
||||||
|
#include "webfuse/adapter/impl/session.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
struct wf_impl_jsonrpc_proxy * wf_impl_operations_context_get_proxy(
|
||||||
|
struct wf_impl_operations_context * context,
|
||||||
|
fuse_ino_t inode)
|
||||||
|
{
|
||||||
|
struct wf_impl_jsonrpc_proxy * proxy = NULL;
|
||||||
|
|
||||||
|
struct wf_impl_session_manager * session_manger = context->session_manager;
|
||||||
|
struct wf_impl_session * session = wf_impl_session_manager_get_by_inode(session_manger, inode);
|
||||||
|
if (NULL != session)
|
||||||
|
{
|
||||||
|
proxy = &session->rpc;
|
||||||
|
}
|
||||||
|
|
||||||
|
return proxy;
|
||||||
|
}
|
Loading…
Reference in new issue