afb-hsrv: Adapt to next version of libmicrohttpd
[src/app-framework-binder.git] / src / afb-hsrv.c
index 6cb0bb4..685c55b 100644 (file)
 #include <errno.h>
 #include <sys/stat.h>
 
+#include <json-c/json.h>
 #include <microhttpd.h>
+#if MHD_VERSION < 0x00095206
+# define MHD_ALLOW_SUSPEND_RESUME MHD_USE_SUSPEND_RESUME
+#endif
 
 #include "afb-method.h"
 #include "afb-context.h"
@@ -108,6 +112,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;
@@ -154,6 +159,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 +181,16 @@ 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);
+                       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;
                        }
                }
@@ -191,6 +208,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) {
@@ -219,7 +240,7 @@ static int access_handler(
        }
 
        /* no handler */
-       WARNING("Unhandled request to %s", hreq->url);
+       NOTICE("Unhandled request to %s", hreq->url);
        afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
        return MHD_YES;
 }
@@ -247,7 +268,7 @@ static void do_run(int signum, void *arg)
        fdev_set_events(hsrv->fdev, EPOLLIN);
 }
 
-void run_micro_httpd(struct afb_hsrv *hsrv)
+void afb_hsrv_run(struct afb_hsrv *hsrv)
 {
        fdev_set_events(hsrv->fdev, 0);
        if (jobs_queue(hsrv, 0, do_run, hsrv) < 0)
@@ -256,7 +277,7 @@ void run_micro_httpd(struct afb_hsrv *hsrv)
 
 static void listen_callback(void *hsrv, uint32_t revents, struct fdev *fdev)
 {
-       run_micro_httpd(hsrv);
+       afb_hsrv_run(hsrv);
 }
 
 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
@@ -399,7 +420,7 @@ int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection
        const union MHD_DaemonInfo *info;
 
        httpd = MHD_start_daemon(
-               MHD_USE_EPOLL | MHD_ALLOW_UPGRADE | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
+               MHD_USE_EPOLL | MHD_ALLOW_UPGRADE | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_ALLOW_SUSPEND_RESUME,
                port,                           /* port */
                new_client_handler, NULL,       /* Tcp Accept call back + extra attribute */
                access_handler, hsrv,   /* Http Request Call back + extra attribute */