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