X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-websock.c;h=285d65daecf912eea82c92c0de7c8c09f104eac9;hb=fcef933b69ccc363ad10d9f7308f02c5dc474ff6;hp=b6056adcd0e7edf7219e08966bca78671ec1fe5d;hpb=ca208671cc79bbc05c574df788035878e5d39382;p=src%2Fapp-framework-binder.git diff --git a/src/afb-websock.c b/src/afb-websock.c index b6056adc..285d65da 100644 --- a/src/afb-websock.c +++ b/src/afb-websock.c @@ -2,8 +2,6 @@ * Copyright 2016 IoT.bzh * Author: José Bollo * - * 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 @@ -18,79 +16,29 @@ */ #define _GNU_SOURCE -#include + +#include #include #include -#include +#include #include -#include -#include - -#include "websock.h" +#include -#include "../include/local-def.h" +#include "afb-ws-json.h" #include "afb-method.h" #include "afb-hreq.h" +#include "afb-websock.h" + +/**************** WebSocket connection upgrade ****************************/ static const char websocket_s[] = "websocket"; static const char sec_websocket_key_s[] = "Sec-WebSocket-Key"; static const char sec_websocket_version_s[] = "Sec-WebSocket-Version"; static const char sec_websocket_accept_s[] = "Sec-WebSocket-Accept"; static const char sec_websocket_protocol_s[] = "Sec-WebSocket-Protocol"; -static const char websocket_uuid[] = "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 const char websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; static void enc64(unsigned char *in, char *out) { @@ -108,10 +56,10 @@ static void make_accept_value(const char *key, char result[29]) { unsigned char md[SHA_DIGEST_LENGTH+1]; size_t len = strlen(key); - char *buffer = alloca(len + sizeof websocket_uuid - 1); + char *buffer = alloca(len + sizeof websocket_guid - 1); memcpy(buffer, key, len); - memcpy(buffer + len, websocket_uuid, sizeof websocket_uuid - 1); - SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_uuid - 1), md); + memcpy(buffer + len, websocket_guid, sizeof websocket_guid - 1); + SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_guid - 1), md); assert(SHA_DIGEST_LENGTH == 20); md[20] = 0; enc64(&md[0], &result[0]); @@ -125,30 +73,60 @@ 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); -printf("!!!%.*s!!!\n",len,header); + 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)(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *closure); +}; + +static const struct protodef protodefs[] = { + { "x-afb-ws-json1", (void*)afb_ws_json_create }, + { NULL, NULL } +}; + +static const struct protodef *search_proto(const char *protocols) +{ + int i; + size_t len; + + 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; + } +} + +int afb_websock_check_upgrade(struct afb_hreq *hreq) { const char *connection, *upgrade, *key, *version, *protocols; char acceptval[29]; int vernum; - struct MHD_Response *response; + const struct protodef *proto; + void *ws; /* is an upgrade to websocket ? */ upgrade = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_UPGRADE); @@ -157,11 +135,13 @@ int afb_websock_check(struct afb_hreq *hreq, int *later) /* is a connection for upgrade ? */ connection = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONNECTION); - if (connection == NULL || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE)) + 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)) + if (hreq->method != afb_method_get + || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1)) return 0; /* has a key and a version ? */ @@ -173,44 +153,40 @@ int afb_websock_check(struct afb_hreq *hreq, int *later) /* 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; + afb_hreq_reply_empty(hreq, MHD_HTTP_UPGRADE_REQUIRED, + sec_websocket_version_s, "13", NULL); return 1; } /* is the protocol supported ? */ protocols = afb_hreq_get_header(hreq, sec_websocket_protocol_s); + proto = protocols == NULL ? NULL : search_proto(protocols); + if (proto == NULL) { + afb_hreq_reply_error(hreq, MHD_HTTP_PRECONDITION_FAILED); + return 1; + } + + /* create the web socket */ + /* TODO fullfil ondisconnection and records!!! */ + ws = proto->create(dup(MHD_get_connection_info(hreq->connection,MHD_CONNECTION_INFO_CONNECTION_FD)->connect_fd), + afb_hreq_context(hreq), + (void*)MHD_resume_connection, + hreq->connection); + if (ws == NULL) { + afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR); + return 1; + } /* send the accept connection */ 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; + afb_hreq_reply_empty(hreq, MHD_HTTP_SWITCHING_PROTOCOLS, + sec_websocket_accept_s, acceptval, + sec_websocket_protocol_s, proto->name, + MHD_HTTP_HEADER_CONNECTION, MHD_HTTP_HEADER_UPGRADE, + MHD_HTTP_HEADER_UPGRADE, websocket_s, + NULL); + + hreq->upgrade = 1; return 1; } -struct afb_websock *afb_websock_create(struct MHD_Connection *connection) -{ - 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; - } - } - return result; -} -