summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose Bollo <jose.bollo@iot.bzh>2018-08-01 13:44:23 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2018-08-02 15:49:43 +0200
commite39610f8c9b2e6bbb8a460f7d7ccccbc5161b4ed (patch)
tree9f11498d5ba8793c925a4b1a36591dfdc736adbb
parent10aa15afc5bc7321d0049823dd173dda11ff3724 (diff)
Improve parsing of json string
Change-Id: I7100961ef8f8a75d623a0667178f191dd4cfb9d3 Signed-off-by: Jose Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afb-api-dbus.c26
-rw-r--r--src/afb-api-so-v2.c6
-rw-r--r--src/afb-hsrv.c14
-rw-r--r--src/afb-proto-ws.c5
-rw-r--r--src/afb-wsj1.c8
-rw-r--r--src/main-afb-client-demo.c7
-rw-r--r--src/main-afb-daemon.c5
7 files changed, 46 insertions, 25 deletions
diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c
index 227658f4..a9880350 100644
--- a/src/afb-api-dbus.c
+++ b/src/afb-api-dbus.c
@@ -286,6 +286,8 @@ static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_
int rc;
struct dbus_memo *memo;
const char *json, *error, *info;
+ struct json_object *object;
+ enum json_tokener_error jerr;
/* retrieve the recorded data */
memo = userdata;
@@ -297,7 +299,14 @@ static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_
afb_xreq_reply(memo->xreq, NULL, "error", "dbus error");
} else {
/* report the answer */
- afb_xreq_reply(memo->xreq, *json ? json_tokener_parse(json) : NULL, *error ? error : NULL, *info ? info : NULL);
+ if (!*json)
+ object = NULL;
+ else {
+ object = json_tokener_parse_verbose(json, &jerr);
+ if (jerr != json_tokener_success)
+ object = json_object_new_string(json);
+ }
+ afb_xreq_reply(memo->xreq, object, *error ? error : NULL, *info ? info : NULL);
}
api_dbus_client_memo_destroy(memo);
return 1;
@@ -358,11 +367,14 @@ static int api_dbus_client_on_broadcast_event(sd_bus_message *m, void *userdata,
{
struct json_object *object;
const char *event, *data;
+ enum json_tokener_error jerr;
int rc = sd_bus_message_read(m, "ss", &event, &data);
if (rc < 0)
ERROR("unreadable broadcasted event");
else {
- object = json_tokener_parse(data);
+ object = json_tokener_parse_verbose(data, &jerr);
+ if (jerr != json_tokener_success)
+ object = json_object_new_string(data);
afb_evt_broadcast(event, object);
}
return 1;
@@ -441,6 +453,7 @@ static void api_dbus_client_event_push(struct api_dbus *api, int id, const char
{
struct json_object *object;
struct dbus_event *ev;
+ enum json_tokener_error jerr;
/* retrieves the event */
ev = api_dbus_client_event_search(api, id, name);
@@ -450,7 +463,9 @@ static void api_dbus_client_event_push(struct api_dbus *api, int id, const char
}
/* destroys the event */
- object = json_tokener_parse(data);
+ object = json_tokener_parse_verbose(data, &jerr);
+ if (jerr != json_tokener_success)
+ object = json_object_new_string(data);
afb_evt_event_x2_push(ev->event, object);
}
@@ -926,6 +941,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
uint32_t flags;
struct afb_session *session;
struct listener *listener;
+ enum json_tokener_error jerr;
/* check the interface */
if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
@@ -961,8 +977,8 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
dreq->xreq.context.flags = flags;
dreq->xreq.cred = afb_cred_mixed_on_behalf_import(listener->origin->cred, uuid, creds && creds[0] ? creds : NULL);
dreq->message = sd_bus_message_ref(message);
- dreq->json = json_tokener_parse(dreq->request);
- if (dreq->json == NULL && strcmp(dreq->request, "null")) {
+ dreq->json = json_tokener_parse_verbose(dreq->request, &jerr);
+ if (jerr != json_tokener_success) {
/* lazy error detection of json request. Is it to improve? */
dreq->json = json_object_new_string(dreq->request);
}
diff --git a/src/afb-api-so-v2.c b/src/afb-api-so-v2.c
index 6c98ce4d..39107887 100644
--- a/src/afb-api-so-v2.c
+++ b/src/afb-api-so-v2.c
@@ -75,11 +75,11 @@ struct json_object *afb_api_so_v2_make_description_openAPIv3(const struct afb_bi
char buffer[256];
const struct afb_verb_v2 *verb;
struct json_object *r, *f, *a, *i, *p, *g;
-
+ enum json_tokener_error jerr;
if (binding->specification) {
- r = json_tokener_parse(binding->specification);
- if (r)
+ r = json_tokener_parse_verbose(binding->specification, &jerr);
+ if (jerr == json_tokener_success)
return r;
}
diff --git a/src/afb-hsrv.c b/src/afb-hsrv.c
index efa74f4b..7e680cc4 100644
--- a/src/afb-hsrv.c
+++ b/src/afb-hsrv.c
@@ -109,6 +109,7 @@ static int access_handler(
struct afb_hsrv *hsrv;
struct hsrv_handler *iter;
const char *type;
+ enum json_tokener_error jerr;
hsrv = cls;
hreq = *recordreq;
@@ -179,12 +180,13 @@ static int access_handler(
}
} else if (hreq->tokener) {
hreq->json = json_tokener_parse_ex(hreq->tokener, upload_data, (int)*upload_data_size);
- switch (json_tokener_get_error(hreq->tokener)) {
- case json_tokener_success:
- case json_tokener_continue:
- break;
- default:
- ERROR("error in POST json: %s", json_tokener_error_desc(json_tokener_get_error(hreq->tokener)));
+ jerr = json_tokener_get_error(hreq->tokener);
+ if (jerr == json_tokener_continue) {
+ hreq->json = json_tokener_parse_ex(hreq->tokener, "", 1);
+ jerr = json_tokener_get_error(hreq->tokener);
+ }
+ if (jerr != json_tokener_success) {
+ ERROR("error in POST json: %s", json_tokener_error_desc(jerr));
afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST);
return MHD_YES;
}
diff --git a/src/afb-proto-ws.c b/src/afb-proto-ws.c
index 142afa98..c079bf69 100644
--- a/src/afb-proto-ws.c
+++ b/src/afb-proto-ws.c
@@ -261,10 +261,11 @@ static int readbuf_object(struct readbuf *rb, struct json_object **object)
{
const char *string;
struct json_object *o;
+ enum json_tokener_error jerr;
int rc = readbuf_string(rb, &string, NULL);
if (rc) {
- o = json_tokener_parse(string);
- if (o == NULL && strcmp(string, "null"))
+ o = json_tokener_parse_verbose(string, &jerr);
+ if (jerr != json_tokener_success)
o = json_object_new_string(string);
*object = o;
}
diff --git a/src/afb-wsj1.c b/src/afb-wsj1.c
index f2b4a084..c8572a50 100644
--- a/src/afb-wsj1.c
+++ b/src/afb-wsj1.c
@@ -390,15 +390,15 @@ const char *afb_wsj1_msg_object_s(struct afb_wsj1_msg *msg)
struct json_object *afb_wsj1_msg_object_j(struct afb_wsj1_msg *msg)
{
+ enum json_tokener_error jerr;
struct json_object *object = msg->object_j;
if (object == NULL) {
pthread_mutex_lock(&msg->wsj1->mutex);
json_tokener_reset(msg->wsj1->tokener);
- object = json_tokener_parse_ex(msg->wsj1->tokener, msg->object_s, (int)msg->object_s_length);
- if (object == NULL && json_tokener_get_error(msg->wsj1->tokener) == json_tokener_continue)
- object = json_tokener_parse_ex(msg->wsj1->tokener, "", 1);
+ object = json_tokener_parse_ex(msg->wsj1->tokener, msg->object_s, 1 + (int)msg->object_s_length);
+ jerr = json_tokener_get_error(msg->wsj1->tokener);
pthread_mutex_unlock(&msg->wsj1->mutex);
- if (object == NULL) {
+ if (jerr != json_tokener_success) {
/* lazy error detection of json request. Is it to improve? */
object = json_object_new_string_len(msg->object_s, (int)msg->object_s_length);
}
diff --git a/src/main-afb-client-demo.c b/src/main-afb-client-demo.c
index 47171c20..1d056365 100644
--- a/src/main-afb-client-demo.c
+++ b/src/main-afb-client-demo.c
@@ -538,6 +538,7 @@ static void pws_call(const char *verb, const char *object)
char *key;
int rc;
struct json_object *o;
+ enum json_tokener_error jerr;
/* allocates an id for the request */
rc = asprintf(&key, "%d:%s", ++num, verb);
@@ -548,11 +549,11 @@ static void pws_call(const char *verb, const char *object)
/* send the request */
callcount++;
- if (object == NULL || object[0] == 0 || !strcmp(object, "null"))
+ if (object == NULL || object[0] == 0)
o = NULL;
else {
- o = json_tokener_parse(object);
- if (!o)
+ o = json_tokener_parse_verbose(object, &jerr);
+ if (jerr != json_tokener_success)
o = json_object_new_string(object);
}
rc = afb_proto_ws_client_call(pws, verb, o, sessionid, key, NULL);
diff --git a/src/main-afb-daemon.c b/src/main-afb-daemon.c
index f1996811..df0efbcc 100644
--- a/src/main-afb-daemon.c
+++ b/src/main-afb-daemon.c
@@ -552,6 +552,7 @@ static struct afb_xreq_query_itf startup_xreq_itf =
static void startup_call_current(struct startup_req *sreq)
{
const char *api, *verb, *json;
+ enum json_tokener_error jerr;
sreq->callspec = json_object_get_string(json_object_array_get_idx(sreq->calls, sreq->index)),
api = sreq->callspec;
@@ -566,8 +567,8 @@ static void startup_call_current(struct startup_req *sreq)
sreq->verb = strndup(verb + 1, json - verb - 1);
sreq->xreq.request.called_api = sreq->api;
sreq->xreq.request.called_verb = sreq->verb;
- sreq->xreq.json = json_tokener_parse(json + 1);
- if (sreq->api && sreq->verb && sreq->xreq.json) {
+ sreq->xreq.json = json_tokener_parse_verbose(json + 1, &jerr);
+ if (sreq->api && sreq->verb && jerr == json_tokener_success) {
afb_xreq_process(&sreq->xreq, main_apiset);
return;
}