splitting rest-api in two parts
[src/app-framework-binder.git] / src / afb-websock.c
1 /*
2  * Copyright 2016 IoT.bzh
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Inspired by the work of 
6  *
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 #define _GNU_SOURCE
21 #include <microhttpd.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <sys/uio.h>
25
26 #include <openssl/sha.h>
27 #include <openssl/bio.h>
28 #include <openssl/evp.h>
29
30 #include "websock.h"
31
32 #include "../include/local-def.h"
33
34 #include "afb-method.h"
35 #include "afb-hreq.h"
36
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_uuid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
43
44
45 struct afb_websock
46 {
47         int fd;
48         struct MHD_Connection *connection;
49         struct websock *ws;
50 };
51
52 static ssize_t afb_websock_writev(struct afb_websock *ws, const struct iovec *iov, int iovcnt)
53 {
54         ssize_t rc;
55         do {
56                 rc = writev(ws->fd, iov, iovcnt);
57         } while(rc == -1 && errno == EINTR);
58         return rc;
59 }
60
61 static ssize_t afb_websock_readv(struct afb_websock *ws, const struct iovec *iov, int iovcnt)
62 {
63         ssize_t rc;
64         do {
65                 rc = readv(ws->fd, iov, iovcnt);
66         } while(rc == -1 && errno == EINTR);
67         return rc;
68 }
69
70 static void afb_websock_disconnect(struct afb_websock *ws)
71 {
72 }
73
74 static void afb_websock_on_close(struct afb_websock *ws, uint16_t code, size_t size)
75 {
76 }
77
78 static void afb_websock_on_content(struct afb_websock *ws, int last, size_t size)
79 {
80 }
81
82 static struct websock_itf afb_websock_itf = {
83         .writev = (void*)afb_websock_writev,
84         .readv = (void*)afb_websock_readv,
85         .disconnect = (void*)afb_websock_disconnect,
86
87         .on_ping = NULL,
88         .on_pong = NULL,
89         .on_close = (void*)afb_websock_on_close,
90         .on_text = (void*)afb_websock_on_content,
91         .on_binary = (void*)afb_websock_on_content,
92         .on_continue = (void*)afb_websock_on_content
93 };
94
95 static void enc64(unsigned char *in, char *out)
96 {
97         static const char tob64[] =
98                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
99                 "abcdefghijklmnopqrstuvwxyz"
100                 "0123456789+/";
101         out[0] = tob64[in[0] >> 2];
102         out[1] = tob64[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
103         out[2] = tob64[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
104         out[3] = tob64[in[2] & 0x3f];
105 }
106
107 static void make_accept_value(const char *key, char result[29])
108 {
109         unsigned char md[SHA_DIGEST_LENGTH+1];
110         size_t len = strlen(key);
111         char *buffer = alloca(len + sizeof websocket_uuid - 1);
112         memcpy(buffer, key, len);
113         memcpy(buffer + len, websocket_uuid, sizeof websocket_uuid - 1);
114         SHA1((const unsigned char *)buffer, (unsigned long)(len + sizeof websocket_uuid - 1), md);
115         assert(SHA_DIGEST_LENGTH == 20);
116         md[20] = 0;
117         enc64(&md[0], &result[0]);
118         enc64(&md[3], &result[4]);
119         enc64(&md[6], &result[8]);
120         enc64(&md[9], &result[12]);
121         enc64(&md[12], &result[16]);
122         enc64(&md[15], &result[20]);
123         enc64(&md[18], &result[24]);
124         result[27] = '=';
125         result[28] = 0;
126 }
127
128 static int headerhas(const char *header, const char *needle)
129 {
130         static const char sep[] = " \t,";
131         size_t len, n;
132
133         n = strlen(needle);
134         for(;;) {
135                 header += strspn(header, sep);
136                 if (!*header)
137                         return 0;
138                 len = strcspn(header, sep);
139 printf("!!!%.*s!!!\n",len,header);
140                 if (n == len && 0 == strncasecmp(needle, header, n))
141                         return 1;
142                 header += len;
143         }
144 }
145
146 int afb_websock_check(struct afb_hreq *hreq, int *later)
147 {
148         const char *connection, *upgrade, *key, *version, *protocols;
149         char acceptval[29];
150         int vernum;
151         struct MHD_Response *response;
152
153         /* is an upgrade to websocket ? */
154         upgrade = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_UPGRADE);
155 printf("upgrade %s\n", upgrade);
156         if (upgrade == NULL || strcasecmp(upgrade, websocket_s))
157                 return 0;
158
159         /* is a connection for upgrade ? */
160         connection = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONNECTION);
161 printf("connection %s\n", connection);
162         if (connection == NULL || !headerhas (connection, MHD_HTTP_HEADER_UPGRADE))
163                 return 0;
164
165         /* is a get ? */
166         if(hreq->method != afb_method_get || strcasecmp(hreq->version, MHD_HTTP_VERSION_1_1))
167                 return 0;
168
169         /* has a key and a version ? */
170         key = afb_hreq_get_header(hreq, sec_websocket_key_s);
171         version = afb_hreq_get_header(hreq, sec_websocket_version_s);
172 printf("key %s\n", key);
173 printf("version %s\n", connection);
174         if (key == NULL || version == NULL)
175                 return 0;
176
177         /* is a supported version ? */
178         vernum = atoi(version);
179         if (vernum != 13) {
180                 response = MHD_create_response_from_data(0,NULL,0,0);
181                 MHD_add_response_header (response, sec_websocket_version_s, "13");
182                 MHD_queue_response (hreq->connection, MHD_HTTP_BAD_REQUEST, response);
183                 MHD_destroy_response (response);
184                 *later = 1;
185                 return 1;
186         }
187
188         /* is the protocol supported ? */
189         protocols = afb_hreq_get_header(hreq, sec_websocket_protocol_s);
190
191         /* send the accept connection */
192         make_accept_value(key, acceptval);
193         response = MHD_create_response_from_data(0,NULL,0,0);
194         MHD_add_response_header (response, sec_websocket_accept_s, acceptval);
195         MHD_add_response_header (response, MHD_HTTP_HEADER_CONNECTION, MHD_HTTP_HEADER_UPGRADE);
196         MHD_add_response_header (response, MHD_HTTP_HEADER_UPGRADE, websocket_s);
197         MHD_queue_response (hreq->connection, MHD_HTTP_SWITCHING_PROTOCOLS, response);
198         MHD_destroy_response (response);
199
200         *later = 0;
201         return 1;
202 }
203
204 struct afb_websock *afb_websock_create(struct MHD_Connection *connection)
205 {
206         struct afb_websock *result;
207
208         result = malloc(sizeof * result);
209         if (result) {
210                 result->connection = connection;
211                 result->fd = MHD_get_connection_info(connection, MHD_CONNECTION_INFO_CONNECTION_FD)->connect_fd;
212                 result->ws = websock_create(&afb_websock_itf, result);
213                 if (result->ws == NULL) {
214                         free(result);
215                         result = NULL;
216                 }
217         }
218         return result;
219 }
220