Update copyright dates
[src/app-framework-binder.git] / src / afb-websock.c
index b604432..df835d6 100644 (file)
@@ -1,9 +1,7 @@
 /*
- * Copyright 2016 IoT.bzh
+ * Copyright (C) 2015-2020 "IoT.bzh"
  * Author: José Bollo <jose.bollo@iot.bzh>
  *
- * Inspired by the work of 
- *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  */
 
 #define _GNU_SOURCE
-#include <microhttpd.h>
+
+#include <stdlib.h>
 #include <assert.h>
 #include <errno.h>
-#include <sys/uio.h>
+#include <string.h>
 
 #include <openssl/sha.h>
-#include <openssl/bio.h>
-#include <openssl/evp.h>
-
-#include "websock.h"
-
-#include "../include/local-def.h"
+#include <microhttpd.h>
 
 #include "afb-method.h"
+#include "afb-context.h"
 #include "afb-hreq.h"
+#include "afb-websock.h"
+#include "afb-ws-json1.h"
+#include "afb-fdev.h"
+#include "fdev.h"
+
+/**************** WebSocket connection upgrade ****************************/
 
 static const char websocket_s[] = "websocket";
 static const char sec_websocket_key_s[] = "Sec-WebSocket-Key";
@@ -41,57 +42,6 @@ static const char sec_websocket_accept_s[] = "Sec-WebSocket-Accept";
 static const char sec_websocket_protocol_s[] = "Sec-WebSocket-Protocol";
 static const char websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
 
-
-struct afb_websock
-{
-       int fd;
-       struct MHD_Connection *connection;
-       struct websock *ws;
-};
-
-static ssize_t afb_websock_writev(struct afb_websock *ws, const struct iovec *iov, int iovcnt)
-{
-       ssize_t rc;
-       do {
-               rc = writev(ws->fd, iov, iovcnt);
-       } while(rc == -1 && errno == EINTR);
-       return rc;
-}
-
-static ssize_t afb_websock_readv(struct afb_websock *ws, const struct iovec *iov, int iovcnt)
-{
-       ssize_t rc;
-       do {
-               rc = readv(ws->fd, iov, iovcnt);
-       } while(rc == -1 && errno == EINTR);
-       return rc;
-}
-
-static void afb_websock_disconnect(struct afb_websock *ws)
-{
-}
-
-static void afb_websock_on_close(struct afb_websock *ws, uint16_t code, size_t size)
-{
-}
-
-static void afb_websock_on_content(struct afb_websock *ws, int last, size_t size)
-{
-}
-
-static struct websock_itf afb_websock_itf = {
-       .writev = (void*)afb_websock_writev,
-       .readv = (void*)afb_websock_readv,
-       .disconnect = (void*)afb_websock_disconnect,
-
-       .on_ping = NULL,
-       .on_pong = NULL,
-       .on_close = (void*)afb_websock_on_close,
-       .on_text = (void*)afb_websock_on_content,
-       .on_binary = (void*)afb_websock_on_content,
-       .on_continue = (void*)afb_websock_on_content
-};
-
 static void enc64(unsigned char *in, char *out)
 {
        static const char tob64[] =
@@ -125,91 +75,183 @@ static void make_accept_value(const char *key, char result[29])
        result[28] = 0;
 }
 
+static const char vseparators[] = " \t,";
+
 static int headerhas(const char *header, const char *needle)
 {
-       static const char sep[] = " \t,";
        size_t len, n;
 
        n = strlen(needle);
        for(;;) {
-               header += strspn(header, sep);
+               header += strspn(header, vseparators);
                if (!*header)
                        return 0;
-               len = strcspn(header, sep);
+               len = strcspn(header, vseparators);
                if (n == len && 0 == strncasecmp(needle, header, n))
                        return 1;
                header += len;
        }
 }
 
-int afb_websock_check(struct afb_hreq *hreq, int *later)
+struct protodef
 {
+       const char *name;
+       void *(*create)(struct fdev *fdev, 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)
+{
+       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)
+                       return NULL;
+               len = strcspn(protocols, vseparators);
+               for (i = 0 ; protodefs[i].name != NULL ; i++)
+                       if (!strncasecmp(protodefs[i].name, protocols, len)
+                        && !protodefs[i].name[len])
+                               return &protodefs[i];
+               protocols += len;
+       }
+}
+
+struct memo_websocket {
+       const struct protodef *proto;
+       struct afb_hreq *hreq;
+       struct afb_apiset *apiset;
+};
+
+static void close_websocket(void *closure)
+{
+       struct MHD_UpgradeResponseHandle *urh = closure;
+       MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE);
+}
+
+static void upgrade_to_websocket(
+                       void *cls,
+                       struct MHD_Connection *connection,
+                       void *con_cls,
+                       const char *extra_in,
+                       size_t extra_in_size,
+                       MHD_socket sock,
+                       struct MHD_UpgradeResponseHandle *urh)
+{
+       struct memo_websocket *memo = cls;
+       void *ws;
+       struct fdev *fdev;
+
+       fdev = afb_fdev_create(sock);
+       if (!fdev) {
+               /* TODO */
+               close_websocket(urh);
+       } else {
+               fdev_set_autoclose(fdev, 0);
+               ws = memo->proto->create(fdev, memo->apiset, &memo->hreq->xreq.context, close_websocket, urh);
+               if (ws == NULL) {
+                       /* TODO */
+                       close_websocket(urh);
+               }
+       }
+#if MHD_VERSION <= 0x00095900
+       afb_hreq_unref(memo->hreq);
+#endif
+       free(memo);
+}
+
+static int check_websocket_upgrade(struct MHD_Connection *con, const struct protodef *protodefs, struct afb_hreq *hreq, struct afb_apiset *apiset)
+{
+       struct memo_websocket *memo;
+       struct MHD_Response *response;
        const char *connection, *upgrade, *key, *version, *protocols;
        char acceptval[29];
        int vernum;
-       struct MHD_Response *response;
+       const struct protodef *proto;
 
        /* is an upgrade to websocket ? */
-       upgrade = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_UPGRADE);
+       upgrade = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_UPGRADE);
        if (upgrade == NULL || strcasecmp(upgrade, websocket_s))
                return 0;
 
        /* is a connection for upgrade ? */
-       connection = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONNECTION);
-       if (connection == NULL || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
-               return 0;
-
-       /* is a get ? */
-       if(hreq->method != afb_method_get || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1))
+       connection = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION);
+       if (connection == NULL
+        || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
                return 0;
 
        /* has a key and a version ? */
-       key = afb_hreq_get_header(hreq, sec_websocket_key_s);
-       version = afb_hreq_get_header(hreq, sec_websocket_version_s);
+       key = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_key_s);
+       version = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_version_s);
        if (key == NULL || version == NULL)
                return 0;
 
        /* is a supported version ? */
        vernum = atoi(version);
        if (vernum != 13) {
-               response = MHD_create_response_from_data(0,NULL,0,0);
-               MHD_add_response_header (response, sec_websocket_version_s, "13");
-               MHD_queue_response (hreq->connection, MHD_HTTP_BAD_REQUEST, response);
-               MHD_destroy_response (response);
-               *later = 1;
+               response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
+               MHD_add_response_header(response, sec_websocket_version_s, "13");
+               MHD_queue_response(con, MHD_HTTP_UPGRADE_REQUIRED, response);
+               MHD_destroy_response(response);
                return 1;
        }
 
        /* is the protocol supported ? */
-       protocols = afb_hreq_get_header(hreq, sec_websocket_protocol_s);
+       protocols = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_protocol_s);
+       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);
+               MHD_destroy_response(response);
+               return 1;
+       }
+
+       /* record context */
+       memo = malloc(sizeof *memo);
+       if (memo == 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;
+       }
+       memo->proto = proto;
+       memo->hreq = hreq;
+       memo->apiset = apiset;
 
        /* send the accept connection */
+       response = MHD_create_response_for_upgrade(upgrade_to_websocket, memo);
        make_accept_value(key, acceptval);
-       response = MHD_create_response_from_data(0,NULL,0,0);
-       MHD_add_response_header (response, sec_websocket_accept_s, acceptval);
-       MHD_add_response_header (response, MHD_HTTP_HEADER_CONNECTION, MHD_HTTP_HEADER_UPGRADE);
-       MHD_add_response_header (response, MHD_HTTP_HEADER_UPGRADE, websocket_s);
-       MHD_queue_response (hreq->connection, MHD_HTTP_SWITCHING_PROTOCOLS, response);
-       MHD_destroy_response (response);
-
-       *later = 0;
+       MHD_add_response_header(response, sec_websocket_accept_s, acceptval);
+       MHD_add_response_header(response, sec_websocket_protocol_s, proto->name);
+       MHD_add_response_header(response, MHD_HTTP_HEADER_UPGRADE, websocket_s);
+       MHD_queue_response(con, MHD_HTTP_SWITCHING_PROTOCOLS, response);
+       MHD_destroy_response(response);
+
        return 1;
 }
 
-struct afb_websock *afb_websock_create(struct MHD_Connection *connection)
+static const struct protodef protodefs[] = {
+       { "x-afb-ws-json1",     (void*)afb_ws_json1_create },
+       { NULL, NULL }
+};
+
+int afb_websock_check_upgrade(struct afb_hreq *hreq, struct afb_apiset *apiset)
 {
-       struct afb_websock *result;
-
-       result = malloc(sizeof * result);
-       if (result) {
-               result->connection = connection;
-               result->fd = MHD_get_connection_info(connection, MHD_CONNECTION_INFO_CONNECTION_FD)->connect_fd;
-               result->ws = websock_create(&afb_websock_itf, result);
-               if (result->ws == NULL) {
-                       free(result);
-                       result = NULL;
-               }
+       int rc;
+
+       /* is a get ? */
+       if (hreq->method != afb_method_get
+        || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1))
+               return 0;
+
+       rc = check_websocket_upgrade(hreq->connection, protodefs, hreq, apiset);
+       if (rc == 1) {
+               hreq->replied = 1;
        }
-       return result;
+       return rc;
 }