valgrind drop header allocation down http path
[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 #ifndef LWS_NO_SERVER
57 extern int handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi);
58 #endif
59
60 int
61 libwebsocket_read(struct libwebsocket_context *context,
62                      struct libwebsocket *wsi, unsigned char * buf, size_t len)
63 {
64         size_t n;
65
66         switch (wsi->state) {
67         case WSI_STATE_HTTP_ISSUING_FILE:
68         case WSI_STATE_HTTP:
69                 wsi->state = WSI_STATE_HTTP_HEADERS;
70                 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
71                 wsi->u.hdr.lextable_pos = 0;
72                 /* fallthru */
73         case WSI_STATE_HTTP_HEADERS:
74
75                 lwsl_parser("issuing %d bytes to parser\n", (int)len);
76 #ifdef _DEBUG
77                 //fwrite(buf, 1, len, stderr);
78 #endif
79
80 #ifndef LWS_NO_CLIENT
81
82 //              lwsl_info("mode=%d\n", wsi->mode);
83
84                 switch (wsi->mode) {
85                 case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY:
86                 case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE:
87                 case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY:
88                 case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT:
89                 case LWS_CONNMODE_WS_CLIENT:
90                         for (n = 0; n < len; n++)
91                                 if (libwebsocket_client_rx_sm(wsi, *buf++)) {
92                                         lwsl_info("libwebsocket_client_rx_sm failed\n");
93                                         goto bail;
94                                 }
95                         return 0;
96                 default:
97                         break;
98                 }
99 #endif
100 #ifndef LWS_NO_SERVER
101                 /* LWS_CONNMODE_WS_SERVING */
102
103                 for (n = 0; n < len; n++)
104                         if (libwebsocket_parse(wsi, *buf++)) {
105                                 lwsl_info("libwebsocket_parse failed\n");
106                                 goto bail;
107                         }
108
109                 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
110                         break;
111
112                 lwsl_parser("libwebsocket_parse sees parsing complete\n");
113
114                 /* is this websocket protocol or normal http 1.0? */
115
116                 if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) ||
117                              !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
118                         wsi->state = WSI_STATE_HTTP;
119                         n = 0;
120                         if (wsi->protocol->callback)
121                                 n = wsi->protocol->callback(context, wsi,
122                                                 LWS_CALLBACK_HTTP,
123                                                 wsi->user_space,
124                                                 lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI),
125                                                 lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI));
126
127                         /* drop the header info */
128                         if (wsi->u.hdr.ah)
129                                 free(wsi->u.hdr.ah);
130
131                         if (n) {
132                                 lwsl_info("LWS_CALLBACK_HTTP wanted to close\n");
133                                 goto bail;
134                         }
135
136                         return 0;
137                 }
138
139                 if (!wsi->protocol)
140                         lwsl_err("NULL protocol at libwebsocket_read\n");
141
142                 /*
143                  * It's websocket
144                  *
145                  * Make sure user side is happy about protocol
146                  */
147
148                 while (wsi->protocol->callback) {
149
150                         if (!lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
151                                 if (wsi->protocol->name == NULL)
152                                         break;
153                         } else
154                                 if (wsi->protocol->name && strcmp(
155                                         lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL),
156                                                       wsi->protocol->name) == 0)
157                                         break;
158
159                         wsi->protocol++;
160                 }
161
162                 /* we didn't find a protocol he wanted? */
163
164                 if (wsi->protocol->callback == NULL) {
165                         if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) == NULL) {
166                                 lwsl_info("[no protocol] "
167                                         "mapped to protocol 0 handler\n");
168                                 wsi->protocol = &context->protocols[0];
169                         } else {
170                                 lwsl_err("Requested protocol %s "
171                                                 "not supported\n",
172                                                 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL));
173                                 goto bail;
174                         }
175                 }
176
177                 /*
178                  * Give the user code a chance to study the request and
179                  * have the opportunity to deny it
180                  */
181
182                 if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
183                                 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
184                                 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL),
185                                                                      NULL, 0)) {
186                         lwsl_warn("User code denied connection\n");
187                         goto bail;
188                 }
189
190
191                 /*
192                  * Perform the handshake according to the protocol version the
193                  * client announced
194                  */
195
196                 switch (wsi->ietf_spec_revision) {
197                 case 13:
198                         lwsl_parser("libwebsocket_parse calling handshake_04\n");
199                         if (handshake_0405(context, wsi)) {
200                                 lwsl_info("handshake_0405 xor 05 has failed the connection\n");
201                                 goto bail;
202                         }
203                         break;
204
205                 default:
206                         lwsl_warn("Unknown client spec version %d\n",
207                                                        wsi->ietf_spec_revision);
208                         goto bail;
209                 }
210
211                 /* drop the header info */
212
213                 if (wsi->u.hdr.ah)
214                         free(wsi->u.hdr.ah);
215
216                 wsi->mode = LWS_CONNMODE_WS_SERVING;
217
218                 /* union transition */
219                 memset(&wsi->u, 0, sizeof wsi->u);
220
221                 /*
222                  * create the frame buffer for this connection according to the
223                  * size mentioned in the protocol definition.  If 0 there, use
224                  * a big default for compatibility
225                  */
226
227                 n = wsi->protocol->rx_buffer_size;
228                 if (!n)
229                         n = LWS_MAX_SOCKET_IO_BUF;
230                 n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
231                 wsi->u.ws.rx_user_buffer = malloc(n);
232                 if (!wsi->u.ws.rx_user_buffer) {
233                         lwsl_err("Out of Mem allocating rx buffer %d\n", n);
234                         goto bail;
235                 }
236                 lwsl_info("Allocating RX buffer %d\n", n);
237
238                 lwsl_parser("accepted v%02d connection\n",
239                                                        wsi->ietf_spec_revision);
240 #endif
241                 break;
242
243         case WSI_STATE_AWAITING_CLOSE_ACK:
244         case WSI_STATE_ESTABLISHED:
245 #ifndef LWS_NO_CLIENT
246                 switch (wsi->mode) {
247                 case LWS_CONNMODE_WS_CLIENT:
248                         for (n = 0; n < len; n++)
249                                 if (libwebsocket_client_rx_sm(wsi, *buf++) < 0) {
250                                         lwsl_info("client rx has bailed\n");
251                                         goto bail;
252                                 }
253
254                         return 0;
255                 default:
256                         break;
257                 }
258 #endif
259 #ifndef LWS_NO_SERVER
260                 /* LWS_CONNMODE_WS_SERVING */
261
262                 if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
263                         lwsl_info("interpret_incoming_packet has bailed\n");
264                         goto bail;
265                 }
266 #endif
267                 break;
268         default:
269                 lwsl_err("libwebsocket_read: Unhandled state\n");
270                 break;
271         }
272
273         return 0;
274
275 bail:
276         lwsl_info("closing connection at libwebsocket_read bail:\n");
277
278         libwebsocket_close_and_free_session(context, wsi,
279                                                      LWS_CLOSE_STATUS_NOSTATUS);
280
281         return -1;
282 }