introduce LWS_CALLBACK_FILTER_HTTP_CONNECTION
[platform/upstream/libwebsockets.git] / lib / 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 /*
25  * -04 of the protocol (actually the 80th version) has a radically different
26  * handshake.  The 04 spec gives the following idea
27  *
28  *    The handshake from the client looks as follows:
29  *
30  *      GET /chat HTTP/1.1
31  *      Host: server.example.com
32  *      Upgrade: websocket
33  *      Connection: Upgrade
34  *      Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
35  *      Sec-WebSocket-Origin: http://example.com
36  *      Sec-WebSocket-Protocol: chat, superchat
37  *      Sec-WebSocket-Version: 4
38  *
39  *  The handshake from the server looks as follows:
40  *
41  *       HTTP/1.1 101 Switching Protocols
42  *       Upgrade: websocket
43  *       Connection: Upgrade
44  *       Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
45  *       Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
46  *       Sec-WebSocket-Protocol: chat
47  */
48
49 /*
50  * We have to take care about parsing because the headers may be split
51  * into multiple fragments.  They may contain unknown headers with arbitrary
52  * argument lengths.  So, we parse using a single-character at a time state
53  * machine that is completely independent of packet size.
54  */
55
56 LWS_VISIBLE int
57 libwebsocket_read(struct libwebsocket_context *context,
58                      struct libwebsocket *wsi, unsigned char *buf, size_t len)
59 {
60         size_t n;
61         struct allocated_headers *ah;
62         char *uri_ptr;
63         int uri_len;
64
65         switch (wsi->state) {
66         case WSI_STATE_HTTP_ISSUING_FILE:
67         case WSI_STATE_HTTP:
68                 wsi->state = WSI_STATE_HTTP_HEADERS;
69                 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
70                 wsi->u.hdr.lextable_pos = 0;
71                 /* fallthru */
72         case WSI_STATE_HTTP_HEADERS:
73
74                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
75
76 #ifndef LWS_NO_CLIENT
77                 switch (wsi->mode) {
78                 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
79                 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
80                 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
81                 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
82                 case LWS_CONNMODE_WS_CLIENT:
83                         for (n = 0; n < len; n++)
84                                 if (libwebsocket_client_rx_sm(wsi, *buf++)) {
85                                         lwsl_info("client_rx_sm failed\n");
86                                         goto bail;
87                                 }
88                         return 0;
89                 default:
90                         break;
91                 }
92 #endif
93 #ifndef LWS_NO_SERVER
94                 /* LWS_CONNMODE_WS_SERVING */
95
96                 for (n = 0; n < len; n++)
97                         if (libwebsocket_parse(wsi, *buf++)) {
98                                 lwsl_info("libwebsocket_parse failed\n");
99                                 goto bail_nuke_ah;
100                         }
101
102                 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
103                         break;
104
105                 lwsl_parser("libwebsocket_parse sees parsing complete\n");
106
107                 wsi->mode = LWS_CONNMODE_PRE_WS_SERVING_ACCEPT;
108
109                 /* is this websocket protocol or normal http 1.0? */
110
111                 if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) ||
112                              !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
113
114                         /* it's not websocket.... shall we accept it as http? */
115
116                         if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI)) {
117                                 lwsl_warn("Missing URI in HTTP request\n");
118                                 goto bail_nuke_ah;
119                         }
120
121                         lwsl_info("HTTP request for '%s'\n",
122                                 lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI));
123
124                         if (libwebsocket_ensure_user_space(wsi))
125                                 goto bail_nuke_ah;
126
127                         /*
128                          * Hm we still need the headers so the
129                          * callback can look at leaders like the URI, but we
130                          * need to transition to http union state.... hold a
131                          * copy of u.hdr.ah and deallocate afterwards
132                          */
133
134                         ah = wsi->u.hdr.ah;
135                         uri_ptr = lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI);
136                         uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI);
137
138                         /* union transition */
139                         memset(&wsi->u, 0, sizeof(wsi->u));
140                         wsi->mode = LWS_CONNMODE_HTTP_SERVING_ACCEPTED;
141                         wsi->state = WSI_STATE_HTTP;
142
143                         /* expose it at the same offset as u.hdr */
144                         wsi->u.http.ah = ah;
145
146                         n = 0;
147                         if (wsi->protocol->callback)
148                                 n = wsi->protocol->callback(context, wsi,
149                                     LWS_CALLBACK_FILTER_HTTP_CONNECTION,
150                                     wsi->user_space, uri_ptr, uri_len);
151
152                         if (!n && wsi->protocol->callback)
153                                 n = wsi->protocol->callback(context, wsi,
154                                     LWS_CALLBACK_HTTP,
155                                     wsi->user_space, uri_ptr, uri_len);
156
157                         /* now drop the header info we kept a pointer to */
158                         if (ah)
159                                 free(ah);
160                         /* not possible to continue to use past here */
161                         wsi->u.http.ah = NULL;
162
163                         if (n) {
164                                 lwsl_info("LWS_CALLBACK_HTTP closing\n");
165                                 goto bail; /* struct ah ptr already nuked */
166                         }
167
168                         return 0;
169                 }
170
171                 if (!wsi->protocol)
172                         lwsl_err("NULL protocol at libwebsocket_read\n");
173
174                 /*
175                  * It's websocket
176                  *
177                  * Make sure user side is happy about protocol
178                  */
179
180                 while (wsi->protocol->callback) {
181
182                         if (!lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
183                                 if (wsi->protocol->name == NULL)
184                                         break;
185                         } else
186                                 if (wsi->protocol->name && strcmp(
187                                         lws_hdr_simple_ptr(wsi,
188                                                 WSI_TOKEN_PROTOCOL),
189                                                       wsi->protocol->name) == 0)
190                                         break;
191
192                         wsi->protocol++;
193                 }
194
195                 /* we didn't find a protocol he wanted? */
196
197                 if (wsi->protocol->callback == NULL) {
198                         if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) ==
199                                                                          NULL) {
200                                 lwsl_info("no protocol -> prot 0 handler\n");
201                                 wsi->protocol = &context->protocols[0];
202                         } else {
203                                 lwsl_err("Req protocol %s not supported\n",
204                                    lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL));
205                                 goto bail_nuke_ah;
206                         }
207                 }
208
209                 /* allocate wsi->user storage */
210                 if (libwebsocket_ensure_user_space(wsi))
211                                 goto bail_nuke_ah;
212
213                 /*
214                  * Give the user code a chance to study the request and
215                  * have the opportunity to deny it
216                  */
217
218                 if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
219                                 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
220                                 wsi->user_space,
221                               lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) {
222                         lwsl_warn("User code denied connection\n");
223                         goto bail_nuke_ah;
224                 }
225
226
227                 /*
228                  * Perform the handshake according to the protocol version the
229                  * client announced
230                  */
231
232                 switch (wsi->ietf_spec_revision) {
233                 case 13:
234                         lwsl_parser("lws_parse calling handshake_04\n");
235                         if (handshake_0405(context, wsi)) {
236                                 lwsl_info("hs0405 has failed the connection\n");
237                                 goto bail_nuke_ah;
238                         }
239                         break;
240
241                 default:
242                         lwsl_warn("Unknown client spec version %d\n",
243                                                        wsi->ietf_spec_revision);
244                         goto bail_nuke_ah;
245                 }
246
247                 /* drop the header info -- no bail_nuke_ah after this */
248
249                 if (wsi->u.hdr.ah)
250                         free(wsi->u.hdr.ah);
251
252                 wsi->mode = LWS_CONNMODE_WS_SERVING;
253
254                 /* union transition */
255                 memset(&wsi->u, 0, sizeof(wsi->u));
256                 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_ALLOW;
257
258                 /*
259                  * create the frame buffer for this connection according to the
260                  * size mentioned in the protocol definition.  If 0 there, use
261                  * a big default for compatibility
262                  */
263
264                 n = wsi->protocol->rx_buffer_size;
265                 if (!n)
266                         n = LWS_MAX_SOCKET_IO_BUF;
267                 n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
268                 wsi->u.ws.rx_user_buffer = malloc(n);
269                 if (!wsi->u.ws.rx_user_buffer) {
270                         lwsl_err("Out of Mem allocating rx buffer %d\n", n);
271                         goto bail;
272                 }
273                 lwsl_info("Allocating RX buffer %d\n", n);
274
275                 if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF,  &n, sizeof n)) {
276                         lwsl_warn("Failed to set SNDBUF to %d", n);
277                         goto bail;
278                 }
279
280                 lwsl_parser("accepted v%02d connection\n",
281                                                        wsi->ietf_spec_revision);
282 #endif
283                 break;
284
285         case WSI_STATE_AWAITING_CLOSE_ACK:
286         case WSI_STATE_ESTABLISHED:
287 #ifndef LWS_NO_CLIENT
288                 switch (wsi->mode) {
289                 case LWS_CONNMODE_WS_CLIENT:
290                         for (n = 0; n < len; n++)
291                                 if (libwebsocket_client_rx_sm(
292                                                              wsi, *buf++) < 0) {
293                                         lwsl_info("client rx has bailed\n");
294                                         goto bail;
295                                 }
296
297                         return 0;
298                 default:
299                         break;
300                 }
301 #endif
302 #ifndef LWS_NO_SERVER
303                 /* LWS_CONNMODE_WS_SERVING */
304
305                 if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
306                         lwsl_info("interpret_incoming_packet has bailed\n");
307                         goto bail;
308                 }
309 #endif
310                 break;
311         default:
312                 lwsl_err("libwebsocket_read: Unhandled state\n");
313                 break;
314         }
315
316         return 0;
317
318 bail_nuke_ah:
319         /* drop the header info */
320         if (wsi->u.hdr.ah)
321                 free(wsi->u.hdr.ah);
322
323 bail:
324         lwsl_info("closing connection at libwebsocket_read bail:\n");
325
326         libwebsocket_close_and_free_session(context, wsi,
327                                                      LWS_CLOSE_STATUS_NOSTATUS);
328
329         return -1;
330 }