2020-06-28 17:43:08 +00:00
|
|
|
#include "webfuse/impl/jsonrpc/server.h"
|
|
|
|
#include "webfuse/impl/jsonrpc/method.h"
|
|
|
|
#include "webfuse/impl/jsonrpc/request.h"
|
2020-07-16 20:36:12 +00:00
|
|
|
#include "webfuse/impl/json/node.h"
|
2020-06-28 13:51:34 +00:00
|
|
|
#include "webfuse/status.h"
|
2020-06-28 17:43:08 +00:00
|
|
|
#include "webfuse/impl/util/util.h"
|
2020-03-01 15:55:58 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct wf_jsonrpc_server
|
|
|
|
{
|
|
|
|
struct wf_jsonrpc_method * methods;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_server_init(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_server * server);
|
|
|
|
|
|
|
|
static void
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_server_cleanup(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_server * server);
|
|
|
|
|
|
|
|
struct wf_jsonrpc_server *
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_server_create(void)
|
2020-03-01 15:55:58 +00:00
|
|
|
{
|
|
|
|
struct wf_jsonrpc_server * server = malloc(sizeof(struct wf_jsonrpc_server));
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_server_init(server);
|
2020-03-01 15:55:58 +00:00
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_server_dispose(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_server * server)
|
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_server_cleanup(server);
|
2020-03-01 15:55:58 +00:00
|
|
|
free(server);
|
|
|
|
}
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
static void wf_impl_jsonrpc_server_init(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_server * server)
|
|
|
|
{
|
|
|
|
server->methods = NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
static void wf_impl_jsonrpc_server_cleanup(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_server * server)
|
|
|
|
{
|
|
|
|
struct wf_jsonrpc_method * current = server->methods;
|
|
|
|
while (NULL != current)
|
|
|
|
{
|
|
|
|
struct wf_jsonrpc_method * next = current->next;
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_method_dispose(current);
|
2020-03-01 15:55:58 +00:00
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
server->methods = NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
void wf_impl_jsonrpc_server_add(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_server * server,
|
|
|
|
char const * method_name,
|
|
|
|
wf_jsonrpc_method_invoke_fn * invoke,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_method * method = wf_impl_jsonrpc_method_create(method_name, invoke, user_data);
|
2020-03-01 15:55:58 +00:00
|
|
|
method->next = server->methods;
|
|
|
|
server->methods = method;
|
|
|
|
}
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
static void wf_impl_jsonrpc_server_invalid_method_invoke(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_request * request,
|
2020-03-29 15:40:33 +00:00
|
|
|
char const * WF_UNUSED_PARAM(method_name),
|
2020-07-16 20:36:12 +00:00
|
|
|
struct wf_json const * WF_UNUSED_PARAM(params),
|
2020-03-29 15:40:33 +00:00
|
|
|
void * WF_UNUSED_PARAM(user_data))
|
2020-03-01 15:55:58 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED, "not implemented");
|
2020-03-01 15:55:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
static struct wf_jsonrpc_method const wf_impl_jsonrpc_server_invalid_method =
|
2020-03-01 15:55:58 +00:00
|
|
|
{
|
|
|
|
.next = NULL,
|
|
|
|
.name = "<invalid>",
|
2020-06-28 17:43:08 +00:00
|
|
|
.invoke = &wf_impl_jsonrpc_server_invalid_method_invoke,
|
2020-03-01 15:55:58 +00:00
|
|
|
.user_data = NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct wf_jsonrpc_method const *
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_server_get_method(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_server * server,
|
|
|
|
char const * method_name)
|
|
|
|
{
|
|
|
|
struct wf_jsonrpc_method const * current = server->methods;
|
|
|
|
while (NULL != current)
|
|
|
|
{
|
|
|
|
if (0 == strcmp(method_name, current->name))
|
|
|
|
{
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
return &wf_impl_jsonrpc_server_invalid_method;
|
2020-03-01 15:55:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
void wf_impl_jsonrpc_server_process(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_server * server,
|
2020-07-16 20:36:12 +00:00
|
|
|
struct wf_json const * request_data,
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_send_fn * send,
|
|
|
|
void * user_data)
|
|
|
|
{
|
2020-07-16 20:36:12 +00:00
|
|
|
struct wf_json const * method_holder = wf_impl_json_object_get(request_data, "method");
|
|
|
|
struct wf_json const * params = wf_impl_json_object_get(request_data, "params");
|
|
|
|
struct wf_json const * id_holder = wf_impl_json_object_get(request_data, "id");
|
2020-03-01 15:55:58 +00:00
|
|
|
|
2020-07-16 20:36:12 +00:00
|
|
|
if ((WF_JSON_TYPE_STRING == wf_impl_json_type(method_holder)) &&
|
|
|
|
((WF_JSON_TYPE_ARRAY == wf_impl_json_type(params)) || (WF_JSON_TYPE_OBJECT == wf_impl_json_type(params))) &&
|
|
|
|
(WF_JSON_TYPE_INT == wf_impl_json_type(id_holder)))
|
2020-03-01 15:55:58 +00:00
|
|
|
{
|
2020-07-16 20:36:12 +00:00
|
|
|
char const * method_name = wf_impl_json_string_get(method_holder);
|
|
|
|
int id = wf_impl_json_int_get(id_holder);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_request * request = wf_impl_jsonrpc_request_create(id, send, user_data);
|
|
|
|
struct wf_jsonrpc_method const * method = wf_impl_jsonrpc_server_get_method(server, method_name);
|
2020-03-01 15:55:58 +00:00
|
|
|
|
|
|
|
method->invoke(request, method_name, params, method->user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|