libuv: handle signals only if requested
[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 #ifndef LWS_NO_EXTENSIONS
26 LWS_VISIBLE int
27 lws_extension_server_handshake(struct lws *wsi, char **p)
28 {
29         struct lws_context *context = wsi->context;
30         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
31         const struct lws_extension *ext;
32         char ext_name[128];
33         int ext_count = 0;
34         int more = 1;
35         char ignore;
36         int n, m;
37         char *c;
38
39         /*
40          * Figure out which extensions the client has that we want to
41          * enable on this connection, and give him back the list
42          */
43         if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS))
44                 return 0;
45
46         /*
47          * break down the list of client extensions
48          * and go through them
49          */
50
51         if (lws_hdr_copy(wsi, (char *)pt->serv_buf, LWS_MAX_SOCKET_IO_BUF,
52                          WSI_TOKEN_EXTENSIONS) < 0)
53                 return 1;
54
55         c = (char *)pt->serv_buf;
56         lwsl_parser("WSI_TOKEN_EXTENSIONS = '%s'\n", c);
57         wsi->count_act_ext = 0;
58         n = 0;
59         ignore = 0;
60         while (more) {
61
62                 if (*c && (*c != ',' && *c != '\t')) {
63                         if (*c == ';')
64                                 ignore = 1;
65                         if (ignore || *c == ' ') {
66                                 c++;
67                                 continue;
68                         }
69                         ext_name[n] = *c++;
70                         if (n < sizeof(ext_name) - 1)
71                                 n++;
72                         continue;
73                 }
74                 ext_name[n] = '\0';
75
76                 ignore = 0;
77                 if (!*c)
78                         more = 0;
79                 else {
80                         c++;
81                         if (!n)
82                                 continue;
83                 }
84
85                 /* check a client's extension against our support */
86
87                 ext = lws_get_context(wsi)->extensions;
88
89                 while (ext && ext->callback) {
90
91                         if (strcmp(ext_name, ext->name)) {
92                                 ext++;
93                                 continue;
94                         }
95 #if 0
96                         m = ext->callback(lws_get_context(wsi), ext, wsi,
97                                           LWS_EXT_CB_ARGS_VALIDATE,
98                                           NULL, start + n, 0);
99                         if (m) {
100                                 ext++;
101                                 continue;
102                         }
103 #endif
104                         /*
105                          * oh, we do support this one he asked for... but let's
106                          * ask user code if it's OK to apply it on this
107                          * particular connection + protocol
108                          */
109                         m = lws_get_context(wsi)->protocols[0].callback(wsi,
110                                 LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
111                                 wsi->user_space, ext_name, 0);
112
113                         /*
114                          * zero return from callback means go ahead and allow
115                          * the extension, it's what we get if the callback is
116                          * unhandled
117                          */
118
119                         if (m) {
120                                 ext++;
121                                 continue;
122                         }
123
124                         /* apply it */
125
126                         if (ext_count)
127                                 *(*p)++ = ',';
128                         else
129                                 LWS_CPYAPP(*p, "\x0d\x0aSec-WebSocket-Extensions: ");
130                         *p += sprintf(*p, "%s", ext_name);
131                         ext_count++;
132
133                         /* instantiate the extension on this conn */
134
135                         wsi->active_extensions[wsi->count_act_ext] = ext;
136
137                         /* allow him to construct his context */
138
139                         ext->callback(lws_get_context(wsi), ext, wsi,
140                                       LWS_EXT_CB_CONSTRUCT,
141                                       (void *)&wsi->act_ext_user[wsi->count_act_ext],
142                                       NULL, 0);
143
144                         wsi->count_act_ext++;
145                         lwsl_parser("count_act_ext <- %d\n", wsi->count_act_ext);
146
147                         ext++;
148                 }
149
150                 n = 0;
151         }
152
153         return 0;
154 }
155 #endif
156 int
157 handshake_0405(struct lws_context *context, struct lws *wsi)
158 {
159         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
160         unsigned char hash[20];
161         int n, accept_len;
162         char *response;
163         char *p;
164
165         if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
166             !lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
167                 lwsl_parser("handshake_04 missing pieces\n");
168                 /* completed header processing, but missing some bits */
169                 goto bail;
170         }
171
172         if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >= MAX_WEBSOCKET_04_KEY_LEN) {
173                 lwsl_warn("Client key too long %d\n", MAX_WEBSOCKET_04_KEY_LEN);
174                 goto bail;
175         }
176
177         /*
178          * since key length is restricted above (currently 128), cannot
179          * overflow
180          */
181         n = sprintf((char *)pt->serv_buf,
182                     "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
183                     lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
184
185         lws_SHA1(pt->serv_buf, n, hash);
186
187         accept_len = lws_b64_encode_string((char *)hash, 20, (char *)pt->serv_buf,
188                                            LWS_MAX_SOCKET_IO_BUF);
189         if (accept_len < 0) {
190                 lwsl_warn("Base64 encoded hash too long\n");
191                 goto bail;
192         }
193
194         /* allocate the per-connection user memory (if any) */
195         if (lws_ensure_user_space(wsi))
196                 goto bail;
197
198         /* create the response packet */
199
200         /* make a buffer big enough for everything */
201
202         response = (char *)pt->serv_buf + MAX_WEBSOCKET_04_KEY_LEN + LWS_PRE;
203         p = response;
204         LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
205                       "Upgrade: WebSocket\x0d\x0a"
206                       "Connection: Upgrade\x0d\x0a"
207                       "Sec-WebSocket-Accept: ");
208         strcpy(p, (char *)pt->serv_buf);
209         p += accept_len;
210
211         if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
212                 LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
213                 n = lws_hdr_copy(wsi, p, 128, WSI_TOKEN_PROTOCOL);
214                 if (n < 0)
215                         goto bail;
216                 p += n;
217         }
218
219 #ifndef LWS_NO_EXTENSIONS
220         /*
221          * Figure out which extensions the client has that we want to
222          * enable on this connection, and give him back the list
223          */
224         if (lws_extension_server_handshake(wsi, &p))
225                 goto bail;
226 #endif
227
228         //LWS_CPYAPP(p, "\x0d\x0a""An-unknown-header: blah");
229
230         /* end of response packet */
231
232         LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
233
234         if (!lws_any_extension_handled(wsi, LWS_EXT_CB_HANDSHAKE_REPLY_TX,
235                                        response, p - response)) {
236
237                 /* okay send the handshake response accepting the connection */
238
239                 lwsl_parser("issuing resp pkt %d len\n", (int)(p - response));
240 #ifdef DEBUG
241                 fwrite(response, 1,  p - response, stderr);
242 #endif
243                 n = lws_write(wsi, (unsigned char *)response,
244                               p - response, LWS_WRITE_HTTP_HEADERS);
245                 if (n != (p - response)) {
246                         lwsl_debug("handshake_0405: ERROR writing to socket\n");
247                         goto bail;
248                 }
249
250         }
251
252         /* alright clean up and set ourselves into established state */
253
254         wsi->state = LWSS_ESTABLISHED;
255         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
256
257         /* notify user code that we're ready to roll */
258
259         if (wsi->protocol->callback)
260                 if (wsi->protocol->callback(wsi, LWS_CALLBACK_ESTABLISHED,
261                                             wsi->user_space,
262 #ifdef LWS_OPENSSL_SUPPORT
263                                             wsi->ssl,
264 #else
265                                             NULL,
266 #endif
267                                             0))
268                         goto bail;
269
270         return 0;
271
272
273 bail:
274         /* caller will free up his parsing allocations */
275         return -1;
276 }
277