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