2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 #include <openssl/sha.h>
26 #include <microhttpd.h>
28 #include "afb-method.h"
29 #include "afb-context.h"
31 #include "afb-websock.h"
32 #include "afb-ws-json1.h"
35 /**************** WebSocket connection upgrade ****************************/
37 static const char websocket_s[] = "websocket";
38 static const char sec_websocket_key_s[] = "Sec-WebSocket-Key";
39 static const char sec_websocket_version_s[] = "Sec-WebSocket-Version";
40 static const char sec_websocket_accept_s[] = "Sec-WebSocket-Accept";
41 static const char sec_websocket_protocol_s[] = "Sec-WebSocket-Protocol";
42 static const char websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
44 static void enc64(unsigned char *in, char *out)
46 static const char tob64[] =
47 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48 "abcdefghijklmnopqrstuvwxyz"
50 out[0] = tob64[in[0] >> 2];
51 out[1] = tob64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
52 out[2] = tob64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
53 out[3] = tob64[in[2] & 0x3f];
56 static void make_accept_value(const char *key, char result[29])
58 unsigned char md[SHA_DIGEST_LENGTH+1];
59 size_t len = strlen(key);
60 char *buffer = alloca(len + sizeof websocket_guid - 1);
61 memcpy(buffer, key, len);
62 memcpy(buffer + len, websocket_guid, sizeof websocket_guid - 1);
63 SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_guid - 1), md);
64 assert(SHA_DIGEST_LENGTH == 20);
66 enc64(&md[0], &result[0]);
67 enc64(&md[3], &result[4]);
68 enc64(&md[6], &result[8]);
69 enc64(&md[9], &result[12]);
70 enc64(&md[12], &result[16]);
71 enc64(&md[15], &result[20]);
72 enc64(&md[18], &result[24]);
77 static const char vseparators[] = " \t,";
79 static int headerhas(const char *header, const char *needle)
85 header += strspn(header, vseparators);
88 len = strcspn(header, vseparators);
89 if (n == len && 0 == strncasecmp(needle, header, n))
98 void *(*create)(struct fdev *fdev, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure);
101 static const struct protodef *search_proto(const struct protodef *protodefs, const char *protocols)
106 if (protocols == NULL) {
108 return protodefs != NULL && protodefs->name != NULL ? protodefs : NULL;
111 protocols += strspn(protocols, vseparators);
114 len = strcspn(protocols, vseparators);
115 for (i = 0 ; protodefs[i].name != NULL ; i++)
116 if (!strncasecmp(protodefs[i].name, protocols, len)
117 && !protodefs[i].name[len])
118 return &protodefs[i];
123 struct memo_websocket {
124 const struct protodef *proto;
125 struct afb_hreq *hreq;
126 struct afb_apiset *apiset;
129 static void close_websocket(void *closure)
131 struct MHD_UpgradeResponseHandle *urh = closure;
132 MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE);
135 static void upgrade_to_websocket(
137 struct MHD_Connection *connection,
139 const char *extra_in,
140 size_t extra_in_size,
142 struct MHD_UpgradeResponseHandle *urh)
144 struct memo_websocket *memo = cls;
148 fdev = afb_fdev_create(sock);
151 close_websocket(urh);
153 ws = memo->proto->create(fdev, memo->apiset, &memo->hreq->xreq.context, close_websocket, urh);
156 close_websocket(urh);
159 afb_hreq_unref(memo->hreq);
163 static int check_websocket_upgrade(struct MHD_Connection *con, const struct protodef *protodefs, struct afb_hreq *hreq, struct afb_apiset *apiset)
165 struct memo_websocket *memo;
166 struct MHD_Response *response;
167 const char *connection, *upgrade, *key, *version, *protocols;
170 const struct protodef *proto;
172 /* is an upgrade to websocket ? */
173 upgrade = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_UPGRADE);
174 if (upgrade == NULL || strcasecmp(upgrade, websocket_s))
177 /* is a connection for upgrade ? */
178 connection = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION);
179 if (connection == NULL
180 || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
183 /* has a key and a version ? */
184 key = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_key_s);
185 version = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_version_s);
186 if (key == NULL || version == NULL)
189 /* is a supported version ? */
190 vernum = atoi(version);
192 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
193 MHD_add_response_header(response, sec_websocket_version_s, "13");
194 MHD_queue_response(con, MHD_HTTP_UPGRADE_REQUIRED, response);
195 MHD_destroy_response(response);
199 /* is the protocol supported ? */
200 protocols = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_protocol_s);
201 proto = search_proto(protodefs, protocols);
203 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
204 MHD_queue_response(con, MHD_HTTP_PRECONDITION_FAILED, response);
205 MHD_destroy_response(response);
210 memo = malloc(sizeof *memo);
212 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
213 MHD_queue_response(con, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
214 MHD_destroy_response(response);
219 memo->apiset = apiset;
221 /* send the accept connection */
222 response = MHD_create_response_for_upgrade(upgrade_to_websocket, memo);
223 make_accept_value(key, acceptval);
224 MHD_add_response_header(response, sec_websocket_accept_s, acceptval);
225 MHD_add_response_header(response, sec_websocket_protocol_s, proto->name);
226 MHD_add_response_header(response, MHD_HTTP_HEADER_UPGRADE, websocket_s);
227 MHD_queue_response(con, MHD_HTTP_SWITCHING_PROTOCOLS, response);
228 MHD_destroy_response(response);
233 static const struct protodef protodefs[] = {
234 { "x-afb-ws-json1", (void*)afb_ws_json1_create },
238 int afb_websock_check_upgrade(struct afb_hreq *hreq, struct afb_apiset *apiset)
243 if (hreq->method != afb_method_get
244 || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1))
247 rc = check_websocket_upgrade(hreq->connection, protodefs, hreq, apiset);