2 * Copyright 2016 IoT.bzh
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Inspired by the work of
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <microhttpd.h>
27 #include <openssl/sha.h>
28 #include <openssl/bio.h>
29 #include <openssl/evp.h>
33 #include "local-def.h"
35 #include "afb-method.h"
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";
49 struct MHD_Connection *connection;
53 static ssize_t afb_websock_writev(struct afb_websock *ws, const struct iovec *iov, int iovcnt)
57 rc = writev(ws->fd, iov, iovcnt);
58 } while(rc == -1 && errno == EINTR);
62 static ssize_t afb_websock_readv(struct afb_websock *ws, const struct iovec *iov, int iovcnt)
66 rc = readv(ws->fd, iov, iovcnt);
67 } while(rc == -1 && errno == EINTR);
71 static void afb_websock_disconnect(struct afb_websock *ws)
75 static void afb_websock_on_close(struct afb_websock *ws, uint16_t code, size_t size)
79 static void afb_websock_on_content(struct afb_websock *ws, int last, size_t size)
83 static struct websock_itf afb_websock_itf = {
84 .writev = (void*)afb_websock_writev,
85 .readv = (void*)afb_websock_readv,
86 .disconnect = (void*)afb_websock_disconnect,
90 .on_close = (void*)afb_websock_on_close,
91 .on_text = (void*)afb_websock_on_content,
92 .on_binary = (void*)afb_websock_on_content,
93 .on_continue = (void*)afb_websock_on_content
96 static void enc64(unsigned char *in, char *out)
98 static const char tob64[] =
99 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
100 "abcdefghijklmnopqrstuvwxyz"
102 out[0] = tob64[in[0] >> 2];
103 out[1] = tob64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
104 out[2] = tob64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
105 out[3] = tob64[in[2] & 0x3f];
108 static void make_accept_value(const char *key, char result[29])
110 unsigned char md[SHA_DIGEST_LENGTH+1];
111 size_t len = strlen(key);
112 char *buffer = alloca(len + sizeof websocket_guid - 1);
113 memcpy(buffer, key, len);
114 memcpy(buffer + len, websocket_guid, sizeof websocket_guid - 1);
115 SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_guid - 1), md);
116 assert(SHA_DIGEST_LENGTH == 20);
118 enc64(&md[0], &result[0]);
119 enc64(&md[3], &result[4]);
120 enc64(&md[6], &result[8]);
121 enc64(&md[9], &result[12]);
122 enc64(&md[12], &result[16]);
123 enc64(&md[15], &result[20]);
124 enc64(&md[18], &result[24]);
129 static int headerhas(const char *header, const char *needle)
131 static const char sep[] = " \t,";
136 header += strspn(header, sep);
139 len = strcspn(header, sep);
140 if (n == len && 0 == strncasecmp(needle, header, n))
146 int afb_websock_check(struct afb_hreq *hreq, int *later)
148 const char *connection, *upgrade, *key, *version, *protocols;
151 struct MHD_Response *response;
153 /* is an upgrade to websocket ? */
154 upgrade = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_UPGRADE);
155 if (upgrade == NULL || strcasecmp(upgrade, websocket_s))
158 /* is a connection for upgrade ? */
159 connection = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONNECTION);
160 if (connection == NULL || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
164 if(hreq->method != afb_method_get || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1))
167 /* has a key and a version ? */
168 key = afb_hreq_get_header(hreq, sec_websocket_key_s);
169 version = afb_hreq_get_header(hreq, sec_websocket_version_s);
170 if (key == NULL || version == NULL)
173 /* is a supported version ? */
174 vernum = atoi(version);
176 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
177 MHD_add_response_header (response, sec_websocket_version_s, "13");
178 MHD_queue_response (hreq->connection, MHD_HTTP_BAD_REQUEST, response);
179 MHD_destroy_response (response);
184 /* is the protocol supported ? */
185 protocols = afb_hreq_get_header(hreq, sec_websocket_protocol_s);
187 /* send the accept connection */
188 make_accept_value(key, acceptval);
189 response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
190 MHD_add_response_header (response, sec_websocket_accept_s, acceptval);
191 MHD_add_response_header (response, MHD_HTTP_HEADER_CONNECTION, MHD_HTTP_HEADER_UPGRADE);
192 MHD_add_response_header (response, MHD_HTTP_HEADER_UPGRADE, websocket_s);
193 MHD_queue_response (hreq->connection, MHD_HTTP_SWITCHING_PROTOCOLS, response);
194 MHD_destroy_response (response);
200 struct afb_websock *afb_websock_create(struct MHD_Connection *connection)
202 struct afb_websock *result;
204 result = malloc(sizeof * result);
206 result->connection = connection;
207 result->fd = MHD_get_connection_info(connection, MHD_CONNECTION_INFO_CONNECTION_FD)->connect_fd;
208 result->ws = websock_create(&afb_websock_itf, result);
209 if (result->ws == NULL) {