X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafb-websock.c;h=523ad8ff1978e259fb9353a09ab5e1a85c6702e2;hb=a8e971702f23ee67e02b4716ad4159f12cefdca6;hp=285d65daecf912eea82c92c0de7c8c09f104eac9;hpb=fcef933b69ccc363ad10d9f7308f02c5dc474ff6;p=src%2Fapp-framework-binder.git diff --git a/src/afb-websock.c b/src/afb-websock.c index 285d65da..523ad8ff 100644 --- a/src/afb-websock.c +++ b/src/afb-websock.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 IoT.bzh + * Copyright (C) 2016, 2017 "IoT.bzh" * Author: José Bollo * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,11 +25,11 @@ #include #include -#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,19 +94,18 @@ static int headerhas(const char *header, const char *needle) struct protodef { const char *name; - void *(*create)(int fd, struct AFB_clientCtx *context, void (*cleanup)(void*), void *closure); + void *(*create)(int fd, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_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) +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) @@ -120,8 +119,10 @@ static const struct protodef *search_proto(const char *protocols) } } -int afb_websock_check_upgrade(struct afb_hreq *hreq) +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]; int vernum; @@ -129,64 +130,94 @@ int afb_websock_check_upgrade(struct afb_hreq *hreq) void *ws; /* 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); + connection = MHD_lookup_connection_value(con, MHD_HEADER_KIND, 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)) - 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) { - afb_hreq_reply_empty(hreq, MHD_HTTP_UPGRADE_REQUIRED, - sec_websocket_version_s, "13", NULL); + 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); - proto = protocols == NULL ? NULL : search_proto(protocols); + protocols = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_protocol_s); + proto = search_proto(protodefs, protocols); if (proto == NULL) { - afb_hreq_reply_error(hreq, MHD_HTTP_PRECONDITION_FAILED); + 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; } /* 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); + 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) { - afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR); + 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; } /* send the accept connection */ make_accept_value(key, acceptval); - 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; + response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT); + 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_CONNECTION, MHD_HTTP_HEADER_UPGRADE); + MHD_add_response_header(response, MHD_HTTP_HEADER_UPGRADE, websocket_s); + MHD_queue_response(con, MHD_HTTP_SWITCHING_PROTOCOLS, response); + MHD_destroy_response(response); + + *websock = ws; return 1; } +static const struct protodef protodefs[] = { + { "x-afb-ws-json1", afb_ws_json1_create }, + { NULL, NULL } +}; + +int afb_websock_check_upgrade(struct afb_hreq *hreq, struct afb_apiset *apiset) +{ + void *ws; + int rc; + + /* is a get ? */ + if (hreq->method != afb_method_get + || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1)) + return 0; + + ws = NULL; + 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; +} +