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