Introduce apiset for grouping apis
[src/app-framework-binder.git] / src / afb-websock.c
index 283a88b..523ad8f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 IoT.bzh
+ * Copyright (C) 2016, 2017 "IoT.bzh"
  * Author: José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
 #include <openssl/sha.h>
 #include <microhttpd.h>
 
-#include "afb-ws-json.h"
-
 #include "afb-method.h"
+#include "afb-context.h"
 #include "afb-hreq.h"
 #include "afb-websock.h"
+#include "afb-ws-json1.h"
 
 /**************** WebSocket connection upgrade ****************************/
 
@@ -94,7 +94,7 @@ static int headerhas(const char *header, const char *needle)
 struct protodef
 {
        const char *name;
-       void *(*create)(int fd, void *context, void (*cleanup)(void*), void *cleanup_closure);
+       void *(*create)(int fd, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure);
 };
 
 static const struct protodef *search_proto(const struct protodef *protodefs, const char *protocols)
@@ -102,6 +102,10 @@ static const struct protodef *search_proto(const struct protodef *protodefs, con
        int i;
        size_t len;
 
+       if (protocols == NULL) {
+               /* return NULL; */
+               return protodefs != NULL && protodefs->name != NULL ? protodefs : NULL;
+       }
        for(;;) {
                protocols += strspn(protocols, vseparators);
                if (!*protocols)
@@ -115,8 +119,9 @@ static const struct protodef *search_proto(const struct protodef *protodefs, con
        }
 }
 
-static int check_websocket_upgrade(struct MHD_Connection *con, const struct protodef *protodefs, void *context, void **websock)
+static int check_websocket_upgrade(struct MHD_Connection *con, const struct protodef *protodefs, void *context, void **websock, struct afb_apiset *apiset)
 {
+       const union MHD_ConnectionInfo *info;
        struct MHD_Response *response;
        const char *connection, *upgrade, *key, *version, *protocols;
        char acceptval[29];
@@ -153,7 +158,7 @@ static int check_websocket_upgrade(struct MHD_Connection *con, const struct prot
 
        /* is the protocol supported ? */
        protocols = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_protocol_s);
-       proto = protocols == NULL ? NULL : search_proto(protodefs, protocols);
+       proto = search_proto(protodefs, protocols);
        if (proto == NULL) {
                response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
                MHD_queue_response(con, MHD_HTTP_PRECONDITION_FAILED, response);
@@ -162,10 +167,14 @@ static int check_websocket_upgrade(struct MHD_Connection *con, const struct prot
        }
 
        /* create the web socket */
-       ws = proto->create(dup(MHD_get_connection_info(con, MHD_CONNECTION_INFO_CONNECTION_FD)->connect_fd),
-                       context,
-                       (void*)MHD_resume_connection,
-                       con);
+       info = MHD_get_connection_info(con, MHD_CONNECTION_INFO_CONNECTION_FD);
+       if (info == NULL) {
+               response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
+               MHD_queue_response(con, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
+               MHD_destroy_response(response);
+               return 1;
+       }
+       ws = proto->create(info->connect_fd, apiset, context, (void*)MHD_resume_connection, con);
        if (ws == NULL) {
                response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
                MHD_queue_response(con, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
@@ -188,11 +197,11 @@ static int check_websocket_upgrade(struct MHD_Connection *con, const struct prot
 }
 
 static const struct protodef protodefs[] = {
-       { "x-afb-ws-json1",     (void*)afb_ws_json_create },
+       { "x-afb-ws-json1",     afb_ws_json1_create },
        { NULL, NULL }
 };
 
-int afb_websock_check_upgrade(struct afb_hreq *hreq)
+int afb_websock_check_upgrade(struct afb_hreq *hreq, struct afb_apiset *apiset)
 {
        void *ws;
        int rc;
@@ -203,9 +212,12 @@ int afb_websock_check_upgrade(struct afb_hreq *hreq)
                return 0;
 
        ws = NULL;
-       rc = check_websocket_upgrade(hreq->connection, protodefs, afb_hreq_context(hreq), &ws);
-       if (rc && ws != NULL)
-               hreq->upgrade = 1;
+       rc = check_websocket_upgrade(hreq->connection, protodefs, &hreq->xreq.context, &ws, apiset);
+       if (rc == 1) {
+               hreq->replied = 1;
+               if (ws != NULL)
+                       hreq->upgrade = 1;
+       }
        return rc;
 }