blob: 3007426b923663647f66c05d67f4ad1c3f63fa4d [file] [log] [blame]
Andy Green7c212cc2010-11-08 20:20:42 +00001/*
2 * libwebsockets - small server side websockets and web server implementation
Andy Greene77ddd82010-11-13 10:03:47 +00003 *
Andy Greena1ce6be2013-01-18 11:43:21 +08004 * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
Andy Green7c212cc2010-11-08 20:20:42 +00005 *
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
Andy Green7c212cc2010-11-08 20:20:42 +000024/*
Andy Greend1b11e32011-01-18 15:39:02 +000025 * -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
Andy Greene2522172011-01-18 17:14:03 +000037 * Sec-WebSocket-Version: 4
Andy Greend1b11e32011-01-18 15:39:02 +000038 *
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/*
Andy Green7c212cc2010-11-08 20:20:42 +000050 * 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
Peter Pentchev9a4fef72013-03-30 09:52:21 +080056LWS_VISIBLE int
Andy Green6ee372f2012-04-09 15:09:01 +080057libwebsocket_read(struct libwebsocket_context *context,
Andy Greenb5b23192013-02-11 17:13:32 +080058 struct libwebsocket *wsi, unsigned char *buf, size_t len)
Andy Green7c212cc2010-11-08 20:20:42 +000059{
60 size_t n;
Andy Green0caf9c52013-02-18 09:48:31 +080061 struct allocated_headers *ah;
62 char *uri_ptr;
63 int uri_len;
Andy Greene77ddd82010-11-13 10:03:47 +000064
Andy Green7c212cc2010-11-08 20:20:42 +000065 switch (wsi->state) {
Andy Greend280b6e2013-01-15 13:40:23 +080066 case WSI_STATE_HTTP_ISSUING_FILE:
Andy Green7c212cc2010-11-08 20:20:42 +000067 case WSI_STATE_HTTP:
68 wsi->state = WSI_STATE_HTTP_HEADERS;
Andy Green623a98d2013-01-21 11:04:23 +080069 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
70 wsi->u.hdr.lextable_pos = 0;
Andy Green7c212cc2010-11-08 20:20:42 +000071 /* fallthru */
72 case WSI_STATE_HTTP_HEADERS:
Andy Greene77ddd82010-11-13 10:03:47 +000073
Andy Green43db0452013-01-10 19:50:35 +080074 lwsl_parser("issuing %d bytes to parser\n", (int)len);
Andy Green4739e5c2011-01-22 12:51:57 +000075
Andy Green03674a62013-01-16 11:47:40 +080076#ifndef LWS_NO_CLIENT
Andy Greenf3d3b402011-02-09 07:16:34 +000077 switch (wsi->mode) {
Andy Greena41314f2011-05-23 10:00:03 +010078 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:
Andy Greenf3d3b402011-02-09 07:16:34 +000082 case LWS_CONNMODE_WS_CLIENT:
Andy Green4739e5c2011-01-22 12:51:57 +000083 for (n = 0; n < len; n++)
Andy Green3455e672013-02-04 08:55:42 +080084 if (libwebsocket_client_rx_sm(wsi, *buf++)) {
Andy Greenb5b23192013-02-11 17:13:32 +080085 lwsl_info("client_rx_sm failed\n");
Andy Green3455e672013-02-04 08:55:42 +080086 goto bail;
87 }
Andy Green4739e5c2011-01-22 12:51:57 +000088 return 0;
Andy Greenf3d3b402011-02-09 07:16:34 +000089 default:
90 break;
Andy Green4739e5c2011-01-22 12:51:57 +000091 }
Andy Green03674a62013-01-16 11:47:40 +080092#endif
Andy Greena1ce6be2013-01-18 11:43:21 +080093#ifndef LWS_NO_SERVER
Andy Greenf3d3b402011-02-09 07:16:34 +000094 /* LWS_CONNMODE_WS_SERVING */
Andy Green4739e5c2011-01-22 12:51:57 +000095
Andy Greene77ddd82010-11-13 10:03:47 +000096 for (n = 0; n < len; n++)
Andy Green3455e672013-02-04 08:55:42 +080097 if (libwebsocket_parse(wsi, *buf++)) {
98 lwsl_info("libwebsocket_parse failed\n");
Andy Green38c570c2013-03-09 11:52:18 +080099 goto bail_nuke_ah;
Andy Green3455e672013-02-04 08:55:42 +0800100 }
Andy Greene77ddd82010-11-13 10:03:47 +0000101
Andy Green623a98d2013-01-21 11:04:23 +0800102 if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
Andy Green7c212cc2010-11-08 20:20:42 +0000103 break;
104
Andy Green43db0452013-01-10 19:50:35 +0800105 lwsl_parser("libwebsocket_parse sees parsing complete\n");
Andy Green4739e5c2011-01-22 12:51:57 +0000106
Andy Green38c570c2013-03-09 11:52:18 +0800107 wsi->mode = LWS_CONNMODE_PRE_WS_SERVING_ACCEPT;
108
Andy Green7c212cc2010-11-08 20:20:42 +0000109 /* is this websocket protocol or normal http 1.0? */
Andy Greene77ddd82010-11-13 10:03:47 +0000110
Andy Green16ab3182013-02-10 18:02:31 +0800111 if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) ||
112 !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) {
Andy Green3ee9b312013-02-12 12:52:39 +0800113
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");
Andy Green38c570c2013-03-09 11:52:18 +0800118 goto bail_nuke_ah;
Andy Green3ee9b312013-02-12 12:52:39 +0800119 }
120
Andy Green0caf9c52013-02-18 09:48:31 +0800121 lwsl_info("HTTP request for '%s'\n",
122 lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI));
Andy Green3ee9b312013-02-12 12:52:39 +0800123
Andy Green38c570c2013-03-09 11:52:18 +0800124 if (libwebsocket_ensure_user_space(wsi))
125 goto bail_nuke_ah;
Andy Greene803c822013-02-14 23:18:10 +0800126
Andy Green0caf9c52013-02-18 09:48:31 +0800127 /*
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
Andy Green7cf6cb02013-05-19 14:04:10 +0800141 wsi->mode = LWS_CONNMODE_HTTP_SERVING_ACCEPTED;
Nick Dowellf803c0d2012-04-05 10:29:29 +0800142 wsi->state = WSI_STATE_HTTP;
Andy Green4708a022013-02-11 11:27:44 +0800143 n = 0;
Andy Green4f3943a2010-11-12 10:44:16 +0000144 if (wsi->protocol->callback)
Andy Green4708a022013-02-11 11:27:44 +0800145 n = wsi->protocol->callback(context, wsi,
Andy Greenb5b23192013-02-11 17:13:32 +0800146 LWS_CALLBACK_HTTP,
Andy Green0caf9c52013-02-18 09:48:31 +0800147 wsi->user_space, uri_ptr, uri_len);
Andy Green4708a022013-02-11 11:27:44 +0800148
Andy Green0caf9c52013-02-18 09:48:31 +0800149 /* now drop the header info we kept a pointer to */
150 if (ah)
151 free(ah);
Andy Green4708a022013-02-11 11:27:44 +0800152
153 if (n) {
Andy Greenb5b23192013-02-11 17:13:32 +0800154 lwsl_info("LWS_CALLBACK_HTTP closing\n");
Andy Green96d48fd2013-09-18 08:32:55 +0800155 goto bail; /* struct ah ptr already nuked */
Andy Green4708a022013-02-11 11:27:44 +0800156 }
157
Andy Green7c212cc2010-11-08 20:20:42 +0000158 return 0;
159 }
160
Andy Greencc012472011-11-07 19:53:23 +0800161 if (!wsi->protocol)
Andy Green43db0452013-01-10 19:50:35 +0800162 lwsl_err("NULL protocol at libwebsocket_read\n");
Andy Greena41314f2011-05-23 10:00:03 +0100163
Andy Greene2522172011-01-18 17:14:03 +0000164 /*
165 * It's websocket
166 *
167 * Make sure user side is happy about protocol
168 */
Andy Greena2b0ab02010-11-11 12:28:29 +0000169
Andy Green4f3943a2010-11-12 10:44:16 +0000170 while (wsi->protocol->callback) {
171
Andy Green16ab3182013-02-10 18:02:31 +0800172 if (!lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) {
Andy Green4f3943a2010-11-12 10:44:16 +0000173 if (wsi->protocol->name == NULL)
174 break;
175 } else
Nick Dowell5519d9f2012-04-03 17:22:19 +0100176 if (wsi->protocol->name && strcmp(
Andy Greenb5b23192013-02-11 17:13:32 +0800177 lws_hdr_simple_ptr(wsi,
178 WSI_TOKEN_PROTOCOL),
Andy Green4f3943a2010-11-12 10:44:16 +0000179 wsi->protocol->name) == 0)
180 break;
Andy Greene77ddd82010-11-13 10:03:47 +0000181
Andy Green4f3943a2010-11-12 10:44:16 +0000182 wsi->protocol++;
183 }
Andy Green47943ae2010-11-12 11:15:49 +0000184
185 /* we didn't find a protocol he wanted? */
Andy Greene77ddd82010-11-13 10:03:47 +0000186
Andy Green4f3943a2010-11-12 10:44:16 +0000187 if (wsi->protocol->callback == NULL) {
Andy Greenb5b23192013-02-11 17:13:32 +0800188 if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) ==
189 NULL) {
190 lwsl_info("no protocol -> prot 0 handler\n");
Andy Green5fc75a92013-01-30 12:26:14 +0800191 wsi->protocol = &context->protocols[0];
192 } else {
Andy Greenb5b23192013-02-11 17:13:32 +0800193 lwsl_err("Req protocol %s not supported\n",
194 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL));
Andy Green38c570c2013-03-09 11:52:18 +0800195 goto bail_nuke_ah;
Andy Green5fc75a92013-01-30 12:26:14 +0800196 }
Andy Green4f3943a2010-11-12 10:44:16 +0000197 }
198
Andy Green96d48fd2013-09-18 08:32:55 +0800199 /* allocate wsi->user storage */
200 if (libwebsocket_ensure_user_space(wsi))
201 goto bail_nuke_ah;
202
Andy Greene77ddd82010-11-13 10:03:47 +0000203 /*
Andy Greenc85619d2011-02-13 08:25:26 +0000204 * Give the user code a chance to study the request and
205 * have the opportunity to deny it
206 */
207
Andy Green62c54d22011-02-14 09:14:25 +0000208 if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
Andy Greenc85619d2011-02-13 08:25:26 +0000209 LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
Andy Green96d48fd2013-09-18 08:32:55 +0800210 wsi->user_space,
211 lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) {
Andy Green43db0452013-01-10 19:50:35 +0800212 lwsl_warn("User code denied connection\n");
Andy Green96d48fd2013-09-18 08:32:55 +0800213 goto bail_nuke_ah;
Andy Greenc85619d2011-02-13 08:25:26 +0000214 }
215
216
217 /*
Andy Green38e57bb2011-01-19 12:20:27 +0000218 * Perform the handshake according to the protocol version the
219 * client announced
Andy Greene2522172011-01-18 17:14:03 +0000220 */
Andy Greene77ddd82010-11-13 10:03:47 +0000221
Andy Greene2522172011-01-18 17:14:03 +0000222 switch (wsi->ietf_spec_revision) {
Andy Greend85cb202011-09-25 09:32:54 +0100223 case 13:
Andy Greenb5b23192013-02-11 17:13:32 +0800224 lwsl_parser("lws_parse calling handshake_04\n");
Andy Green3182ece2013-01-20 17:08:31 +0800225 if (handshake_0405(context, wsi)) {
Andy Greenb5b23192013-02-11 17:13:32 +0800226 lwsl_info("hs0405 has failed the connection\n");
Andy Green96d48fd2013-09-18 08:32:55 +0800227 goto bail_nuke_ah;
Andy Green3182ece2013-01-20 17:08:31 +0800228 }
Andy Greenbfb051f2011-02-09 08:49:14 +0000229 break;
230
Andy Greene2522172011-01-18 17:14:03 +0000231 default:
Andy Green43db0452013-01-10 19:50:35 +0800232 lwsl_warn("Unknown client spec version %d\n",
Andy Greene2522172011-01-18 17:14:03 +0000233 wsi->ietf_spec_revision);
Andy Green96d48fd2013-09-18 08:32:55 +0800234 goto bail_nuke_ah;
Andy Green7c212cc2010-11-08 20:20:42 +0000235 }
Andy Greene77ddd82010-11-13 10:03:47 +0000236
Andy Green96d48fd2013-09-18 08:32:55 +0800237 /* drop the header info -- no bail_nuke_ah after this */
Andy Green2b57a342013-02-06 15:15:25 +0900238
Andy Green16ab3182013-02-10 18:02:31 +0800239 if (wsi->u.hdr.ah)
240 free(wsi->u.hdr.ah);
Andy Green2b57a342013-02-06 15:15:25 +0900241
Andy Greend280b6e2013-01-15 13:40:23 +0800242 wsi->mode = LWS_CONNMODE_WS_SERVING;
243
Andy Green623a98d2013-01-21 11:04:23 +0800244 /* union transition */
Andy Greenb5b23192013-02-11 17:13:32 +0800245 memset(&wsi->u, 0, sizeof(wsi->u));
Andy Greenca0a1292013-03-16 11:24:23 +0800246 wsi->u.ws.rxflow_change_to = LWS_RXFLOW_ALLOW;
Andy Green623a98d2013-01-21 11:04:23 +0800247
Andy Green54495112013-02-06 21:10:16 +0900248 /*
249 * create the frame buffer for this connection according to the
250 * size mentioned in the protocol definition. If 0 there, use
251 * a big default for compatibility
252 */
253
254 n = wsi->protocol->rx_buffer_size;
255 if (!n)
256 n = LWS_MAX_SOCKET_IO_BUF;
257 n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING;
258 wsi->u.ws.rx_user_buffer = malloc(n);
259 if (!wsi->u.ws.rx_user_buffer) {
260 lwsl_err("Out of Mem allocating rx buffer %d\n", n);
Andy Green0480f642013-02-08 20:10:03 +0800261 goto bail;
Andy Green54495112013-02-06 21:10:16 +0900262 }
Andy Green0480f642013-02-08 20:10:03 +0800263 lwsl_info("Allocating RX buffer %d\n", n);
Andy Green54495112013-02-06 21:10:16 +0900264
Andy Green01b206e2013-03-23 09:53:17 +0800265 if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n)) {
266 lwsl_warn("Failed to set SNDBUF to %d", n);
267 goto bail;
268 }
269
Andy Green43db0452013-01-10 19:50:35 +0800270 lwsl_parser("accepted v%02d connection\n",
Andy Greenbfb051f2011-02-09 08:49:14 +0000271 wsi->ietf_spec_revision);
Andy Greena1ce6be2013-01-18 11:43:21 +0800272#endif
Andy Green7c212cc2010-11-08 20:20:42 +0000273 break;
274
Andy Greenda527df2011-03-07 07:08:12 +0000275 case WSI_STATE_AWAITING_CLOSE_ACK:
Andy Green7c212cc2010-11-08 20:20:42 +0000276 case WSI_STATE_ESTABLISHED:
Andy Green03674a62013-01-16 11:47:40 +0800277#ifndef LWS_NO_CLIENT
Andy Greenf3d3b402011-02-09 07:16:34 +0000278 switch (wsi->mode) {
279 case LWS_CONNMODE_WS_CLIENT:
Andy Green4739e5c2011-01-22 12:51:57 +0000280 for (n = 0; n < len; n++)
Andy Greenb5b23192013-02-11 17:13:32 +0800281 if (libwebsocket_client_rx_sm(
282 wsi, *buf++) < 0) {
Andy Green3182ece2013-01-20 17:08:31 +0800283 lwsl_info("client rx has bailed\n");
Andy Green66a16f32011-05-24 22:07:45 +0100284 goto bail;
Andy Green3182ece2013-01-20 17:08:31 +0800285 }
Andy Green4739e5c2011-01-22 12:51:57 +0000286
287 return 0;
Andy Greenf3d3b402011-02-09 07:16:34 +0000288 default:
289 break;
Andy Green4739e5c2011-01-22 12:51:57 +0000290 }
Andy Green03674a62013-01-16 11:47:40 +0800291#endif
Andy Greena1ce6be2013-01-18 11:43:21 +0800292#ifndef LWS_NO_SERVER
Andy Greenf3d3b402011-02-09 07:16:34 +0000293 /* LWS_CONNMODE_WS_SERVING */
294
Andy Green3182ece2013-01-20 17:08:31 +0800295 if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
296 lwsl_info("interpret_incoming_packet has bailed\n");
Andy Green7c212cc2010-11-08 20:20:42 +0000297 goto bail;
Andy Green3182ece2013-01-20 17:08:31 +0800298 }
Andy Greena1ce6be2013-01-18 11:43:21 +0800299#endif
Andy Green7c212cc2010-11-08 20:20:42 +0000300 break;
301 default:
Andy Greend280b6e2013-01-15 13:40:23 +0800302 lwsl_err("libwebsocket_read: Unhandled state\n");
Andy Green7c212cc2010-11-08 20:20:42 +0000303 break;
304 }
Andy Greene77ddd82010-11-13 10:03:47 +0000305
Andy Green7c212cc2010-11-08 20:20:42 +0000306 return 0;
Andy Greene77ddd82010-11-13 10:03:47 +0000307
Andy Green38c570c2013-03-09 11:52:18 +0800308bail_nuke_ah:
309 /* drop the header info */
310 if (wsi->u.hdr.ah)
311 free(wsi->u.hdr.ah);
312
Andy Green7c212cc2010-11-08 20:20:42 +0000313bail:
Andy Green3182ece2013-01-20 17:08:31 +0800314 lwsl_info("closing connection at libwebsocket_read bail:\n");
Andy Green2b57a342013-02-06 15:15:25 +0900315
Peter Hinz56885f32011-03-02 22:03:47 +0000316 libwebsocket_close_and_free_session(context, wsi,
Andy Green687b0182011-02-26 11:04:01 +0000317 LWS_CLOSE_STATUS_NOSTATUS);
Andy Green6964bb52011-01-23 16:50:33 +0000318
Andy Green7c212cc2010-11-08 20:20:42 +0000319 return -1;
320}