2 * Copyright 2016 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-ws-json.h"
30 #include "afb-method.h"
31 #include "afb-context.h"
33 #include "afb-websock.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)(int fd, void *context, void (*cleanup)(void*), void *cleanup_closure);
101 static const struct protodef *search_proto(const struct protodef *protodefs, const char *protocols)
107 protocols += strspn(protocols, vseparators);
110 len = strcspn(protocols, vseparators);
111 for (i = 0 ; protodefs[i].name != NULL ; i++)
112 if (!strncasecmp(protodefs[i].name, protocols, len)
113 && !protodefs[i].name[len])
114 return &protodefs[i];
119 static int check_websocket_upgrade(struct MHD_Connection *con, const struct protodef *protodefs, void *context, void **websock)
121 const union MHD_ConnectionInfo *info;
122 struct MHD_Response *response;
123 const char *connection, *upgrade, *key, *version, *protocols;
126 const struct protodef *proto;
129 /* is an upgrade to websocket ? */
130 upgrade = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_UPGRADE);
131 if (upgrade == NULL || strcasecmp(upgrade, websocket_s))
134 /* is a connection for upgrade ? */
135 connection = MHD_lookup_connection_value(con, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONNECTION);
136 if (connection == NULL
137 || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
140 /* has a key and a version ? */
141 key = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_key_s);
142 version = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_version_s);
143 if (key == NULL || version == NULL)
146 /* is a supported version ? */
147 vernum = atoi(version);
149 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
150 MHD_add_response_header(response, sec_websocket_version_s, "13");
151 MHD_queue_response(con, MHD_HTTP_UPGRADE_REQUIRED, response);
152 MHD_destroy_response(response);
156 /* is the protocol supported ? */
157 protocols = MHD_lookup_connection_value(con, MHD_HEADER_KIND, sec_websocket_protocol_s);
158 proto = protocols == NULL ? NULL : search_proto(protodefs, protocols);
160 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
161 MHD_queue_response(con, MHD_HTTP_PRECONDITION_FAILED, response);
162 MHD_destroy_response(response);
166 /* create the web socket */
167 info = MHD_get_connection_info(con, MHD_CONNECTION_INFO_CONNECTION_FD);
169 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
170 MHD_queue_response(con, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
171 MHD_destroy_response(response);
174 ws = proto->create(info->connect_fd, context, (void*)MHD_resume_connection, con);
176 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
177 MHD_queue_response(con, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
178 MHD_destroy_response(response);
182 /* send the accept connection */
183 make_accept_value(key, acceptval);
184 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
185 MHD_add_response_header(response, sec_websocket_accept_s, acceptval);
186 MHD_add_response_header(response, sec_websocket_protocol_s, proto->name);
187 MHD_add_response_header(response, MHD_HTTP_HEADER_CONNECTION, MHD_HTTP_HEADER_UPGRADE);
188 MHD_add_response_header(response, MHD_HTTP_HEADER_UPGRADE, websocket_s);
189 MHD_queue_response(con, MHD_HTTP_SWITCHING_PROTOCOLS, response);
190 MHD_destroy_response(response);
196 static const struct protodef protodefs[] = {
197 { "x-afb-ws-json1", (void*)afb_ws_json_create },
201 int afb_websock_check_upgrade(struct afb_hreq *hreq)
207 if (hreq->method != afb_method_get
208 || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1))
212 rc = check_websocket_upgrade(hreq->connection, protodefs, hreq->context.session, &ws);