2 * Copyright (C) 2016-2019 "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.
18 #if WITH_LIBMICROHTTPD
27 #include <openssl/sha.h>
28 #include <microhttpd.h>
30 #include "afb-method.h"
31 #include "afb-context.h"
33 #include "afb-websock.h"
34 #include "afb-ws-json1.h"
38 /**************** WebSocket connection upgrade ****************************/
40 static const char websocket_s[] = "websocket";
41 static const char sec_websocket_key_s[] = "Sec-WebSocket-Key";
42 static const char sec_websocket_version_s[] = "Sec-WebSocket-Version";
43 static const char sec_websocket_accept_s[] = "Sec-WebSocket-Accept";
44 static const char sec_websocket_protocol_s[] = "Sec-WebSocket-Protocol";
45 static const char websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
47 static void enc64(unsigned char *in, char *out)
49 static const char tob64[] =
50 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
51 "abcdefghijklmnopqrstuvwxyz"
53 out[0] = tob64[in[0] >> 2];
54 out[1] = tob64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
55 out[2] = tob64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
56 out[3] = tob64[in[2] & 0x3f];
59 static void make_accept_value(const char *key, char result[29])
61 unsigned char md[SHA_DIGEST_LENGTH+1];
62 size_t len = strlen(key);
63 char *buffer = alloca(len + sizeof websocket_guid - 1);
64 memcpy(buffer, key, len);
65 memcpy(buffer + len, websocket_guid, sizeof websocket_guid - 1);
66 SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_guid - 1), md);
67 assert(SHA_DIGEST_LENGTH == 20);
69 enc64(&md[0], &result[0]);
70 enc64(&md[3], &result[4]);
71 enc64(&md[6], &result[8]);
72 enc64(&md[9], &result[12]);
73 enc64(&md[12], &result[16]);
74 enc64(&md[15], &result[20]);
75 enc64(&md[18], &result[24]);
80 static const char vseparators[] = " \t,";
82 static int headerhas(const char *header, const char *needle)
88 header += strspn(header, vseparators);
91 len = strcspn(header, vseparators);
92 if (n == len && 0 == strncasecmp(needle, header, n))
101 void *(*create)(struct fdev *fdev, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure);
104 static const struct protodef *search_proto(const struct protodef *protodefs, const char *protocols)
109 if (protocols == NULL) {
111 return protodefs != NULL && protodefs->name != NULL ? protodefs : NULL;
114 protocols += strspn(protocols, vseparators);
117 len = strcspn(protocols, vseparators);
118 for (i = 0 ; protodefs[i].name != NULL ; i++)
119 if (!strncasecmp(protodefs[i].name, protocols, len)
120 && !protodefs[i].name[len])
121 return &protodefs[i];
126 struct memo_websocket {
127 const struct protodef *proto;
128 struct afb_hreq *hreq;
129 struct afb_apiset *apiset;
132 static void close_websocket(void *closure)
134 struct MHD_UpgradeResponseHandle *urh = closure;
135 MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE);
138 static void upgrade_to_websocket(
140 struct MHD_Connection *connection,
142 const char *extra_in,
143 size_t extra_in_size,
145 struct MHD_UpgradeResponseHandle *urh)
147 struct memo_websocket *memo = cls;
151 fdev = afb_fdev_create(sock);
154 close_websocket(urh);
156 fdev_set_autoclose(fdev, 0);
157 ws = memo->proto->create(fdev, memo->apiset, &memo->hreq->xreq.context, close_websocket, urh);
160 close_websocket(urh);
163 #if MHD_VERSION <= 0x00095900
164 afb_hreq_unref(memo->hreq);
169 static int check_websocket_upgrade(struct MHD_Connection *con, const struct protodef *protodefs, struct afb_hreq *hreq, struct afb_apiset *apiset)
171 struct memo_websocket *memo;
172 struct MHD_Response *response;
173 const char *connection, *upgrade, *key, *version, *protocols;
176 const struct protodef *proto;
178 /* is an upgrade to websocket ? */
179 upgrade = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_UPGRADE);
180 if (upgrade == NULL || strcasecmp(upgrade, websocket_s))
183 /* is a connection for upgrade ? */
184 connection = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION);
185 if (connection == NULL
186 || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
189 /* has a key and a version ? */
190 key = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_key_s);
191 version = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_version_s);
192 if (key == NULL || version == NULL)
195 /* is a supported version ? */
196 vernum = atoi(version);
198 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
199 MHD_add_response_header(response, sec_websocket_version_s, "13");
200 MHD_queue_response(con, MHD_HTTP_UPGRADE_REQUIRED, response);
201 MHD_destroy_response(response);
205 /* is the protocol supported ? */
206 protocols = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_protocol_s);
207 proto = search_proto(protodefs, protocols);
209 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
210 MHD_queue_response(con, MHD_HTTP_PRECONDITION_FAILED, response);
211 MHD_destroy_response(response);
216 memo = malloc(sizeof *memo);
218 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
219 MHD_queue_response(con, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
220 MHD_destroy_response(response);
225 memo->apiset = apiset;
227 /* send the accept connection */
228 response = MHD_create_response_for_upgrade(upgrade_to_websocket, memo);
229 make_accept_value(key, acceptval);
230 MHD_add_response_header(response, sec_websocket_accept_s, acceptval);
231 MHD_add_response_header(response, sec_websocket_protocol_s, proto->name);
232 MHD_add_response_header(response, MHD_HTTP_HEADER_UPGRADE, websocket_s);
233 MHD_queue_response(con, MHD_HTTP_SWITCHING_PROTOCOLS, response);
234 MHD_destroy_response(response);
239 static const struct protodef protodefs[] = {
240 { "x-afb-ws-json1", (void*)afb_ws_json1_create },
244 int afb_websock_check_upgrade(struct afb_hreq *hreq, struct afb_apiset *apiset)
249 if (hreq->method != afb_method_get
250 || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1))
253 rc = check_websocket_upgrade(hreq->connection, protodefs, hreq, apiset);