clean: fixes for appveyor warnings
[platform/upstream/libwebsockets.git] / lib / server-handshake.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 #define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
25
26 #ifndef LWS_NO_EXTENSIONS
27 static int
28 lws_extension_server_handshake(struct lws *wsi, char **p, int budget)
29 {
30         struct lws_context *context = wsi->context;
31         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
32         char ext_name[64], *args, *end = (*p) + budget - 1;
33         const struct lws_ext_options *opts, *po;
34         const struct lws_extension *ext;
35         struct lws_ext_option_arg oa;
36         int n, m, more = 1;
37         int ext_count = 0;
38         char ignore;
39         char *c;
40
41         /*
42          * Figure out which extensions the client has that we want to
43          * enable on this connection, and give him back the list
44          */
45         if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS))
46                 return 0;
47
48         /*
49          * break down the list of client extensions
50          * and go through them
51          */
52
53         if (lws_hdr_copy(wsi, (char *)pt->serv_buf, context->pt_serv_buf_size,
54                          WSI_TOKEN_EXTENSIONS) < 0)
55                 return 1;
56
57         c = (char *)pt->serv_buf;
58         lwsl_parser("WSI_TOKEN_EXTENSIONS = '%s'\n", c);
59         wsi->count_act_ext = 0;
60         ignore = 0;
61         n = 0;
62         args = NULL;
63
64         /*
65          * We may get a simple request
66          *
67          * Sec-WebSocket-Extensions: permessage-deflate
68          *
69          * or an elaborated one with requested options
70          *
71          * Sec-WebSocket-Extensions: permessage-deflate; \
72          *                           server_no_context_takeover; \
73          *                           client_no_context_takeover
74          */
75
76         while (more) {
77
78                 if (*c && (*c != ',' && *c != '\t')) {
79                         if (*c == ';') {
80                                 ignore = 1;
81                                 args = c + 1;
82                         }
83                         if (ignore || *c == ' ') {
84                                 c++;
85                                 continue;
86                         }
87                         ext_name[n] = *c++;
88                         if (n < sizeof(ext_name) - 1)
89                                 n++;
90                         continue;
91                 }
92                 ext_name[n] = '\0';
93
94                 ignore = 0;
95                 if (!*c)
96                         more = 0;
97                 else {
98                         c++;
99                         if (!n)
100                                 continue;
101                 }
102
103                 while (args && *args && *args == ' ')
104                         args++;
105
106                 /* check a client's extension against our support */
107
108                 ext = wsi->vhost->extensions;
109
110                 while (ext && ext->callback) {
111
112                         if (strcmp(ext_name, ext->name)) {
113                                 ext++;
114                                 continue;
115                         }
116
117                         /*
118                          * oh, we do support this one he asked for... but let's
119                          * confirm he only gave it once
120                          */
121                         for (m = 0; m < wsi->count_act_ext; m++)
122                                 if (wsi->active_extensions[m] == ext) {
123                                         lwsl_info("extension mentioned twice\n");
124                                         return 1; /* shenanigans */
125                                 }
126
127                         /*
128                          * ask user code if it's OK to apply it on this
129                          * particular connection + protocol
130                          */
131                         m = (wsi->protocol->callback)(wsi,
132                                 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
133                                 wsi->user_space, ext_name, 0);
134
135                         /*
136                          * zero return from callback means go ahead and allow
137                          * the extension, it's what we get if the callback is
138                          * unhandled
139                          */
140                         if (m) {
141                                 ext++;
142                                 continue;
143                         }
144
145                         /* apply it */
146
147                         ext_count++;
148
149                         /* instantiate the extension on this conn */
150
151                         wsi->active_extensions[wsi->count_act_ext] = ext;
152
153                         /* allow him to construct his context */
154
155                         if (ext->callback(lws_get_context(wsi), ext, wsi,
156                                           LWS_EXT_CB_CONSTRUCT,
157                                           (void *)&wsi->act_ext_user[
158                                                             wsi->count_act_ext],
159                                           (void *)&opts, 0)) {
160                                 lwsl_notice("ext %s failed construction\n",
161                                             ext_name);
162                                 ext_count--;
163                                 ext++;
164
165                                 continue;
166                         }
167
168                         if (ext_count > 1)
169                                 *(*p)++ = ',';
170                         else
171                                 LWS_CPYAPP(*p,
172                                           "\x0d\x0aSec-WebSocket-Extensions: ");
173                         *p += lws_snprintf(*p, (end - *p), "%s", ext_name);
174
175                         /*
176                          *  go through the options trying to apply the
177                          * recognized ones
178                          */
179
180                         lwsl_debug("ext args %s", args);
181
182                         while (args && *args && *args != ',') {
183                                 while (*args == ' ')
184                                         args++;
185                                 po = opts;
186                                 while (po->name) {
187                                         lwsl_debug("'%s' '%s'\n", po->name, args);
188                                         /* only support arg-less options... */
189                                         if (po->type == EXTARG_NONE &&
190                                             !strncmp(args, po->name,
191                                                             strlen(po->name))) {
192                                                 oa.option_name = NULL;
193                                                 oa.option_index = po - opts;
194                                                 oa.start = NULL;
195                                                 lwsl_debug("setting %s\n", po->name);
196                                                 if (!ext->callback(
197                                                                 lws_get_context(wsi), ext, wsi,
198                                                                   LWS_EXT_CB_OPTION_SET,
199                                                                   wsi->act_ext_user[
200                                                                          wsi->count_act_ext],
201                                                                   &oa, (end - *p))) {
202
203                                                         *p += lws_snprintf(*p, (end - *p), "; %s", po->name);
204                                                         lwsl_debug("adding option %s\n", po->name);
205                                                 }
206                                         }
207                                         po++;
208                                 }
209                                 while (*args && *args != ',' && *args != ';')
210                                         args++;
211                         }
212
213                         wsi->count_act_ext++;
214                         lwsl_parser("count_act_ext <- %d\n",
215                                     wsi->count_act_ext);
216
217                         ext++;
218                 }
219
220                 n = 0;
221                 args = NULL;
222         }
223
224         return 0;
225 }
226 #endif
227 int
228 handshake_0405(struct lws_context *context, struct lws *wsi)
229 {
230         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
231         unsigned char hash[20];
232         int n, accept_len;
233         char *response;
234         char *p;
235
236         if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
237             !lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
238                 lwsl_parser("handshake_04 missing pieces\n");
239                 /* completed header processing, but missing some bits */
240                 goto bail;
241         }
242
243         if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >= MAX_WEBSOCKET_04_KEY_LEN) {
244                 lwsl_warn("Client key too long %d\n", MAX_WEBSOCKET_04_KEY_LEN);
245                 goto bail;
246         }
247
248         /*
249          * since key length is restricted above (currently 128), cannot
250          * overflow
251          */
252         n = sprintf((char *)pt->serv_buf,
253                     "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
254                     lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
255
256         lws_SHA1(pt->serv_buf, n, hash);
257
258         accept_len = lws_b64_encode_string((char *)hash, 20,
259                         (char *)pt->serv_buf, context->pt_serv_buf_size);
260         if (accept_len < 0) {
261                 lwsl_warn("Base64 encoded hash too long\n");
262                 goto bail;
263         }
264
265         /* allocate the per-connection user memory (if any) */
266         if (lws_ensure_user_space(wsi))
267                 goto bail;
268
269         /* create the response packet */
270
271         /* make a buffer big enough for everything */
272
273         response = (char *)pt->serv_buf + MAX_WEBSOCKET_04_KEY_LEN + LWS_PRE;
274         p = response;
275         LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
276                       "Upgrade: WebSocket\x0d\x0a"
277                       "Connection: Upgrade\x0d\x0a"
278                       "Sec-WebSocket-Accept: ");
279         strcpy(p, (char *)pt->serv_buf);
280         p += accept_len;
281
282         /* we can only return the protocol header if:
283          *  - one came in, and ... */
284         if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL) &&
285             /*  - it is not an empty string */
286             wsi->protocol->name &&
287             wsi->protocol->name[0]) {
288                 LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
289                 p += lws_snprintf(p, 128, "%s", wsi->protocol->name);
290         }
291
292 #ifndef LWS_NO_EXTENSIONS
293         /*
294          * Figure out which extensions the client has that we want to
295          * enable on this connection, and give him back the list.
296          *
297          * Give him a limited write bugdet
298          */
299         if (lws_extension_server_handshake(wsi, &p, 192))
300                 goto bail;
301 #endif
302
303         //LWS_CPYAPP(p, "\x0d\x0a""An-unknown-header: blah");
304
305         /* end of response packet */
306
307         LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
308
309         if (!lws_any_extension_handled(wsi, LWS_EXT_CB_HANDSHAKE_REPLY_TX,
310                                        response, p - response)) {
311
312                 /* okay send the handshake response accepting the connection */
313
314                 lwsl_parser("issuing resp pkt %d len\n", (int)(p - response));
315 #if defined(DEBUG) && ! defined(LWS_WITH_ESP8266)
316                 fwrite(response, 1,  p - response, stderr);
317 #endif
318                 n = lws_write(wsi, (unsigned char *)response,
319                               p - response, LWS_WRITE_HTTP_HEADERS);
320                 if (n != (p - response)) {
321                         lwsl_debug("handshake_0405: ERROR writing to socket\n");
322                         goto bail;
323                 }
324
325         }
326
327         /* alright clean up and set ourselves into established state */
328
329         wsi->state = LWSS_ESTABLISHED;
330         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
331
332         {
333                 const char * uri_ptr =
334                         lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI);
335                 int uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI);
336                 const struct lws_http_mount *hit =
337                         lws_find_mount(wsi, uri_ptr, uri_len);
338                 if (hit && hit->cgienv &&
339                     wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_PMO,
340                         wsi->user_space, (void *)hit->cgienv, 0))
341                         return 1;
342         }
343
344         return 0;
345
346
347 bail:
348         /* caller will free up his parsing allocations */
349         return -1;
350 }
351