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"
34 /**************** WebSocket connection upgrade ****************************/
36 static const char websocket_s[] = "websocket";
37 static const char sec_websocket_key_s[] = "Sec-WebSocket-Key";
38 static const char sec_websocket_version_s[] = "Sec-WebSocket-Version";
39 static const char sec_websocket_accept_s[] = "Sec-WebSocket-Accept";
40 static const char sec_websocket_protocol_s[] = "Sec-WebSocket-Protocol";
41 static const char websocket_guid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
43 static void enc64(unsigned char *in, char *out)
45 static const char tob64[] =
46 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 "abcdefghijklmnopqrstuvwxyz"
49 out[0] = tob64[in[0] >> 2];
50 out[1] = tob64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
51 out[2] = tob64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
52 out[3] = tob64[in[2] & 0x3f];
55 static void make_accept_value(const char *key, char result[29])
57 unsigned char md[SHA_DIGEST_LENGTH+1];
58 size_t len = strlen(key);
59 char *buffer = alloca(len + sizeof websocket_guid - 1);
60 memcpy(buffer, key, len);
61 memcpy(buffer + len, websocket_guid, sizeof websocket_guid - 1);
62 SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_guid - 1), md);
63 assert(SHA_DIGEST_LENGTH == 20);
65 enc64(&md[0], &result[0]);
66 enc64(&md[3], &result[4]);
67 enc64(&md[6], &result[8]);
68 enc64(&md[9], &result[12]);
69 enc64(&md[12], &result[16]);
70 enc64(&md[15], &result[20]);
71 enc64(&md[18], &result[24]);
76 static const char vseparators[] = " \t,";
78 static int headerhas(const char *header, const char *needle)
84 header += strspn(header, vseparators);
87 len = strcspn(header, vseparators);
88 if (n == len && 0 == strncasecmp(needle, header, n))
97 void *(*create)(int fd, struct afb_apiset *apiset, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure);
100 static const struct protodef *search_proto(const struct protodef *protodefs, const char *protocols)
105 if (protocols == NULL) {
107 return protodefs != NULL && protodefs->name != NULL ? protodefs : NULL;
110 protocols += strspn(protocols, vseparators);
113 len = strcspn(protocols, vseparators);
114 for (i = 0 ; protodefs[i].name != NULL ; i++)
115 if (!strncasecmp(protodefs[i].name, protocols, len)
116 && !protodefs[i].name[len])
117 return &protodefs[i];
122 struct memo_websocket {
123 const struct protodef *proto;
124 struct afb_hreq *hreq;
125 struct afb_apiset *apiset;
128 static void close_websocket(void *closure)
130 struct MHD_UpgradeResponseHandle *urh = closure;
131 MHD_upgrade_action (urh, MHD_UPGRADE_ACTION_CLOSE);
134 static void upgrade_to_websocket(
136 struct MHD_Connection *connection,
138 const char *extra_in,
139 size_t extra_in_size,
141 struct MHD_UpgradeResponseHandle *urh)
143 struct memo_websocket *memo = cls;
146 ws = memo->proto->create(sock, memo->apiset, &memo->hreq->xreq.context, close_websocket, urh);
149 close_websocket(urh);
151 afb_hreq_unref(memo->hreq);
155 static int check_websocket_upgrade(struct MHD_Connection *con, const struct protodef *protodefs, struct afb_hreq *hreq, struct afb_apiset *apiset)
157 struct memo_websocket *memo;
158 struct MHD_Response *response;
159 const char *connection, *upgrade, *key, *version, *protocols;
162 const struct protodef *proto;
164 /* is an upgrade to websocket ? */
165 upgrade = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_UPGRADE);
166 if (upgrade == NULL || strcasecmp(upgrade, websocket_s))
169 /* is a connection for upgrade ? */
170 connection = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION);
171 if (connection == NULL
172 || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
175 /* has a key and a version ? */
176 key = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_key_s);
177 version = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_version_s);
178 if (key == NULL || version == NULL)
181 /* is a supported version ? */
182 vernum = atoi(version);
184 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
185 MHD_add_response_header(response, sec_websocket_version_s, "13");
186 MHD_queue_response(con, MHD_HTTP_UPGRADE_REQUIRED, response);
187 MHD_destroy_response(response);
191 /* is the protocol supported ? */
192 protocols = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_protocol_s);
193 proto = search_proto(protodefs, protocols);
195 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
196 MHD_queue_response(con, MHD_HTTP_PRECONDITION_FAILED, response);
197 MHD_destroy_response(response);
202 memo = malloc(sizeof *memo);
204 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
205 MHD_queue_response(con, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
206 MHD_destroy_response(response);
211 memo->apiset = apiset;
213 /* send the accept connection */
214 response = MHD_create_response_for_upgrade(upgrade_to_websocket, memo);
215 make_accept_value(key, acceptval);
216 MHD_add_response_header(response, sec_websocket_accept_s, acceptval);
217 MHD_add_response_header(response, sec_websocket_protocol_s, proto->name);
218 MHD_add_response_header(response, MHD_HTTP_HEADER_CONNECTION, MHD_HTTP_HEADER_UPGRADE);
219 MHD_add_response_header(response, MHD_HTTP_HEADER_UPGRADE, websocket_s);
220 MHD_queue_response(con, MHD_HTTP_SWITCHING_PROTOCOLS, response);
221 MHD_destroy_response(response);
226 static const struct protodef protodefs[] = {
227 { "x-afb-ws-json1", (void*)afb_ws_json1_create },
231 int afb_websock_check_upgrade(struct afb_hreq *hreq, struct afb_apiset *apiset)
236 if (hreq->method != afb_method_get
237 || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1))
240 rc = check_websocket_upgrade(hreq->connection, protodefs, hreq, apiset);