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