remove all support for pre v13 protocols
[profile/ivi/libwebsockets.git] / lib / parsers.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
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
24 #ifdef WIN32
25 #include <io.h>
26 #endif
27
28
29 unsigned char lextable[] = {
30 /* pos 0: state 0 */
31    0x47 /* 'G' */, 0x07 /* to pos 14 state 1 */,
32    0x48 /* 'H' */, 0x0A /* to pos 22 state 5 */,
33    0x43 /* 'C' */, 0x0F /* to pos 34 state 10 */,
34    0x53 /* 'S' */, 0x19 /* to pos 56 state 21 */,
35    0x55 /* 'U' */, 0x3F /* to pos 134 state 51 */,
36    0x4F /* 'O' */, 0x46 /* to pos 150 state 59 */,
37    0x8D /* '.' */, 0x52 /* to pos 176 state 72 */,
38 /* pos 14: state 1 */
39    0xC5 /* 'E' */, 0x01 /* to pos 16 state 2 */,
40 /* pos 16: state 2 */
41    0xD4 /* 'T' */, 0x01 /* to pos 18 state 3 */,
42 /* pos 18: state 3 */
43    0xA0 /* ' ' */, 0x01 /* to pos 20 state 4 */,
44 /* pos 20: state 4 */
45    0x80, 0x00 /* terminal marker */, 
46 /* pos 22: state 5 */
47    0x6F /* 'o' */, 0x02 /* to pos 26 state 6 */,
48    0xD4 /* 'T' */, 0x76 /* to pos 260 state 114 */,
49 /* pos 26: state 6 */
50    0xF3 /* 's' */, 0x01 /* to pos 28 state 7 */,
51 /* pos 28: state 7 */
52    0xF4 /* 't' */, 0x01 /* to pos 30 state 8 */,
53 /* pos 30: state 8 */
54    0xBA /* ':' */, 0x01 /* to pos 32 state 9 */,
55 /* pos 32: state 9 */
56    0x81, 0x00 /* terminal marker */, 
57 /* pos 34: state 10 */
58    0xEF /* 'o' */, 0x01 /* to pos 36 state 11 */,
59 /* pos 36: state 11 */
60    0xEE /* 'n' */, 0x01 /* to pos 38 state 12 */,
61 /* pos 38: state 12 */
62    0xEE /* 'n' */, 0x01 /* to pos 40 state 13 */,
63 /* pos 40: state 13 */
64    0xE5 /* 'e' */, 0x01 /* to pos 42 state 14 */,
65 /* pos 42: state 14 */
66    0xE3 /* 'c' */, 0x01 /* to pos 44 state 15 */,
67 /* pos 44: state 15 */
68    0xF4 /* 't' */, 0x01 /* to pos 46 state 16 */,
69 /* pos 46: state 16 */
70    0xE9 /* 'i' */, 0x01 /* to pos 48 state 17 */,
71 /* pos 48: state 17 */
72    0xEF /* 'o' */, 0x01 /* to pos 50 state 18 */,
73 /* pos 50: state 18 */
74    0xEE /* 'n' */, 0x01 /* to pos 52 state 19 */,
75 /* pos 52: state 19 */
76    0xBA /* ':' */, 0x01 /* to pos 54 state 20 */,
77 /* pos 54: state 20 */
78    0x82, 0x00 /* terminal marker */, 
79 /* pos 56: state 21 */
80    0xE5 /* 'e' */, 0x01 /* to pos 58 state 22 */,
81 /* pos 58: state 22 */
82    0xE3 /* 'c' */, 0x01 /* to pos 60 state 23 */,
83 /* pos 60: state 23 */
84    0xAD /* '-' */, 0x01 /* to pos 62 state 24 */,
85 /* pos 62: state 24 */
86    0xD7 /* 'W' */, 0x01 /* to pos 64 state 25 */,
87 /* pos 64: state 25 */
88    0xE5 /* 'e' */, 0x01 /* to pos 66 state 26 */,
89 /* pos 66: state 26 */
90    0xE2 /* 'b' */, 0x01 /* to pos 68 state 27 */,
91 /* pos 68: state 27 */
92    0xD3 /* 'S' */, 0x01 /* to pos 70 state 28 */,
93 /* pos 70: state 28 */
94    0xEF /* 'o' */, 0x01 /* to pos 72 state 29 */,
95 /* pos 72: state 29 */
96    0xE3 /* 'c' */, 0x01 /* to pos 74 state 30 */,
97 /* pos 74: state 30 */
98    0xEB /* 'k' */, 0x01 /* to pos 76 state 31 */,
99 /* pos 76: state 31 */
100    0xE5 /* 'e' */, 0x01 /* to pos 78 state 32 */,
101 /* pos 78: state 32 */
102    0xF4 /* 't' */, 0x01 /* to pos 80 state 33 */,
103 /* pos 80: state 33 */
104    0xAD /* '-' */, 0x01 /* to pos 82 state 34 */,
105 /* pos 82: state 34 */
106    0x4B /* 'K' */, 0x08 /* to pos 98 state 35 */,
107    0x50 /* 'P' */, 0x10 /* to pos 116 state 42 */,
108    0x44 /* 'D' */, 0x27 /* to pos 164 state 66 */,
109    0x56 /* 'V' */, 0x2F /* to pos 182 state 75 */,
110    0x4F /* 'O' */, 0x36 /* to pos 198 state 83 */,
111    0x45 /* 'E' */, 0x3C /* to pos 212 state 90 */,
112    0x41 /* 'A' */, 0x46 /* to pos 234 state 101 */,
113    0xCE /* 'N' */, 0x4C /* to pos 248 state 108 */,
114 /* pos 98: state 35 */
115    0xE5 /* 'e' */, 0x01 /* to pos 100 state 36 */,
116 /* pos 100: state 36 */
117    0xF9 /* 'y' */, 0x01 /* to pos 102 state 37 */,
118 /* pos 102: state 37 */
119    0x31 /* '1' */, 0x03 /* to pos 108 state 38 */,
120    0x32 /* '2' */, 0x04 /* to pos 112 state 40 */,
121    0xBA /* ':' */, 0x25 /* to pos 180 state 74 */,
122 /* pos 108: state 38 */
123    0xBA /* ':' */, 0x01 /* to pos 110 state 39 */,
124 /* pos 110: state 39 */
125    0x83, 0x00 /* terminal marker */, 
126 /* pos 112: state 40 */
127    0xBA /* ':' */, 0x01 /* to pos 114 state 41 */,
128 /* pos 114: state 41 */
129    0x84, 0x00 /* terminal marker */, 
130 /* pos 116: state 42 */
131    0xF2 /* 'r' */, 0x01 /* to pos 118 state 43 */,
132 /* pos 118: state 43 */
133    0xEF /* 'o' */, 0x01 /* to pos 120 state 44 */,
134 /* pos 120: state 44 */
135    0xF4 /* 't' */, 0x01 /* to pos 122 state 45 */,
136 /* pos 122: state 45 */
137    0xEF /* 'o' */, 0x01 /* to pos 124 state 46 */,
138 /* pos 124: state 46 */
139    0xE3 /* 'c' */, 0x01 /* to pos 126 state 47 */,
140 /* pos 126: state 47 */
141    0xEF /* 'o' */, 0x01 /* to pos 128 state 48 */,
142 /* pos 128: state 48 */
143    0xEC /* 'l' */, 0x01 /* to pos 130 state 49 */,
144 /* pos 130: state 49 */
145    0xBA /* ':' */, 0x01 /* to pos 132 state 50 */,
146 /* pos 132: state 50 */
147    0x85, 0x00 /* terminal marker */, 
148 /* pos 134: state 51 */
149    0xF0 /* 'p' */, 0x01 /* to pos 136 state 52 */,
150 /* pos 136: state 52 */
151    0xE7 /* 'g' */, 0x01 /* to pos 138 state 53 */,
152 /* pos 138: state 53 */
153    0xF2 /* 'r' */, 0x01 /* to pos 140 state 54 */,
154 /* pos 140: state 54 */
155    0xE1 /* 'a' */, 0x01 /* to pos 142 state 55 */,
156 /* pos 142: state 55 */
157    0xE4 /* 'd' */, 0x01 /* to pos 144 state 56 */,
158 /* pos 144: state 56 */
159    0xE5 /* 'e' */, 0x01 /* to pos 146 state 57 */,
160 /* pos 146: state 57 */
161    0xBA /* ':' */, 0x01 /* to pos 148 state 58 */,
162 /* pos 148: state 58 */
163    0x86, 0x00 /* terminal marker */, 
164 /* pos 150: state 59 */
165    0xF2 /* 'r' */, 0x01 /* to pos 152 state 60 */,
166 /* pos 152: state 60 */
167    0xE9 /* 'i' */, 0x01 /* to pos 154 state 61 */,
168 /* pos 154: state 61 */
169    0xE7 /* 'g' */, 0x01 /* to pos 156 state 62 */,
170 /* pos 156: state 62 */
171    0xE9 /* 'i' */, 0x01 /* to pos 158 state 63 */,
172 /* pos 158: state 63 */
173    0xEE /* 'n' */, 0x01 /* to pos 160 state 64 */,
174 /* pos 160: state 64 */
175    0xBA /* ':' */, 0x01 /* to pos 162 state 65 */,
176 /* pos 162: state 65 */
177    0x87, 0x00 /* terminal marker */, 
178 /* pos 164: state 66 */
179    0xF2 /* 'r' */, 0x01 /* to pos 166 state 67 */,
180 /* pos 166: state 67 */
181    0xE1 /* 'a' */, 0x01 /* to pos 168 state 68 */,
182 /* pos 168: state 68 */
183    0xE6 /* 'f' */, 0x01 /* to pos 170 state 69 */,
184 /* pos 170: state 69 */
185    0xF4 /* 't' */, 0x01 /* to pos 172 state 70 */,
186 /* pos 172: state 70 */
187    0xBA /* ':' */, 0x01 /* to pos 174 state 71 */,
188 /* pos 174: state 71 */
189    0x88, 0x00 /* terminal marker */, 
190 /* pos 176: state 72 */
191    0x8A /* '.' */, 0x01 /* to pos 178 state 73 */,
192 /* pos 178: state 73 */
193    0x89, 0x00 /* terminal marker */, 
194 /* pos 180: state 74 */
195    0x8A, 0x00 /* terminal marker */, 
196 /* pos 182: state 75 */
197    0xE5 /* 'e' */, 0x01 /* to pos 184 state 76 */,
198 /* pos 184: state 76 */
199    0xF2 /* 'r' */, 0x01 /* to pos 186 state 77 */,
200 /* pos 186: state 77 */
201    0xF3 /* 's' */, 0x01 /* to pos 188 state 78 */,
202 /* pos 188: state 78 */
203    0xE9 /* 'i' */, 0x01 /* to pos 190 state 79 */,
204 /* pos 190: state 79 */
205    0xEF /* 'o' */, 0x01 /* to pos 192 state 80 */,
206 /* pos 192: state 80 */
207    0xEE /* 'n' */, 0x01 /* to pos 194 state 81 */,
208 /* pos 194: state 81 */
209    0xBA /* ':' */, 0x01 /* to pos 196 state 82 */,
210 /* pos 196: state 82 */
211    0x8B, 0x00 /* terminal marker */, 
212 /* pos 198: state 83 */
213    0xF2 /* 'r' */, 0x01 /* to pos 200 state 84 */,
214 /* pos 200: state 84 */
215    0xE9 /* 'i' */, 0x01 /* to pos 202 state 85 */,
216 /* pos 202: state 85 */
217    0xE7 /* 'g' */, 0x01 /* to pos 204 state 86 */,
218 /* pos 204: state 86 */
219    0xE9 /* 'i' */, 0x01 /* to pos 206 state 87 */,
220 /* pos 206: state 87 */
221    0xEE /* 'n' */, 0x01 /* to pos 208 state 88 */,
222 /* pos 208: state 88 */
223    0xBA /* ':' */, 0x01 /* to pos 210 state 89 */,
224 /* pos 210: state 89 */
225    0x8C, 0x00 /* terminal marker */, 
226 /* pos 212: state 90 */
227    0xF8 /* 'x' */, 0x01 /* to pos 214 state 91 */,
228 /* pos 214: state 91 */
229    0xF4 /* 't' */, 0x01 /* to pos 216 state 92 */,
230 /* pos 216: state 92 */
231    0xE5 /* 'e' */, 0x01 /* to pos 218 state 93 */,
232 /* pos 218: state 93 */
233    0xEE /* 'n' */, 0x01 /* to pos 220 state 94 */,
234 /* pos 220: state 94 */
235    0xF3 /* 's' */, 0x01 /* to pos 222 state 95 */,
236 /* pos 222: state 95 */
237    0xE9 /* 'i' */, 0x01 /* to pos 224 state 96 */,
238 /* pos 224: state 96 */
239    0xEF /* 'o' */, 0x01 /* to pos 226 state 97 */,
240 /* pos 226: state 97 */
241    0xEE /* 'n' */, 0x01 /* to pos 228 state 98 */,
242 /* pos 228: state 98 */
243    0xF3 /* 's' */, 0x01 /* to pos 230 state 99 */,
244 /* pos 230: state 99 */
245    0xBA /* ':' */, 0x01 /* to pos 232 state 100 */,
246 /* pos 232: state 100 */
247    0x8D, 0x00 /* terminal marker */, 
248 /* pos 234: state 101 */
249    0xE3 /* 'c' */, 0x01 /* to pos 236 state 102 */,
250 /* pos 236: state 102 */
251    0xE3 /* 'c' */, 0x01 /* to pos 238 state 103 */,
252 /* pos 238: state 103 */
253    0xE5 /* 'e' */, 0x01 /* to pos 240 state 104 */,
254 /* pos 240: state 104 */
255    0xF0 /* 'p' */, 0x01 /* to pos 242 state 105 */,
256 /* pos 242: state 105 */
257    0xF4 /* 't' */, 0x01 /* to pos 244 state 106 */,
258 /* pos 244: state 106 */
259    0xBA /* ':' */, 0x01 /* to pos 246 state 107 */,
260 /* pos 246: state 107 */
261    0x8E, 0x00 /* terminal marker */, 
262 /* pos 248: state 108 */
263    0xEF /* 'o' */, 0x01 /* to pos 250 state 109 */,
264 /* pos 250: state 109 */
265    0xEE /* 'n' */, 0x01 /* to pos 252 state 110 */,
266 /* pos 252: state 110 */
267    0xE3 /* 'c' */, 0x01 /* to pos 254 state 111 */,
268 /* pos 254: state 111 */
269    0xE5 /* 'e' */, 0x01 /* to pos 256 state 112 */,
270 /* pos 256: state 112 */
271    0xBA /* ':' */, 0x01 /* to pos 258 state 113 */,
272 /* pos 258: state 113 */
273    0x8F, 0x00 /* terminal marker */, 
274 /* pos 260: state 114 */
275    0xD4 /* 'T' */, 0x01 /* to pos 262 state 115 */,
276 /* pos 262: state 115 */
277    0xD0 /* 'P' */, 0x01 /* to pos 264 state 116 */,
278 /* pos 264: state 116 */
279    0xAF /* '/' */, 0x01 /* to pos 266 state 117 */,
280 /* pos 266: state 117 */
281    0xB1 /* '1' */, 0x01 /* to pos 268 state 118 */,
282 /* pos 268: state 118 */
283    0xAE /* '.' */, 0x01 /* to pos 270 state 119 */,
284 /* pos 270: state 119 */
285    0xB1 /* '1' */, 0x01 /* to pos 272 state 120 */,
286 /* pos 272: state 120 */
287    0xA0 /* ' ' */, 0x01 /* to pos 274 state 121 */,
288 /* pos 274: state 121 */
289    0x90, 0x00 /* terminal marker */, 
290 /* total size 276 bytes */
291 };
292
293 int lextable_decode(int pos, char c)
294 {
295         while (pos >= 0) {
296                 if (lextable[pos + 1] == 0) // terminal marker
297                         return pos;
298
299                 if ((lextable[pos] & 0x7f) == c)
300                         return pos + (lextable[pos + 1] << 1);
301
302                 if (lextable[pos] & 0x80)
303                         return -1;
304
305                 pos += 2;
306         }
307         return pos;
308 }
309
310
311
312 int libwebsocket_parse(struct libwebsocket *wsi, unsigned char c)
313 {
314         int n;
315
316         switch (wsi->parser_state) {
317         case WSI_TOKEN_GET_URI:
318         case WSI_TOKEN_HOST:
319         case WSI_TOKEN_CONNECTION:
320         case WSI_TOKEN_KEY1:
321         case WSI_TOKEN_KEY2:
322         case WSI_TOKEN_PROTOCOL:
323         case WSI_TOKEN_UPGRADE:
324         case WSI_TOKEN_ORIGIN:
325         case WSI_TOKEN_SWORIGIN:
326         case WSI_TOKEN_DRAFT:
327         case WSI_TOKEN_CHALLENGE:
328         case WSI_TOKEN_KEY:
329         case WSI_TOKEN_VERSION:
330         case WSI_TOKEN_ACCEPT:
331         case WSI_TOKEN_NONCE:
332         case WSI_TOKEN_EXTENSIONS:
333         case WSI_TOKEN_HTTP:
334         case WSI_TOKEN_MUXURL:
335
336                 lwsl_parser("WSI_TOKEN_(%d) '%c'\n", wsi->parser_state, c);
337
338                 /* collect into malloc'd buffers */
339                 /* optional space swallow */
340                 if (!wsi->utf8_token[wsi->parser_state].token_len && c == ' ')
341                         break;
342
343                 /* special case space terminator for get-uri */
344                 if (wsi->parser_state == WSI_TOKEN_GET_URI && c == ' ') {
345                         wsi->utf8_token[wsi->parser_state].token[
346                            wsi->utf8_token[wsi->parser_state].token_len] = '\0';
347 //                      lwsl_parser("uri '%s'\n", wsi->utf8_token[wsi->parser_state].token);
348                         wsi->parser_state = WSI_TOKEN_SKIPPING;
349                         break;
350                 }
351
352                 /* allocate appropriate memory */
353                 if (wsi->utf8_token[wsi->parser_state].token_len ==
354                                                    wsi->current_alloc_len - 1) {
355                         /* need to extend */
356                         wsi->current_alloc_len += LWS_ADDITIONAL_HDR_ALLOC;
357                         if (wsi->current_alloc_len >= LWS_MAX_HEADER_LEN) {
358                                 /* it's waaay to much payload, fail it */
359                                 strcpy(wsi->utf8_token[wsi->parser_state].token,
360                                    "!!! Length exceeded maximum supported !!!");
361                                 wsi->parser_state = WSI_TOKEN_SKIPPING;
362                                 break;
363                         }
364                         wsi->utf8_token[wsi->parser_state].token = (char *)
365                                realloc(wsi->utf8_token[wsi->parser_state].token,
366                                                         wsi->current_alloc_len);
367                         if (wsi->utf8_token[wsi->parser_state].token == NULL) {
368                                 lwsl_err("Out of mem\n");
369                                 return -1;
370                         }
371                 }
372
373                 /* bail at EOL */
374                 if (wsi->parser_state != WSI_TOKEN_CHALLENGE && c == '\x0d') {
375                         wsi->utf8_token[wsi->parser_state].token[
376                            wsi->utf8_token[wsi->parser_state].token_len] = '\0';
377                         wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
378                         lwsl_parser("*\n");
379                         break;
380                 }
381
382                 wsi->utf8_token[wsi->parser_state].token[
383                             wsi->utf8_token[wsi->parser_state].token_len++] = c;
384
385                 /* per-protocol end of headers management */
386
387                 if (wsi->parser_state != WSI_TOKEN_CHALLENGE)
388                         break;
389
390                 /* -76 has no version header ... server */
391                 if (!wsi->utf8_token[WSI_TOKEN_VERSION].token_len &&
392                    wsi->mode != LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY &&
393                               wsi->utf8_token[wsi->parser_state].token_len != 8)
394                         break;
395
396                 /* -76 has no version header ... client */
397                 if (!wsi->utf8_token[WSI_TOKEN_VERSION].token_len &&
398                    wsi->mode == LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY &&
399                         wsi->utf8_token[wsi->parser_state].token_len != 16)
400                         break;
401
402                 /* <= 03 has old handshake with version header needs 8 bytes */
403                 if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len &&
404                          atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token) < 4 &&
405                               wsi->utf8_token[wsi->parser_state].token_len != 8)
406                         break;
407
408                 /* no payload challenge in 01 + */
409
410                 if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len &&
411                            atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token) > 0) {
412                         wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len = 0;
413                         free(wsi->utf8_token[WSI_TOKEN_CHALLENGE].token);
414                         wsi->utf8_token[WSI_TOKEN_CHALLENGE].token = NULL;
415                 }
416
417                 /* For any supported protocol we have enough payload */
418
419                 lwsl_parser("Setting WSI_PARSING_COMPLETE\n");
420                 wsi->parser_state = WSI_PARSING_COMPLETE;
421                 break;
422
423         case WSI_INIT_TOKEN_MUXURL:
424                 wsi->parser_state = WSI_TOKEN_MUXURL;
425                 wsi->current_alloc_len = LWS_INITIAL_HDR_ALLOC;
426
427                 wsi->utf8_token[wsi->parser_state].token = (char *)
428                                          malloc(wsi->current_alloc_len);
429                 if (wsi->utf8_token[wsi->parser_state].token == NULL) {
430                         lwsl_err("Out of mem\n");
431                         return -1;
432                 }
433                 wsi->utf8_token[wsi->parser_state].token_len = 0;
434                 break;
435
436                 /* collecting and checking a name part */
437         case WSI_TOKEN_NAME_PART:
438                 lwsl_parser("WSI_TOKEN_NAME_PART '%c'\n", c);
439
440                 if (wsi->name_buffer_pos == sizeof(wsi->name_buffer) - 1) {
441                         /* name bigger than we can handle, skip until next */
442                         wsi->parser_state = WSI_TOKEN_SKIPPING;
443                         break;
444                 }
445                 wsi->name_buffer[wsi->name_buffer_pos++] = c;
446                 wsi->name_buffer[wsi->name_buffer_pos] = '\0';
447
448                 wsi->lextable_pos = lextable_decode(wsi->lextable_pos, c);
449                 if (lextable[wsi->lextable_pos + 1] == 0) {
450
451                         n = lextable[wsi->lextable_pos] & 0x7f;
452
453                         lwsl_parser("known hdr '%s'\n", wsi->name_buffer);
454
455                         /*
456                          * WSORIGIN is protocol equiv to ORIGIN,
457                          * JWebSocket likes to send it, map to ORIGIN
458                          */
459                         if (n == WSI_TOKEN_SWORIGIN)
460                                 n = WSI_TOKEN_ORIGIN;
461
462                         wsi->parser_state = (enum lws_token_indexes) (WSI_TOKEN_GET_URI + n);
463
464                         n = WSI_TOKEN_COUNT;
465
466                         /*  If the header has been seen already, just append */
467                         if (!wsi->utf8_token[wsi->parser_state].token) {
468
469                                 wsi->current_alloc_len = LWS_INITIAL_HDR_ALLOC;
470                                 wsi->utf8_token[wsi->parser_state].token = (char *)
471                                                          malloc(wsi->current_alloc_len);
472                                 if (wsi->utf8_token[wsi->parser_state].token == NULL) {
473                                         lwsl_err("Out of mem\n");
474                                         return -1;
475                                 }
476                                 wsi->utf8_token[wsi->parser_state].token_len = 0;
477                         }
478                 }
479
480                 /* colon delimiter means we just don't know this name */
481
482                 if (wsi->parser_state == WSI_TOKEN_NAME_PART) {
483                         if (c == ':') {
484                                 lwsl_parser("skipping unknown header '%s'\n",
485                                                           wsi->name_buffer);
486                                 wsi->parser_state = WSI_TOKEN_SKIPPING;
487                                 break;
488                         }
489
490                         if (c == ' ' &&
491                                 !wsi->utf8_token[WSI_TOKEN_GET_URI].token_len) {
492                                 lwsl_parser("unknown method '%s'\n",
493                                                           wsi->name_buffer);
494                                 wsi->parser_state = WSI_TOKEN_GET_URI;
495                                 wsi->current_alloc_len = LWS_INITIAL_HDR_ALLOC;
496                                 wsi->utf8_token[WSI_TOKEN_GET_URI].token =
497                                         (char *)malloc(wsi->current_alloc_len);
498                                 if (wsi->utf8_token[WSI_TOKEN_GET_URI].token == NULL) {
499                                         lwsl_err("Out of mem\n");
500                                         return -1;
501                                 }
502                                 break;
503                         }
504                 }
505
506                 if (wsi->parser_state != WSI_TOKEN_CHALLENGE)
507                         break;
508
509                 /* don't look for payload when it can just be http headers */
510
511                 if (!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len) {
512                         /* they're HTTP headers, not websocket upgrade! */
513                         lwsl_parser("Setting WSI_PARSING_COMPLETE "
514                                                          "from http headers\n");
515                         wsi->parser_state = WSI_PARSING_COMPLETE;
516                 }
517
518                 /* 04 version has no packet content after end of hdrs */
519
520                 if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len &&
521                          atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token) >= 4) {
522                         lwsl_parser("04 header completed\n");
523                         wsi->parser_state = WSI_PARSING_COMPLETE;
524                         wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len = 0;
525                         free(wsi->utf8_token[WSI_TOKEN_CHALLENGE].token);
526                         wsi->utf8_token[WSI_TOKEN_CHALLENGE].token = NULL;
527                 }
528
529                 /* client parser? */
530
531                 lwsl_parser("04 header completed\n");
532                 wsi->parser_state = WSI_PARSING_COMPLETE;
533
534                 break;
535
536                 /* skipping arg part of a name we didn't recognize */
537         case WSI_TOKEN_SKIPPING:
538                 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
539                 if (c == '\x0d')
540                         wsi->parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
541                 break;
542         case WSI_TOKEN_SKIPPING_SAW_CR:
543                 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
544                 if (c == '\x0a') {
545                         wsi->parser_state = WSI_TOKEN_NAME_PART;
546                         wsi->lextable_pos = 0;
547                 } else
548                         wsi->parser_state = WSI_TOKEN_SKIPPING;
549                 wsi->name_buffer_pos = 0;
550                 break;
551                 /* we're done, ignore anything else */
552         case WSI_PARSING_COMPLETE:
553                 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
554                 break;
555
556         default:        /* keep gcc happy */
557                 break;
558         }
559
560         return 0;
561 }
562
563
564 /**
565  * lws_frame_is_binary: true if the current frame was sent in binary mode
566  *
567  * @wsi: the connection we are inquiring about
568  *
569  * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
570  * it's interested to see if the frame it's dealing with was sent in binary
571  * mode.
572  */
573
574 int lws_frame_is_binary(struct libwebsocket *wsi)
575 {
576         return wsi->frame_is_binary;
577 }
578
579 int
580 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c)
581 {
582         int n;
583         struct lws_tokens eff_buf;
584 #ifndef LWS_NO_EXTENSIONS
585         int handled;
586         int m;
587 #endif
588
589 #if 0
590         lwsl_debug("RX: %02X ", c);
591 #endif
592
593         switch (wsi->lws_rx_parse_state) {
594         case LWS_RXPS_NEW:
595
596                 switch (wsi->ietf_spec_revision) {
597                 case 13:
598                         /*
599                          * no prepended frame key any more
600                          */
601                         wsi->all_zero_nonce = 1;
602                         goto handle_first;
603
604                 default:
605                         lwsl_warn("libwebsocket_rx_sm doesn't know "
606                             "about spec version %d\n", wsi->ietf_spec_revision);
607                         break;
608                 }
609                 break;
610         case LWS_RXPS_04_MASK_NONCE_1:
611                 wsi->frame_masking_nonce_04[1] = c;
612                 if (c)
613                         wsi->all_zero_nonce = 0;
614                 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_2;
615                 break;
616         case LWS_RXPS_04_MASK_NONCE_2:
617                 wsi->frame_masking_nonce_04[2] = c;
618                 if (c)
619                         wsi->all_zero_nonce = 0;
620                 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_3;
621                 break;
622         case LWS_RXPS_04_MASK_NONCE_3:
623                 wsi->frame_masking_nonce_04[3] = c;
624                 if (c)
625                         wsi->all_zero_nonce = 0;
626
627                 /*
628                  * start from the zero'th byte in the XOR key buffer since
629                  * this is the start of a frame with a new key
630                  */
631
632                 wsi->frame_mask_index = 0;
633
634                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
635                 break;
636
637         /*
638          *  04 logical framing from the spec (all this is masked when incoming
639          *  and has to be unmasked)
640          *
641          * We ignore the possibility of extension data because we don't
642          * negotiate any extensions at the moment.
643          *
644          *    0                   1                   2                   3
645          *    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
646          *   +-+-+-+-+-------+-+-------------+-------------------------------+
647          *   |F|R|R|R| opcode|R| Payload len |    Extended payload length    |
648          *   |I|S|S|S|  (4)  |S|     (7)     |             (16/63)           |
649          *   |N|V|V|V|       |V|             |   (if payload len==126/127)   |
650          *   | |1|2|3|       |4|             |                               |
651          *   +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
652          *   |     Extended payload length continued, if payload len == 127  |
653          *   + - - - - - - - - - - - - - - - +-------------------------------+
654          *   |                               |         Extension data        |
655          *   +-------------------------------+ - - - - - - - - - - - - - - - +
656          *   :                                                               :
657          *   +---------------------------------------------------------------+
658          *   :                       Application data                        :
659          *   +---------------------------------------------------------------+
660          *
661          *  We pass payload through to userland as soon as we get it, ignoring
662          *  FIN.  It's up to userland to buffer it up if it wants to see a
663          *  whole unfragmented block of the original size (which may be up to
664          *  2^63 long!)
665          */
666
667         case LWS_RXPS_04_FRAME_HDR_1:
668 handle_first:
669
670                 /*
671                  * 04 spec defines the opcode like this: (1, 2, and 3 are
672                  * "control frame" opcodes which may not be fragmented or
673                  * have size larger than 126)
674                  *
675                  *       frame-opcode           =
676                  *             %x0 ; continuation frame
677                  *              / %x1 ; connection close
678                  *              / %x2 ; ping
679                  *              / %x3 ; pong
680                  *              / %x4 ; text frame
681                  *              / %x5 ; binary frame
682                  *              / %x6-F ; reserved
683                  *
684                  *              FIN (b7)
685                  */
686
687                 wsi->opcode = c & 0xf;
688                 wsi->rsv = c & 0x70;
689                 wsi->final = !!((c >> 7) & 1);
690                 switch (wsi->opcode) {
691                 case LWS_WS_OPCODE_07__TEXT_FRAME:
692                 case LWS_WS_OPCODE_07__BINARY_FRAME:
693                         wsi->frame_is_binary = wsi->opcode == LWS_WS_OPCODE_07__BINARY_FRAME;
694                         break;
695                 }
696                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
697                 break;
698
699         case LWS_RXPS_04_FRAME_HDR_LEN:
700
701                 wsi->this_frame_masked = !!(c & 0x80);
702
703                 switch (c & 0x7f) {
704                 case 126:
705                         /* control frames are not allowed to have big lengths */
706                         if (wsi->opcode & 8)
707                                 goto illegal_ctl_length;
708
709                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
710                         break;
711                 case 127:
712                         /* control frames are not allowed to have big lengths */
713                         if (wsi->opcode & 8)
714                                 goto illegal_ctl_length;
715
716                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
717                         break;
718                 default:
719                         wsi->rx_packet_length = c & 0x7f;
720                         if (wsi->this_frame_masked)
721                                 wsi->lws_rx_parse_state =
722                                                 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
723                         else
724                                 wsi->lws_rx_parse_state =
725                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
726                         break;
727                 }
728                 break;
729
730         case LWS_RXPS_04_FRAME_HDR_LEN16_2:
731                 wsi->rx_packet_length = c << 8;
732                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
733                 break;
734
735         case LWS_RXPS_04_FRAME_HDR_LEN16_1:
736                 wsi->rx_packet_length |= c;
737                 if (wsi->this_frame_masked)
738                         wsi->lws_rx_parse_state =
739                                         LWS_RXPS_07_COLLECT_FRAME_KEY_1;
740                 else
741                         wsi->lws_rx_parse_state =
742                                 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
743                 break;
744
745         case LWS_RXPS_04_FRAME_HDR_LEN64_8:
746                 if (c & 0x80) {
747                         lwsl_warn("b63 of length must be zero\n");
748                         /* kill the connection */
749                         return -1;
750                 }
751 #if defined __LP64__
752                 wsi->rx_packet_length = ((size_t)c) << 56;
753 #else
754                 wsi->rx_packet_length = 0;
755 #endif
756                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
757                 break;
758
759         case LWS_RXPS_04_FRAME_HDR_LEN64_7:
760 #if defined __LP64__
761                 wsi->rx_packet_length |= ((size_t)c) << 48;
762 #endif
763                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
764                 break;
765
766         case LWS_RXPS_04_FRAME_HDR_LEN64_6:
767 #if defined __LP64__
768                 wsi->rx_packet_length |= ((size_t)c) << 40;
769 #endif
770                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
771                 break;
772
773         case LWS_RXPS_04_FRAME_HDR_LEN64_5:
774 #if defined __LP64__
775                 wsi->rx_packet_length |= ((size_t)c) << 32;
776 #endif
777                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
778                 break;
779
780         case LWS_RXPS_04_FRAME_HDR_LEN64_4:
781                 wsi->rx_packet_length |= ((size_t)c) << 24;
782                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
783                 break;
784
785         case LWS_RXPS_04_FRAME_HDR_LEN64_3:
786                 wsi->rx_packet_length |= ((size_t)c) << 16;
787                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
788                 break;
789
790         case LWS_RXPS_04_FRAME_HDR_LEN64_2:
791                 wsi->rx_packet_length |= ((size_t)c) << 8;
792                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
793                 break;
794
795         case LWS_RXPS_04_FRAME_HDR_LEN64_1:
796                 wsi->rx_packet_length |= ((size_t)c);
797                 if (wsi->this_frame_masked)
798                         wsi->lws_rx_parse_state =
799                                         LWS_RXPS_07_COLLECT_FRAME_KEY_1;
800                 else
801                         wsi->lws_rx_parse_state =
802                                 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
803                 break;
804
805         case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
806                 wsi->frame_masking_nonce_04[0] = c;
807                 if (c)
808                         wsi->all_zero_nonce = 0;
809                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
810                 break;
811
812         case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
813                 wsi->frame_masking_nonce_04[1] = c;
814                 if (c)
815                         wsi->all_zero_nonce = 0;
816                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
817                 break;
818
819         case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
820                 wsi->frame_masking_nonce_04[2] = c;
821                 if (c)
822                         wsi->all_zero_nonce = 0;
823                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
824                 break;
825
826         case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
827                 wsi->frame_masking_nonce_04[3] = c;
828                 if (c)
829                         wsi->all_zero_nonce = 0;
830                 wsi->lws_rx_parse_state =
831                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
832                 wsi->frame_mask_index = 0;
833                 break;
834
835
836         case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
837
838                 if (wsi->all_zero_nonce)
839                         wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
840                                (wsi->rx_user_buffer_head++)] = c;
841                 else
842                         wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
843                                (wsi->rx_user_buffer_head++)] =
844                 c ^ wsi->frame_masking_nonce_04[(wsi->frame_mask_index++) & 3];
845
846                 if (--wsi->rx_packet_length == 0) {
847                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
848                         goto spill;
849                 }
850                 if (wsi->rx_user_buffer_head != MAX_USER_RX_BUFFER)
851                         break;
852 spill:
853                 /*
854                  * is this frame a control packet we should take care of at this
855                  * layer?  If so service it and hide it from the user callback
856                  */
857
858                 lwsl_parser("spill on %s\n", wsi->protocol->name);
859
860                 switch (wsi->opcode) {
861                 case LWS_WS_OPCODE_07__CLOSE:
862                         /* is this an acknowledgement of our close? */
863                         if (wsi->state == WSI_STATE_AWAITING_CLOSE_ACK) {
864                                 /*
865                                  * fine he has told us he is closing too, let's
866                                  * finish our close
867                                  */
868                                 lwsl_parser("seen client close ack\n");
869                                 return -1;
870                         }
871                         lwsl_parser("server sees client close packet\n");
872                         /* parrot the close packet payload back */
873                         n = libwebsocket_write(wsi, (unsigned char *)
874                            &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
875                                      wsi->rx_user_buffer_head, LWS_WRITE_CLOSE);
876                         if (n)
877                                 lwsl_info("write of close ack failed %d\n", n);
878                         wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY;
879                         /* close the connection */
880                         return -1;
881
882                 case LWS_WS_OPCODE_07__PING:
883                         lwsl_info("received %d byte ping, sending pong\n", wsi->rx_user_buffer_head);
884                         lwsl_hexdump(&wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head);
885                         /* parrot the ping packet payload back as a pong */
886                         n = libwebsocket_write(wsi, (unsigned char *)
887                             &wsi->rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING], wsi->rx_user_buffer_head, LWS_WRITE_PONG);
888                         /* ... then just drop it */
889                         wsi->rx_user_buffer_head = 0;
890                         return 0;
891
892                 case LWS_WS_OPCODE_07__PONG:
893                         /* keep the statistics... */
894                         wsi->pings_vs_pongs--;
895                         /* ... then just drop it */
896                         wsi->rx_user_buffer_head = 0;
897                         return 0;
898
899                 case LWS_WS_OPCODE_07__TEXT_FRAME:
900                 case LWS_WS_OPCODE_07__BINARY_FRAME:
901                 case LWS_WS_OPCODE_07__CONTINUATION:
902                         break;
903
904                 default:
905 #ifndef LWS_NO_EXTENSIONS
906                         lwsl_parser("passing opcode %x up to exts\n", wsi->opcode);
907
908                         /*
909                          * It's something special we can't understand here.
910                          * Pass the payload up to the extension's parsing
911                          * state machine.
912                          */
913
914                         eff_buf.token = &wsi->rx_user_buffer[
915                                                    LWS_SEND_BUFFER_PRE_PADDING];
916                         eff_buf.token_len = wsi->rx_user_buffer_head;
917
918                         handled = 0;
919                         for (n = 0; n < wsi->count_active_extensions; n++) {
920                                 m = wsi->active_extensions[n]->callback(
921                                         wsi->protocol->owning_server,
922                                         wsi->active_extensions[n], wsi,
923                                         LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
924                                             wsi->active_extensions_user[n],
925                                                                    &eff_buf, 0);
926                                 if (m)
927                                         handled = 1;
928                         }
929
930                         if (!handled)
931 #endif
932                                 lwsl_ext("Unhandled extended opcode "
933                                         "0x%x - ignoring frame\n", wsi->opcode);
934
935                         wsi->rx_user_buffer_head = 0;
936                         return 0;
937                 }
938
939                 /*
940                  * No it's real payload, pass it up to the user callback.
941                  * It's nicely buffered with the pre-padding taken care of
942                  * so it can be sent straight out again using libwebsocket_write
943                  */
944
945                 eff_buf.token = &wsi->rx_user_buffer[
946                                                 LWS_SEND_BUFFER_PRE_PADDING];
947                 eff_buf.token_len = wsi->rx_user_buffer_head;
948 #ifndef LWS_NO_EXTENSIONS
949                 for (n = 0; n < wsi->count_active_extensions; n++) {
950                         m = wsi->active_extensions[n]->callback(
951                                 wsi->protocol->owning_server,
952                                 wsi->active_extensions[n], wsi,
953                                 LWS_EXT_CALLBACK_PAYLOAD_RX,
954                                 wsi->active_extensions_user[n],
955                                 &eff_buf, 0);
956                         if (m < 0) {
957                                 lwsl_ext(
958                                   "Extension '%s' failed to handle payload!\n",
959                                               wsi->active_extensions[n]->name);
960                                 return -1;
961                         }
962                 }
963 #endif
964                 if (eff_buf.token_len > 0) {
965                     eff_buf.token[eff_buf.token_len] = '\0';
966
967                     if (wsi->protocol->callback)
968                             user_callback_handle_rxflow(wsi->protocol->callback,
969                                                     wsi->protocol->owning_server,
970                                                     wsi, LWS_CALLBACK_RECEIVE,
971                                                     wsi->user_space,
972                                                     eff_buf.token,
973                                                     eff_buf.token_len);
974                     else
975                             lwsl_err("No callback on payload spill!\n");
976                 }
977
978                 wsi->rx_user_buffer_head = 0;
979                 break;
980         }
981
982         return 0;
983
984 illegal_ctl_length:
985
986         lwsl_warn("Control frame asking for "
987                         "extended length is illegal\n");
988         /* kill the connection */
989         return -1;
990 }
991
992
993 int libwebsocket_interpret_incoming_packet(struct libwebsocket *wsi,
994                                                  unsigned char *buf, size_t len)
995 {
996         size_t n;
997         int m;
998         int clear_rxflow = !!wsi->rxflow_buffer;
999         struct libwebsocket_context *context = wsi->protocol->owning_server;
1000
1001 #if 0
1002         lwsl_parser("received %d byte packet\n", (int)len);
1003         lwsl_hexdump(buf, len);
1004 #endif
1005
1006         if (buf && wsi->rxflow_buffer)
1007                 lwsl_err("!!!! libwebsocket_interpret_incoming_packet: was pending rxflow, data loss\n");
1008
1009         /* let the rx protocol state machine have as much as it needs */
1010
1011         n = 0;
1012         if (!buf) {
1013                 lwsl_info("dumping stored rxflow buffer len %d pos=%d\n", wsi->rxflow_len, wsi->rxflow_pos);
1014                 buf = wsi->rxflow_buffer;
1015                 n = wsi->rxflow_pos;
1016                 len = wsi->rxflow_len;
1017                 /* let's pretend he's already allowing input */
1018                 context->fds[wsi->position_in_fds_table].events |= POLLIN;
1019         }
1020
1021         while (n < len) {
1022                 if (!(context->fds[wsi->position_in_fds_table].events & POLLIN)) {
1023                         /* his RX is flowcontrolled */
1024                         if (!wsi->rxflow_buffer) { /* a new rxflow in effect, buffer it and warn caller */
1025                                 lwsl_info("new rxflow input buffer len %d\n", len - n);
1026                                 wsi->rxflow_buffer = (unsigned char *)malloc(len - n);
1027                                 wsi->rxflow_len = len - n;
1028                                 wsi->rxflow_pos = 0;
1029                                 memcpy(wsi->rxflow_buffer, buf + n, len - n);
1030                         } else {
1031                                 lwsl_info("re-using rxflow input buffer\n");
1032                                 /* rxflow while we were spilling previous rxflow buffer */
1033                                 wsi->rxflow_pos = n;
1034                         }
1035                         return 1;
1036                 }
1037                 m = libwebsocket_rx_sm(wsi, buf[n]);
1038                 if (m < 0)
1039                         return -1;
1040                 n++;
1041         }
1042
1043         if (clear_rxflow) {
1044                 lwsl_info("flow: clearing it\n");
1045                 free(wsi->rxflow_buffer);
1046                 wsi->rxflow_buffer = NULL;
1047                 context->fds[wsi->position_in_fds_table].events &= ~POLLIN;
1048         }
1049
1050         return 0;
1051 }
1052
1053
1054 /**
1055  * libwebsockets_remaining_packet_payload() - Bytes to come before "overall"
1056  *                                            rx packet is complete
1057  * @wsi:                Websocket instance (available from user callback)
1058  *
1059  *      This function is intended to be called from the callback if the
1060  *  user code is interested in "complete packets" from the client.
1061  *  libwebsockets just passes through payload as it comes and issues a buffer
1062  *  additionally when it hits a built-in limit.  The LWS_CALLBACK_RECEIVE
1063  *  callback handler can use this API to find out if the buffer it has just
1064  *  been given is the last piece of a "complete packet" from the client --
1065  *  when that is the case libwebsockets_remaining_packet_payload() will return
1066  *  0.
1067  *
1068  *  Many protocols won't care becuse their packets are always small.
1069  */
1070
1071 size_t
1072 libwebsockets_remaining_packet_payload(struct libwebsocket *wsi)
1073 {
1074         return wsi->rx_packet_length;
1075 }