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 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 24 | #if defined(WIN32) || defined(_WIN32) |
David Galeano | aeb5748 | 2013-01-09 15:25:05 +0800 | [diff] [blame] | 25 | #include <io.h> |
| 26 | #endif |
| 27 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 28 | |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 29 | unsigned char lextable[] = { |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 30 | #include "lextable.h" |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | int lextable_decode(int pos, char c) |
| 34 | { |
| 35 | while (pos >= 0) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 36 | if (lextable[pos + 1] == 0) /* terminal marker */ |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 37 | return pos; |
| 38 | |
Andy Green | 909a372 | 2013-11-13 08:03:05 +0800 | [diff] [blame] | 39 | /* case insensitive - RFC2616 */ |
| 40 | if ((lextable[pos] & 0x7f) == tolower(c)) |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 41 | return pos + (lextable[pos + 1] << 1); |
| 42 | |
| 43 | if (lextable[pos] & 0x80) |
| 44 | return -1; |
| 45 | |
| 46 | pos += 2; |
| 47 | } |
| 48 | return pos; |
| 49 | } |
| 50 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 51 | int lws_allocate_header_table(struct libwebsocket *wsi) |
| 52 | { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 53 | wsi->u.hdr.ah = malloc(sizeof(*wsi->u.hdr.ah)); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 54 | if (wsi->u.hdr.ah == NULL) { |
| 55 | lwsl_err("Out of memory\n"); |
| 56 | return -1; |
| 57 | } |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 58 | memset(wsi->u.hdr.ah->frag_index, 0, sizeof(wsi->u.hdr.ah->frag_index)); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 59 | wsi->u.hdr.ah->next_frag_index = 0; |
| 60 | wsi->u.hdr.ah->pos = 0; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 61 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 65 | LWS_VISIBLE int lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 66 | { |
| 67 | int n; |
| 68 | int len = 0; |
| 69 | |
| 70 | n = wsi->u.hdr.ah->frag_index[h]; |
| 71 | if (n == 0) |
| 72 | return 0; |
| 73 | |
| 74 | do { |
| 75 | len += wsi->u.hdr.ah->frags[n].len; |
| 76 | n = wsi->u.hdr.ah->frags[n].next_frag_index; |
| 77 | } while (n); |
| 78 | |
| 79 | return len; |
| 80 | } |
| 81 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 82 | LWS_VISIBLE int lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 83 | enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 84 | { |
| 85 | int toklen = lws_hdr_total_length(wsi, h); |
| 86 | int n; |
| 87 | |
| 88 | if (toklen >= len) |
| 89 | return -1; |
| 90 | |
| 91 | n = wsi->u.hdr.ah->frag_index[h]; |
| 92 | if (n == 0) |
| 93 | return 0; |
| 94 | |
| 95 | do { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 96 | strcpy(dest, |
| 97 | &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 98 | dest += wsi->u.hdr.ah->frags[n].len; |
| 99 | n = wsi->u.hdr.ah->frags[n].next_frag_index; |
| 100 | } while (n); |
| 101 | |
| 102 | return toklen; |
| 103 | } |
| 104 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 105 | char *lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 106 | { |
| 107 | int n; |
| 108 | |
| 109 | n = wsi->u.hdr.ah->frag_index[h]; |
| 110 | if (!n) |
| 111 | return NULL; |
| 112 | |
| 113 | return &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]; |
| 114 | } |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 115 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 116 | int lws_hdr_simple_create(struct libwebsocket *wsi, |
| 117 | enum lws_token_indexes h, const char *s) |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 118 | { |
| 119 | wsi->u.hdr.ah->next_frag_index++; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 120 | if (wsi->u.hdr.ah->next_frag_index == |
| 121 | sizeof(wsi->u.hdr.ah->frags) / sizeof(wsi->u.hdr.ah->frags[0])) { |
| 122 | lwsl_warn("More hdr frags than we can deal with, dropping\n"); |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->next_frag_index; |
| 127 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 128 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset = |
| 129 | wsi->u.hdr.ah->pos; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 130 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 131 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].next_frag_index = |
| 132 | 0; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 133 | |
| 134 | do { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 135 | if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) { |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 136 | lwsl_err("Ran out of header data space\n"); |
| 137 | return -1; |
| 138 | } |
| 139 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s; |
| 140 | if (*s) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 141 | wsi->u.hdr.ah->frags[ |
| 142 | wsi->u.hdr.ah->next_frag_index].len++; |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 143 | } while (*s++); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 148 | static char char_to_hex(const char c) |
| 149 | { |
| 150 | if (c >= '0' && c <= '9') |
| 151 | return c - '0'; |
| 152 | |
| 153 | if (c >= 'a' && c <= 'f') |
| 154 | return c - 'a' + 10; |
| 155 | |
| 156 | if (c >= 'A' && c <= 'F') |
| 157 | return c - 'A' + 10; |
| 158 | |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | static int issue_char(struct libwebsocket *wsi, unsigned char c) |
| 163 | { |
| 164 | if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) { |
| 165 | lwsl_warn("excessive header content\n"); |
| 166 | return -1; |
| 167 | } |
| 168 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c; |
| 169 | if (c) |
| 170 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 175 | int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c) |
| 176 | { |
| 177 | int n; |
| 178 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 179 | switch (wsi->u.hdr.parser_state) { |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 180 | case WSI_TOKEN_GET_URI: |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 181 | case WSI_TOKEN_POST_URI: |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 182 | case WSI_TOKEN_HOST: |
| 183 | case WSI_TOKEN_CONNECTION: |
| 184 | case WSI_TOKEN_KEY1: |
| 185 | case WSI_TOKEN_KEY2: |
| 186 | case WSI_TOKEN_PROTOCOL: |
| 187 | case WSI_TOKEN_UPGRADE: |
| 188 | case WSI_TOKEN_ORIGIN: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 189 | case WSI_TOKEN_SWORIGIN: |
Andy Green | ce510c6 | 2010-11-11 12:48:13 +0000 | [diff] [blame] | 190 | case WSI_TOKEN_DRAFT: |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 191 | case WSI_TOKEN_CHALLENGE: |
Andy Green | d1b11e3 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 192 | case WSI_TOKEN_KEY: |
| 193 | case WSI_TOKEN_VERSION: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 194 | case WSI_TOKEN_ACCEPT: |
| 195 | case WSI_TOKEN_NONCE: |
Andy Green | 18910c5 | 2011-02-09 08:58:42 +0000 | [diff] [blame] | 196 | case WSI_TOKEN_EXTENSIONS: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 197 | case WSI_TOKEN_HTTP: |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 198 | case WSI_TOKEN_HTTP_ACCEPT: |
| 199 | case WSI_TOKEN_HTTP_IF_MODIFIED_SINCE: |
| 200 | case WSI_TOKEN_HTTP_ACCEPT_ENCODING: |
| 201 | case WSI_TOKEN_HTTP_ACCEPT_LANGUAGE: |
| 202 | case WSI_TOKEN_HTTP_PRAGMA: |
| 203 | case WSI_TOKEN_HTTP_CACHE_CONTROL: |
| 204 | case WSI_TOKEN_HTTP_AUTHORIZATION: |
| 205 | case WSI_TOKEN_HTTP_COOKIE: |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 206 | case WSI_TOKEN_HTTP_CONTENT_LENGTH: |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 207 | case WSI_TOKEN_HTTP_CONTENT_TYPE: |
| 208 | case WSI_TOKEN_HTTP_DATE: |
| 209 | case WSI_TOKEN_HTTP_RANGE: |
| 210 | case WSI_TOKEN_HTTP_REFERER: |
| 211 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 212 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 213 | lwsl_parser("WSI_TOK_(%d) '%c'\n", wsi->u.hdr.parser_state, c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 214 | |
| 215 | /* collect into malloc'd buffers */ |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 216 | /* optional initial space swallow */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 217 | if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->frag_index[ |
| 218 | wsi->u.hdr.parser_state]].len && c == ' ') |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 219 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 220 | |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 221 | if ((wsi->u.hdr.parser_state != WSI_TOKEN_GET_URI) && (wsi->u.hdr.parser_state != WSI_TOKEN_POST_URI)) |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 222 | goto check_eol; |
| 223 | |
| 224 | /* special URI processing... end at space */ |
| 225 | |
| 226 | if (c == ' ') { |
| 227 | /* enforce starting with / */ |
| 228 | if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len) |
| 229 | if (issue_char(wsi, '/') < 0) |
| 230 | return -1; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 231 | c = '\0'; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 232 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 233 | goto spill; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 236 | /* special URI processing... convert %xx */ |
| 237 | |
| 238 | switch (wsi->u.hdr.ues) { |
| 239 | case URIES_IDLE: |
| 240 | if (c == '%') { |
| 241 | wsi->u.hdr.ues = URIES_SEEN_PERCENT; |
| 242 | goto swallow; |
| 243 | } |
| 244 | break; |
| 245 | case URIES_SEEN_PERCENT: |
| 246 | if (char_to_hex(c) < 0) { |
| 247 | /* regurgitate */ |
| 248 | if (issue_char(wsi, '%') < 0) |
| 249 | return -1; |
| 250 | wsi->u.hdr.ues = URIES_IDLE; |
| 251 | /* continue on to assess c */ |
| 252 | break; |
| 253 | } |
| 254 | wsi->u.hdr.esc_stash = c; |
| 255 | wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1; |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 256 | goto swallow; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 257 | |
| 258 | case URIES_SEEN_PERCENT_H1: |
| 259 | if (char_to_hex(c) < 0) { |
| 260 | /* regurgitate */ |
| 261 | issue_char(wsi, '%'); |
| 262 | wsi->u.hdr.ues = URIES_IDLE; |
| 263 | /* regurgitate + assess */ |
| 264 | if (libwebsocket_parse(wsi, wsi->u.hdr.esc_stash) < 0) |
| 265 | return -1; |
| 266 | /* continue on to assess c */ |
| 267 | break; |
| 268 | } |
| 269 | c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) | |
| 270 | char_to_hex(c); |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 271 | wsi->u.hdr.ues = URIES_IDLE; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * special URI processing... |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 277 | * convert /.. or /... or /../ etc to / |
| 278 | * convert /./ to / |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 279 | * convert // or /// etc to / |
| 280 | * leave /.dir or whatever alone |
| 281 | */ |
| 282 | |
| 283 | switch (wsi->u.hdr.ups) { |
| 284 | case URIPS_IDLE: |
| 285 | /* issue the first / always */ |
| 286 | if (c == '/') |
| 287 | wsi->u.hdr.ups = URIPS_SEEN_SLASH; |
| 288 | break; |
| 289 | case URIPS_SEEN_SLASH: |
| 290 | /* swallow subsequent slashes */ |
| 291 | if (c == '/') |
| 292 | goto swallow; |
| 293 | /* track and swallow the first . after / */ |
| 294 | if (c == '.') { |
| 295 | wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT; |
| 296 | goto swallow; |
| 297 | } else |
| 298 | wsi->u.hdr.ups = URIPS_IDLE; |
| 299 | break; |
| 300 | case URIPS_SEEN_SLASH_DOT: |
| 301 | /* swallow second . */ |
| 302 | if (c == '.') { |
Andy Green | d3f6873 | 2013-11-13 06:53:21 +0800 | [diff] [blame] | 303 | /* |
| 304 | * back up one dir level if possible |
| 305 | * safe against header fragmentation because |
| 306 | * the method URI can only be in 1 fragment |
| 307 | */ |
| 308 | if (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 2) { |
| 309 | wsi->u.hdr.ah->pos--; |
| 310 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--; |
| 311 | do { |
| 312 | wsi->u.hdr.ah->pos--; |
| 313 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--; |
| 314 | } while (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 1 && |
| 315 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos] != '/'); |
| 316 | } |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 317 | wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT; |
| 318 | goto swallow; |
| 319 | } |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 320 | /* change /./ to / */ |
| 321 | if (c == '/') { |
| 322 | wsi->u.hdr.ups = URIPS_SEEN_SLASH; |
| 323 | goto swallow; |
| 324 | } |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 325 | /* it was like /.dir ... regurgitate the . */ |
| 326 | wsi->u.hdr.ups = URIPS_IDLE; |
| 327 | issue_char(wsi, '.'); |
| 328 | break; |
| 329 | |
| 330 | case URIPS_SEEN_SLASH_DOT_DOT: |
| 331 | /* swallow prior .. chars and any subsequent . */ |
| 332 | if (c == '.') |
| 333 | goto swallow; |
Andy Green | 6f42910 | 2013-11-11 06:14:52 +0800 | [diff] [blame] | 334 | /* last issued was /, so another / == // */ |
| 335 | if (c == '/') |
| 336 | goto swallow; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 337 | else /* last we issued was / so SEEN_SLASH */ |
| 338 | wsi->u.hdr.ups = URIPS_SEEN_SLASH; |
| 339 | break; |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 340 | case URIPS_ARGUMENTS: |
| 341 | /* leave them alone */ |
| 342 | break; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | check_eol: |
| 346 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 347 | /* bail at EOL */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 348 | if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE && |
| 349 | c == '\x0d') { |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 350 | c = '\0'; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 351 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 352 | lwsl_parser("*\n"); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 355 | if (c == '?') { /* start of URI arguments */ |
| 356 | /* seal off uri header */ |
| 357 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0'; |
| 358 | |
| 359 | /* move to using WSI_TOKEN_HTTP_URI_ARGS */ |
| 360 | wsi->u.hdr.ah->next_frag_index++; |
| 361 | wsi->u.hdr.ah->frags[ |
| 362 | wsi->u.hdr.ah->next_frag_index].offset = |
| 363 | wsi->u.hdr.ah->pos; |
| 364 | wsi->u.hdr.ah->frags[ |
| 365 | wsi->u.hdr.ah->next_frag_index].len = 0; |
| 366 | wsi->u.hdr.ah->frags[ |
| 367 | wsi->u.hdr.ah->next_frag_index].next_frag_index = 0; |
| 368 | |
| 369 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = |
| 370 | wsi->u.hdr.ah->next_frag_index; |
| 371 | |
| 372 | /* defeat normal uri path processing */ |
| 373 | wsi->u.hdr.ups = URIPS_ARGUMENTS; |
| 374 | goto swallow; |
| 375 | } |
| 376 | |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 377 | spill: |
| 378 | if (issue_char(wsi, c) < 0) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 379 | return -1; |
Andy Green | b1a9e50 | 2013-11-10 15:15:21 +0800 | [diff] [blame] | 380 | swallow: |
Andy Green | d1b11e3 | 2011-01-18 15:39:02 +0000 | [diff] [blame] | 381 | /* per-protocol end of headers management */ |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 382 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 383 | if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE) |
| 384 | goto set_parsing_complete; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 385 | break; |
| 386 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 387 | /* collecting and checking a name part */ |
| 388 | case WSI_TOKEN_NAME_PART: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 389 | lwsl_parser("WSI_TOKEN_NAME_PART '%c'\n", c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 390 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 391 | wsi->u.hdr.lextable_pos = |
| 392 | lextable_decode(wsi->u.hdr.lextable_pos, c); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 393 | |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 394 | if (wsi->u.hdr.lextable_pos < 0) { |
| 395 | /* this is not a header we know about */ |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 396 | if (wsi->u.hdr.ah->frag_index[WSI_TOKEN_GET_URI] || wsi->u.hdr.ah->frag_index[WSI_TOKEN_POST_URI] || |
Andy Green | 646c98a | 2013-03-09 09:09:05 +0800 | [diff] [blame] | 397 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP]) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 398 | /* |
| 399 | * altready had the method, no idea what |
| 400 | * this crap is, ignore |
| 401 | */ |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 402 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
| 403 | break; |
| 404 | } |
Andy Green | 3ee9b31 | 2013-02-12 12:52:39 +0800 | [diff] [blame] | 405 | /* |
| 406 | * hm it's an unknown http method in fact, |
| 407 | * treat as dangerous |
| 408 | */ |
| 409 | |
| 410 | lwsl_info("Unknown method - dropping\n"); |
| 411 | return -1; |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 412 | } |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 413 | if (lextable[wsi->u.hdr.lextable_pos + 1] == 0) { |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 414 | |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 415 | /* terminal state */ |
| 416 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 417 | n = lextable[wsi->u.hdr.lextable_pos] & 0x7f; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 418 | |
Andy Green | cc7cb68 | 2013-02-18 10:22:42 +0800 | [diff] [blame] | 419 | lwsl_parser("known hdr %d\n", n); |
Andy Green | fd96330 | 2012-04-03 17:02:20 +0200 | [diff] [blame] | 420 | |
Andy Green | 94f9465 | 2013-02-12 13:10:19 +0800 | [diff] [blame] | 421 | if (n == WSI_TOKEN_GET_URI && |
| 422 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_GET_URI]) { |
| 423 | lwsl_warn("Duplicated GET\n"); |
| 424 | return -1; |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 425 | } else if (n == WSI_TOKEN_POST_URI && |
| 426 | wsi->u.hdr.ah->frag_index[WSI_TOKEN_POST_URI]) { |
| 427 | lwsl_warn("Duplicated POST\n"); |
| 428 | return -1; |
Andy Green | 94f9465 | 2013-02-12 13:10:19 +0800 | [diff] [blame] | 429 | } |
| 430 | |
Andy Green | fd96330 | 2012-04-03 17:02:20 +0200 | [diff] [blame] | 431 | /* |
| 432 | * WSORIGIN is protocol equiv to ORIGIN, |
| 433 | * JWebSocket likes to send it, map to ORIGIN |
| 434 | */ |
| 435 | if (n == WSI_TOKEN_SWORIGIN) |
| 436 | n = WSI_TOKEN_ORIGIN; |
| 437 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 438 | wsi->u.hdr.parser_state = (enum lws_token_indexes) |
| 439 | (WSI_TOKEN_GET_URI + n); |
| 440 | if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE) |
| 441 | goto set_parsing_complete; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 442 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 443 | goto start_fragment; |
| 444 | } |
| 445 | break; |
Nick Dowell | 3059263 | 2012-04-05 10:31:48 +0800 | [diff] [blame] | 446 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 447 | start_fragment: |
| 448 | wsi->u.hdr.ah->next_frag_index++; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 449 | if (wsi->u.hdr.ah->next_frag_index == |
| 450 | sizeof(wsi->u.hdr.ah->frags) / |
| 451 | sizeof(wsi->u.hdr.ah->frags[0])) { |
| 452 | lwsl_warn("More hdr frags than we can deal with\n"); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 453 | return -1; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 456 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset = |
| 457 | wsi->u.hdr.ah->pos; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 458 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 459 | wsi->u.hdr.ah->frags[ |
| 460 | wsi->u.hdr.ah->next_frag_index].next_frag_index = 0; |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 461 | |
| 462 | n = wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state]; |
| 463 | if (!n) { /* first fragment */ |
| 464 | wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state] = |
| 465 | wsi->u.hdr.ah->next_frag_index; |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 466 | break; |
| 467 | } |
| 468 | /* continuation */ |
| 469 | while (wsi->u.hdr.ah->frags[n].next_frag_index) |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 470 | n = wsi->u.hdr.ah->frags[n].next_frag_index; |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 471 | wsi->u.hdr.ah->frags[n].next_frag_index = |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 472 | wsi->u.hdr.ah->next_frag_index; |
| 473 | |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 474 | if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) { |
| 475 | lwsl_warn("excessive header content\n"); |
| 476 | return -1; |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 477 | } |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 478 | |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 479 | wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = ' '; |
| 480 | wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++; |
Andy Green | e4dffc9 | 2013-02-04 09:24:18 +0800 | [diff] [blame] | 481 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 482 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 483 | /* skipping arg part of a name we didn't recognize */ |
| 484 | case WSI_TOKEN_SKIPPING: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 485 | lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c); |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 486 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 487 | if (c == '\x0d') |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 488 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 489 | break; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 490 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 491 | case WSI_TOKEN_SKIPPING_SAW_CR: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 492 | lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c); |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 493 | if (c == '\x0a') { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 494 | wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART; |
| 495 | wsi->u.hdr.lextable_pos = 0; |
Andy Green | 6d1fcb7 | 2013-01-18 01:55:48 +0800 | [diff] [blame] | 496 | } else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 497 | wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 498 | break; |
| 499 | /* we're done, ignore anything else */ |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 500 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 501 | case WSI_PARSING_COMPLETE: |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 502 | lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c); |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 503 | break; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 504 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 505 | default: /* keep gcc happy */ |
| 506 | break; |
| 507 | } |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 508 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 509 | return 0; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 510 | |
| 511 | set_parsing_complete: |
| 512 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 513 | if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) { |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 514 | if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION)) |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 515 | wsi->ietf_spec_revision = |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 516 | atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION)); |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 517 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 518 | lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision); |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 519 | } |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 520 | wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE; |
Andy Green | 224149a | 2013-02-11 21:43:41 +0800 | [diff] [blame] | 521 | wsi->hdr_parsing_completed = 1; |
Andy Green | 177ca78 | 2013-02-04 09:09:19 +0800 | [diff] [blame] | 522 | |
| 523 | return 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 526 | |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 527 | /** |
| 528 | * lws_frame_is_binary: true if the current frame was sent in binary mode |
| 529 | * |
| 530 | * @wsi: the connection we are inquiring about |
| 531 | * |
| 532 | * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if |
| 533 | * it's interested to see if the frame it's dealing with was sent in binary |
| 534 | * mode. |
| 535 | */ |
| 536 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 537 | LWS_VISIBLE int lws_frame_is_binary(struct libwebsocket *wsi) |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 538 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 539 | return wsi->u.ws.frame_is_binary; |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 540 | } |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 541 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 542 | int |
| 543 | libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c) |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 544 | { |
| 545 | int n; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 546 | struct lws_tokens eff_buf; |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 547 | int ret = 0; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 548 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 549 | int handled; |
| 550 | int m; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 551 | #endif |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 552 | |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 553 | #if 0 |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 554 | lwsl_debug("RX: %02X ", c); |
Andy Green | 6ee372f | 2012-04-09 15:09:01 +0800 | [diff] [blame] | 555 | #endif |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 556 | |
| 557 | switch (wsi->lws_rx_parse_state) { |
| 558 | case LWS_RXPS_NEW: |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 559 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 560 | switch (wsi->ietf_spec_revision) { |
Andy Green | d85cb20 | 2011-09-25 09:32:54 +0100 | [diff] [blame] | 561 | case 13: |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 562 | /* |
| 563 | * no prepended frame key any more |
| 564 | */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 565 | wsi->u.ws.all_zero_nonce = 1; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 566 | goto handle_first; |
| 567 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 568 | default: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 569 | lwsl_warn("lws_rx_sm: unknown spec version %d\n", |
| 570 | wsi->ietf_spec_revision); |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 571 | break; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 572 | } |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 573 | break; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 574 | case LWS_RXPS_04_MASK_NONCE_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 575 | wsi->u.ws.frame_masking_nonce_04[1] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 576 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 577 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 578 | wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_2; |
| 579 | break; |
| 580 | case LWS_RXPS_04_MASK_NONCE_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 581 | wsi->u.ws.frame_masking_nonce_04[2] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 582 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 583 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 584 | wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_3; |
| 585 | break; |
| 586 | case LWS_RXPS_04_MASK_NONCE_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 587 | wsi->u.ws.frame_masking_nonce_04[3] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 588 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 589 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 590 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 591 | /* |
| 592 | * start from the zero'th byte in the XOR key buffer since |
| 593 | * this is the start of a frame with a new key |
| 594 | */ |
| 595 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 596 | wsi->u.ws.frame_mask_index = 0; |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 597 | |
Andy Green | 3e5eb78 | 2011-01-18 18:14:26 +0000 | [diff] [blame] | 598 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1; |
| 599 | break; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 600 | |
| 601 | /* |
| 602 | * 04 logical framing from the spec (all this is masked when incoming |
| 603 | * and has to be unmasked) |
| 604 | * |
| 605 | * We ignore the possibility of extension data because we don't |
| 606 | * negotiate any extensions at the moment. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 607 | * |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 608 | * 0 1 2 3 |
| 609 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 610 | * +-+-+-+-+-------+-+-------------+-------------------------------+ |
| 611 | * |F|R|R|R| opcode|R| Payload len | Extended payload length | |
| 612 | * |I|S|S|S| (4) |S| (7) | (16/63) | |
| 613 | * |N|V|V|V| |V| | (if payload len==126/127) | |
| 614 | * | |1|2|3| |4| | | |
| 615 | * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - + |
| 616 | * | Extended payload length continued, if payload len == 127 | |
| 617 | * + - - - - - - - - - - - - - - - +-------------------------------+ |
| 618 | * | | Extension data | |
| 619 | * +-------------------------------+ - - - - - - - - - - - - - - - + |
| 620 | * : : |
| 621 | * +---------------------------------------------------------------+ |
| 622 | * : Application data : |
| 623 | * +---------------------------------------------------------------+ |
| 624 | * |
| 625 | * We pass payload through to userland as soon as we get it, ignoring |
| 626 | * FIN. It's up to userland to buffer it up if it wants to see a |
| 627 | * whole unfragmented block of the original size (which may be up to |
| 628 | * 2^63 long!) |
| 629 | */ |
| 630 | |
| 631 | case LWS_RXPS_04_FRAME_HDR_1: |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 632 | handle_first: |
| 633 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 634 | wsi->u.ws.opcode = c & 0xf; |
| 635 | wsi->u.ws.rsv = c & 0x70; |
| 636 | wsi->u.ws.final = !!((c >> 7) & 1); |
Andy Green | 1bc12f9 | 2013-02-28 17:11:29 +0800 | [diff] [blame] | 637 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 638 | switch (wsi->u.ws.opcode) { |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 639 | case LWS_WS_OPCODE_07__TEXT_FRAME: |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 640 | case LWS_WS_OPCODE_07__BINARY_FRAME: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 641 | wsi->u.ws.frame_is_binary = |
| 642 | wsi->u.ws.opcode == LWS_WS_OPCODE_07__BINARY_FRAME; |
Andy Green | 10601c1 | 2013-01-19 10:39:35 +0800 | [diff] [blame] | 643 | break; |
| 644 | } |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 645 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN; |
| 646 | break; |
| 647 | |
| 648 | case LWS_RXPS_04_FRAME_HDR_LEN: |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 649 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 650 | wsi->u.ws.this_frame_masked = !!(c & 0x80); |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 651 | |
Andy Green | 5bf6578 | 2011-09-25 10:46:31 +0100 | [diff] [blame] | 652 | switch (c & 0x7f) { |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 653 | case 126: |
| 654 | /* control frames are not allowed to have big lengths */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 655 | if (wsi->u.ws.opcode & 8) |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 656 | goto illegal_ctl_length; |
| 657 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 658 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2; |
| 659 | break; |
| 660 | case 127: |
| 661 | /* control frames are not allowed to have big lengths */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 662 | if (wsi->u.ws.opcode & 8) |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 663 | goto illegal_ctl_length; |
| 664 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 665 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8; |
| 666 | break; |
| 667 | default: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 668 | wsi->u.ws.rx_packet_length = c & 0x7f; |
| 669 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 670 | wsi->lws_rx_parse_state = |
| 671 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 672 | else |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 673 | if (wsi->u.ws.rx_packet_length) |
| 674 | wsi->lws_rx_parse_state = |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 675 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 676 | else { |
| 677 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
| 678 | goto spill; |
| 679 | } |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 680 | break; |
| 681 | } |
| 682 | break; |
| 683 | |
| 684 | case LWS_RXPS_04_FRAME_HDR_LEN16_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 685 | wsi->u.ws.rx_packet_length = c << 8; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 686 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1; |
| 687 | break; |
| 688 | |
| 689 | case LWS_RXPS_04_FRAME_HDR_LEN16_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 690 | wsi->u.ws.rx_packet_length |= c; |
| 691 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 692 | wsi->lws_rx_parse_state = |
| 693 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 694 | else |
| 695 | wsi->lws_rx_parse_state = |
| 696 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 697 | break; |
| 698 | |
| 699 | case LWS_RXPS_04_FRAME_HDR_LEN64_8: |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 700 | if (c & 0x80) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 701 | lwsl_warn("b63 of length must be zero\n"); |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 702 | /* kill the connection */ |
| 703 | return -1; |
| 704 | } |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 705 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 706 | wsi->u.ws.rx_packet_length = ((size_t)c) << 56; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 707 | #else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 708 | wsi->u.ws.rx_packet_length = 0; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 709 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 710 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7; |
| 711 | break; |
| 712 | |
| 713 | case LWS_RXPS_04_FRAME_HDR_LEN64_7: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 714 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 715 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 48; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 716 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 717 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6; |
| 718 | break; |
| 719 | |
| 720 | case LWS_RXPS_04_FRAME_HDR_LEN64_6: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 721 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 722 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 40; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 723 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 724 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5; |
| 725 | break; |
| 726 | |
| 727 | case LWS_RXPS_04_FRAME_HDR_LEN64_5: |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 728 | #if defined __LP64__ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 729 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 32; |
Andy Green | f55830d | 2011-01-27 06:45:53 +0000 | [diff] [blame] | 730 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 731 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4; |
| 732 | break; |
| 733 | |
| 734 | case LWS_RXPS_04_FRAME_HDR_LEN64_4: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 735 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 24; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 736 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3; |
| 737 | break; |
| 738 | |
| 739 | case LWS_RXPS_04_FRAME_HDR_LEN64_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 740 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 16; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 741 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2; |
| 742 | break; |
| 743 | |
| 744 | case LWS_RXPS_04_FRAME_HDR_LEN64_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 745 | wsi->u.ws.rx_packet_length |= ((size_t)c) << 8; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 746 | wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1; |
| 747 | break; |
| 748 | |
| 749 | case LWS_RXPS_04_FRAME_HDR_LEN64_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 750 | wsi->u.ws.rx_packet_length |= ((size_t)c); |
| 751 | if (wsi->u.ws.this_frame_masked) |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 752 | wsi->lws_rx_parse_state = |
| 753 | LWS_RXPS_07_COLLECT_FRAME_KEY_1; |
| 754 | else |
| 755 | wsi->lws_rx_parse_state = |
| 756 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 757 | break; |
| 758 | |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 759 | case LWS_RXPS_07_COLLECT_FRAME_KEY_1: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 760 | wsi->u.ws.frame_masking_nonce_04[0] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 761 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 762 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 763 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2; |
| 764 | break; |
| 765 | |
| 766 | case LWS_RXPS_07_COLLECT_FRAME_KEY_2: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 767 | wsi->u.ws.frame_masking_nonce_04[1] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 768 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 769 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 770 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3; |
| 771 | break; |
| 772 | |
| 773 | case LWS_RXPS_07_COLLECT_FRAME_KEY_3: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 774 | wsi->u.ws.frame_masking_nonce_04[2] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 775 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 776 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 777 | wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4; |
| 778 | break; |
| 779 | |
| 780 | case LWS_RXPS_07_COLLECT_FRAME_KEY_4: |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 781 | wsi->u.ws.frame_masking_nonce_04[3] = c; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 782 | if (c) |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 783 | wsi->u.ws.all_zero_nonce = 0; |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 784 | wsi->lws_rx_parse_state = |
| 785 | LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 786 | wsi->u.ws.frame_mask_index = 0; |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 787 | if (wsi->u.ws.rx_packet_length == 0) { |
| 788 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
Andy Green | 1bc12f9 | 2013-02-28 17:11:29 +0800 | [diff] [blame] | 789 | goto spill; |
Andy Green | 8ea1955 | 2014-02-15 16:00:37 +0800 | [diff] [blame] | 790 | } |
Andy Green | 283d0a2 | 2011-04-24 05:46:23 +0100 | [diff] [blame] | 791 | break; |
| 792 | |
| 793 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 794 | case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 795 | |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 796 | if (!wsi->u.ws.rx_user_buffer) |
| 797 | lwsl_err("NULL user buffer...\n"); |
| 798 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 799 | if (wsi->u.ws.all_zero_nonce) |
| 800 | wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + |
| 801 | (wsi->u.ws.rx_user_buffer_head++)] = c; |
Andy Green | 92970ba | 2011-02-10 09:22:35 +0000 | [diff] [blame] | 802 | else |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 803 | wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING + |
| 804 | (wsi->u.ws.rx_user_buffer_head++)] = |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 805 | c ^ wsi->u.ws.frame_masking_nonce_04[ |
| 806 | (wsi->u.ws.frame_mask_index++) & 3]; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 807 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 808 | if (--wsi->u.ws.rx_packet_length == 0) { |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 809 | /* spill because we have the whole frame */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 810 | wsi->lws_rx_parse_state = LWS_RXPS_NEW; |
| 811 | goto spill; |
| 812 | } |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 813 | |
| 814 | /* |
| 815 | * if there's no protocol max frame size given, we are |
| 816 | * supposed to default to LWS_MAX_SOCKET_IO_BUF |
| 817 | */ |
| 818 | |
| 819 | if (!wsi->protocol->rx_buffer_size && |
| 820 | wsi->u.ws.rx_user_buffer_head != |
| 821 | LWS_MAX_SOCKET_IO_BUF) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 822 | break; |
Andy Green | c2bb3dc | 2013-02-14 11:25:44 +0800 | [diff] [blame] | 823 | else |
| 824 | if (wsi->protocol->rx_buffer_size && |
| 825 | wsi->u.ws.rx_user_buffer_head != |
| 826 | wsi->protocol->rx_buffer_size) |
| 827 | break; |
| 828 | |
| 829 | /* spill because we filled our rx buffer */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 830 | spill: |
| 831 | /* |
| 832 | * is this frame a control packet we should take care of at this |
| 833 | * layer? If so service it and hide it from the user callback |
| 834 | */ |
| 835 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 836 | lwsl_parser("spill on %s\n", wsi->protocol->name); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 837 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 838 | switch (wsi->u.ws.opcode) { |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 839 | case LWS_WS_OPCODE_07__CLOSE: |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 840 | /* is this an acknowledgement of our close? */ |
| 841 | if (wsi->state == WSI_STATE_AWAITING_CLOSE_ACK) { |
| 842 | /* |
| 843 | * fine he has told us he is closing too, let's |
| 844 | * finish our close |
| 845 | */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 846 | lwsl_parser("seen client close ack\n"); |
Andy Green | da527df | 2011-03-07 07:08:12 +0000 | [diff] [blame] | 847 | return -1; |
| 848 | } |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 849 | lwsl_parser("server sees client close packet\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 850 | /* parrot the close packet payload back */ |
| 851 | n = libwebsocket_write(wsi, (unsigned char *) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 852 | &wsi->u.ws.rx_user_buffer[ |
| 853 | LWS_SEND_BUFFER_PRE_PADDING], |
| 854 | wsi->u.ws.rx_user_buffer_head, |
| 855 | LWS_WRITE_CLOSE); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 856 | if (n < 0) |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 857 | lwsl_info("write of close ack failed %d\n", n); |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 858 | wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 859 | /* close the connection */ |
| 860 | return -1; |
| 861 | |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 862 | case LWS_WS_OPCODE_07__PING: |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 863 | lwsl_info("received %d byte ping, sending pong\n", |
| 864 | wsi->u.ws.rx_user_buffer_head); |
| 865 | lwsl_hexdump(&wsi->u.ws.rx_user_buffer[ |
| 866 | LWS_SEND_BUFFER_PRE_PADDING], |
| 867 | wsi->u.ws.rx_user_buffer_head); |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 868 | /* parrot the ping packet payload back as a pong */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 869 | n = libwebsocket_write(wsi, (unsigned char *) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 870 | &wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], |
| 871 | wsi->u.ws.rx_user_buffer_head, LWS_WRITE_PONG); |
Andy Green | fc7c5e4 | 2013-02-23 10:50:10 +0800 | [diff] [blame] | 872 | if (n < 0) |
| 873 | return -1; |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 874 | /* ... then just drop it */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 875 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 876 | return 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 877 | |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 878 | case LWS_WS_OPCODE_07__PONG: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 879 | /* ... then just drop it */ |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 880 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 881 | return 0; |
| 882 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 883 | case LWS_WS_OPCODE_07__TEXT_FRAME: |
| 884 | case LWS_WS_OPCODE_07__BINARY_FRAME: |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 885 | case LWS_WS_OPCODE_07__CONTINUATION: |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 886 | break; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 887 | |
| 888 | default: |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 889 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 890 | lwsl_parser("passing opc %x up to exts\n", |
| 891 | wsi->u.ws.opcode); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 892 | |
| 893 | /* |
| 894 | * It's something special we can't understand here. |
| 895 | * Pass the payload up to the extension's parsing |
| 896 | * state machine. |
| 897 | */ |
| 898 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 899 | eff_buf.token = &wsi->u.ws.rx_user_buffer[ |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 900 | LWS_SEND_BUFFER_PRE_PADDING]; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 901 | eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 902 | |
| 903 | handled = 0; |
| 904 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 905 | m = wsi->active_extensions[n]->callback( |
| 906 | wsi->protocol->owning_server, |
| 907 | wsi->active_extensions[n], wsi, |
| 908 | LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX, |
| 909 | wsi->active_extensions_user[n], |
| 910 | &eff_buf, 0); |
| 911 | if (m) |
| 912 | handled = 1; |
| 913 | } |
| 914 | |
Andy Green | 25a56b0 | 2011-09-25 08:47:57 +0100 | [diff] [blame] | 915 | if (!handled) |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 916 | #endif |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 917 | lwsl_ext("ext opc opcode 0x%x unknown\n", |
| 918 | wsi->u.ws.opcode); |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 919 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 920 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 921 | return 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /* |
| 925 | * No it's real payload, pass it up to the user callback. |
| 926 | * It's nicely buffered with the pre-padding taken care of |
| 927 | * so it can be sent straight out again using libwebsocket_write |
| 928 | */ |
| 929 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 930 | eff_buf.token = &wsi->u.ws.rx_user_buffer[ |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 931 | LWS_SEND_BUFFER_PRE_PADDING]; |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 932 | eff_buf.token_len = wsi->u.ws.rx_user_buffer_head; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 933 | #ifndef LWS_NO_EXTENSIONS |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 934 | for (n = 0; n < wsi->count_active_extensions; n++) { |
| 935 | m = wsi->active_extensions[n]->callback( |
| 936 | wsi->protocol->owning_server, |
| 937 | wsi->active_extensions[n], wsi, |
| 938 | LWS_EXT_CALLBACK_PAYLOAD_RX, |
| 939 | wsi->active_extensions_user[n], |
| 940 | &eff_buf, 0); |
| 941 | if (m < 0) { |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 942 | lwsl_ext( |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 943 | "Extension '%s' failed to handle payload!\n", |
| 944 | wsi->active_extensions[n]->name); |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 945 | return -1; |
| 946 | } |
| 947 | } |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 948 | #endif |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 949 | if (eff_buf.token_len > 0) { |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 950 | eff_buf.token[eff_buf.token_len] = '\0'; |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 951 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 952 | if (wsi->protocol->callback) |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 953 | ret = user_callback_handle_rxflow( |
| 954 | wsi->protocol->callback, |
| 955 | wsi->protocol->owning_server, |
| 956 | wsi, LWS_CALLBACK_RECEIVE, |
| 957 | wsi->user_space, |
| 958 | eff_buf.token, |
| 959 | eff_buf.token_len); |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 960 | else |
Andy Green | f7609e9 | 2013-01-14 13:10:55 +0800 | [diff] [blame] | 961 | lwsl_err("No callback on payload spill!\n"); |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 962 | } |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 963 | |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 964 | wsi->u.ws.rx_user_buffer_head = 0; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 965 | break; |
| 966 | } |
| 967 | |
Andy Green | aedc953 | 2013-02-10 21:21:24 +0800 | [diff] [blame] | 968 | return ret; |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 969 | |
| 970 | illegal_ctl_length: |
| 971 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 972 | lwsl_warn("Control frame with xtended length is illegal\n"); |
Andy Green | 23545db | 2011-04-24 06:19:22 +0100 | [diff] [blame] | 973 | /* kill the connection */ |
| 974 | return -1; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 978 | int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi, |
| 979 | unsigned char *buf, size_t len) |
| 980 | { |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 981 | size_t n = 0; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 982 | int m; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 983 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 984 | #if 0 |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 985 | lwsl_parser("received %d byte packet\n", (int)len); |
Andy Green | 0303db4 | 2013-01-17 14:46:43 +0800 | [diff] [blame] | 986 | lwsl_hexdump(buf, len); |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 987 | #endif |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 988 | |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 989 | /* let the rx protocol state machine have as much as it needs */ |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 990 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 991 | while (n < len) { |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 992 | /* |
| 993 | * we were accepting input but now we stopped doing so |
| 994 | */ |
| 995 | if (!(wsi->u.ws.rxflow_change_to & LWS_RXFLOW_ALLOW)) { |
| 996 | /* his RX is flowcontrolled, don't send remaining now */ |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 997 | if (!wsi->u.ws.rxflow_buffer) { |
| 998 | /* a new rxflow, buffer it and warn caller */ |
| 999 | lwsl_info("new rxflow input buffer len %d\n", |
| 1000 | len - n); |
| 1001 | wsi->u.ws.rxflow_buffer = |
| 1002 | (unsigned char *)malloc(len - n); |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1003 | wsi->u.ws.rxflow_len = len - n; |
| 1004 | wsi->u.ws.rxflow_pos = 0; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1005 | memcpy(wsi->u.ws.rxflow_buffer, |
| 1006 | buf + n, len - n); |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1007 | } else |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1008 | /* rxflow while we were spilling prev rxflow */ |
Andy Green | 98ba9e0 | 2013-03-23 09:44:47 +0800 | [diff] [blame] | 1009 | lwsl_info("stalling in existing rxflow buf\n"); |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1010 | |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1011 | return 1; |
| 1012 | } |
Andy Green | ca0a129 | 2013-03-16 11:24:23 +0800 | [diff] [blame] | 1013 | |
| 1014 | /* account for what we're using in rxflow buffer */ |
| 1015 | if (wsi->u.ws.rxflow_buffer) |
| 1016 | wsi->u.ws.rxflow_pos++; |
| 1017 | |
| 1018 | /* process the byte */ |
| 1019 | m = libwebsocket_rx_sm(wsi, buf[n++]); |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1020 | if (m < 0) |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1021 | return -1; |
Andy Green | 706961d | 2013-01-17 16:50:35 +0800 | [diff] [blame] | 1022 | } |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1023 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1024 | return 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1028 | /** |
| 1029 | * libwebsockets_remaining_packet_payload() - Bytes to come before "overall" |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1030 | * rx packet is complete |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1031 | * @wsi: Websocket instance (available from user callback) |
| 1032 | * |
| 1033 | * This function is intended to be called from the callback if the |
| 1034 | * user code is interested in "complete packets" from the client. |
| 1035 | * libwebsockets just passes through payload as it comes and issues a buffer |
| 1036 | * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE |
| 1037 | * callback handler can use this API to find out if the buffer it has just |
| 1038 | * been given is the last piece of a "complete packet" from the client -- |
| 1039 | * when that is the case libwebsockets_remaining_packet_payload() will return |
| 1040 | * 0. |
| 1041 | * |
| 1042 | * Many protocols won't care becuse their packets are always small. |
| 1043 | */ |
| 1044 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1045 | LWS_VISIBLE size_t |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1046 | libwebsockets_remaining_packet_payload(struct libwebsocket *wsi) |
| 1047 | { |
Andy Green | 623a98d | 2013-01-21 11:04:23 +0800 | [diff] [blame] | 1048 | return wsi->u.ws.rx_packet_length; |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1049 | } |