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