uriencoding deal with0uriencoded question mark properly
[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, enc = 0;
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                         enc = 1;
290                         wsi->u.hdr.ues = URIES_IDLE;
291                         break;
292                 }
293
294                 /*
295                  * special URI processing...
296                  *  convert /.. or /... or /../ etc to /
297                  *  convert /./ to /
298                  *  convert // or /// etc to /
299                  *  leave /.dir or whatever alone
300                  */
301
302                 switch (wsi->u.hdr.ups) {
303                 case URIPS_IDLE:
304                         /* issue the first / always */
305                         if (c == '/')
306                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
307                         break;
308                 case URIPS_SEEN_SLASH:
309                         /* swallow subsequent slashes */
310                         if (c == '/')
311                                 goto swallow;
312                         /* track and swallow the first . after / */
313                         if (c == '.') {
314                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
315                                 goto swallow;
316                         }
317                         wsi->u.hdr.ups = URIPS_IDLE;
318                         break;
319                 case URIPS_SEEN_SLASH_DOT:
320                         /* swallow second . */
321                         if (c == '.') {
322                                 /*
323                                  * back up one dir level if possible
324                                  * safe against header fragmentation because
325                                  * the method URI can only be in 1 fragment
326                                  */
327                                 if (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 2) {
328                                         wsi->u.hdr.ah->pos--;
329                                         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
330                                         do {
331                                                 wsi->u.hdr.ah->pos--;
332                                                 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
333                                         } while (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 1 &&
334                                                         wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos] != '/');
335                                 }
336                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
337                                 goto swallow;
338                         }
339                         /* change /./ to / */
340                         if (c == '/') {
341                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
342                                 goto swallow;
343                         }
344                         /* it was like /.dir ... regurgitate the . */
345                         wsi->u.hdr.ups = URIPS_IDLE;
346                         issue_char(wsi, '.');
347                         break;
348
349                 case URIPS_SEEN_SLASH_DOT_DOT:
350                         /* swallow prior .. chars and any subsequent . */
351                         if (c == '.')
352                                 goto swallow;
353                         /* last issued was /, so another / == // */
354                         if (c == '/')
355                                 goto swallow;
356                         /* last we issued was / so SEEN_SLASH */
357                         wsi->u.hdr.ups = URIPS_SEEN_SLASH;
358                         break;
359                 case URIPS_ARGUMENTS:
360                         /* leave them alone */
361                         break;
362                 }
363
364                 if (c == '?' && !enc) { /* start of URI arguments */
365                         /* seal off uri header */
366                         wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
367
368                         /* move to using WSI_TOKEN_HTTP_URI_ARGS */
369                         wsi->u.hdr.ah->next_frag_index++;
370                         wsi->u.hdr.ah->frags[
371                                 wsi->u.hdr.ah->next_frag_index].offset =
372                                                              wsi->u.hdr.ah->pos;
373                         wsi->u.hdr.ah->frags[
374                                         wsi->u.hdr.ah->next_frag_index].len = 0;
375                         wsi->u.hdr.ah->frags[
376                             wsi->u.hdr.ah->next_frag_index].next_frag_index = 0;
377
378                         wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] =
379                                                  wsi->u.hdr.ah->next_frag_index;
380
381                         /* defeat normal uri path processing */
382                         wsi->u.hdr.ups = URIPS_ARGUMENTS;
383                         goto swallow;
384                 }
385
386 check_eol:
387
388                 /* bail at EOL */
389                 if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
390                                                                   c == '\x0d') {
391                         c = '\0';
392                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
393                         lwsl_parser("*\n");
394                 }
395
396                 n = issue_char(wsi, c);
397                 if ((int)n < 0)
398                         return -1;
399                 if (n > 0)
400                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
401
402 swallow:
403                 /* per-protocol end of headers management */
404
405                 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
406                         goto set_parsing_complete;
407                 break;
408
409                 /* collecting and checking a name part */
410         case WSI_TOKEN_NAME_PART:
411                 lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode);
412
413                 wsi->u.hdr.lextable_pos =
414                                 lextable_decode(wsi->u.hdr.lextable_pos, c);
415                 /*
416                  * Server needs to look out for unknown methods...
417                  */
418                 if (wsi->u.hdr.lextable_pos < 0 &&
419                     wsi->mode == LWS_CONNMODE_HTTP_SERVING) {
420                         /* this is not a header we know about */
421                         for (m = 0; m < ARRAY_SIZE(methods); m++)
422                                 if (wsi->u.hdr.ah->frag_index[methods[m]]) {
423                                         /*
424                                          * already had the method, no idea what
425                                          * this crap from the client is, ignore
426                                          */
427                                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
428                                         break;
429                                 }
430                         /*
431                          * hm it's an unknown http method from a client in fact,
432                          * treat as dangerous
433                          */
434                         if (m == ARRAY_SIZE(methods)) {
435                                 lwsl_info("Unknown method - dropping\n");
436                                 return -1;
437                         }
438                         break;
439                 }
440                 /*
441                  * ...otherwise for a client, let him ignore unknown headers
442                  * coming from the server
443                  */
444                 if (wsi->u.hdr.lextable_pos < 0) {
445                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
446                         break;
447                 }
448
449                 if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
450                         /* terminal state */
451
452                         n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) |
453                                         lextable[wsi->u.hdr.lextable_pos + 1];
454
455                         lwsl_parser("known hdr %d\n", n);
456                         for (m = 0; m < ARRAY_SIZE(methods); m++)
457                                 if (n == methods[m] &&
458                                                 wsi->u.hdr.ah->frag_index[
459                                                         methods[m]]) {
460                                         lwsl_warn("Duplicated method\n");
461                                         return -1;
462                                 }
463
464                         /*
465                          * WSORIGIN is protocol equiv to ORIGIN,
466                          * JWebSocket likes to send it, map to ORIGIN
467                          */
468                         if (n == WSI_TOKEN_SWORIGIN)
469                                 n = WSI_TOKEN_ORIGIN;
470
471                         wsi->u.hdr.parser_state = (enum lws_token_indexes)
472                                                         (WSI_TOKEN_GET_URI + n);
473
474                         if (context->token_limits)
475                                 wsi->u.hdr.current_token_limit =
476                                         context->token_limits->token_limit[wsi->u.hdr.parser_state];
477                         else
478                                 wsi->u.hdr.current_token_limit = sizeof(wsi->u.hdr.ah->data);
479
480                         if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
481                                 goto set_parsing_complete;
482
483                         goto start_fragment;
484                 }
485                 break;
486
487 start_fragment:
488                 wsi->u.hdr.ah->next_frag_index++;
489                 if (wsi->u.hdr.ah->next_frag_index ==
490                                 sizeof(wsi->u.hdr.ah->frags) /
491                                               sizeof(wsi->u.hdr.ah->frags[0])) {
492                         lwsl_warn("More hdr frags than we can deal with\n");
493                         return -1;
494                 }
495
496                 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset =
497                                                              wsi->u.hdr.ah->pos;
498                 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0;
499                 wsi->u.hdr.ah->frags[
500                             wsi->u.hdr.ah->next_frag_index].next_frag_index = 0;
501
502                 n = wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state];
503                 if (!n) { /* first fragment */
504                         wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state] =
505                                                  wsi->u.hdr.ah->next_frag_index;
506                         break;
507                 }
508                 /* continuation */
509                 while (wsi->u.hdr.ah->frags[n].next_frag_index)
510                                 n = wsi->u.hdr.ah->frags[n].next_frag_index;
511                 wsi->u.hdr.ah->frags[n].next_frag_index =
512                                                  wsi->u.hdr.ah->next_frag_index;
513
514                 if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
515                         lwsl_warn("excessive header content\n");
516                         return -1;
517                 }
518
519                 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = ' ';
520                 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++;
521                 break;
522
523                 /* skipping arg part of a name we didn't recognize */
524         case WSI_TOKEN_SKIPPING:
525                 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
526
527                 if (c == '\x0d')
528                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
529                 break;
530
531         case WSI_TOKEN_SKIPPING_SAW_CR:
532                 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
533                 if (c == '\x0a') {
534                         wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
535                         wsi->u.hdr.lextable_pos = 0;
536                 } else
537                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
538                 break;
539                 /* we're done, ignore anything else */
540
541         case WSI_PARSING_COMPLETE:
542                 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
543                 break;
544         }
545
546         return 0;
547
548 set_parsing_complete:
549
550         if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
551                 if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
552                         wsi->ietf_spec_revision =
553                                atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
554
555                 lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
556         }
557         wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
558         wsi->hdr_parsing_completed = 1;
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 LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
575 {
576         return wsi->u.ws.frame_is_binary;
577 }
578
579 int
580 lws_rx_sm(struct lws *wsi, unsigned char c)
581 {
582         struct lws_tokens eff_buf;
583         int ret = 0;
584         int callback_action = LWS_CALLBACK_RECEIVE;
585
586         switch (wsi->lws_rx_parse_state) {
587         case LWS_RXPS_NEW:
588
589                 switch (wsi->ietf_spec_revision) {
590                 case 13:
591                         /*
592                          * no prepended frame key any more
593                          */
594                         wsi->u.ws.all_zero_nonce = 1;
595                         goto handle_first;
596
597                 default:
598                         lwsl_warn("lws_rx_sm: unknown spec version %d\n",
599                                                        wsi->ietf_spec_revision);
600                         break;
601                 }
602                 break;
603         case LWS_RXPS_04_MASK_NONCE_1:
604                 wsi->u.ws.frame_masking_nonce_04[1] = c;
605                 if (c)
606                         wsi->u.ws.all_zero_nonce = 0;
607                 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_2;
608                 break;
609         case LWS_RXPS_04_MASK_NONCE_2:
610                 wsi->u.ws.frame_masking_nonce_04[2] = c;
611                 if (c)
612                         wsi->u.ws.all_zero_nonce = 0;
613                 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_3;
614                 break;
615         case LWS_RXPS_04_MASK_NONCE_3:
616                 wsi->u.ws.frame_masking_nonce_04[3] = c;
617                 if (c)
618                         wsi->u.ws.all_zero_nonce = 0;
619
620                 /*
621                  * start from the zero'th byte in the XOR key buffer since
622                  * this is the start of a frame with a new key
623                  */
624
625                 wsi->u.ws.frame_mask_index = 0;
626
627                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
628                 break;
629
630         /*
631          *  04 logical framing from the spec (all this is masked when incoming
632          *  and has to be unmasked)
633          *
634          * We ignore the possibility of extension data because we don't
635          * negotiate any extensions at the moment.
636          *
637          *    0                   1                   2                   3
638          *    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
639          *   +-+-+-+-+-------+-+-------------+-------------------------------+
640          *   |F|R|R|R| opcode|R| Payload len |    Extended payload length    |
641          *   |I|S|S|S|  (4)  |S|     (7)     |             (16/63)           |
642          *   |N|V|V|V|       |V|             |   (if payload len==126/127)   |
643          *   | |1|2|3|       |4|             |                               |
644          *   +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
645          *   |     Extended payload length continued, if payload len == 127  |
646          *   + - - - - - - - - - - - - - - - +-------------------------------+
647          *   |                               |         Extension data        |
648          *   +-------------------------------+ - - - - - - - - - - - - - - - +
649          *   :                                                               :
650          *   +---------------------------------------------------------------+
651          *   :                       Application data                        :
652          *   +---------------------------------------------------------------+
653          *
654          *  We pass payload through to userland as soon as we get it, ignoring
655          *  FIN.  It's up to userland to buffer it up if it wants to see a
656          *  whole unfragmented block of the original size (which may be up to
657          *  2^63 long!)
658          */
659
660         case LWS_RXPS_04_FRAME_HDR_1:
661 handle_first:
662
663                 wsi->u.ws.opcode = c & 0xf;
664                 wsi->u.ws.rsv = c & 0x70;
665                 wsi->u.ws.final = !!((c >> 7) & 1);
666
667                 switch (wsi->u.ws.opcode) {
668                 case LWS_WS_OPCODE_07__TEXT_FRAME:
669                 case LWS_WS_OPCODE_07__BINARY_FRAME:
670                         wsi->u.ws.frame_is_binary =
671                              wsi->u.ws.opcode == LWS_WS_OPCODE_07__BINARY_FRAME;
672                         break;
673                 }
674                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
675                 break;
676
677         case LWS_RXPS_04_FRAME_HDR_LEN:
678
679                 wsi->u.ws.this_frame_masked = !!(c & 0x80);
680
681                 switch (c & 0x7f) {
682                 case 126:
683                         /* control frames are not allowed to have big lengths */
684                         if (wsi->u.ws.opcode & 8)
685                                 goto illegal_ctl_length;
686
687                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
688                         break;
689                 case 127:
690                         /* control frames are not allowed to have big lengths */
691                         if (wsi->u.ws.opcode & 8)
692                                 goto illegal_ctl_length;
693
694                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
695                         break;
696                 default:
697                         wsi->u.ws.rx_packet_length = c & 0x7f;
698                         if (wsi->u.ws.this_frame_masked)
699                                 wsi->lws_rx_parse_state =
700                                                 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
701                         else
702                                 if (wsi->u.ws.rx_packet_length)
703                                         wsi->lws_rx_parse_state =
704                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
705                                 else {
706                                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
707                                         goto spill;
708                                 }
709                         break;
710                 }
711                 break;
712
713         case LWS_RXPS_04_FRAME_HDR_LEN16_2:
714                 wsi->u.ws.rx_packet_length = c << 8;
715                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
716                 break;
717
718         case LWS_RXPS_04_FRAME_HDR_LEN16_1:
719                 wsi->u.ws.rx_packet_length |= c;
720                 if (wsi->u.ws.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         case LWS_RXPS_04_FRAME_HDR_LEN64_8:
729                 if (c & 0x80) {
730                         lwsl_warn("b63 of length must be zero\n");
731                         /* kill the connection */
732                         return -1;
733                 }
734 #if defined __LP64__
735                 wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
736 #else
737                 wsi->u.ws.rx_packet_length = 0;
738 #endif
739                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
740                 break;
741
742         case LWS_RXPS_04_FRAME_HDR_LEN64_7:
743 #if defined __LP64__
744                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
745 #endif
746                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
747                 break;
748
749         case LWS_RXPS_04_FRAME_HDR_LEN64_6:
750 #if defined __LP64__
751                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
752 #endif
753                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
754                 break;
755
756         case LWS_RXPS_04_FRAME_HDR_LEN64_5:
757 #if defined __LP64__
758                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
759 #endif
760                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
761                 break;
762
763         case LWS_RXPS_04_FRAME_HDR_LEN64_4:
764                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
765                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
766                 break;
767
768         case LWS_RXPS_04_FRAME_HDR_LEN64_3:
769                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
770                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
771                 break;
772
773         case LWS_RXPS_04_FRAME_HDR_LEN64_2:
774                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
775                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
776                 break;
777
778         case LWS_RXPS_04_FRAME_HDR_LEN64_1:
779                 wsi->u.ws.rx_packet_length |= ((size_t)c);
780                 if (wsi->u.ws.this_frame_masked)
781                         wsi->lws_rx_parse_state =
782                                         LWS_RXPS_07_COLLECT_FRAME_KEY_1;
783                 else
784                         wsi->lws_rx_parse_state =
785                                 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
786                 break;
787
788         case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
789                 wsi->u.ws.frame_masking_nonce_04[0] = c;
790                 if (c)
791                         wsi->u.ws.all_zero_nonce = 0;
792                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
793                 break;
794
795         case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
796                 wsi->u.ws.frame_masking_nonce_04[1] = c;
797                 if (c)
798                         wsi->u.ws.all_zero_nonce = 0;
799                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
800                 break;
801
802         case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
803                 wsi->u.ws.frame_masking_nonce_04[2] = c;
804                 if (c)
805                         wsi->u.ws.all_zero_nonce = 0;
806                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
807                 break;
808
809         case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
810                 wsi->u.ws.frame_masking_nonce_04[3] = c;
811                 if (c)
812                         wsi->u.ws.all_zero_nonce = 0;
813                 wsi->lws_rx_parse_state =
814                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
815                 wsi->u.ws.frame_mask_index = 0;
816                 if (wsi->u.ws.rx_packet_length == 0) {
817                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
818                         goto spill;
819                 }
820                 break;
821
822
823         case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
824
825                 if (!wsi->u.ws.rx_user_buffer) {
826                         lwsl_err("NULL user buffer...\n");
827                         return 1;
828                 }
829
830                 if (wsi->u.ws.all_zero_nonce)
831                         wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
832                                (wsi->u.ws.rx_user_buffer_head++)] = c;
833                 else
834                         wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
835                                (wsi->u.ws.rx_user_buffer_head++)] =
836                                    c ^ wsi->u.ws.frame_masking_nonce_04[
837                                             (wsi->u.ws.frame_mask_index++) & 3];
838
839                 if (--wsi->u.ws.rx_packet_length == 0) {
840                         /* spill because we have the whole frame */
841                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
842                         goto spill;
843                 }
844
845                 /*
846                  * if there's no protocol max frame size given, we are
847                  * supposed to default to LWS_MAX_SOCKET_IO_BUF
848                  */
849
850                 if (!wsi->protocol->rx_buffer_size &&
851                                         wsi->u.ws.rx_user_buffer_head !=
852                                                           LWS_MAX_SOCKET_IO_BUF)
853                         break;
854                 else
855                         if (wsi->protocol->rx_buffer_size &&
856                                         wsi->u.ws.rx_user_buffer_head !=
857                                                   wsi->protocol->rx_buffer_size)
858                         break;
859
860                 /* spill because we filled our rx buffer */
861 spill:
862                 /*
863                  * is this frame a control packet we should take care of at this
864                  * layer?  If so service it and hide it from the user callback
865                  */
866
867                 lwsl_parser("spill on %s\n", wsi->protocol->name);
868
869                 switch (wsi->u.ws.opcode) {
870                 case LWS_WS_OPCODE_07__CLOSE:
871                         /* is this an acknowledgement of our close? */
872                         if (wsi->state == WSI_STATE_AWAITING_CLOSE_ACK) {
873                                 /*
874                                  * fine he has told us he is closing too, let's
875                                  * finish our close
876                                  */
877                                 lwsl_parser("seen client close ack\n");
878                                 return -1;
879                         }
880                         if (wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY)
881                                 /* if he sends us 2 CLOSE, kill him */
882                                 return -1;
883
884                         lwsl_parser("server sees client close packet\n");
885                         wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY;
886                         /* deal with the close packet contents as a PONG */
887                         wsi->u.ws.payload_is_close = 1;
888                         goto process_as_ping;
889
890                 case LWS_WS_OPCODE_07__PING:
891                         lwsl_info("received %d byte ping, sending pong\n",
892                                                  wsi->u.ws.rx_user_buffer_head);
893
894                         if (wsi->u.ws.ping_pending_flag) {
895                                 /*
896                                  * there is already a pending ping payload
897                                  * we should just log and drop
898                                  */
899                                 lwsl_parser("DROP PING since one pending\n");
900                                 goto ping_drop;
901                         }
902 process_as_ping:
903                         /* control packets can only be < 128 bytes long */
904                         if (wsi->u.ws.rx_user_buffer_head > 128 - 4) {
905                                 lwsl_parser("DROP PING payload too large\n");
906                                 goto ping_drop;
907                         }
908
909                         /* if existing buffer is too small, drop it */
910                         if (wsi->u.ws.ping_payload_buf &&
911                             wsi->u.ws.ping_payload_alloc < wsi->u.ws.rx_user_buffer_head) {
912                                 lws_free2(wsi->u.ws.ping_payload_buf);
913                         }
914
915                         /* if no buffer, allocate it */
916                         if (!wsi->u.ws.ping_payload_buf) {
917                                 wsi->u.ws.ping_payload_buf = lws_malloc(wsi->u.ws.rx_user_buffer_head
918                                                                         + LWS_SEND_BUFFER_PRE_PADDING);
919                                 wsi->u.ws.ping_payload_alloc = wsi->u.ws.rx_user_buffer_head;
920                         }
921
922                         /* stash the pong payload */
923                         memcpy(wsi->u.ws.ping_payload_buf + LWS_SEND_BUFFER_PRE_PADDING,
924                                &wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
925                                 wsi->u.ws.rx_user_buffer_head);
926
927                         wsi->u.ws.ping_payload_len = wsi->u.ws.rx_user_buffer_head;
928                         wsi->u.ws.ping_pending_flag = 1;
929
930                         /* get it sent as soon as possible */
931                         lws_callback_on_writable(lws_get_ctx(wsi), wsi);
932 ping_drop:
933                         wsi->u.ws.rx_user_buffer_head = 0;
934                         return 0;
935
936                 case LWS_WS_OPCODE_07__PONG:
937                         lwsl_info("received pong\n");
938                         lwsl_hexdump(&wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
939                                      wsi->u.ws.rx_user_buffer_head);
940
941                         /* issue it */
942                         callback_action = LWS_CALLBACK_RECEIVE_PONG;
943                         break;
944
945                 case LWS_WS_OPCODE_07__TEXT_FRAME:
946                 case LWS_WS_OPCODE_07__BINARY_FRAME:
947                 case LWS_WS_OPCODE_07__CONTINUATION:
948                         break;
949
950                 default:
951                         lwsl_parser("passing opc %x up to exts\n",
952                                                         wsi->u.ws.opcode);
953                         /*
954                          * It's something special we can't understand here.
955                          * Pass the payload up to the extension's parsing
956                          * state machine.
957                          */
958
959                         eff_buf.token = &wsi->u.ws.rx_user_buffer[
960                                                    LWS_SEND_BUFFER_PRE_PADDING];
961                         eff_buf.token_len = wsi->u.ws.rx_user_buffer_head;
962
963                         if (lws_ext_callback_for_each_active(wsi,
964                                 LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
965                                         &eff_buf, 0) <= 0) /* not handle or fail */
966                                 lwsl_ext("ext opc opcode 0x%x unknown\n",
967                                                               wsi->u.ws.opcode);
968
969                         wsi->u.ws.rx_user_buffer_head = 0;
970                         return 0;
971                 }
972
973                 /*
974                  * No it's real payload, pass it up to the user callback.
975                  * It's nicely buffered with the pre-padding taken care of
976                  * so it can be sent straight out again using lws_write
977                  */
978
979                 eff_buf.token = &wsi->u.ws.rx_user_buffer[
980                                                 LWS_SEND_BUFFER_PRE_PADDING];
981                 eff_buf.token_len = wsi->u.ws.rx_user_buffer_head;
982
983                 if (lws_ext_callback_for_each_active(wsi,
984                                 LWS_EXT_CALLBACK_PAYLOAD_RX, &eff_buf, 0) < 0)
985                         return -1;
986
987                 if (eff_buf.token_len > 0 ||
988                     callback_action == LWS_CALLBACK_RECEIVE_PONG) {
989                         eff_buf.token[eff_buf.token_len] = '\0';
990
991                         if (wsi->protocol->callback) {
992
993                                 if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
994                                     lwsl_info("Doing pong callback\n");
995
996                                 ret = user_callback_handle_rxflow(
997                                                 wsi->protocol->callback,
998                                                 lws_get_ctx(wsi),
999                                                 wsi,
1000                                                 (enum lws_callback_reasons)callback_action,
1001                                                 wsi->user_space,
1002                                                 eff_buf.token,
1003                                                 eff_buf.token_len);
1004                         }
1005                         else
1006                                 lwsl_err("No callback on payload spill!\n");
1007                 }
1008
1009                 wsi->u.ws.rx_user_buffer_head = 0;
1010                 break;
1011         }
1012
1013         return ret;
1014
1015 illegal_ctl_length:
1016
1017         lwsl_warn("Control frame with xtended length is illegal\n");
1018         /* kill the connection */
1019         return -1;
1020 }
1021
1022
1023 /**
1024  * lws_remaining_packet_payload() - Bytes to come before "overall"
1025  *                                            rx packet is complete
1026  * @wsi:                Websocket instance (available from user callback)
1027  *
1028  *      This function is intended to be called from the callback if the
1029  *  user code is interested in "complete packets" from the client.
1030  *  libwebsockets just passes through payload as it comes and issues a buffer
1031  *  additionally when it hits a built-in limit.  The LWS_CALLBACK_RECEIVE
1032  *  callback handler can use this API to find out if the buffer it has just
1033  *  been given is the last piece of a "complete packet" from the client --
1034  *  when that is the case lws_remaining_packet_payload() will return
1035  *  0.
1036  *
1037  *  Many protocols won't care becuse their packets are always small.
1038  */
1039
1040 LWS_VISIBLE size_t
1041 lws_remaining_packet_payload(struct lws *wsi)
1042 {
1043         return wsi->u.ws.rx_packet_length;
1044 }