more simplification
[src/app-framework-binder.git] / src / afb-websock.c
index dab4aa0..e8f596c 100644 (file)
 #include <json.h>
 
 #include <openssl/sha.h>
-/*
-#include <openssl/bio.h>
-#include <openssl/evp.h>
-*/
 
 #include "websock.h"
 
-#include "local-def.h"
-
 #include "afb-req-itf.h"
 #include "afb-method.h"
 #include "afb-hreq.h"
@@ -220,15 +214,21 @@ static const struct afb_req_itf wsreq_itf = {
 
 struct afb_websock *afb_websock_create(struct afb_hreq *hreq)
 {
+       int fd;
        struct afb_websock *result;
 
+       fd = MHD_get_connection_info(hreq->connection,
+                               MHD_CONNECTION_INFO_CONNECTION_FD)->connect_fd;
+       fd = dup(fd);
+       if (fd < 0)
+               return NULL;
+
        result = malloc(sizeof * result);
        if (result == NULL)
                goto error;
 
        result->connection = hreq->connection;
-       result->fd = MHD_get_connection_info(hreq->connection,
-                               MHD_CONNECTION_INFO_CONNECTION_FD)->connect_fd;
+       result->fd = fd;
        result->context = ctxClientGet(afb_hreq_context(hreq));
        if (result->context == NULL)
                goto error2;
@@ -255,6 +255,7 @@ error3:
 error2:
        free(result);
 error:
+       close(fd);
        return NULL;
 }
 
@@ -346,7 +347,7 @@ static int aws_handle_json(struct afb_websock *aws, struct json_object *obj)
                goto error;
        verb = &api[lenapi+1];
        for (lenverb = 0 ; verb[lenverb] && verb[lenverb] != '/' ; lenverb++);
-       if (!lenverb || !verb[lenverb])
+       if (!lenverb || verb[lenverb])
                goto error;
 
        /* allocates the request data */
@@ -418,7 +419,7 @@ static struct afb_arg wsreq_get(struct afb_wsreq *wsreq, const char *name)
                arg.value = NULL;
        }
        arg.size = 0;
-       arg.is_file = 0;
+       arg.path = NULL;
        return arg;
 }
 
@@ -429,7 +430,7 @@ static void wsreq_iterate(struct afb_wsreq *wsreq, int (*iterator)(void *closure
        struct json_object_iterator end = json_object_iter_end(wsreq->request);
 
        arg.size = 0;
-       arg.is_file = 0;
+       arg.path = NULL;
        while(!json_object_iter_equal(&it, &end)) {
                arg.name = json_object_iter_peek_name(&it);
                arg.value = json_object_get_string(json_object_iter_peek_value(&it));
@@ -502,6 +503,7 @@ static void wsreq_reply(struct afb_wsreq *wsreq, int retcode, const char *status
        /* emits the reply */
        message = json_object_to_json_string(reply);
        websock_text(wsreq->aws->ws, message, strlen(message));
+       json_object_put(reply);
 
        /* TODO eliminates the wsreq */
 }