afb-wsj1: Fix json parsing of unterminated
[src/app-framework-binder.git] / src / afb-wsj1.c
index 7a8e023..466b58a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017 "IoT.bzh"
+ * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
  * Author: José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,6 +29,7 @@
 
 #include "afb-ws.h"
 #include "afb-wsj1.h"
+#include "fdev.h"
 
 #define CALL 2
 #define RETOK 3
@@ -81,11 +82,13 @@ struct afb_wsj1
        pthread_mutex_t mutex;
 };
 
-struct afb_wsj1 *afb_wsj1_create(struct sd_event *eloop, int fd, struct afb_wsj1_itf *itf, void *closure)
+struct afb_wsj1 *afb_wsj1_create(struct fdev *fdev, struct afb_wsj1_itf *itf, void *closure)
 {
        struct afb_wsj1 *result;
 
-       assert(fd >= 0);
+       assert(fdev);
+       assert(itf);
+       assert(itf->on_call);
 
        result = calloc(1, sizeof * result);
        if (result == NULL)
@@ -100,7 +103,7 @@ struct afb_wsj1 *afb_wsj1_create(struct sd_event *eloop, int fd, struct afb_wsj1
        if (result->tokener == NULL)
                goto error2;
 
-       result->ws = afb_ws_create(eloop, fd, &wsj1_itf, result);
+       result->ws = afb_ws_create(fdev, &wsj1_itf, result);
        if (result->ws == NULL)
                goto error3;
 
@@ -111,7 +114,7 @@ error3:
 error2:
        free(result);
 error:
-       close(fd);
+       fdev_unref(fdev);
        return NULL;
 }
 
@@ -337,7 +340,8 @@ static void wsj1_on_text(struct afb_wsj1 *wsj1, char *text, size_t size)
                free(call);
                break;
        case EVENT:
-               wsj1->itf->on_event(wsj1->closure, msg->event, msg);
+               if (wsj1->itf->on_event != NULL)
+                       wsj1->itf->on_event(wsj1->closure, msg->event, msg);
                break;
        }
        afb_wsj1_msg_unref(msg);
@@ -388,6 +392,8 @@ struct json_object *afb_wsj1_msg_object_j(struct afb_wsj1_msg *msg)
                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);
                pthread_mutex_unlock(&msg->wsj1->mutex);
                if (object == NULL) {
                        /* lazy error detection of json request. Is it to improve? */