summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjobol <jose.bollo@iot.bzh>2018-06-18 13:10:45 +0200
committerjobol <jose.bollo@iot.bzh>2018-06-18 14:10:41 +0200
commit7bf0d9c6f807ffae6d9871c606afeccb9b478d3d (patch)
treedc9cd4cc063f14d495d5f24a5c60d6859cb13cc2
parent4521c1e7ae5371ab9d639adc617d17fb4e8ded0c (diff)
afb-hsrv: Add processing of type application/json
Makes the data posted as application/json directly available through json. The previous processing made content transmitted with the content-type application/json available as a string in the field of name "" of the returned json object. Change-Id: I11615b9b147ee4daa1b1358c2bd36fd839d8a7a9 Signed-off-by: jobol <jose.bollo@iot.bzh>
-rw-r--r--src/afb-hreq.c3
-rw-r--r--src/afb-hreq.h5
-rw-r--r--src/afb-hsrv.c22
3 files changed, 26 insertions, 4 deletions
diff --git a/src/afb-hreq.c b/src/afb-hreq.c
index e5dcf642..fa16565a 100644
--- a/src/afb-hreq.c
+++ b/src/afb-hreq.c
@@ -318,6 +318,9 @@ static void req_destroy(struct afb_xreq *xreq)
if (hreq->postform != NULL)
MHD_destroy_post_processor(hreq->postform);
+ if (hreq->tokener != NULL)
+ json_tokener_free(hreq->tokener);
+
for (data = hreq->data; data; data = hreq->data) {
hreq->data = data->next;
if (data->path) {
diff --git a/src/afb-hreq.h b/src/afb-hreq.h
index b501af6a..bae178db 100644
--- a/src/afb-hreq.h
+++ b/src/afb-hreq.h
@@ -19,8 +19,10 @@
#include "afb-xreq.h"
-struct afb_session;
struct json_object;
+struct json_tokener;
+
+struct afb_session;
struct hreq_data;
struct afb_hsrv;
struct locale_search;
@@ -44,6 +46,7 @@ struct afb_hreq {
struct MHD_PostProcessor *postform;
struct hreq_data *data;
struct json_object *json;
+ struct json_tokener *tokener;
};
extern int afb_hreq_unprefix(struct afb_hreq *request, const char *prefix, size_t length);
diff --git a/src/afb-hsrv.c b/src/afb-hsrv.c
index 6cb0bb45..3bca5839 100644
--- a/src/afb-hsrv.c
+++ b/src/afb-hsrv.c
@@ -27,6 +27,7 @@
#include <errno.h>
#include <sys/stat.h>
+#include <json-c/json.h>
#include <microhttpd.h>
#include "afb-method.h"
@@ -154,6 +155,11 @@ static int access_handler(
}
return MHD_YES;
} else if (strcasestr(type, JSON_CONTENT) != NULL) {
+ hreq->tokener = json_tokener_new();
+ if (hreq->tokener == NULL) {
+ ERROR("Can't create tokener for POST");
+ afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
+ }
return MHD_YES;
} else {
WARNING("Unsupported media type %s", type);
@@ -171,9 +177,15 @@ static int access_handler(
afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
return MHD_YES;
}
- } else {
- if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) {
- afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
+ } 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)));
+ afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST);
return MHD_YES;
}
}
@@ -191,6 +203,10 @@ static int access_handler(
return MHD_YES;
}
}
+ if (hreq->tokener != NULL) {
+ json_tokener_free(hreq->tokener);
+ hreq->tokener = NULL;
+ }
if (hreq->scanned != 0) {
if (hreq->replied == 0 && hreq->suspended == 0) {