2 * Copyright (C) 2016, 2017, 2018 "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"
36 /**************** WebSocket connection upgrade ****************************/
38 static const char websocket_s[] = "websocket";
39 static const char sec_websocket_key_s[] = "Sec-WebSocket-Key";
40 static const char sec_websocket_version_s[] = "Sec-WebSocket-Version";
41 static const char sec_websocket_accept_s[] = "Sec-WebSocket-Accept";
42 static const char sec_websocket_protocol_s[] = "Sec-WebSocket-Protocol";
43 static const char websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
45 static void enc64(unsigned char *in, char *out)
47 static const char tob64[] =
48 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
49 "abcdefghijklmnopqrstuvwxyz"
51 out[0] = tob64[in[0] >> 2];
52 out[1] = tob64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
53 out[2] = tob64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
54 out[3] = tob64[in[2] & 0x3f];
57 static void make_accept_value(const char *key, char result[29])
59 unsigned char md[SHA_DIGEST_LENGTH+1];
60 size_t len = strlen(key);
61 char *buffer = alloca(len + sizeof websocket_guid - 1);
62 memcpy(buffer, key, len);
63 memcpy(buffer + len, websocket_guid, sizeof websocket_guid - 1);
64 SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_guid - 1), md);
65 assert(SHA_DIGEST_LENGTH == 20);
67 enc64(&md[0], &result[0]);
68 enc64(&md[3], &result[4]);
69 enc64(&md[6], &result[8]);
70 enc64(&md[9], &result[12]);
71 enc64(&md[12], &result[16]);
72 enc64(&md[15], &result[20]);
73 enc64(&md[18], &result[24]);
78 static const char vseparators[] = " \t,";
80 static int headerhas(const char *header, const char *needle)
86 header += strspn(header, vseparators);
89 len = strcspn(header, vseparators);
90 if (n == len && 0 == strncasecmp(needle, header, n))
99 void *(*create)(struct fdev *fdev, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure);
102 static const struct protodef *search_proto(const struct protodef *protodefs, const char *protocols)
107 if (protocols == NULL) {
109 return protodefs != NULL && protodefs->name != NULL ? protodefs : NULL;
112 protocols += strspn(protocols, vseparators);
115 len = strcspn(protocols, vseparators);
116 for (i = 0 ; protodefs[i].name != NULL ; i++)
117 if (!strncasecmp(protodefs[i].name, protocols, len)
118 && !protodefs[i].name[len])
119 return &protodefs[i];
124 struct memo_websocket {
125 const struct protodef *proto;
126 struct afb_hreq *hreq;
127 struct afb_apiset *apiset;
130 static void close_websocket(void *closure)
132 struct MHD_UpgradeResponseHandle *urh = closure;
133 MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE);
136 static void upgrade_to_websocket(
138 struct MHD_Connection *connection,
140 const char *extra_in,
141 size_t extra_in_size,
143 struct MHD_UpgradeResponseHandle *urh)
145 struct memo_websocket *memo = cls;
149 fdev = afb_fdev_create(sock);
152 close_websocket(urh);
154 fdev_set_autoclose(fdev, 0);
155 ws = memo->proto->create(fdev, memo->apiset, &memo->hreq->xreq.context, close_websocket, urh);
158 close_websocket(urh);
161 #if MHD_VERSION <= 0x00095900
162 afb_hreq_unref(memo->hreq);
167 static int check_websocket_upgrade(struct MHD_Connection *con, const struct protodef *protodefs, struct afb_hreq *hreq, struct afb_apiset *apiset)
169 struct memo_websocket *memo;
170 struct MHD_Response *response;
171 const char *connection, *upgrade, *key, *version, *protocols;
174 const struct protodef *proto;
176 /* is an upgrade to websocket ? */
177 upgrade = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_UPGRADE);
178 if (upgrade == NULL || strcasecmp(upgrade, websocket_s))
181 /* is a connection for upgrade ? */
182 connection = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION);
183 if (connection == NULL
184 || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
187 /* has a key and a version ? */
188 key = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_key_s);
189 version = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_version_s);
190 if (key == NULL || version == NULL)
193 /* is a supported version ? */
194 vernum = atoi(version);
196 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
197 MHD_add_response_header(response, sec_websocket_version_s, "13");
198 MHD_queue_response(con, MHD_HTTP_UPGRADE_REQUIRED, response);
199 MHD_destroy_response(response);
203 /* is the protocol supported ? */
204 protocols = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_protocol_s);
205 proto = search_proto(protodefs, protocols);
207 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
208 MHD_queue_response(con, MHD_HTTP_PRECONDITION_FAILED, response);
209 MHD_destroy_response(response);
214 memo = malloc(sizeof *memo);
216 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
217 MHD_queue_response(con, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
218 MHD_destroy_response(response);
223 memo->apiset = apiset;
225 /* send the accept connection */
226 response = MHD_create_response_for_upgrade(upgrade_to_websocket, memo);
227 make_accept_value(key, acceptval);
228 MHD_add_response_header(response, sec_websocket_accept_s, acceptval);
229 MHD_add_response_header(response, sec_websocket_protocol_s, proto->name);
230 MHD_add_response_header(response, MHD_HTTP_HEADER_UPGRADE, websocket_s);
231 MHD_queue_response(con, MHD_HTTP_SWITCHING_PROTOCOLS, response);
232 MHD_destroy_response(response);
237 static const struct protodef protodefs[] = {
238 { "x-afb-ws-json1", (void*)afb_ws_json1_create },
242 int afb_websock_check_upgrade(struct afb_hreq *hreq, struct afb_apiset *apiset)
247 if (hreq->method != afb_method_get
248 || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1))
251 rc = check_websocket_upgrade(hreq->connection, protodefs, hreq, apiset);