2020-03-29 15:40:33 +00:00
|
|
|
#include "webfuse/core/jsonrpc/request.h"
|
|
|
|
#include "webfuse/core/jsonrpc/error.h"
|
2019-04-01 20:15:12 +00:00
|
|
|
#include <stdlib.h>
|
2019-02-09 02:08:02 +00:00
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_request
|
2019-02-09 02:08:02 +00:00
|
|
|
{
|
2019-04-01 20:15:12 +00:00
|
|
|
int id;
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_send_fn * send;
|
2019-04-01 20:15:12 +00:00
|
|
|
void * user_data;
|
|
|
|
};
|
|
|
|
|
2020-02-29 01:32:03 +00:00
|
|
|
bool
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_is_request(
|
2019-04-01 20:15:12 +00:00
|
|
|
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)));
|
2019-02-09 02:08:02 +00:00
|
|
|
}
|
2019-04-01 20:15:12 +00:00
|
|
|
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_request *
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_request_create(
|
2019-04-01 20:15:12 +00:00
|
|
|
int id,
|
2020-03-01 15:55:58 +00:00
|
|
|
wf_jsonrpc_send_fn * send,
|
2019-04-01 20:15:12 +00:00
|
|
|
void * user_data)
|
|
|
|
{
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_request * request = malloc(sizeof(struct wf_jsonrpc_request));
|
2020-03-21 20:22:22 +00:00
|
|
|
request->id = id;
|
|
|
|
request->send = send;
|
|
|
|
request->user_data = user_data;
|
2019-04-01 20:15:12 +00:00
|
|
|
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2020-02-29 01:32:03 +00:00
|
|
|
void
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_request_dispose(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_request * request)
|
2019-04-01 20:15:12 +00:00
|
|
|
{
|
|
|
|
free(request);
|
|
|
|
}
|
|
|
|
|
2020-02-29 01:32:03 +00:00
|
|
|
void *
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_request_get_userdata(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_request * request)
|
2019-04-01 20:15:12 +00:00
|
|
|
{
|
|
|
|
return request->user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-29 01:32:03 +00:00
|
|
|
void
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_respond(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_request * request,
|
2019-04-01 20:15:12 +00:00
|
|
|
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);
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_request_dispose(request);
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 15:40:33 +00:00
|
|
|
void wf_jsonrpc_respond_error(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_request * request,
|
2020-02-29 01:32:03 +00:00
|
|
|
int code,
|
|
|
|
char const * message)
|
2019-04-01 20:15:12 +00:00
|
|
|
{
|
|
|
|
json_t * response = json_object();
|
2020-03-29 15:40:33 +00:00
|
|
|
json_object_set_new(response, "error", wf_jsonrpc_error(code, message));
|
2019-04-01 20:15:12 +00:00
|
|
|
json_object_set_new(response, "id", json_integer(request->id));
|
|
|
|
|
|
|
|
request->send(response, request->user_data);
|
|
|
|
json_decref(response);
|
2020-03-29 15:40:33 +00:00
|
|
|
wf_jsonrpc_request_dispose(request);
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
|