2020-06-28 17:43:08 +00:00
|
|
|
#include "webfuse/impl/jsonrpc/response_intern.h"
|
|
|
|
#include "webfuse/impl/jsonrpc/error.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"
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2020-02-29 01:32:03 +00:00
|
|
|
bool
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_is_response(
|
2020-07-16 20:36:12 +00:00
|
|
|
struct wf_json const * message)
|
2019-04-01 20:15:12 +00:00
|
|
|
{
|
2020-07-16 20:36:12 +00:00
|
|
|
struct wf_json const * id = wf_impl_json_object_get(message, "id");
|
|
|
|
struct wf_json const * err = wf_impl_json_object_get(message, "error");
|
|
|
|
struct wf_json const * result = wf_impl_json_object_get(message, "result");
|
2019-04-01 20:15:12 +00:00
|
|
|
|
2020-07-16 20:36:12 +00:00
|
|
|
return ((WF_JSON_TYPE_INT == wf_impl_json_type(id)) &&
|
|
|
|
((WF_JSON_TYPE_OBJECT == wf_impl_json_type(err)) || (WF_JSON_TYPE_UNDEFINED != wf_impl_json_type(result))));
|
2019-04-01 20:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-29 01:32:03 +00:00
|
|
|
void
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_init(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_response * result,
|
2020-07-16 20:36:12 +00:00
|
|
|
struct wf_json const * response)
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
|
|
|
result->id = -1;
|
|
|
|
result->result = NULL;
|
2020-02-28 22:17:41 +00:00
|
|
|
result->error = NULL;
|
2019-01-27 02:45:03 +00:00
|
|
|
|
2020-07-16 20:36:12 +00:00
|
|
|
struct wf_json const * id_holder = wf_impl_json_object_get(response, "id");
|
|
|
|
if (WF_JSON_TYPE_INT != wf_impl_json_type(id_holder))
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
result->error = wf_impl_jsonrpc_error(WF_BAD_FORMAT, "invalid format: missing id");
|
2019-01-27 02:45:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-16 20:36:12 +00:00
|
|
|
result->id = wf_impl_json_int_get(id_holder);
|
|
|
|
result->result = wf_impl_json_object_get(response, "result");
|
|
|
|
if (WF_JSON_TYPE_UNDEFINED == wf_impl_json_type(result->result))
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2020-07-16 17:30:18 +00:00
|
|
|
int code = WF_BAD_FORMAT;
|
|
|
|
char const * message = "invalid format: invalid error object";
|
|
|
|
|
2020-07-16 20:36:12 +00:00
|
|
|
struct wf_json const * error = wf_impl_json_object_get(response, "error");
|
|
|
|
struct wf_json const * code_holder = wf_impl_json_object_get(error, "code");
|
|
|
|
if (WF_JSON_TYPE_INT == wf_impl_json_type(code_holder))
|
2019-01-27 02:45:03 +00:00
|
|
|
{
|
2020-07-16 20:36:12 +00:00
|
|
|
code = wf_impl_json_int_get(code_holder);
|
|
|
|
struct wf_json const * message_holder = wf_impl_json_object_get(error, "message");
|
|
|
|
message = wf_impl_json_string_get(message_holder);
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|
2020-07-16 17:30:18 +00:00
|
|
|
|
|
|
|
result->error = wf_impl_jsonrpc_error(code, message);
|
2019-01-27 02:45:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 01:32:03 +00:00
|
|
|
void
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_cleanup(
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_response * response)
|
2019-02-09 02:08:02 +00:00
|
|
|
{
|
2020-07-16 17:30:18 +00:00
|
|
|
wf_impl_jsonrpc_error_dispose(response->error);
|
2019-02-09 02:08:02 +00:00
|
|
|
}
|