Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 3 | * |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com> |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 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 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 24 | /* |
Andy Green | d1b11e3 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 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 |
Andy Green | e252217 | 2011-01-18 17:14:03 +0000 | [diff] [blame] | 37 | * Sec-WebSocket-Version: 4 |
Andy Green | d1b11e3 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 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 | /* |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 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 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 56 | LWS_VISIBLE int |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 57 | libwebsocket_read(struct libwebsocket_context *context, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 58 | struct libwebsocket *wsi, unsigned char *buf, size_t len) |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 59 | { |
| 60 | size_t n; |
John Clark | 9bdcf18 | 2014-02-10 07:24:29 +0800 | [diff] [blame] | 61 | #ifndef LWS_NO_SERVER |
Andy Green | 0caf9c5 | 2013-02-18 09:48:31 +0800 | [diff] [blame] | 62 | struct allocated_headers *ah; |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 63 | char *uri_ptr = NULL; |
| 64 | int uri_len = 0; |
| 65 | char content_length_str[32]; |
John Clark | 9bdcf18 | 2014-02-10 07:24:29 +0800 | [diff] [blame] | 66 | #endif |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 67 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 68 | switch (wsi->state) { |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 69 | |
| 70 | case WSI_STATE_HTTP_BODY: |
John Clark | 9bdcf18 | 2014-02-10 07:24:29 +0800 | [diff] [blame] | 71 | #ifndef LWS_NO_SERVER |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 72 | http_postbody: |
John Clark | 9bdcf18 | 2014-02-10 07:24:29 +0800 | [diff] [blame] | 73 | #endif |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 74 | while (len--) { |
| 75 | |
| 76 | if (wsi->u.http.content_length_seen >= wsi->u.http.content_length) |
| 77 | break; |
| 78 | |
| 79 | wsi->u.http.post_buffer[wsi->u.http.body_index++] = *buf++; |
| 80 | wsi->u.http.content_length_seen++; |
Andy Green | ca15338 | 2013-12-08 21:26:52 +0800 | [diff] [blame] | 81 | n = wsi->protocol->rx_buffer_size; |
| 82 | if (!n) |
| 83 | n = LWS_MAX_SOCKET_IO_BUF; |
| 84 | |
| 85 | if (wsi->u.http.body_index != n && |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 86 | wsi->u.http.content_length_seen != wsi->u.http.content_length) |
| 87 | continue; |
| 88 | |
| 89 | if (wsi->protocol->callback) { |
| 90 | n = wsi->protocol->callback( |
| 91 | wsi->protocol->owning_server, wsi, |
| 92 | LWS_CALLBACK_HTTP_BODY, |
| 93 | wsi->user_space, wsi->u.http.post_buffer, |
| 94 | wsi->u.http.body_index); |
| 95 | wsi->u.http.body_index = 0; |
| 96 | if (n) |
| 97 | goto bail; |
| 98 | } |
| 99 | |
| 100 | if (wsi->u.http.content_length_seen == wsi->u.http.content_length) { |
| 101 | /* he sent the content in time */ |
| 102 | libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
| 103 | n = wsi->protocol->callback( |
| 104 | wsi->protocol->owning_server, wsi, |
| 105 | LWS_CALLBACK_HTTP_BODY_COMPLETION, |
| 106 | wsi->user_space, NULL, 0); |
| 107 | wsi->u.http.body_index = 0; |
| 108 | if (n) |
| 109 | goto bail; |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * we need to spill here so everything is seen in the case |
| 116 | * there is no content-length |
| 117 | */ |
| 118 | if (wsi->u.http.body_index && wsi->protocol->callback) { |
| 119 | n = wsi->protocol->callback( |
| 120 | wsi->protocol->owning_server, wsi, |
| 121 | LWS_CALLBACK_HTTP_BODY, |
| 122 | wsi->user_space, wsi->u.http.post_buffer, |
| 123 | wsi->u.http.body_index); |
| 124 | wsi->u.http.body_index = 0; |
| 125 | if (n) |
| 126 | goto bail; |
| 127 | } |
| 128 | break; |
| 129 | |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 130 | case WSI_STATE_HTTP_ISSUING_FILE: |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 131 | case WSI_STATE_HTTP: |
| 132 | wsi->state = WSI_STATE_HTTP_HEADERS; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 133 | wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART; |
| 134 | wsi->u.hdr.lextable_pos = 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 135 | /* fallthru */ |
| 136 | case WSI_STATE_HTTP_HEADERS: |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 137 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 138 | lwsl_parser("issuing %d bytes to parser\n", (int)len); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 139 | |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 140 | #ifndef LWS_NO_CLIENT |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 141 | switch (wsi->mode) { |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 142 | case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY: |
| 143 | case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE: |
| 144 | case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY: |
| 145 | case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT: |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 146 | case LWS_CONNMODE_WS_CLIENT: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 147 | for (n = 0; n < len; n++) |
Andy Green | 3455e67 | 2013-02-04 08:55:42 +0800 | [diff] [blame] | 148 | if (libwebsocket_client_rx_sm(wsi, *buf++)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 149 | lwsl_info("client_rx_sm failed\n"); |
Andy Green | 3455e67 | 2013-02-04 08:55:42 +0800 | [diff] [blame] | 150 | goto bail; |
| 151 | } |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 152 | return 0; |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 153 | default: |
| 154 | break; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 155 | } |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 156 | #endif |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 157 | #ifndef LWS_NO_SERVER |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 158 | /* LWS_CONNMODE_WS_SERVING */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 159 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 160 | while (len--) { |
Andy Green | 3455e67 | 2013-02-04 08:55:42 +0800 | [diff] [blame] | 161 | if (libwebsocket_parse(wsi, *buf++)) { |
| 162 | lwsl_info("libwebsocket_parse failed\n"); |
Andy Green | 38c570c | 2013-03-09 11:52:18 +0800 | [diff] [blame] | 163 | goto bail_nuke_ah; |
Andy Green | 3455e67 | 2013-02-04 08:55:42 +0800 | [diff] [blame] | 164 | } |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 165 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 166 | if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE) |
| 167 | continue; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 168 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 169 | lwsl_parser("libwebsocket_parse sees parsing complete\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 170 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 171 | wsi->mode = LWS_CONNMODE_PRE_WS_SERVING_ACCEPT; |
Andy Green | 176de27 | 2014-02-15 14:36:02 +0800 | [diff] [blame] | 172 | libwebsocket_set_timeout(wsi, NO_PENDING_TIMEOUT, 0); |
Andy Green | 38c570c | 2013-03-09 11:52:18 +0800 | [diff] [blame] | 173 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 174 | /* is this websocket protocol or normal http 1.0? */ |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 175 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 176 | if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) || |
| 177 | !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) { |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 178 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 179 | /* it's not websocket.... shall we accept it as http? */ |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 180 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 181 | if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) && |
| 182 | !lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) { |
| 183 | lwsl_warn("Missing URI in HTTP request\n"); |
| 184 | goto bail_nuke_ah; |
| 185 | } |
| 186 | |
| 187 | if (lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) && |
| 188 | lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) { |
| 189 | lwsl_warn("GET and POST methods?\n"); |
| 190 | goto bail_nuke_ah; |
| 191 | } |
| 192 | |
| 193 | if (libwebsocket_ensure_user_space(wsi)) |
| 194 | goto bail_nuke_ah; |
| 195 | |
| 196 | if (lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI)) { |
| 197 | uri_ptr = lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI); |
| 198 | uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI); |
| 199 | lwsl_info("HTTP GET request for '%s'\n", |
| 200 | lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI)); |
| 201 | |
| 202 | } |
| 203 | if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) { |
| 204 | lwsl_info("HTTP POST request for '%s'\n", |
| 205 | lws_hdr_simple_ptr(wsi, WSI_TOKEN_POST_URI)); |
| 206 | uri_ptr = lws_hdr_simple_ptr(wsi, WSI_TOKEN_POST_URI); |
| 207 | uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI); |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Hm we still need the headers so the |
| 212 | * callback can look at leaders like the URI, but we |
| 213 | * need to transition to http union state.... hold a |
| 214 | * copy of u.hdr.ah and deallocate afterwards |
| 215 | */ |
| 216 | ah = wsi->u.hdr.ah; |
| 217 | |
| 218 | /* union transition */ |
| 219 | memset(&wsi->u, 0, sizeof(wsi->u)); |
| 220 | wsi->mode = LWS_CONNMODE_HTTP_SERVING_ACCEPTED; |
| 221 | wsi->state = WSI_STATE_HTTP; |
| 222 | wsi->u.http.fd = -1; |
| 223 | |
| 224 | /* expose it at the same offset as u.hdr */ |
| 225 | wsi->u.http.ah = ah; |
| 226 | |
| 227 | /* HTTP header had a content length? */ |
| 228 | |
| 229 | wsi->u.http.content_length = 0; |
| 230 | if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) |
| 231 | wsi->u.http.content_length = 100 * 1024 * 1024; |
| 232 | |
| 233 | if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH)) { |
| 234 | lws_hdr_copy(wsi, content_length_str, |
| 235 | sizeof(content_length_str) - 1, |
| 236 | WSI_TOKEN_HTTP_CONTENT_LENGTH); |
| 237 | wsi->u.http.content_length = atoi(content_length_str); |
| 238 | } |
| 239 | |
| 240 | if (wsi->u.http.content_length > 0) { |
| 241 | wsi->u.http.body_index = 0; |
Andy Green | ca15338 | 2013-12-08 21:26:52 +0800 | [diff] [blame] | 242 | n = wsi->protocol->rx_buffer_size; |
| 243 | if (!n) |
| 244 | n = LWS_MAX_SOCKET_IO_BUF; |
| 245 | wsi->u.http.post_buffer = malloc(n); |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 246 | if (!wsi->u.http.post_buffer) { |
| 247 | lwsl_err("Unable to allocate post buffer\n"); |
| 248 | n = -1; |
| 249 | goto leave; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | n = 0; |
| 254 | if (wsi->protocol->callback) |
| 255 | n = wsi->protocol->callback(context, wsi, |
| 256 | LWS_CALLBACK_FILTER_HTTP_CONNECTION, |
| 257 | wsi->user_space, uri_ptr, uri_len); |
| 258 | |
Craig McQueen | e049a77 | 2014-02-20 10:27:11 +1100 | [diff] [blame] | 259 | if (!n) { |
| 260 | /* |
| 261 | * if there is content supposed to be coming, |
| 262 | * put a timeout on it having arrived |
| 263 | */ |
| 264 | libwebsocket_set_timeout(wsi, |
| 265 | PENDING_TIMEOUT_HTTP_CONTENT, |
| 266 | AWAITING_TIMEOUT); |
| 267 | |
| 268 | if (wsi->protocol->callback) |
| 269 | n = wsi->protocol->callback(context, wsi, |
| 270 | LWS_CALLBACK_HTTP, |
| 271 | wsi->user_space, uri_ptr, uri_len); |
| 272 | } |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 273 | |
| 274 | leave: |
| 275 | /* now drop the header info we kept a pointer to */ |
| 276 | if (ah) |
| 277 | free(ah); |
| 278 | /* not possible to continue to use past here */ |
| 279 | wsi->u.http.ah = NULL; |
| 280 | |
| 281 | if (n) { |
| 282 | lwsl_info("LWS_CALLBACK_HTTP closing\n"); |
| 283 | goto bail; /* struct ah ptr already nuked */ |
| 284 | } |
| 285 | |
| 286 | /* |
Andy Green | aa08492 | 2013-12-09 11:27:07 +0800 | [diff] [blame] | 287 | * (if callback didn't start sending a file) |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 288 | * deal with anything else as body, whether |
| 289 | * there was a content-length or not |
| 290 | */ |
| 291 | |
Andy Green | aa08492 | 2013-12-09 11:27:07 +0800 | [diff] [blame] | 292 | if (wsi->state != WSI_STATE_HTTP_ISSUING_FILE) |
| 293 | wsi->state = WSI_STATE_HTTP_BODY; |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 294 | goto http_postbody; |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 295 | } |
| 296 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 297 | if (!wsi->protocol) |
| 298 | lwsl_err("NULL protocol at libwebsocket_read\n"); |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 299 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 300 | /* |
| 301 | * It's websocket |
| 302 | * |
| 303 | * Make sure user side is happy about protocol |
| 304 | */ |
| 305 | |
| 306 | while (wsi->protocol->callback) { |
| 307 | |
| 308 | if (!lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) { |
| 309 | if (wsi->protocol->name == NULL) |
| 310 | break; |
| 311 | } else |
| 312 | if (wsi->protocol->name && strcmp( |
| 313 | lws_hdr_simple_ptr(wsi, |
| 314 | WSI_TOKEN_PROTOCOL), |
| 315 | wsi->protocol->name) == 0) |
| 316 | break; |
| 317 | |
| 318 | wsi->protocol++; |
| 319 | } |
| 320 | |
| 321 | /* we didn't find a protocol he wanted? */ |
| 322 | |
| 323 | if (wsi->protocol->callback == NULL) { |
| 324 | if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) == |
| 325 | NULL) { |
| 326 | lwsl_info("no protocol -> prot 0 handler\n"); |
| 327 | wsi->protocol = &context->protocols[0]; |
| 328 | } else { |
| 329 | lwsl_err("Req protocol %s not supported\n", |
| 330 | lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL)); |
| 331 | goto bail_nuke_ah; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /* allocate wsi->user storage */ |
Andy Green | 38c570c | 2013-03-09 11:52:18 +0800 | [diff] [blame] | 336 | if (libwebsocket_ensure_user_space(wsi)) |
| 337 | goto bail_nuke_ah; |
Andy Green | e803c82 | 2013-02-14 23:18:10 +0800 | [diff] [blame] | 338 | |
Andy Green | 0caf9c5 | 2013-02-18 09:48:31 +0800 | [diff] [blame] | 339 | /* |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 340 | * Give the user code a chance to study the request and |
| 341 | * have the opportunity to deny it |
Andy Green | 0caf9c5 | 2013-02-18 09:48:31 +0800 | [diff] [blame] | 342 | */ |
| 343 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 344 | if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi, |
| 345 | LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION, |
| 346 | wsi->user_space, |
| 347 | lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) { |
| 348 | lwsl_warn("User code denied connection\n"); |
| 349 | goto bail_nuke_ah; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /* |
| 354 | * Perform the handshake according to the protocol version the |
| 355 | * client announced |
| 356 | */ |
| 357 | |
| 358 | switch (wsi->ietf_spec_revision) { |
| 359 | case 13: |
| 360 | lwsl_parser("lws_parse calling handshake_04\n"); |
| 361 | if (handshake_0405(context, wsi)) { |
| 362 | lwsl_info("hs0405 has failed the connection\n"); |
| 363 | goto bail_nuke_ah; |
| 364 | } |
| 365 | break; |
| 366 | |
| 367 | default: |
| 368 | lwsl_warn("Unknown client spec version %d\n", |
| 369 | wsi->ietf_spec_revision); |
| 370 | goto bail_nuke_ah; |
| 371 | } |
| 372 | |
| 373 | /* drop the header info -- no bail_nuke_ah after this */ |
| 374 | |
| 375 | if (wsi->u.hdr.ah) |
| 376 | free(wsi->u.hdr.ah); |
| 377 | |
| 378 | wsi->mode = LWS_CONNMODE_WS_SERVING; |
Andy Green | 0caf9c5 | 2013-02-18 09:48:31 +0800 | [diff] [blame] | 379 | |
| 380 | /* union transition */ |
| 381 | memset(&wsi->u, 0, sizeof(wsi->u)); |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 382 | wsi->u.ws.rxflow_change_to = LWS_RXFLOW_ALLOW; |
Andy Green | 84fd949 | 2013-11-09 11:40:32 +0800 | [diff] [blame] | 383 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 384 | /* |
| 385 | * create the frame buffer for this connection according to the |
| 386 | * size mentioned in the protocol definition. If 0 there, use |
| 387 | * a big default for compatibility |
| 388 | */ |
Andy Green | 84fd949 | 2013-11-09 11:40:32 +0800 | [diff] [blame] | 389 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 390 | n = wsi->protocol->rx_buffer_size; |
| 391 | if (!n) |
| 392 | n = LWS_MAX_SOCKET_IO_BUF; |
| 393 | n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING; |
| 394 | wsi->u.ws.rx_user_buffer = malloc(n); |
| 395 | if (!wsi->u.ws.rx_user_buffer) { |
| 396 | lwsl_err("Out of Mem allocating rx buffer %d\n", n); |
| 397 | goto bail; |
| 398 | } |
| 399 | lwsl_info("Allocating RX buffer %d\n", n); |
Andy Green | 19895bc | 2013-11-09 11:59:56 +0800 | [diff] [blame] | 400 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 401 | if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n)) { |
| 402 | lwsl_warn("Failed to set SNDBUF to %d", n); |
| 403 | goto bail; |
Andy Green | 4708a02 | 2013-02-11 11:27:44 +0800 | [diff] [blame] | 404 | } |
| 405 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 406 | lwsl_parser("accepted v%02d connection\n", |
| 407 | wsi->ietf_spec_revision); |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 408 | } /* while all chars are handled */ |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 409 | break; |
John Clark | 9bdcf18 | 2014-02-10 07:24:29 +0800 | [diff] [blame] | 410 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 411 | |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 412 | case WSI_STATE_AWAITING_CLOSE_ACK: |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 413 | case WSI_STATE_ESTABLISHED: |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 414 | #ifndef LWS_NO_CLIENT |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 415 | switch (wsi->mode) { |
| 416 | case LWS_CONNMODE_WS_CLIENT: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 417 | for (n = 0; n < len; n++) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 418 | if (libwebsocket_client_rx_sm( |
| 419 | wsi, *buf++) < 0) { |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 420 | lwsl_info("client rx has bailed\n"); |
Andy Green | 66a16f3 | 2011-05-24 22:07:45 +0100 | [diff] [blame] | 421 | goto bail; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 422 | } |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 423 | |
| 424 | return 0; |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 425 | default: |
| 426 | break; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 427 | } |
Andy Green | 03674a6 | 2013-01-16 11:47:40 +0800 | [diff] [blame] | 428 | #endif |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 429 | #ifndef LWS_NO_SERVER |
Andy Green | f3d3b40 | 2011-02-09 07:16:34 +0000 | [diff] [blame] | 430 | /* LWS_CONNMODE_WS_SERVING */ |
| 431 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 432 | if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) { |
| 433 | lwsl_info("interpret_incoming_packet has bailed\n"); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 434 | goto bail; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 435 | } |
Andy Green | a1ce6be | 2013-01-18 11:43:21 +0800 | [diff] [blame] | 436 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 437 | break; |
| 438 | default: |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 439 | lwsl_err("libwebsocket_read: Unhandled state\n"); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 440 | break; |
| 441 | } |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 442 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 443 | return 0; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 444 | |
John Clark | 9bdcf18 | 2014-02-10 07:24:29 +0800 | [diff] [blame] | 445 | #ifndef LWS_NO_SERVER |
Andy Green | 38c570c | 2013-03-09 11:52:18 +0800 | [diff] [blame] | 446 | bail_nuke_ah: |
| 447 | /* drop the header info */ |
| 448 | if (wsi->u.hdr.ah) |
| 449 | free(wsi->u.hdr.ah); |
John Clark | 9bdcf18 | 2014-02-10 07:24:29 +0800 | [diff] [blame] | 450 | #endif |
Andy Green | 38c570c | 2013-03-09 11:52:18 +0800 | [diff] [blame] | 451 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 452 | bail: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 453 | lwsl_info("closing connection at libwebsocket_read bail:\n"); |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 454 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 455 | libwebsocket_close_and_free_session(context, wsi, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 456 | LWS_CLOSE_STATUS_NOSTATUS); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 457 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 458 | return -1; |
| 459 | } |