ensure that the copy is done by libmicrohttp
[src/app-framework-binder.git] / src / afb-ws-json.c
index 6fc6e3b..4bdda7e 100644 (file)
 #include "afb-req-itf.h"
 #include "afb-apis.h"
 
-static void aws_on_close(struct afb_ws_json *ws, uint16_t code, char *text, size_t size);
+static void aws_on_hangup(struct afb_ws_json *ws);
 static void aws_on_text(struct afb_ws_json *ws, char *text, size_t size);
 
 static struct afb_ws_itf aws_itf = {
-       .on_close = (void*)aws_on_close,
-       .on_text = (void*)aws_on_text,
-       .on_binary = NULL,
+       .on_hangup = (void*)aws_on_hangup,
+       .on_text = (void*)aws_on_text
 };
 
 struct afb_wsreq;
@@ -53,10 +52,9 @@ struct afb_ws_json
        struct afb_ws *ws;
 };
 
-
 static void aws_send_event(struct afb_ws_json *ws, const char *event, struct json_object *object);
 
-static const struct afb_event_sender_itf event_sender_itf = {
+static const struct afb_event_listener_itf event_listener_itf = {
        .send = (void*)aws_send_event
 };
 
@@ -86,13 +84,13 @@ struct afb_ws_json *afb_ws_json_create(int fd, struct AFB_clientCtx *context, vo
        if (result->ws == NULL)
                goto error4;
 
-       if (0 > ctxClientEventSenderAdd(result->context, (struct afb_event_sender){ .itf = &event_sender_itf, .closure = result }))
+       if (0 > ctxClientEventListenerAdd(result->context, (struct afb_event_listener){ .itf = &event_listener_itf, .closure = result }))
                goto error5;
 
        return result;
 
 error5:
-       /* TODO */
+       afb_ws_destroy(result->ws);
 error4:
        json_tokener_free(result->tokener);
 error3:
@@ -104,10 +102,14 @@ error:
        return NULL;
 }
 
-static void aws_on_close(struct afb_ws_json *ws, uint16_t code, char *text, size_t size)
+static void aws_on_hangup(struct afb_ws_json *ws)
 {
-       /* do nothing but free the text */
-       free(text);
+       ctxClientEventListenerRemove(ws->context, (struct afb_event_listener){ .itf = &event_listener_itf, .closure = ws });
+       afb_ws_destroy(ws->ws);
+       json_tokener_free(ws->tokener);
+       if (ws->cleanup != NULL)
+               ws->cleanup(ws->cleanup_closure);
+       free(ws);
 }
 
 #define CALL 2
@@ -140,7 +142,7 @@ static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name);
 static void wsreq_fail(struct afb_wsreq *wsreq, const char *status, const char *info);
 static void wsreq_success(struct afb_wsreq *wsreq, struct json_object *obj, const char *info);
 static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size);
-static void wsreq_send(struct afb_wsreq *wsreq, char *buffer, size_t size);
+static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size);
 static int wsreq_session_create(struct afb_wsreq *wsreq);
 static int wsreq_session_check(struct afb_wsreq *wsreq, int refresh);
 static void wsreq_session_close(struct afb_wsreq *wsreq);
@@ -158,7 +160,6 @@ static const struct afb_req_itf wsreq_itf = {
        .session_close = (void*)wsreq_session_close,
        .context_get = (void*)afb_context_get,
        .context_set = (void*)afb_context_set
-
 };
 
 static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
@@ -281,14 +282,6 @@ static int aws_wsreq_parse(struct afb_wsreq *r, char *text, size_t size)
        /* done */
        r->text = text;
        r->size = size;
-fprintf(stderr, "\n\nONTEXT([%d, %.*s, %.*s/%.*s, %.*s, %.*s])\n\n",
-       r->code,
-       (int)r->idlen, r->id,
-       (int)r->apilen, r->api,
-       (int)r->verblen, r->verb,
-       (int)r->objlen, r->obj,
-       (int)r->toklen, r->tok
-);
        return 1;
 
 bad_header:
@@ -323,7 +316,7 @@ bad_header:
        free(wsreq);
 alloc_error:
        free(text);
-       afb_ws_close(ws->ws, 1008);
+       afb_ws_close(ws->ws, 1008, NULL);
        return;
 }
 
@@ -433,7 +426,7 @@ static const char *wsreq_raw(struct afb_wsreq *wsreq, size_t *size)
        return wsreq->obj;
 }
 
-static void wsreq_send(struct afb_wsreq *wsreq, char *buffer, size_t size)
+static void wsreq_send(struct afb_wsreq *wsreq, const char *buffer, size_t size)
 {
        afb_ws_text(wsreq->aws->ws, buffer, size);
 }