mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
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 issues
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
#include "webfuse/adapter/impl/jsonrpc/method_intern.h"
|
||||
|
||||
#include "webfuse/adapter/impl/jsonrpc/method.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create(
|
||||
char const * name,
|
||||
char const * method_name,
|
||||
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data)
|
||||
{
|
||||
@@ -12,7 +11,7 @@ struct wf_impl_jsonrpc_method * wf_impl_jsonrpc_method_create(
|
||||
if (NULL != method)
|
||||
{
|
||||
method->next = NULL;
|
||||
method->name = strdup(name);
|
||||
method->name = strdup(method_name);
|
||||
method->invoke = invoke;
|
||||
method->user_data = user_data;
|
||||
}
|
||||
|
||||
@@ -1,31 +1,40 @@
|
||||
#ifndef WF_ADAPTER_IMPL_JSONRPC_METHOD_H
|
||||
#define WF_ADAPTER_IMPL_JSONRPC_METHOD_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse/core/status.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef bool wf_impl_jsonrpc_method_invoke_fn(
|
||||
void * user_data,
|
||||
struct json_t const * method_call);
|
||||
struct wf_impl_jsonrpc_request;
|
||||
|
||||
typedef void wf_impl_jsonrpc_method_finished_fn(
|
||||
void * user_data,
|
||||
wf_status status,
|
||||
struct json_t const * result);
|
||||
typedef void wf_impl_jsonrpc_method_invoke_fn(
|
||||
struct wf_impl_jsonrpc_request * request,
|
||||
char const * method_name,
|
||||
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
|
||||
}
|
||||
#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
|
||||
181
lib/webfuse/adapter/impl/jsonrpc/proxy.c
Normal file
181
lib/webfuse/adapter/impl/jsonrpc/proxy.c
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
80
lib/webfuse/adapter/impl/jsonrpc/proxy.h
Normal file
80
lib/webfuse/adapter/impl/jsonrpc/proxy.h
Normal file
@@ -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/core/status_intern.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
json_t * wf_impl_jsonrpc_request_create(
|
||||
char const * method,
|
||||
int id,
|
||||
char const * param_info,
|
||||
va_list args)
|
||||
struct wf_impl_jsonrpc_request
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
int id;
|
||||
wf_impl_jsonrpc_send_fn * send;
|
||||
void * user_data;
|
||||
};
|
||||
|
||||
json_object_set_new(request, "params", params);
|
||||
if (0 != id)
|
||||
{
|
||||
json_object_set_new(request, "id", json_integer(id));
|
||||
}
|
||||
|
||||
return request;
|
||||
bool wf_impl_jsonrpc_is_request(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
json_t * method = json_object_get(message, "method");
|
||||
json_t * params = json_object_get(message, "params");
|
||||
|
||||
return (json_is_integer(id) && json_is_string(method) &&
|
||||
(json_is_array(params) || json_is_object(params)));
|
||||
}
|
||||
|
||||
|
||||
struct wf_impl_jsonrpc_request * wf_impl_jsonrpc_request_create(
|
||||
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)
|
||||
{
|
||||
request->id = id;
|
||||
request->send = send;
|
||||
request->user_data = user_data;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,17 +12,38 @@ using std::size_t;
|
||||
#endif
|
||||
|
||||
#include <jansson.h>
|
||||
#include "webfuse/core/status.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/send_fn.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern json_t * wf_impl_jsonrpc_request_create(
|
||||
char const * method,
|
||||
int id,
|
||||
char const * param_info,
|
||||
va_list args);
|
||||
struct wf_impl_jsonrpc_request;
|
||||
|
||||
extern bool wf_impl_jsonrpc_is_request(
|
||||
json_t * message);
|
||||
|
||||
extern struct wf_impl_jsonrpc_request * wf_impl_jsonrpc_request_create(
|
||||
int id,
|
||||
wf_impl_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
extern void wf_impl_jsonrpc_request_dispose(
|
||||
struct wf_impl_jsonrpc_request * request);
|
||||
|
||||
extern void * wf_impl_jsonrpc_request_get_userdata(
|
||||
struct wf_impl_jsonrpc_request * request);
|
||||
|
||||
extern void wf_impl_jsonrpc_respond(
|
||||
struct wf_impl_jsonrpc_request * request,
|
||||
json_t * result);
|
||||
|
||||
extern void wf_impl_jsonrpc_respond_error(
|
||||
struct wf_impl_jsonrpc_request * request,
|
||||
wf_status status);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
#include "webfuse/adapter/impl/jsonrpc/response.h"
|
||||
|
||||
extern bool wf_impl_jsonrpc_is_response(
|
||||
json_t * message)
|
||||
{
|
||||
json_t * id = json_object_get(message, "id");
|
||||
json_t * err = json_object_get(message, "error");
|
||||
json_t * result = json_object_get(message, "result");
|
||||
|
||||
return (json_is_integer(id) &&
|
||||
(json_is_object(err) || (NULL != result)));
|
||||
}
|
||||
|
||||
|
||||
void wf_impl_jsonrpc_response_init(
|
||||
struct wf_impl_jsonrpc_response * result,
|
||||
char const * buffer,
|
||||
size_t length)
|
||||
json_t * response)
|
||||
{
|
||||
result->status = WF_BAD;
|
||||
result->id = -1;
|
||||
result->result = NULL;
|
||||
|
||||
json_t * response = json_loadb(buffer, length, 0, NULL);
|
||||
if (NULL == response)
|
||||
{
|
||||
result->status = WF_BAD_FORMAT;
|
||||
return;
|
||||
}
|
||||
|
||||
json_t * id_holder = json_object_get(response, "id");
|
||||
if ((NULL == id_holder) || (!json_is_integer(id_holder)))
|
||||
{
|
||||
result->status = WF_BAD_FORMAT;
|
||||
json_decref(response);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -45,8 +48,6 @@ void wf_impl_jsonrpc_response_init(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
json_decref(response);
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_response_cleanup(
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define WF_ADAPTER_IMPL_JSONRPC_RESPONSE_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
@@ -22,10 +23,12 @@ struct wf_impl_jsonrpc_response
|
||||
json_t * result;
|
||||
};
|
||||
|
||||
extern bool wf_impl_jsonrpc_is_response(
|
||||
json_t * message);
|
||||
|
||||
extern void wf_impl_jsonrpc_response_init(
|
||||
struct wf_impl_jsonrpc_response * response,
|
||||
char const * buffer,
|
||||
size_t buffer_length);
|
||||
json_t * message);
|
||||
|
||||
extern void wf_impl_jsonrpc_response_cleanup(
|
||||
struct wf_impl_jsonrpc_response * response);
|
||||
|
||||
23
lib/webfuse/adapter/impl/jsonrpc/send_fn.h
Normal file
23
lib/webfuse/adapter/impl/jsonrpc/send_fn.h
Normal file
@@ -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/method.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/request.h"
|
||||
#include "webfuse/core/util.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "webfuse/adapter/impl/jsonrpc/method_intern.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/request.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/response.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);
|
||||
}
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_server_init(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
struct wf_impl_timeout_manager * timeout_manager)
|
||||
struct wf_impl_jsonrpc_server * server)
|
||||
{
|
||||
server->methods = NULL;
|
||||
server->request.is_pending = false;
|
||||
|
||||
wf_impl_timer_init(&server->request.timer, timeout_manager);
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_server_cleanup(
|
||||
struct wf_impl_jsonrpc_server * server)
|
||||
{
|
||||
wf_impl_timer_cleanup(&server->request.timer);
|
||||
|
||||
if (server->request.is_pending)
|
||||
struct wf_impl_jsonrpc_method * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
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;
|
||||
method->next = NULL;
|
||||
wf_impl_jsonrpc_method_dispose(method);
|
||||
method = next;
|
||||
struct wf_impl_jsonrpc_method * next = current->next;
|
||||
wf_impl_jsonrpc_method_dispose(current);
|
||||
current = next;
|
||||
}
|
||||
server->methods = NULL;
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_server_add(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
char const * name,
|
||||
char const * method_name,
|
||||
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
||||
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;
|
||||
server->methods = method;
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_server_invoke(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
wf_impl_jsonrpc_method_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
)
|
||||
static void wf_impl_jsonrpc_server_invalid_method_invoke(
|
||||
struct wf_impl_jsonrpc_request * request,
|
||||
char const * WF_UNUSED_PARAM(method_name),
|
||||
json_t * WF_UNUSED_PARAM(params),
|
||||
void * WF_UNUSED_PARAM(user_data))
|
||||
{
|
||||
if (!server->request.is_pending)
|
||||
{
|
||||
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);
|
||||
}
|
||||
wf_impl_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED);
|
||||
}
|
||||
|
||||
extern void wf_impl_jsonrpc_server_notify(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
)
|
||||
static struct wf_impl_jsonrpc_method const wf_impl_jsonrpc_server_invalid_method =
|
||||
{
|
||||
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_getmethod(server, method_name);
|
||||
if (NULL != method)
|
||||
{
|
||||
|
||||
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);
|
||||
json_decref(request);
|
||||
}
|
||||
}
|
||||
.next = NULL,
|
||||
.name = "<invalid>",
|
||||
.invoke = &wf_impl_jsonrpc_server_invalid_method_invoke,
|
||||
.user_data = NULL
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
void wf_impl_jsonrpc_server_onresult(
|
||||
static struct wf_impl_jsonrpc_method const * wf_impl_jsonrpc_server_get_method(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
char const * message,
|
||||
size_t length)
|
||||
char const * method_name)
|
||||
{
|
||||
struct wf_impl_jsonrpc_response response;
|
||||
wf_impl_jsonrpc_response_init(&response, message, length);
|
||||
|
||||
if ((server->request.is_pending) && (response.id == server->request.id))
|
||||
struct wf_impl_jsonrpc_method const * current = server->methods;
|
||||
while (NULL != current)
|
||||
{
|
||||
wf_impl_jsonrpc_method_finished_fn * finished = server->request.finished;
|
||||
void * user_data = server->request.user_data;
|
||||
if (0 == strcmp(method_name, current->name))
|
||||
{
|
||||
return current;
|
||||
}
|
||||
|
||||
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, response.status, response.result);
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
wf_impl_jsonrpc_response_cleanup(&response);
|
||||
return &wf_impl_jsonrpc_server_invalid_method;
|
||||
}
|
||||
|
||||
void wf_impl_jsonrpc_server_process(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
json_t * request_data,
|
||||
wf_impl_jsonrpc_send_fn * send,
|
||||
void * user_data)
|
||||
{
|
||||
json_t * method_holder = json_object_get(request_data, "method");
|
||||
json_t * params = json_object_get(request_data, "params");
|
||||
json_t * id_holder = json_object_get(request_data, "id");
|
||||
|
||||
if (json_is_string(method_holder) &&
|
||||
(json_is_array(params) || (json_is_object(params))) &&
|
||||
json_is_integer(id_holder))
|
||||
{
|
||||
char const * method_name = json_string_value(method_holder);
|
||||
int id = json_integer_value(id_holder);
|
||||
struct wf_impl_jsonrpc_request * request = wf_impl_jsonrpc_request_create(id, send, user_data);
|
||||
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_get_method(server, method_name);
|
||||
|
||||
method->invoke(request, method_name, params, method->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,75 +3,46 @@
|
||||
|
||||
#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/core/status.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/send_fn.h"
|
||||
#include "webfuse/adapter/impl/jsonrpc/method.h"
|
||||
#include "webfuse/adapter/impl/time/timeout_manager.h"
|
||||
#include "webfuse/adapter/impl/time/timer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct wf_impl_jsonrpc_request
|
||||
extern "C"
|
||||
{
|
||||
bool is_pending;
|
||||
wf_impl_jsonrpc_method_finished_fn * finished;
|
||||
void * user_data;
|
||||
int id;
|
||||
struct wf_impl_timer timer;
|
||||
};
|
||||
#endif
|
||||
|
||||
struct wf_impl_jsonrpc_server
|
||||
{
|
||||
struct wf_impl_jsonrpc_method * methods;
|
||||
struct wf_impl_jsonrpc_request request;
|
||||
};
|
||||
|
||||
extern void wf_impl_jsonrpc_server_init(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
struct wf_impl_timeout_manager * manager);
|
||||
struct wf_impl_jsonrpc_server * server);
|
||||
|
||||
extern void wf_impl_jsonrpc_server_cleanup(
|
||||
struct wf_impl_jsonrpc_server * server);
|
||||
|
||||
extern void wf_impl_jsonrpc_server_add(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
char const * name,
|
||||
char const * method_name,
|
||||
wf_impl_jsonrpc_method_invoke_fn * invoke,
|
||||
void * user_data );
|
||||
void * user_data);
|
||||
|
||||
extern void wf_impl_jsonrpc_server_invoke(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
wf_impl_jsonrpc_method_finished_fn * finished,
|
||||
void * user_data,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
);
|
||||
|
||||
extern void wf_impl_jsonrpc_server_notify(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
char const * method_name,
|
||||
char const * param_info,
|
||||
...
|
||||
);
|
||||
|
||||
extern void wf_impl_jsonrpc_server_onresult(
|
||||
extern void wf_impl_jsonrpc_server_process(
|
||||
struct wf_impl_jsonrpc_server * server,
|
||||
char const * message,
|
||||
size_t length);
|
||||
json_t * request,
|
||||
wf_impl_jsonrpc_send_fn * send,
|
||||
void * user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user