urldecode forbid malformed
[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 LWS_WARN_UNUSED_RESULT
31 lextable_decode(int pos, char c)
32 {
33         if (c >= 'A' && c <= 'Z')
34                 c += 'a' - 'A';
35
36         while (1) {
37                 if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */
38                         if ((lextable[pos] & 0x7f) != c)
39                                 return -1;
40                         /* fall thru */
41                         pos++;
42                         if (lextable[pos] == FAIL_CHAR)
43                                 return -1;
44                         return pos;
45                 }
46
47                 if (lextable[pos] == FAIL_CHAR)
48                         return -1;
49
50                 /* b7 = 0, end or 3-byte */
51                 if (lextable[pos] < FAIL_CHAR) /* terminal marker */
52                         return pos;
53
54                 if (lextable[pos] == c) /* goto */
55                         return pos + (lextable[pos + 1]) +
56                                                 (lextable[pos + 2] << 8);
57                 /* fall thru goto */
58                 pos += 3;
59                 /* continue */
60         }
61 }
62
63 void
64 lws_header_table_reset(struct lws *wsi, int autoservice)
65 {
66         struct allocated_headers *ah = wsi->u.hdr.ah;
67         struct lws_context_per_thread *pt;
68         struct lws_pollfd *pfd;
69
70         /* if we have the idea we're resetting 'our' ah, must be bound to one */
71         assert(ah);
72         /* ah also concurs with ownership */
73         assert(ah->wsi == wsi);
74
75         /* init the ah to reflect no headers or data have appeared yet */
76         memset(ah->frag_index, 0, sizeof(ah->frag_index));
77         ah->nfrag = 0;
78         ah->pos = 0;
79
80         /* and reset the rx state */
81         ah->rxpos = 0;
82         ah->rxlen = 0;
83
84         /* since we will restart the ah, our new headers are not completed */
85         wsi->hdr_parsing_completed = 0;
86
87         /*
88          * if we inherited pending rx (from socket adoption deferred
89          * processing), apply and free it.
90          */
91         if (wsi->u.hdr.preamble_rx) {
92                 memcpy(ah->rx, wsi->u.hdr.preamble_rx,
93                        wsi->u.hdr.preamble_rx_len);
94                 ah->rxlen = wsi->u.hdr.preamble_rx_len;
95                 lws_free_set_NULL(wsi->u.hdr.preamble_rx);
96
97                 if (autoservice) {
98                         lwsl_notice("%s: calling service on readbuf ah\n", __func__);
99
100                         pt = &wsi->context->pt[(int)wsi->tsi];
101
102                         /* unlike a normal connect, we have the headers already
103                          * (or the first part of them anyway)
104                          */
105                         pfd = &pt->fds[wsi->position_in_fds_table];
106                         pfd->revents |= LWS_POLLIN;
107                         lwsl_err("%s: calling service\n", __func__);
108                         lws_service_fd_tsi(wsi->context, pfd, wsi->tsi);
109                 }
110         }
111 }
112
113 int LWS_WARN_UNUSED_RESULT
114 lws_header_table_attach(struct lws *wsi, int autoservice)
115 {
116         struct lws_context *context = wsi->context;
117         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
118         struct lws_pollargs pa;
119         struct lws **pwsi;
120         int n;
121
122         lwsl_info("%s: wsi %p: ah %p (tsi %d)\n", __func__, (void *)wsi,
123                  (void *)wsi->u.hdr.ah, wsi->tsi);
124
125         /* if we are already bound to one, just clear it down */
126         if (wsi->u.hdr.ah) {
127                 lwsl_info("cleardown\n");
128                 goto reset;
129         }
130
131         lws_pt_lock(pt);
132         pwsi = &pt->ah_wait_list;
133         while (*pwsi) {
134                 if (*pwsi == wsi) {
135                         /* if already waiting on list, if no new ah just ret */
136                         if (pt->ah_count_in_use ==
137                             context->max_http_header_pool) {
138                                 lwsl_err("ah wl denied\n");
139                                 goto bail;
140                         }
141                         /* new ah.... remove ourselves from waiting list */
142                         *pwsi = wsi->u.hdr.ah_wait_list; /* set our prev to our next */
143                         wsi->u.hdr.ah_wait_list = NULL; /* no next any more */
144                         pt->ah_wait_list_length--;
145                         break;
146                 }
147                 pwsi = &(*pwsi)->u.hdr.ah_wait_list;
148         }
149         /*
150          * pool is all busy... add us to waiting list and return that we
151          * weren't able to deliver it right now
152          */
153         if (pt->ah_count_in_use == context->max_http_header_pool) {
154                 lwsl_info("%s: adding %p to ah waiting list\n", __func__, wsi);
155                 wsi->u.hdr.ah_wait_list = pt->ah_wait_list;
156                 pt->ah_wait_list = wsi;
157                 pt->ah_wait_list_length++;
158
159                 /* we cannot accept input then */
160
161                 _lws_change_pollfd(wsi, LWS_POLLIN, 0, &pa);
162                 goto bail;
163         }
164
165         for (n = 0; n < context->max_http_header_pool; n++)
166                 if (!pt->ah_pool[n].in_use)
167                         break;
168
169         /* if the count of in use said something free... */
170         assert(n != context->max_http_header_pool);
171
172         wsi->u.hdr.ah = &pt->ah_pool[n];
173         wsi->u.hdr.ah->in_use = 1;
174         pt->ah_pool[n].wsi = wsi; /* mark our owner */
175         pt->ah_count_in_use++;
176
177         _lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa);
178
179         lwsl_info("%s: wsi %p: ah %p: count %d (on exit)\n", __func__,
180                   (void *)wsi, (void *)wsi->u.hdr.ah, pt->ah_count_in_use);
181
182         lws_pt_unlock(pt);
183
184 reset:
185         lws_header_table_reset(wsi, autoservice);
186         time(&wsi->u.hdr.ah->assigned);
187
188 #ifndef LWS_NO_CLIENT
189         if (wsi->state == LWSS_CLIENT_UNCONNECTED)
190                 lws_client_connect_via_info2(wsi);
191 #endif
192
193         return 0;
194
195 bail:
196         lws_pt_unlock(pt);
197
198         return 1;
199 }
200
201 int lws_header_table_detach(struct lws *wsi, int autoservice)
202 {
203         struct lws_context *context = wsi->context;
204         struct allocated_headers *ah = wsi->u.hdr.ah;
205         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
206         struct lws_pollargs pa;
207         struct lws **pwsi;
208         time_t now;
209
210         lwsl_info("%s: wsi %p: ah %p (tsi=%d, count = %d)\n", __func__,
211                   (void *)wsi, (void *)wsi->u.hdr.ah, wsi->tsi,
212                   pt->ah_count_in_use);
213
214         if (wsi->u.hdr.preamble_rx)
215                 lws_free_set_NULL(wsi->u.hdr.preamble_rx);
216
217         /* may not be detached while he still has unprocessed rx */
218         if (ah && ah->rxpos != ah->rxlen) {
219                 lwsl_err("%s: %p: rxpos:%d, rxlen:%d\n", __func__, wsi,
220                                 ah->rxpos, ah->rxlen);
221                 assert(ah->rxpos == ah->rxlen);
222         }
223
224         lws_pt_lock(pt);
225
226         pwsi = &pt->ah_wait_list;
227         if (!ah) { /* remove from wait list if none attached */
228                 while (*pwsi) {
229                         if (*pwsi == wsi) {
230                                 lwsl_info("%s: wsi %p, remv wait\n",
231                                           __func__, wsi);
232                                 *pwsi = wsi->u.hdr.ah_wait_list;
233                                 wsi->u.hdr.ah_wait_list = NULL;
234                                 pt->ah_wait_list_length--;
235                                 goto bail;
236                         }
237                         pwsi = &(*pwsi)->u.hdr.ah_wait_list;
238                 }
239                 /* no ah, not on list... no more business here */
240                 goto bail;
241         }
242         /* we did have an ah attached */
243         time(&now);
244         if (now - wsi->u.hdr.ah->assigned > 3) {
245                 /*
246                  * we're detaching the ah, but it was held an
247                  * unreasonably long time
248                  */
249                 lwsl_notice("%s: wsi %p: ah held %ds, "
250                             "ah.rxpos %d, ah.rxlen %d, mode/state %d %d,"
251                             "wsi->more_rx_waiting %d\n", __func__, wsi,
252                             (int)(now - wsi->u.hdr.ah->assigned),
253                             ah->rxpos, ah->rxlen, wsi->mode, wsi->state,
254                             wsi->more_rx_waiting);
255         }
256
257         /* if we think we're detaching one, there should be one in use */
258         assert(pt->ah_count_in_use > 0);
259         /* and this specific one should have been in use */
260         assert(wsi->u.hdr.ah->in_use);
261         wsi->u.hdr.ah = NULL;
262         ah->wsi = NULL; /* no owner */
263
264         /* oh there is nobody on the waiting list... leave it at that then */
265         if (!*pwsi) {
266                 ah->in_use = 0;
267                 pt->ah_count_in_use--;
268
269                 goto bail;
270         }
271
272         /* somebody else on same tsi is waiting, give it to oldest guy */
273
274         lwsl_info("pt wait list %p\n", *pwsi);
275         while ((*pwsi)->u.hdr.ah_wait_list)
276                 pwsi = &(*pwsi)->u.hdr.ah_wait_list;
277
278         wsi = *pwsi;
279         lwsl_info("last wsi in wait list %p\n", wsi);
280
281         wsi->u.hdr.ah = ah;
282         ah->wsi = wsi; /* new owner */
283         lws_header_table_reset(wsi, autoservice);
284         time(&wsi->u.hdr.ah->assigned);
285
286         assert(wsi->position_in_fds_table != -1);
287
288         lwsl_info("%s: Enabling %p POLLIN\n", __func__, wsi);
289
290         /* he has been stuck waiting for an ah, but now his wait is over,
291          * let him progress
292          */
293         _lws_change_pollfd(wsi, 0, LWS_POLLIN, &pa);
294
295         /* point prev guy to next guy in list instead */
296         *pwsi = wsi->u.hdr.ah_wait_list;
297         /* the guy who got one is out of the list */
298         wsi->u.hdr.ah_wait_list = NULL;
299         pt->ah_wait_list_length--;
300
301 #ifndef LWS_NO_CLIENT
302         if (wsi->state == LWSS_CLIENT_UNCONNECTED)
303                 lws_client_connect_via_info2(wsi);
304 #endif
305
306         assert(!!pt->ah_wait_list_length == !!(int)(long)pt->ah_wait_list);
307 bail:
308         lws_pt_unlock(pt);
309
310         return 0;
311 }
312
313 /**
314  * lws_hdr_fragment_length: report length of a single fragment of a header
315  *              The returned length does not include the space for a
316  *              terminating '\0'
317  *
318  * @wsi: websocket connection
319  * @h: which header index we are interested in
320  * @frag_idx: which fragment of @h we want to get the length of
321  */
322
323 LWS_VISIBLE int
324 lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx)
325 {
326         int n;
327
328         n = wsi->u.hdr.ah->frag_index[h];
329         if (!n)
330                 return 0;
331         do {
332                 if (!frag_idx)
333                         return wsi->u.hdr.ah->frags[n].len;
334                 n = wsi->u.hdr.ah->frags[n].nfrag;
335         } while (frag_idx-- && n);
336
337         return 0;
338 }
339
340 /**
341  * lws_hdr_total_length: report length of all fragments of a header totalled up
342  *              The returned length does not include the space for a
343  *              terminating '\0'
344  *
345  * @wsi: websocket connection
346  * @h: which header index we are interested in
347  */
348
349 LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h)
350 {
351         int n;
352         int len = 0;
353
354         n = wsi->u.hdr.ah->frag_index[h];
355         if (!n)
356                 return 0;
357         do {
358                 len += wsi->u.hdr.ah->frags[n].len;
359                 n = wsi->u.hdr.ah->frags[n].nfrag;
360         } while (n);
361
362         return len;
363 }
364
365 /**
366  * lws_hdr_copy_fragment: copy a single fragment of the given header to a buffer
367  *              The buffer length @len must include space for an additional
368  *              terminating '\0', or it will fail returning -1.
369  *              If the requested fragment index is not present, it fails
370  *              returning -1.
371  *
372  * @wsi: websocket connection
373  * @dst: destination buffer
374  * @len: length of destination buffer
375  * @h: which header index we are interested in
376  * @frag_index: which fragment of @h we want to copy
377  */
378
379 LWS_VISIBLE int lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len,
380                                       enum lws_token_indexes h, int frag_idx)
381 {
382         int n = 0;
383         int f = wsi->u.hdr.ah->frag_index[h];
384
385         if (!f)
386                 return -1;
387
388         while (n < frag_idx) {
389                 f = wsi->u.hdr.ah->frags[f].nfrag;
390                 if (!f)
391                         return -1;
392                 n++;
393         }
394
395         if (wsi->u.hdr.ah->frags[f].len >= len)
396                 return -1;
397
398         memcpy(dst, wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[f].offset,
399                wsi->u.hdr.ah->frags[f].len);
400         dst[wsi->u.hdr.ah->frags[f].len] = '\0';
401
402         return wsi->u.hdr.ah->frags[f].len;
403 }
404
405 /**
406  * lws_hdr_copy: copy a single fragment of the given header to a buffer
407  *              The buffer length @len must include space for an additional
408  *              terminating '\0', or it will fail returning -1.
409  *
410  * @wsi: websocket connection
411  * @dst: destination buffer
412  * @len: length of destination buffer
413  * @h: which header index we are interested in
414  */
415
416 LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dst, int len,
417                              enum lws_token_indexes h)
418 {
419         int toklen = lws_hdr_total_length(wsi, h);
420         int n;
421
422         if (toklen >= len)
423                 return -1;
424
425         n = wsi->u.hdr.ah->frag_index[h];
426         if (!n)
427                 return 0;
428
429         do {
430                 strcpy(dst, &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]);
431                 dst += wsi->u.hdr.ah->frags[n].len;
432                 n = wsi->u.hdr.ah->frags[n].nfrag;
433         } while (n);
434
435         return toklen;
436 }
437
438 char *lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h)
439 {
440         int n;
441
442         n = wsi->u.hdr.ah->frag_index[h];
443         if (!n)
444                 return NULL;
445
446         return wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[n].offset;
447 }
448
449 int LWS_WARN_UNUSED_RESULT
450 lws_pos_in_bounds(struct lws *wsi)
451 {
452         if (wsi->u.hdr.ah->pos < wsi->context->max_http_header_data)
453                 return 0;
454
455         if (wsi->u.hdr.ah->pos == wsi->context->max_http_header_data) {
456                 lwsl_err("Ran out of header data space\n");
457                 return 1;
458         }
459
460         /*
461          * with these tests everywhere, it should never be able to exceed
462          * the limit, only meet the limit
463          */
464
465         lwsl_err("%s: pos %d, limit %d\n", __func__, wsi->u.hdr.ah->pos,
466                  wsi->context->max_http_header_data);
467         assert(0);
468
469         return 1;
470 }
471
472 int LWS_WARN_UNUSED_RESULT
473 lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h, const char *s)
474 {
475         wsi->u.hdr.ah->nfrag++;
476         if (wsi->u.hdr.ah->nfrag == ARRAY_SIZE(wsi->u.hdr.ah->frags)) {
477                 lwsl_warn("More hdr frags than we can deal with, dropping\n");
478                 return -1;
479         }
480
481         wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->nfrag;
482
483         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].offset = wsi->u.hdr.ah->pos;
484         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len = 0;
485         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].nfrag = 0;
486
487         do {
488                 if (lws_pos_in_bounds(wsi))
489                         return -1;
490
491                 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s;
492                 if (*s)
493                         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
494         } while (*s++);
495
496         return 0;
497 }
498
499 signed char char_to_hex(const char c)
500 {
501         if (c >= '0' && c <= '9')
502                 return c - '0';
503
504         if (c >= 'a' && c <= 'f')
505                 return c - 'a' + 10;
506
507         if (c >= 'A' && c <= 'F')
508                 return c - 'A' + 10;
509
510         return -1;
511 }
512
513 static int LWS_WARN_UNUSED_RESULT
514 issue_char(struct lws *wsi, unsigned char c)
515 {
516         unsigned short frag_len;
517
518         if (lws_pos_in_bounds(wsi))
519                 return -1;
520
521         frag_len = wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len;
522         /*
523          * If we haven't hit the token limit, just copy the character into
524          * the header
525          */
526         if (frag_len < wsi->u.hdr.current_token_limit) {
527                 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c;
528                 if (c)
529                         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
530                 return 0;
531         }
532
533         /* Insert a null character when we *hit* the limit: */
534         if (frag_len == wsi->u.hdr.current_token_limit) {
535                 if (lws_pos_in_bounds(wsi))
536                         return -1;
537                 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
538                 lwsl_warn("header %i exceeds limit %d\n",
539                           wsi->u.hdr.parser_state,
540                           wsi->u.hdr.current_token_limit);
541         }
542
543         return 1;
544 }
545
546 int LWS_WARN_UNUSED_RESULT
547 lws_parse(struct lws *wsi, unsigned char c)
548 {
549         static const unsigned char methods[] = {
550                 WSI_TOKEN_GET_URI,
551                 WSI_TOKEN_POST_URI,
552                 WSI_TOKEN_OPTIONS_URI,
553                 WSI_TOKEN_PUT_URI,
554                 WSI_TOKEN_PATCH_URI,
555                 WSI_TOKEN_DELETE_URI,
556         };
557         struct allocated_headers *ah = wsi->u.hdr.ah;
558         struct lws_context *context = wsi->context;
559         unsigned int n, m, enc = 0;
560
561         assert(wsi->u.hdr.ah);
562
563         switch (wsi->u.hdr.parser_state) {
564         default:
565
566                 lwsl_parser("WSI_TOK_(%d) '%c'\n", wsi->u.hdr.parser_state, c);
567
568                 /* collect into malloc'd buffers */
569                 /* optional initial space swallow */
570                 if (!ah->frags[ah->frag_index[wsi->u.hdr.parser_state]].len &&
571                     c == ' ')
572                         break;
573
574                 for (m = 0; m < ARRAY_SIZE(methods); m++)
575                         if (wsi->u.hdr.parser_state == methods[m])
576                                 break;
577                 if (m == ARRAY_SIZE(methods))
578                         /* it was not any of the methods */
579                         goto check_eol;
580
581                 /* special URI processing... end at space */
582
583                 if (c == ' ') {
584                         /* enforce starting with / */
585                         if (!ah->frags[ah->nfrag].len)
586                                 if (issue_char(wsi, '/') < 0)
587                                         return -1;
588
589                         if (wsi->u.hdr.ups == URIPS_SEEN_SLASH_DOT_DOT) {
590                                 /*
591                                  * back up one dir level if possible
592                                  * safe against header fragmentation because
593                                  * the method URI can only be in 1 fragment
594                                  */
595                                 if (ah->frags[ah->nfrag].len > 2) {
596                                         ah->pos--;
597                                         ah->frags[ah->nfrag].len--;
598                                         do {
599                                                 ah->pos--;
600                                                 ah->frags[ah->nfrag].len--;
601                                         } while (ah->frags[ah->nfrag].len > 1 &&
602                                                  ah->data[ah->pos] != '/');
603                                 }
604                         }
605
606                         /* begin parsing HTTP version: */
607                         if (issue_char(wsi, '\0') < 0)
608                                 return -1;
609                         wsi->u.hdr.parser_state = WSI_TOKEN_HTTP;
610                         goto start_fragment;
611                 }
612
613                 /*
614                  * PRIORITY 1
615                  * special URI processing... convert %xx
616                  */
617
618                 switch (wsi->u.hdr.ues) {
619                 case URIES_IDLE:
620                         if (c == '%') {
621                                 wsi->u.hdr.ues = URIES_SEEN_PERCENT;
622                                 goto swallow;
623                         }
624                         break;
625                 case URIES_SEEN_PERCENT:
626                         if (char_to_hex(c) < 0)
627                                 /* illegal post-% char */
628                                 goto forbid;
629
630                         wsi->u.hdr.esc_stash = c;
631                         wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1;
632                         goto swallow;
633
634                 case URIES_SEEN_PERCENT_H1:
635                         if (char_to_hex(c) < 0)
636                                 /* illegal post-% char */
637                                 goto forbid;
638
639                         c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) |
640                                         char_to_hex(c);
641                         enc = 1;
642                         wsi->u.hdr.ues = URIES_IDLE;
643                         break;
644                 }
645
646                 /*
647                  * PRIORITY 2
648                  * special URI processing...
649                  *  convert /.. or /... or /../ etc to /
650                  *  convert /./ to /
651                  *  convert // or /// etc to /
652                  *  leave /.dir or whatever alone
653                  */
654
655                 switch (wsi->u.hdr.ups) {
656                 case URIPS_IDLE:
657                         if (!c)
658                                 return -1;
659                         /* genuine delimiter */
660                         if ((c == '&' || c == ';') && !enc) {
661                                 if (issue_char(wsi, c) < 0)
662                                         return -1;
663                                 /* swallow the terminator */
664                                 ah->frags[ah->nfrag].len--;
665                                 /* link to next fragment */
666                                 ah->frags[ah->nfrag].nfrag = ah->nfrag + 1;
667                                 ah->nfrag++;
668                                 if (ah->nfrag >= ARRAY_SIZE(ah->frags))
669                                         goto excessive;
670                                 /* start next fragment after the & */
671                                 wsi->u.hdr.post_literal_equal = 0;
672                                 ah->frags[ah->nfrag].offset = ah->pos;
673                                 ah->frags[ah->nfrag].len = 0;
674                                 ah->frags[ah->nfrag].nfrag = 0;
675                                 goto swallow;
676                         }
677                         /* uriencoded = in the name part, disallow */
678                         if (c == '=' && enc && !wsi->u.hdr.post_literal_equal)
679                                 c = '_';
680
681                         /* after the real =, we don't care how many = */
682                         if (c == '=' && !enc)
683                                 wsi->u.hdr.post_literal_equal = 1;
684
685                         /* + to space */
686                         if (c == '+' && !enc)
687                                 c = ' ';
688                         /* issue the first / always */
689                         if (c == '/' && !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS])
690                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
691                         break;
692                 case URIPS_SEEN_SLASH:
693                         /* swallow subsequent slashes */
694                         if (c == '/')
695                                 goto swallow;
696                         /* track and swallow the first . after / */
697                         if (c == '.') {
698                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
699                                 goto swallow;
700                         }
701                         wsi->u.hdr.ups = URIPS_IDLE;
702                         break;
703                 case URIPS_SEEN_SLASH_DOT:
704                         /* swallow second . */
705                         if (c == '.') {
706                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
707                                 goto swallow;
708                         }
709                         /* change /./ to / */
710                         if (c == '/') {
711                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
712                                 goto swallow;
713                         }
714                         /* it was like /.dir ... regurgitate the . */
715                         wsi->u.hdr.ups = URIPS_IDLE;
716                         if (issue_char(wsi, '.') < 0)
717                                 return -1;
718                         break;
719
720                 case URIPS_SEEN_SLASH_DOT_DOT:
721
722                         /* /../ or /..[End of URI] --> backup to last / */
723                         if (c == '/' || c == '?') {
724                                 /*
725                                  * back up one dir level if possible
726                                  * safe against header fragmentation because
727                                  * the method URI can only be in 1 fragment
728                                  */
729                                 if (ah->frags[ah->nfrag].len > 2) {
730                                         ah->pos--;
731                                         ah->frags[ah->nfrag].len--;
732                                         do {
733                                                 ah->pos--;
734                                                 ah->frags[ah->nfrag].len--;
735                                         } while (ah->frags[ah->nfrag].len > 1 &&
736                                                  ah->data[ah->pos] != '/');
737                                 }
738                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
739                                 if (ah->frags[ah->nfrag].len > 1)
740                                         break;
741                                 goto swallow;
742                         }
743
744                         /*  /..[^/] ... regurgitate and allow */
745
746                         if (issue_char(wsi, '.') < 0)
747                                 return -1;
748                         if (issue_char(wsi, '.') < 0)
749                                 return -1;
750                         wsi->u.hdr.ups = URIPS_IDLE;
751                         break;
752                 }
753
754                 if (c == '?' && !enc &&
755                     !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS]) { /* start of URI arguments */
756                         if (wsi->u.hdr.ues != URIES_IDLE)
757                                 goto forbid;
758
759                         /* seal off uri header */
760                         if (issue_char(wsi, '\0') < 0)
761                                 return -1;
762
763                         /* move to using WSI_TOKEN_HTTP_URI_ARGS */
764                         ah->nfrag++;
765                         if (ah->nfrag >= ARRAY_SIZE(ah->frags))
766                                 goto excessive;
767                         ah->frags[ah->nfrag].offset = ah->pos;
768                         ah->frags[ah->nfrag].len = 0;
769                         ah->frags[ah->nfrag].nfrag = 0;
770
771                         wsi->u.hdr.post_literal_equal = 0;
772                         ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = ah->nfrag;
773                         wsi->u.hdr.ups = URIPS_IDLE;
774                         goto swallow;
775                 }
776
777 check_eol:
778                 /* bail at EOL */
779                 if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
780                     c == '\x0d') {
781                         if (wsi->u.hdr.ues != URIES_IDLE)
782                                 goto forbid;
783
784                         c = '\0';
785                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
786                         lwsl_parser("*\n");
787                 }
788
789                 n = issue_char(wsi, c);
790                 if ((int)n < 0)
791                         return -1;
792                 if (n > 0)
793                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
794
795 swallow:
796                 /* per-protocol end of headers management */
797
798                 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
799                         goto set_parsing_complete;
800                 break;
801
802                 /* collecting and checking a name part */
803         case WSI_TOKEN_NAME_PART:
804                 lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode);
805
806                 wsi->u.hdr.lextable_pos =
807                                 lextable_decode(wsi->u.hdr.lextable_pos, c);
808                 /*
809                  * Server needs to look out for unknown methods...
810                  */
811                 if (wsi->u.hdr.lextable_pos < 0 &&
812                     wsi->mode == LWSCM_HTTP_SERVING) {
813                         /* this is not a header we know about */
814                         for (m = 0; m < ARRAY_SIZE(methods); m++)
815                                 if (ah->frag_index[methods[m]]) {
816                                         /*
817                                          * already had the method, no idea what
818                                          * this crap from the client is, ignore
819                                          */
820                                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
821                                         break;
822                                 }
823                         /*
824                          * hm it's an unknown http method from a client in fact,
825                          * treat as dangerous
826                          */
827                         if (m == ARRAY_SIZE(methods)) {
828                                 lwsl_info("Unknown method - dropping\n");
829                                 goto forbid;
830                         }
831                         break;
832                 }
833                 /*
834                  * ...otherwise for a client, let him ignore unknown headers
835                  * coming from the server
836                  */
837                 if (wsi->u.hdr.lextable_pos < 0) {
838                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
839                         break;
840                 }
841
842                 if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
843                         /* terminal state */
844
845                         n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) |
846                                         lextable[wsi->u.hdr.lextable_pos + 1];
847
848                         lwsl_parser("known hdr %d\n", n);
849                         for (m = 0; m < ARRAY_SIZE(methods); m++)
850                                 if (n == methods[m] &&
851                                     ah->frag_index[methods[m]]) {
852                                         lwsl_warn("Duplicated method\n");
853                                         return -1;
854                                 }
855
856                         /*
857                          * WSORIGIN is protocol equiv to ORIGIN,
858                          * JWebSocket likes to send it, map to ORIGIN
859                          */
860                         if (n == WSI_TOKEN_SWORIGIN)
861                                 n = WSI_TOKEN_ORIGIN;
862
863                         wsi->u.hdr.parser_state = (enum lws_token_indexes)
864                                                         (WSI_TOKEN_GET_URI + n);
865
866                         if (context->token_limits)
867                                 wsi->u.hdr.current_token_limit =
868                                         context->token_limits->token_limit[
869                                                        wsi->u.hdr.parser_state];
870                         else
871                                 wsi->u.hdr.current_token_limit =
872                                         wsi->context->max_http_header_data;
873
874                         if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
875                                 goto set_parsing_complete;
876
877                         goto start_fragment;
878                 }
879                 break;
880
881 start_fragment:
882                 ah->nfrag++;
883 excessive:
884                 if (ah->nfrag == ARRAY_SIZE(ah->frags)) {
885                         lwsl_warn("More hdr frags than we can deal with\n");
886                         return -1;
887                 }
888
889                 ah->frags[ah->nfrag].offset = ah->pos;
890                 ah->frags[ah->nfrag].len = 0;
891                 ah->frags[ah->nfrag].nfrag = 0;
892
893                 n = ah->frag_index[wsi->u.hdr.parser_state];
894                 if (!n) { /* first fragment */
895                         ah->frag_index[wsi->u.hdr.parser_state] = ah->nfrag;
896                         break;
897                 }
898                 /* continuation */
899                 while (ah->frags[n].nfrag)
900                         n = ah->frags[n].nfrag;
901                 ah->frags[n].nfrag = ah->nfrag;
902
903                 if (issue_char(wsi, ' ') < 0)
904                         return -1;
905                 break;
906
907                 /* skipping arg part of a name we didn't recognize */
908         case WSI_TOKEN_SKIPPING:
909                 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
910
911                 if (c == '\x0d')
912                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
913                 break;
914
915         case WSI_TOKEN_SKIPPING_SAW_CR:
916                 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
917                 if (wsi->u.hdr.ues != URIES_IDLE)
918                         goto forbid;
919                 if (c == '\x0a') {
920                         wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
921                         wsi->u.hdr.lextable_pos = 0;
922                 } else
923                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
924                 break;
925                 /* we're done, ignore anything else */
926
927         case WSI_PARSING_COMPLETE:
928                 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
929                 break;
930         }
931
932         return 0;
933
934 set_parsing_complete:
935         if (wsi->u.hdr.ues != URIES_IDLE)
936                 goto forbid;
937         if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
938                 if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
939                         wsi->ietf_spec_revision =
940                                atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
941
942                 lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
943         }
944         wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
945         wsi->hdr_parsing_completed = 1;
946
947         return 0;
948
949 forbid:
950         lwsl_notice(" forbidding on uri sanitation\n");
951         lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
952         return -1;
953 }
954
955
956 /**
957  * lws_frame_is_binary: true if the current frame was sent in binary mode
958  *
959  * @wsi: the connection we are inquiring about
960  *
961  * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
962  * it's interested to see if the frame it's dealing with was sent in binary
963  * mode.
964  */
965
966 LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
967 {
968         return wsi->u.ws.frame_is_binary;
969 }
970
971 int
972 lws_rx_sm(struct lws *wsi, unsigned char c)
973 {
974         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
975         int callback_action = LWS_CALLBACK_RECEIVE;
976         int ret = 0, n, rx_draining_ext = 0;
977         struct lws_tokens eff_buf;
978
979         if (wsi->socket_is_permanently_unusable)
980                 return -1;
981
982         switch (wsi->lws_rx_parse_state) {
983         case LWS_RXPS_NEW:
984                 if (wsi->u.ws.rx_draining_ext) {
985                         struct lws **w = &pt->rx_draining_ext_list;
986
987                         eff_buf.token = NULL;
988                         eff_buf.token_len = 0;
989                         wsi->u.ws.rx_draining_ext = 0;
990                         /* remove us from context draining ext list */
991                         while (*w) {
992                                 if (*w == wsi) {
993                                         *w = wsi->u.ws.rx_draining_ext_list;
994                                         break;
995                                 }
996                                 w = &((*w)->u.ws.rx_draining_ext_list);
997                         }
998                         wsi->u.ws.rx_draining_ext_list = NULL;
999                         rx_draining_ext = 1;
1000                         lwsl_err("%s: doing draining flow\n", __func__);
1001
1002                         goto drain_extension;
1003                 }
1004                 switch (wsi->ietf_spec_revision) {
1005                 case 13:
1006                         /*
1007                          * no prepended frame key any more
1008                          */
1009                         wsi->u.ws.all_zero_nonce = 1;
1010                         goto handle_first;
1011
1012                 default:
1013                         lwsl_warn("lws_rx_sm: unknown spec version %d\n",
1014                                                        wsi->ietf_spec_revision);
1015                         break;
1016                 }
1017                 break;
1018         case LWS_RXPS_04_mask_1:
1019                 wsi->u.ws.mask[1] = c;
1020                 if (c)
1021                         wsi->u.ws.all_zero_nonce = 0;
1022                 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_2;
1023                 break;
1024         case LWS_RXPS_04_mask_2:
1025                 wsi->u.ws.mask[2] = c;
1026                 if (c)
1027                         wsi->u.ws.all_zero_nonce = 0;
1028                 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_3;
1029                 break;
1030         case LWS_RXPS_04_mask_3:
1031                 wsi->u.ws.mask[3] = c;
1032                 if (c)
1033                         wsi->u.ws.all_zero_nonce = 0;
1034
1035                 /*
1036                  * start from the zero'th byte in the XOR key buffer since
1037                  * this is the start of a frame with a new key
1038                  */
1039
1040                 wsi->u.ws.mask_idx = 0;
1041
1042                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
1043                 break;
1044
1045         /*
1046          *  04 logical framing from the spec (all this is masked when incoming
1047          *  and has to be unmasked)
1048          *
1049          * We ignore the possibility of extension data because we don't
1050          * negotiate any extensions at the moment.
1051          *
1052          *    0                   1                   2                   3
1053          *    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
1054          *   +-+-+-+-+-------+-+-------------+-------------------------------+
1055          *   |F|R|R|R| opcode|R| Payload len |    Extended payload length    |
1056          *   |I|S|S|S|  (4)  |S|     (7)     |             (16/63)           |
1057          *   |N|V|V|V|       |V|             |   (if payload len==126/127)   |
1058          *   | |1|2|3|       |4|             |                               |
1059          *   +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
1060          *   |     Extended payload length continued, if payload len == 127  |
1061          *   + - - - - - - - - - - - - - - - +-------------------------------+
1062          *   |                               |         Extension data        |
1063          *   +-------------------------------+ - - - - - - - - - - - - - - - +
1064          *   :                                                               :
1065          *   +---------------------------------------------------------------+
1066          *   :                       Application data                        :
1067          *   +---------------------------------------------------------------+
1068          *
1069          *  We pass payload through to userland as soon as we get it, ignoring
1070          *  FIN.  It's up to userland to buffer it up if it wants to see a
1071          *  whole unfragmented block of the original size (which may be up to
1072          *  2^63 long!)
1073          */
1074
1075         case LWS_RXPS_04_FRAME_HDR_1:
1076 handle_first:
1077
1078                 wsi->u.ws.opcode = c & 0xf;
1079                 wsi->u.ws.rsv = c & 0x70;
1080                 wsi->u.ws.final = !!((c >> 7) & 1);
1081
1082                 switch (wsi->u.ws.opcode) {
1083                 case LWSWSOPC_TEXT_FRAME:
1084                 case LWSWSOPC_BINARY_FRAME:
1085                         wsi->u.ws.rsv_first_msg = (c & 0x70);
1086                         wsi->u.ws.frame_is_binary =
1087                              wsi->u.ws.opcode == LWSWSOPC_BINARY_FRAME;
1088                         break;
1089                 case 3:
1090                 case 4:
1091                 case 5:
1092                 case 6:
1093                 case 7:
1094                 case 0xb:
1095                 case 0xc:
1096                 case 0xd:
1097                 case 0xe:
1098                 case 0xf:
1099                         lwsl_info("illegal opcode\n");
1100                         return -1;
1101                 }
1102                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
1103                 break;
1104
1105         case LWS_RXPS_04_FRAME_HDR_LEN:
1106
1107                 wsi->u.ws.this_frame_masked = !!(c & 0x80);
1108
1109                 switch (c & 0x7f) {
1110                 case 126:
1111                         /* control frames are not allowed to have big lengths */
1112                         if (wsi->u.ws.opcode & 8)
1113                                 goto illegal_ctl_length;
1114
1115                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
1116                         break;
1117                 case 127:
1118                         /* control frames are not allowed to have big lengths */
1119                         if (wsi->u.ws.opcode & 8)
1120                                 goto illegal_ctl_length;
1121
1122                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
1123                         break;
1124                 default:
1125                         wsi->u.ws.rx_packet_length = c & 0x7f;
1126                         if (wsi->u.ws.this_frame_masked)
1127                                 wsi->lws_rx_parse_state =
1128                                                 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
1129                         else
1130                                 if (wsi->u.ws.rx_packet_length)
1131                                         wsi->lws_rx_parse_state =
1132                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1133                                 else {
1134                                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1135                                         goto spill;
1136                                 }
1137                         break;
1138                 }
1139                 break;
1140
1141         case LWS_RXPS_04_FRAME_HDR_LEN16_2:
1142                 wsi->u.ws.rx_packet_length = c << 8;
1143                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
1144                 break;
1145
1146         case LWS_RXPS_04_FRAME_HDR_LEN16_1:
1147                 wsi->u.ws.rx_packet_length |= c;
1148                 if (wsi->u.ws.this_frame_masked)
1149                         wsi->lws_rx_parse_state =
1150                                         LWS_RXPS_07_COLLECT_FRAME_KEY_1;
1151                 else
1152                         wsi->lws_rx_parse_state =
1153                                 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1154                 break;
1155
1156         case LWS_RXPS_04_FRAME_HDR_LEN64_8:
1157                 if (c & 0x80) {
1158                         lwsl_warn("b63 of length must be zero\n");
1159                         /* kill the connection */
1160                         return -1;
1161                 }
1162 #if defined __LP64__
1163                 wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
1164 #else
1165                 wsi->u.ws.rx_packet_length = 0;
1166 #endif
1167                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
1168                 break;
1169
1170         case LWS_RXPS_04_FRAME_HDR_LEN64_7:
1171 #if defined __LP64__
1172                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
1173 #endif
1174                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
1175                 break;
1176
1177         case LWS_RXPS_04_FRAME_HDR_LEN64_6:
1178 #if defined __LP64__
1179                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
1180 #endif
1181                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
1182                 break;
1183
1184         case LWS_RXPS_04_FRAME_HDR_LEN64_5:
1185 #if defined __LP64__
1186                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
1187 #endif
1188                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
1189                 break;
1190
1191         case LWS_RXPS_04_FRAME_HDR_LEN64_4:
1192                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
1193                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
1194                 break;
1195
1196         case LWS_RXPS_04_FRAME_HDR_LEN64_3:
1197                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
1198                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
1199                 break;
1200
1201         case LWS_RXPS_04_FRAME_HDR_LEN64_2:
1202                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
1203                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
1204                 break;
1205
1206         case LWS_RXPS_04_FRAME_HDR_LEN64_1:
1207                 wsi->u.ws.rx_packet_length |= ((size_t)c);
1208                 if (wsi->u.ws.this_frame_masked)
1209                         wsi->lws_rx_parse_state =
1210                                         LWS_RXPS_07_COLLECT_FRAME_KEY_1;
1211                 else
1212                         wsi->lws_rx_parse_state =
1213                                 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1214                 break;
1215
1216         case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
1217                 wsi->u.ws.mask[0] = c;
1218                 if (c)
1219                         wsi->u.ws.all_zero_nonce = 0;
1220                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
1221                 break;
1222
1223         case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
1224                 wsi->u.ws.mask[1] = c;
1225                 if (c)
1226                         wsi->u.ws.all_zero_nonce = 0;
1227                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
1228                 break;
1229
1230         case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
1231                 wsi->u.ws.mask[2] = c;
1232                 if (c)
1233                         wsi->u.ws.all_zero_nonce = 0;
1234                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
1235                 break;
1236
1237         case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
1238                 wsi->u.ws.mask[3] = c;
1239                 if (c)
1240                         wsi->u.ws.all_zero_nonce = 0;
1241                 wsi->lws_rx_parse_state =
1242                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1243                 wsi->u.ws.mask_idx = 0;
1244                 if (wsi->u.ws.rx_packet_length == 0) {
1245                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1246                         goto spill;
1247                 }
1248                 break;
1249
1250
1251         case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
1252                 assert(wsi->u.ws.rx_ubuf);
1253
1254                 if (wsi->u.ws.rx_ubuf_head + LWS_PRE >=
1255                     wsi->u.ws.rx_ubuf_alloc) {
1256                         lwsl_err("Attempted overflow \n");
1257                         return -1;
1258                 }
1259                 if (wsi->u.ws.all_zero_nonce)
1260                         wsi->u.ws.rx_ubuf[LWS_PRE +
1261                                          (wsi->u.ws.rx_ubuf_head++)] = c;
1262                 else
1263                         wsi->u.ws.rx_ubuf[LWS_PRE +
1264                                (wsi->u.ws.rx_ubuf_head++)] =
1265                                    c ^ wsi->u.ws.mask[
1266                                             (wsi->u.ws.mask_idx++) & 3];
1267
1268                 if (--wsi->u.ws.rx_packet_length == 0) {
1269                         /* spill because we have the whole frame */
1270                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1271                         goto spill;
1272                 }
1273
1274                 /*
1275                  * if there's no protocol max frame size given, we are
1276                  * supposed to default to LWS_MAX_SOCKET_IO_BUF
1277                  */
1278
1279                 if (!wsi->protocol->rx_buffer_size &&
1280                                         wsi->u.ws.rx_ubuf_head !=
1281                                                           LWS_MAX_SOCKET_IO_BUF)
1282                         break;
1283                 else
1284                         if (wsi->protocol->rx_buffer_size &&
1285                                         wsi->u.ws.rx_ubuf_head !=
1286                                                   wsi->protocol->rx_buffer_size)
1287                         break;
1288
1289                 /* spill because we filled our rx buffer */
1290 spill:
1291                 /*
1292                  * is this frame a control packet we should take care of at this
1293                  * layer?  If so service it and hide it from the user callback
1294                  */
1295
1296                 lwsl_parser("spill on %s\n", wsi->protocol->name);
1297
1298                 switch (wsi->u.ws.opcode) {
1299                 case LWSWSOPC_CLOSE:
1300
1301                         /* is this an acknowledgement of our close? */
1302                         if (wsi->state == LWSS_AWAITING_CLOSE_ACK) {
1303                                 /*
1304                                  * fine he has told us he is closing too, let's
1305                                  * finish our close
1306                                  */
1307                                 lwsl_parser("seen client close ack\n");
1308                                 return -1;
1309                         }
1310                         if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
1311                                 /* if he sends us 2 CLOSE, kill him */
1312                                 return -1;
1313
1314                         if (user_callback_handle_rxflow(
1315                                         wsi->protocol->callback, wsi,
1316                                         LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
1317                                         wsi->user_space,
1318                                         &wsi->u.ws.rx_ubuf[LWS_PRE],
1319                                         wsi->u.ws.rx_ubuf_head))
1320                                 return -1;
1321
1322                         lwsl_parser("server sees client close packet\n");
1323                         wsi->state = LWSS_RETURNED_CLOSE_ALREADY;
1324                         /* deal with the close packet contents as a PONG */
1325                         wsi->u.ws.payload_is_close = 1;
1326                         goto process_as_ping;
1327
1328                 case LWSWSOPC_PING:
1329                         lwsl_info("received %d byte ping, sending pong\n",
1330                                                  wsi->u.ws.rx_ubuf_head);
1331
1332                         if (wsi->u.ws.ping_pending_flag) {
1333                                 /*
1334                                  * there is already a pending ping payload
1335                                  * we should just log and drop
1336                                  */
1337                                 lwsl_parser("DROP PING since one pending\n");
1338                                 goto ping_drop;
1339                         }
1340 process_as_ping:
1341                         /* control packets can only be < 128 bytes long */
1342                         if (wsi->u.ws.rx_ubuf_head > 128 - 3) {
1343                                 lwsl_parser("DROP PING payload too large\n");
1344                                 goto ping_drop;
1345                         }
1346
1347                         /* stash the pong payload */
1348                         memcpy(wsi->u.ws.ping_payload_buf + LWS_PRE,
1349                                &wsi->u.ws.rx_ubuf[LWS_PRE],
1350                                 wsi->u.ws.rx_ubuf_head);
1351
1352                         wsi->u.ws.ping_payload_len = wsi->u.ws.rx_ubuf_head;
1353                         wsi->u.ws.ping_pending_flag = 1;
1354
1355                         /* get it sent as soon as possible */
1356                         lws_callback_on_writable(wsi);
1357 ping_drop:
1358                         wsi->u.ws.rx_ubuf_head = 0;
1359                         return 0;
1360
1361                 case LWSWSOPC_PONG:
1362                         lwsl_info("received pong\n");
1363                         lwsl_hexdump(&wsi->u.ws.rx_ubuf[LWS_PRE],
1364                                      wsi->u.ws.rx_ubuf_head);
1365
1366                         /* issue it */
1367                         callback_action = LWS_CALLBACK_RECEIVE_PONG;
1368                         break;
1369
1370                 case LWSWSOPC_TEXT_FRAME:
1371                 case LWSWSOPC_BINARY_FRAME:
1372                 case LWSWSOPC_CONTINUATION:
1373                         break;
1374
1375                 default:
1376                         lwsl_parser("passing opc %x up to exts\n",
1377                                     wsi->u.ws.opcode);
1378                         /*
1379                          * It's something special we can't understand here.
1380                          * Pass the payload up to the extension's parsing
1381                          * state machine.
1382                          */
1383
1384                         eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1385                         eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1386
1387                         if (lws_ext_cb_active(wsi, LWS_EXT_CB_EXTENDED_PAYLOAD_RX,
1388                                               &eff_buf, 0) <= 0)
1389                                 /* not handle or fail */
1390                                 lwsl_ext("ext opc opcode 0x%x unknown\n",
1391                                          wsi->u.ws.opcode);
1392
1393                         wsi->u.ws.rx_ubuf_head = 0;
1394                         return 0;
1395                 }
1396
1397                 /*
1398                  * No it's real payload, pass it up to the user callback.
1399                  * It's nicely buffered with the pre-padding taken care of
1400                  * so it can be sent straight out again using lws_write
1401                  */
1402
1403                 eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1404                 eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1405
1406 drain_extension:
1407                 lwsl_ext("%s: passing %d to ext\n", __func__, eff_buf.token_len);
1408
1409                 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
1410                     wsi->state == LWSS_AWAITING_CLOSE_ACK)
1411                         goto already_done;
1412
1413                 n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &eff_buf, 0);
1414                 if (n < 0) {
1415                         /*
1416                          * we may rely on this to get RX, just drop connection
1417                          */
1418                         wsi->socket_is_permanently_unusable = 1;
1419                         return -1;
1420                 }
1421
1422                 if (rx_draining_ext && eff_buf.token_len == 0)
1423                         goto already_done;
1424
1425                 if (n && eff_buf.token_len) {
1426                         /* extension had more... main loop will come back */
1427                         wsi->u.ws.rx_draining_ext = 1;
1428                         wsi->u.ws.rx_draining_ext_list = pt->rx_draining_ext_list;
1429                         pt->rx_draining_ext_list = wsi;
1430                 }
1431
1432                 if (eff_buf.token_len > 0 ||
1433                     callback_action == LWS_CALLBACK_RECEIVE_PONG) {
1434                         eff_buf.token[eff_buf.token_len] = '\0';
1435
1436                         if (wsi->protocol->callback) {
1437
1438                                 if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
1439                                         lwsl_info("Doing pong callback\n");
1440
1441                                 ret = user_callback_handle_rxflow(
1442                                                 wsi->protocol->callback,
1443                                                 wsi,
1444                                                 (enum lws_callback_reasons)callback_action,
1445                                                 wsi->user_space,
1446                                                 eff_buf.token,
1447                                                 eff_buf.token_len);
1448                         }
1449                         else
1450                                 lwsl_err("No callback on payload spill!\n");
1451                 }
1452
1453 already_done:
1454                 wsi->u.ws.rx_ubuf_head = 0;
1455                 break;
1456         }
1457
1458         return ret;
1459
1460 illegal_ctl_length:
1461
1462         lwsl_warn("Control frame with xtended length is illegal\n");
1463         /* kill the connection */
1464         return -1;
1465 }
1466
1467
1468 /**
1469  * lws_remaining_packet_payload() - Bytes to come before "overall"
1470  *                                            rx packet is complete
1471  * @wsi:                Websocket instance (available from user callback)
1472  *
1473  *      This function is intended to be called from the callback if the
1474  *  user code is interested in "complete packets" from the client.
1475  *  libwebsockets just passes through payload as it comes and issues a buffer
1476  *  additionally when it hits a built-in limit.  The LWS_CALLBACK_RECEIVE
1477  *  callback handler can use this API to find out if the buffer it has just
1478  *  been given is the last piece of a "complete packet" from the client --
1479  *  when that is the case lws_remaining_packet_payload() will return
1480  *  0.
1481  *
1482  *  Many protocols won't care becuse their packets are always small.
1483  */
1484
1485 LWS_VISIBLE size_t
1486 lws_remaining_packet_payload(struct lws *wsi)
1487 {
1488         return wsi->u.ws.rx_packet_length;
1489 }
1490
1491 /* Once we reach LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED, we know how much
1492  * to expect in that state and can deal with it in bulk more efficiently.
1493  */
1494
1495 void
1496 lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf,
1497                                    size_t *len)
1498 {
1499         unsigned char *buffer = *buf, mask[4];
1500         int buffer_size, n;
1501         unsigned int avail;
1502         char *rx_ubuf;
1503
1504         if (wsi->protocol->rx_buffer_size)
1505                 buffer_size = wsi->protocol->rx_buffer_size;
1506         else
1507                 buffer_size = LWS_MAX_SOCKET_IO_BUF;
1508         avail = buffer_size - wsi->u.ws.rx_ubuf_head;
1509
1510         /* do not consume more than we should */
1511         if (avail > wsi->u.ws.rx_packet_length)
1512                 avail = wsi->u.ws.rx_packet_length;
1513
1514         /* do not consume more than what is in the buffer */
1515         if (avail > *len)
1516                 avail = *len;
1517
1518         /* we want to leave 1 byte for the parser to handle properly */
1519         if (avail <= 1)
1520                 return;
1521
1522         avail--;
1523         rx_ubuf = wsi->u.ws.rx_ubuf + LWS_PRE + wsi->u.ws.rx_ubuf_head;
1524         if (wsi->u.ws.all_zero_nonce)
1525                 memcpy(rx_ubuf, buffer, avail);
1526         else {
1527
1528                 for (n = 0; n < 4; n++)
1529                         mask[n] = wsi->u.ws.mask[(wsi->u.ws.mask_idx + n) & 3];
1530
1531                 /* deal with 4-byte chunks using unwrapped loop */
1532                 n = avail >> 2;
1533                 while (n--) {
1534                         *(rx_ubuf++) = *(buffer++) ^ mask[0];
1535                         *(rx_ubuf++) = *(buffer++) ^ mask[1];
1536                         *(rx_ubuf++) = *(buffer++) ^ mask[2];
1537                         *(rx_ubuf++) = *(buffer++) ^ mask[3];
1538                 }
1539                 /* and the remaining bytes bytewise */
1540                 for (n = 0; n < (int)(avail & 3); n++)
1541                         *(rx_ubuf++) = *(buffer++) ^ mask[n];
1542
1543                 wsi->u.ws.mask_idx = (wsi->u.ws.mask_idx + avail) & 3;
1544         }
1545
1546         (*buf) += avail;
1547         wsi->u.ws.rx_ubuf_head += avail;
1548         wsi->u.ws.rx_packet_length -= avail;
1549         *len -= avail;
1550 }