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