1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

refactor: replaced jansson by own json parser implementation

This commit is contained in:
Falk Werner
2020-07-12 00:39:55 +02:00
parent 63ca5d5a6d
commit 63cc5b5388
35 changed files with 443 additions and 423 deletions

View File

@@ -1,17 +1,28 @@
#include "webfuse_provider/impl/jsonrpc/error.h"
json_t *
wfp_jsonrpc_error(
#include <stdlib.h>
#include <string.h>
struct wfp_jsonrpc_error *
wfp_jsonrpc_error_create(
int code,
char const * message)
{
json_t * error = json_object();
json_object_set_new(error, "code", json_integer(code));
json_object_set_new(error, "message", json_string(message));
struct wfp_jsonrpc_error * error = malloc(sizeof(struct wfp_jsonrpc_error));
error->code = code;
error->message = strdup(message);
return error;
}
void
wfp_jsonrpc_error_dispose(
struct wfp_jsonrpc_error * error)
{
free(error->message);
free(error);
}
void
wfp_jsonrpc_propate_error(
wfp_jsonrpc_proxy_finished_fn * finised,
@@ -19,9 +30,10 @@ wfp_jsonrpc_propate_error(
int code,
char const * message)
{
json_t * error = wfp_jsonrpc_error(code, message);
finised(user_data, NULL, error);
struct wfp_jsonrpc_error error;
error.code = code;
error.message = (char*) message;
json_decref(error);
finised(user_data, NULL, &error);
}

View File

@@ -1,7 +1,6 @@
#ifndef WFP_JSONRPC_ERROR_H
#define WFP_JSONRPC_ERROR_H
#include <jansson.h>
#include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h"
#ifdef __cplusplus
@@ -9,11 +8,21 @@ extern "C"
{
#endif
extern json_t *
wfp_jsonrpc_error(
struct wfp_jsonrpc_error
{
int code;
char * message;
};
extern struct wfp_jsonrpc_error *
wfp_jsonrpc_error_create(
int code,
char const * message);
extern void
wfp_jsonrpc_error_dispose(
struct wfp_jsonrpc_error * error);
extern void
wfp_jsonrpc_propate_error(
wfp_jsonrpc_proxy_finished_fn * finised,

View File

@@ -183,7 +183,7 @@ extern void wfp_jsonrpc_proxy_vnotify(
void wfp_jsonrpc_proxy_onresult(
struct wfp_jsonrpc_proxy * proxy,
json_t * message)
struct wfp_json const * message)
{
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, message);

View File

@@ -11,7 +11,6 @@
using std::size_t;
#endif
#include <jansson.h>
#include "webfuse_provider/impl/jsonrpc/send_fn.h"
#include "webfuse_provider/impl/jsonrpc/proxy_finished_fn.h"
@@ -22,6 +21,7 @@ extern "C" {
struct wfp_jsonrpc_proxy;
struct wfp_timer_manager;
struct wfp_json_writer;
struct wfp_json;
typedef void
wfp_jsonrpc_custom_write_fn(
@@ -70,7 +70,7 @@ extern void wfp_jsonrpc_proxy_notify(
extern void wfp_jsonrpc_proxy_onresult(
struct wfp_jsonrpc_proxy * proxy,
json_t * message);
struct wfp_json const * message);
#ifdef __cplusplus
}

View File

@@ -1,17 +1,18 @@
#ifndef WFP_JSONRPC_PROXY_FINISHED_FN_H
#define WFP_JSONRPC_PROXY_FINISHED_FN_H
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_json;
struct wfp_jsonrpc_error;
typedef void wfp_jsonrpc_proxy_finished_fn(
void * user_data,
json_t const * result,
json_t const * error);
struct wfp_json const * result,
struct wfp_jsonrpc_error const * error);
#ifdef __cplusplus
}

View File

@@ -1,14 +1,17 @@
#include "webfuse_provider/impl/jsonrpc/request.h"
#include "webfuse_provider/impl/json/node.h"
#include <stdlib.h>
bool
wfp_jsonrpc_is_request(
json_t * message)
struct wfp_json const * 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");
if (NULL == message) { return false; }
return (json_is_integer(id) && json_is_string(method) &&
(json_is_array(params) || json_is_object(params)));
struct wfp_json const * id = wfp_impl_json_object_get(message, "id");
struct wfp_json const * method = wfp_impl_json_object_get(message, "method");
struct wfp_json const * params = wfp_impl_json_object_get(message, "params");
return (wfp_impl_json_is_int(id) && wfp_impl_json_is_string(method) &&
(wfp_impl_json_is_array(params) || wfp_impl_json_is_object(params)));
}

View File

@@ -5,15 +5,15 @@
#include <stdbool.h>
#endif
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_json;
extern bool wfp_jsonrpc_is_request(
json_t * message);
struct wfp_json const * message);
#ifdef __cplusplus
}

View File

@@ -1,53 +1,57 @@
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
#include "webfuse_provider/impl/jsonrpc/error.h"
#include "webfuse_provider/status.h"
#include "webfuse_provider/impl/json/node.h"
bool
wfp_jsonrpc_is_response(
json_t * message)
struct wfp_json const * 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");
if (NULL == message) { return false; }
return (json_is_integer(id) &&
(json_is_object(err) || (NULL != result)));
struct wfp_json const * id = wfp_impl_json_object_get(message, "id");
struct wfp_json const * err = wfp_impl_json_object_get(message, "error");
struct wfp_json const * result = wfp_impl_json_object_get(message, "result");
return (wfp_impl_json_is_int(id) &&
(wfp_impl_json_is_object(err) || (NULL != result)));
}
void
wfp_jsonrpc_response_init(
struct wfp_jsonrpc_response * result,
json_t * response)
struct wfp_json const * response)
{
result->id = -1;
result->result = NULL;
result->error = NULL;
json_t * id_holder = json_object_get(response, "id");
if (!json_is_integer(id_holder))
struct wfp_json const * id_holder = wfp_impl_json_object_get(response, "id");
if (!wfp_impl_json_is_int(id_holder))
{
result->error = wfp_jsonrpc_error(WFP_BAD_FORMAT, "invalid format: missing id");
result->error = wfp_jsonrpc_error_create(WFP_BAD_FORMAT, "invalid format: missing id");
return;
}
result->id = json_integer_value(id_holder);
result->result = json_object_get(response, "result");
if (NULL != result->result)
result->id = wfp_impl_json_get_int(id_holder);
result->result = wfp_impl_json_object_get(response, "result");
if (NULL == result->result)
{
json_incref(result->result);
}
else
{
json_t * error = json_object_get(response, "error");
if ((json_is_object(error)) && (json_is_integer(json_object_get(error, "code"))))
struct wfp_json const * error = wfp_impl_json_object_get(response, "error");
if ((wfp_impl_json_is_object(error)) && (wfp_impl_json_is_int(wfp_impl_json_object_get(error, "code"))))
{
result->error = error;
json_incref(result->error);
int code = wfp_impl_json_get_int(wfp_impl_json_object_get(error, "code"));
char const * message = "";
if (wfp_impl_json_is_string(wfp_impl_json_object_get(error, "message")))
{
message = wfp_impl_json_get_string(wfp_impl_json_object_get(error, "message"));
}
result->error = wfp_jsonrpc_error_create(code, message);
}
else
{
result->error = wfp_jsonrpc_error(WFP_BAD_FORMAT, "invalid format: invalid error object");
result->error = wfp_jsonrpc_error_create(WFP_BAD_FORMAT, "invalid format: invalid error object");
}
}
}
@@ -56,13 +60,8 @@ void
wfp_jsonrpc_response_cleanup(
struct wfp_jsonrpc_response * response)
{
if (NULL != response->result)
{
json_decref(response->result);
}
if (NULL != response->error)
{
json_decref(response->error);
}
if (NULL != response->error)
{
wfp_jsonrpc_error_dispose(response->error);
}
}

View File

@@ -5,15 +5,16 @@
#include <stdbool.h>
#endif
#include <jansson.h>
#ifdef __cplusplus
extern "C"
{
#endif
extern bool wfp_jsonrpc_is_response(
json_t * message);
struct wfp_json;
extern bool
wfp_jsonrpc_is_response(
struct wfp_json const * message);
#ifdef __cplusplus
}

View File

@@ -14,16 +14,19 @@ using std::size_t;
extern "C" {
#endif
struct wfp_json;
struct wfp_jsonrpc_error;
struct wfp_jsonrpc_response
{
json_t * result;
json_t * error;
struct wfp_json const * result;
struct wfp_jsonrpc_error * error;
int id;
};
extern void wfp_jsonrpc_response_init(
struct wfp_jsonrpc_response * response,
json_t * message);
struct wfp_json const * message);
extern void wfp_jsonrpc_response_cleanup(
struct wfp_jsonrpc_response * response);