Fix client disconnection close
[src/app-framework-binder.git] / src / afb-xreq.c
index 8f246b3..9c089bc 100644 (file)
@@ -24,6 +24,9 @@
 #include <stdarg.h>
 
 #include <json-c/json.h>
+#if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
+#define JSON_C_TO_STRING_NOSLASHESCAPE 0
+#endif
 
 #include <afb/afb-binding-v1.h>
 #include <afb/afb-binding-v2.h>
@@ -197,12 +200,17 @@ static int xreq_legacy_subscribe_event_x1_cb(struct afb_req_x2 *closure, struct
 
 int afb_xreq_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
 {
-       if (xreq->listener)
-               return afb_evt_event_x2_add_watch(xreq->listener, event);
-       if (xreq->queryitf->subscribe)
-               return xreq->queryitf->subscribe(xreq, event);
-       ERROR("no event listener, subscription impossible");
-       errno = EINVAL;
+       if (xreq->replied) {
+               ERROR("request replied, subscription impossible");
+               errno = EINVAL;
+       } else {
+               if (xreq->listener)
+                       return afb_evt_event_x2_add_watch(xreq->listener, event);
+               if (xreq->queryitf->subscribe)
+                       return xreq->queryitf->subscribe(xreq, event);
+               ERROR("no event listener, subscription impossible");
+               errno = ENOTSUP;
+       }
        return -1;
 }
 
@@ -219,12 +227,17 @@ static int xreq_legacy_unsubscribe_event_x1_cb(struct afb_req_x2 *closure, struc
 
 int afb_xreq_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
 {
-       if (xreq->listener)
-               return afb_evt_event_x2_remove_watch(xreq->listener, event);
-       if (xreq->queryitf->unsubscribe)
-               return xreq->queryitf->unsubscribe(xreq, event);
-       ERROR("no event listener, unsubscription impossible");
-       errno = EINVAL;
+       if (xreq->replied) {
+               ERROR("request replied, unsubscription impossible");
+               errno = EINVAL;
+       } else {
+               if (xreq->listener)
+                       return afb_evt_event_x2_remove_watch(xreq->listener, event);
+               if (xreq->queryitf->unsubscribe)
+                       return xreq->queryitf->unsubscribe(xreq, event);
+               ERROR("no event listener, unsubscription impossible");
+               errno = ENOTSUP;
+       }
        return -1;
 }
 
@@ -666,7 +679,7 @@ void afb_xreq_reply_f(struct afb_xreq *xreq, struct json_object *obj, const char
 const char *afb_xreq_raw(struct afb_xreq *xreq, size_t *size)
 {
        struct json_object *obj = xreq_json_cb(xreq_to_req_x2(xreq));
-       const char *result = json_object_to_json_string(obj);
+       const char *result = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_NOSLASHESCAPE);
        if (size != NULL)
                *size = strlen(result);
        return result;