clean signed mismatches and protect ssl specific code
[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 static 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                         /* begin parsing HTTP version: */
590                         if (issue_char(wsi, '\0') < 0)
591                                 return -1;
592                         wsi->u.hdr.parser_state = WSI_TOKEN_HTTP;
593                         goto start_fragment;
594                 }
595
596                 /* special URI processing... convert %xx */
597
598                 switch (wsi->u.hdr.ues) {
599                 case URIES_IDLE:
600                         if (c == '%') {
601                                 wsi->u.hdr.ues = URIES_SEEN_PERCENT;
602                                 goto swallow;
603                         }
604                         break;
605                 case URIES_SEEN_PERCENT:
606                         if (char_to_hex(c) < 0) {
607                                 /* regurgitate */
608                                 if (issue_char(wsi, '%') < 0)
609                                         return -1;
610                                 wsi->u.hdr.ues = URIES_IDLE;
611                                 /* continue on to assess c */
612                                 break;
613                         }
614                         wsi->u.hdr.esc_stash = c;
615                         wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1;
616                         goto swallow;
617
618                 case URIES_SEEN_PERCENT_H1:
619                         if (char_to_hex(c) < 0) {
620                                 /* regurgitate */
621                                 if (issue_char(wsi, '%') < 0)
622                                         return -1;
623                                 wsi->u.hdr.ues = URIES_IDLE;
624                                 /* regurgitate + assess */
625                                 if (lws_parse(wsi, wsi->u.hdr.esc_stash) < 0)
626                                         return -1;
627                                 /* continue on to assess c */
628                                 break;
629                         }
630                         c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) |
631                                         char_to_hex(c);
632                         enc = 1;
633                         wsi->u.hdr.ues = URIES_IDLE;
634                         break;
635                 }
636
637                 /*
638                  * special URI processing...
639                  *  convert /.. or /... or /../ etc to /
640                  *  convert /./ to /
641                  *  convert // or /// etc to /
642                  *  leave /.dir or whatever alone
643                  */
644
645                 switch (wsi->u.hdr.ups) {
646                 case URIPS_IDLE:
647                         if (!c)
648                                 return -1;
649                         /* genuine delimiter */
650                         if ((c == '&' || c == ';') && !enc) {
651                                 if (issue_char(wsi, c) < 0)
652                                         return -1;
653                                 /* swallow the terminator */
654                                 ah->frags[ah->nfrag].len--;
655                                 /* link to next fragment */
656                                 ah->frags[ah->nfrag].nfrag = ah->nfrag + 1;
657                                 ah->nfrag++;
658                                 if (ah->nfrag >= ARRAY_SIZE(ah->frags))
659                                         goto excessive;
660                                 /* start next fragment after the & */
661                                 wsi->u.hdr.post_literal_equal = 0;
662                                 ah->frags[ah->nfrag].offset = ah->pos;
663                                 ah->frags[ah->nfrag].len = 0;
664                                 ah->frags[ah->nfrag].nfrag = 0;
665                                 goto swallow;
666                         }
667                         /* uriencoded = in the name part, disallow */
668                         if (c == '=' && enc && !wsi->u.hdr.post_literal_equal)
669                                 c = '_';
670
671                         /* after the real =, we don't care how many = */
672                         if (c == '=' && !enc)
673                                 wsi->u.hdr.post_literal_equal = 1;
674
675                         /* + to space */
676                         if (c == '+' && !enc)
677                                 c = ' ';
678                         /* issue the first / always */
679                         if (c == '/' && !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS])
680                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
681                         break;
682                 case URIPS_SEEN_SLASH:
683                         /* swallow subsequent slashes */
684                         if (c == '/')
685                                 goto swallow;
686                         /* track and swallow the first . after / */
687                         if (c == '.') {
688                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
689                                 goto swallow;
690                         }
691                         wsi->u.hdr.ups = URIPS_IDLE;
692                         break;
693                 case URIPS_SEEN_SLASH_DOT:
694                         /* swallow second . */
695                         if (c == '.') {
696                                 /*
697                                  * back up one dir level if possible
698                                  * safe against header fragmentation because
699                                  * the method URI can only be in 1 fragment
700                                  */
701                                 if (ah->frags[ah->nfrag].len > 2) {
702                                         ah->pos--;
703                                         ah->frags[ah->nfrag].len--;
704                                         do {
705                                                 ah->pos--;
706                                                 ah->frags[ah->nfrag].len--;
707                                         } while (ah->frags[ah->nfrag].len > 1 &&
708                                                  ah->data[ah->pos] != '/');
709                                 }
710                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
711                                 goto swallow;
712                         }
713                         /* change /./ to / */
714                         if (c == '/') {
715                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
716                                 goto swallow;
717                         }
718                         /* it was like /.dir ... regurgitate the . */
719                         wsi->u.hdr.ups = URIPS_IDLE;
720                         if (issue_char(wsi, '.') < 0)
721                                 return -1;
722                         break;
723
724                 case URIPS_SEEN_SLASH_DOT_DOT:
725                         /* swallow prior .. chars and any subsequent . */
726                         if (c == '.')
727                                 goto swallow;
728                         /* last issued was /, so another / == // */
729                         if (c == '/')
730                                 goto swallow;
731                         /* last we issued was / so SEEN_SLASH */
732                         wsi->u.hdr.ups = URIPS_SEEN_SLASH;
733                         break;
734                 }
735
736                 if (c == '?' && !enc &&
737                     !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS]) { /* start of URI arguments */
738                         /* seal off uri header */
739                         if (issue_char(wsi, '\0') < 0)
740                                 return -1;
741
742                         /* move to using WSI_TOKEN_HTTP_URI_ARGS */
743                         ah->nfrag++;
744                         if (ah->nfrag >= ARRAY_SIZE(ah->frags))
745                                 goto excessive;
746                         ah->frags[ah->nfrag].offset = ah->pos;
747                         ah->frags[ah->nfrag].len = 0;
748                         ah->frags[ah->nfrag].nfrag = 0;
749
750                         wsi->u.hdr.post_literal_equal = 0;
751                         ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = ah->nfrag;
752                         wsi->u.hdr.ups = URIPS_IDLE;
753                         goto swallow;
754                 }
755
756 check_eol:
757
758                 /* bail at EOL */
759                 if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
760                     c == '\x0d') {
761                         c = '\0';
762                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
763                         lwsl_parser("*\n");
764                 }
765
766                 n = issue_char(wsi, c);
767                 if ((int)n < 0)
768                         return -1;
769                 if (n > 0)
770                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
771
772 swallow:
773                 /* per-protocol end of headers management */
774
775                 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
776                         goto set_parsing_complete;
777                 break;
778
779                 /* collecting and checking a name part */
780         case WSI_TOKEN_NAME_PART:
781                 lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode);
782
783                 wsi->u.hdr.lextable_pos =
784                                 lextable_decode(wsi->u.hdr.lextable_pos, c);
785                 /*
786                  * Server needs to look out for unknown methods...
787                  */
788                 if (wsi->u.hdr.lextable_pos < 0 &&
789                     wsi->mode == LWSCM_HTTP_SERVING) {
790                         /* this is not a header we know about */
791                         for (m = 0; m < ARRAY_SIZE(methods); m++)
792                                 if (ah->frag_index[methods[m]]) {
793                                         /*
794                                          * already had the method, no idea what
795                                          * this crap from the client is, ignore
796                                          */
797                                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
798                                         break;
799                                 }
800                         /*
801                          * hm it's an unknown http method from a client in fact,
802                          * treat as dangerous
803                          */
804                         if (m == ARRAY_SIZE(methods)) {
805                                 lwsl_info("Unknown method - dropping\n");
806                                 return -1;
807                         }
808                         break;
809                 }
810                 /*
811                  * ...otherwise for a client, let him ignore unknown headers
812                  * coming from the server
813                  */
814                 if (wsi->u.hdr.lextable_pos < 0) {
815                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
816                         break;
817                 }
818
819                 if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
820                         /* terminal state */
821
822                         n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) |
823                                         lextable[wsi->u.hdr.lextable_pos + 1];
824
825                         lwsl_parser("known hdr %d\n", n);
826                         for (m = 0; m < ARRAY_SIZE(methods); m++)
827                                 if (n == methods[m] &&
828                                     ah->frag_index[methods[m]]) {
829                                         lwsl_warn("Duplicated method\n");
830                                         return -1;
831                                 }
832
833                         /*
834                          * WSORIGIN is protocol equiv to ORIGIN,
835                          * JWebSocket likes to send it, map to ORIGIN
836                          */
837                         if (n == WSI_TOKEN_SWORIGIN)
838                                 n = WSI_TOKEN_ORIGIN;
839
840                         wsi->u.hdr.parser_state = (enum lws_token_indexes)
841                                                         (WSI_TOKEN_GET_URI + n);
842
843                         if (context->token_limits)
844                                 wsi->u.hdr.current_token_limit =
845                                         context->token_limits->token_limit[
846                                                        wsi->u.hdr.parser_state];
847                         else
848                                 wsi->u.hdr.current_token_limit =
849                                         wsi->context->max_http_header_data;
850
851                         if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
852                                 goto set_parsing_complete;
853
854                         goto start_fragment;
855                 }
856                 break;
857
858 start_fragment:
859                 ah->nfrag++;
860 excessive:
861                 if (ah->nfrag == ARRAY_SIZE(ah->frags)) {
862                         lwsl_warn("More hdr frags than we can deal with\n");
863                         return -1;
864                 }
865
866                 ah->frags[ah->nfrag].offset = ah->pos;
867                 ah->frags[ah->nfrag].len = 0;
868                 ah->frags[ah->nfrag].nfrag = 0;
869
870                 n = ah->frag_index[wsi->u.hdr.parser_state];
871                 if (!n) { /* first fragment */
872                         ah->frag_index[wsi->u.hdr.parser_state] = ah->nfrag;
873                         break;
874                 }
875                 /* continuation */
876                 while (ah->frags[n].nfrag)
877                         n = ah->frags[n].nfrag;
878                 ah->frags[n].nfrag = ah->nfrag;
879
880                 if (issue_char(wsi, ' ') < 0)
881                         return -1;
882                 break;
883
884                 /* skipping arg part of a name we didn't recognize */
885         case WSI_TOKEN_SKIPPING:
886                 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
887
888                 if (c == '\x0d')
889                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
890                 break;
891
892         case WSI_TOKEN_SKIPPING_SAW_CR:
893                 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
894                 if (c == '\x0a') {
895                         wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
896                         wsi->u.hdr.lextable_pos = 0;
897                 } else
898                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
899                 break;
900                 /* we're done, ignore anything else */
901
902         case WSI_PARSING_COMPLETE:
903                 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
904                 break;
905         }
906
907         return 0;
908
909 set_parsing_complete:
910
911         if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
912                 if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
913                         wsi->ietf_spec_revision =
914                                atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
915
916                 lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
917         }
918         wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
919         wsi->hdr_parsing_completed = 1;
920
921         return 0;
922 }
923
924
925 /**
926  * lws_frame_is_binary: true if the current frame was sent in binary mode
927  *
928  * @wsi: the connection we are inquiring about
929  *
930  * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
931  * it's interested to see if the frame it's dealing with was sent in binary
932  * mode.
933  */
934
935 LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
936 {
937         return wsi->u.ws.frame_is_binary;
938 }
939
940 int
941 lws_rx_sm(struct lws *wsi, unsigned char c)
942 {
943         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
944         int callback_action = LWS_CALLBACK_RECEIVE;
945         int ret = 0, n, rx_draining_ext = 0;
946         struct lws_tokens eff_buf;
947
948         if (wsi->socket_is_permanently_unusable)
949                 return -1;
950
951         switch (wsi->lws_rx_parse_state) {
952         case LWS_RXPS_NEW:
953                 if (wsi->u.ws.rx_draining_ext) {
954                         struct lws **w = &pt->rx_draining_ext_list;
955
956                         eff_buf.token = NULL;
957                         eff_buf.token_len = 0;
958                         wsi->u.ws.rx_draining_ext = 0;
959                         /* remove us from context draining ext list */
960                         while (*w) {
961                                 if (*w == wsi) {
962                                         *w = wsi->u.ws.rx_draining_ext_list;
963                                         break;
964                                 }
965                                 w = &((*w)->u.ws.rx_draining_ext_list);
966                         }
967                         wsi->u.ws.rx_draining_ext_list = NULL;
968                         rx_draining_ext = 1;
969                         lwsl_err("%s: doing draining flow\n", __func__);
970
971                         goto drain_extension;
972                 }
973                 switch (wsi->ietf_spec_revision) {
974                 case 13:
975                         /*
976                          * no prepended frame key any more
977                          */
978                         wsi->u.ws.all_zero_nonce = 1;
979                         goto handle_first;
980
981                 default:
982                         lwsl_warn("lws_rx_sm: unknown spec version %d\n",
983                                                        wsi->ietf_spec_revision);
984                         break;
985                 }
986                 break;
987         case LWS_RXPS_04_mask_1:
988                 wsi->u.ws.mask[1] = c;
989                 if (c)
990                         wsi->u.ws.all_zero_nonce = 0;
991                 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_2;
992                 break;
993         case LWS_RXPS_04_mask_2:
994                 wsi->u.ws.mask[2] = c;
995                 if (c)
996                         wsi->u.ws.all_zero_nonce = 0;
997                 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_3;
998                 break;
999         case LWS_RXPS_04_mask_3:
1000                 wsi->u.ws.mask[3] = c;
1001                 if (c)
1002                         wsi->u.ws.all_zero_nonce = 0;
1003
1004                 /*
1005                  * start from the zero'th byte in the XOR key buffer since
1006                  * this is the start of a frame with a new key
1007                  */
1008
1009                 wsi->u.ws.mask_idx = 0;
1010
1011                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
1012                 break;
1013
1014         /*
1015          *  04 logical framing from the spec (all this is masked when incoming
1016          *  and has to be unmasked)
1017          *
1018          * We ignore the possibility of extension data because we don't
1019          * negotiate any extensions at the moment.
1020          *
1021          *    0                   1                   2                   3
1022          *    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
1023          *   +-+-+-+-+-------+-+-------------+-------------------------------+
1024          *   |F|R|R|R| opcode|R| Payload len |    Extended payload length    |
1025          *   |I|S|S|S|  (4)  |S|     (7)     |             (16/63)           |
1026          *   |N|V|V|V|       |V|             |   (if payload len==126/127)   |
1027          *   | |1|2|3|       |4|             |                               |
1028          *   +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
1029          *   |     Extended payload length continued, if payload len == 127  |
1030          *   + - - - - - - - - - - - - - - - +-------------------------------+
1031          *   |                               |         Extension data        |
1032          *   +-------------------------------+ - - - - - - - - - - - - - - - +
1033          *   :                                                               :
1034          *   +---------------------------------------------------------------+
1035          *   :                       Application data                        :
1036          *   +---------------------------------------------------------------+
1037          *
1038          *  We pass payload through to userland as soon as we get it, ignoring
1039          *  FIN.  It's up to userland to buffer it up if it wants to see a
1040          *  whole unfragmented block of the original size (which may be up to
1041          *  2^63 long!)
1042          */
1043
1044         case LWS_RXPS_04_FRAME_HDR_1:
1045 handle_first:
1046
1047                 wsi->u.ws.opcode = c & 0xf;
1048                 wsi->u.ws.rsv = c & 0x70;
1049                 wsi->u.ws.final = !!((c >> 7) & 1);
1050
1051                 switch (wsi->u.ws.opcode) {
1052                 case LWSWSOPC_TEXT_FRAME:
1053                 case LWSWSOPC_BINARY_FRAME:
1054                         wsi->u.ws.rsv_first_msg = (c & 0x70);
1055                         wsi->u.ws.frame_is_binary =
1056                              wsi->u.ws.opcode == LWSWSOPC_BINARY_FRAME;
1057                         break;
1058                 case 3:
1059                 case 4:
1060                 case 5:
1061                 case 6:
1062                 case 7:
1063                 case 0xb:
1064                 case 0xc:
1065                 case 0xd:
1066                 case 0xe:
1067                 case 0xf:
1068                         lwsl_info("illegal opcode\n");
1069                         return -1;
1070                 }
1071                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
1072                 break;
1073
1074         case LWS_RXPS_04_FRAME_HDR_LEN:
1075
1076                 wsi->u.ws.this_frame_masked = !!(c & 0x80);
1077
1078                 switch (c & 0x7f) {
1079                 case 126:
1080                         /* control frames are not allowed to have big lengths */
1081                         if (wsi->u.ws.opcode & 8)
1082                                 goto illegal_ctl_length;
1083
1084                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
1085                         break;
1086                 case 127:
1087                         /* control frames are not allowed to have big lengths */
1088                         if (wsi->u.ws.opcode & 8)
1089                                 goto illegal_ctl_length;
1090
1091                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
1092                         break;
1093                 default:
1094                         wsi->u.ws.rx_packet_length = c & 0x7f;
1095                         if (wsi->u.ws.this_frame_masked)
1096                                 wsi->lws_rx_parse_state =
1097                                                 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
1098                         else
1099                                 if (wsi->u.ws.rx_packet_length)
1100                                         wsi->lws_rx_parse_state =
1101                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1102                                 else {
1103                                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1104                                         goto spill;
1105                                 }
1106                         break;
1107                 }
1108                 break;
1109
1110         case LWS_RXPS_04_FRAME_HDR_LEN16_2:
1111                 wsi->u.ws.rx_packet_length = c << 8;
1112                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
1113                 break;
1114
1115         case LWS_RXPS_04_FRAME_HDR_LEN16_1:
1116                 wsi->u.ws.rx_packet_length |= c;
1117                 if (wsi->u.ws.this_frame_masked)
1118                         wsi->lws_rx_parse_state =
1119                                         LWS_RXPS_07_COLLECT_FRAME_KEY_1;
1120                 else
1121                         wsi->lws_rx_parse_state =
1122                                 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1123                 break;
1124
1125         case LWS_RXPS_04_FRAME_HDR_LEN64_8:
1126                 if (c & 0x80) {
1127                         lwsl_warn("b63 of length must be zero\n");
1128                         /* kill the connection */
1129                         return -1;
1130                 }
1131 #if defined __LP64__
1132                 wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
1133 #else
1134                 wsi->u.ws.rx_packet_length = 0;
1135 #endif
1136                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
1137                 break;
1138
1139         case LWS_RXPS_04_FRAME_HDR_LEN64_7:
1140 #if defined __LP64__
1141                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
1142 #endif
1143                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
1144                 break;
1145
1146         case LWS_RXPS_04_FRAME_HDR_LEN64_6:
1147 #if defined __LP64__
1148                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
1149 #endif
1150                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
1151                 break;
1152
1153         case LWS_RXPS_04_FRAME_HDR_LEN64_5:
1154 #if defined __LP64__
1155                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
1156 #endif
1157                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
1158                 break;
1159
1160         case LWS_RXPS_04_FRAME_HDR_LEN64_4:
1161                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
1162                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
1163                 break;
1164
1165         case LWS_RXPS_04_FRAME_HDR_LEN64_3:
1166                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
1167                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
1168                 break;
1169
1170         case LWS_RXPS_04_FRAME_HDR_LEN64_2:
1171                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
1172                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
1173                 break;
1174
1175         case LWS_RXPS_04_FRAME_HDR_LEN64_1:
1176                 wsi->u.ws.rx_packet_length |= ((size_t)c);
1177                 if (wsi->u.ws.this_frame_masked)
1178                         wsi->lws_rx_parse_state =
1179                                         LWS_RXPS_07_COLLECT_FRAME_KEY_1;
1180                 else
1181                         wsi->lws_rx_parse_state =
1182                                 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1183                 break;
1184
1185         case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
1186                 wsi->u.ws.mask[0] = c;
1187                 if (c)
1188                         wsi->u.ws.all_zero_nonce = 0;
1189                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
1190                 break;
1191
1192         case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
1193                 wsi->u.ws.mask[1] = c;
1194                 if (c)
1195                         wsi->u.ws.all_zero_nonce = 0;
1196                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
1197                 break;
1198
1199         case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
1200                 wsi->u.ws.mask[2] = c;
1201                 if (c)
1202                         wsi->u.ws.all_zero_nonce = 0;
1203                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
1204                 break;
1205
1206         case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
1207                 wsi->u.ws.mask[3] = c;
1208                 if (c)
1209                         wsi->u.ws.all_zero_nonce = 0;
1210                 wsi->lws_rx_parse_state =
1211                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1212                 wsi->u.ws.mask_idx = 0;
1213                 if (wsi->u.ws.rx_packet_length == 0) {
1214                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1215                         goto spill;
1216                 }
1217                 break;
1218
1219
1220         case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
1221                 assert(wsi->u.ws.rx_ubuf);
1222
1223                 if (wsi->u.ws.rx_ubuf_head + LWS_PRE >=
1224                     wsi->u.ws.rx_ubuf_alloc) {
1225                         lwsl_err("Attempted overflow \n");
1226                         return -1;
1227                 }
1228                 if (wsi->u.ws.all_zero_nonce)
1229                         wsi->u.ws.rx_ubuf[LWS_PRE +
1230                                          (wsi->u.ws.rx_ubuf_head++)] = c;
1231                 else
1232                         wsi->u.ws.rx_ubuf[LWS_PRE +
1233                                (wsi->u.ws.rx_ubuf_head++)] =
1234                                    c ^ wsi->u.ws.mask[
1235                                             (wsi->u.ws.mask_idx++) & 3];
1236
1237                 if (--wsi->u.ws.rx_packet_length == 0) {
1238                         /* spill because we have the whole frame */
1239                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1240                         goto spill;
1241                 }
1242
1243                 /*
1244                  * if there's no protocol max frame size given, we are
1245                  * supposed to default to LWS_MAX_SOCKET_IO_BUF
1246                  */
1247
1248                 if (!wsi->protocol->rx_buffer_size &&
1249                                         wsi->u.ws.rx_ubuf_head !=
1250                                                           LWS_MAX_SOCKET_IO_BUF)
1251                         break;
1252                 else
1253                         if (wsi->protocol->rx_buffer_size &&
1254                                         wsi->u.ws.rx_ubuf_head !=
1255                                                   wsi->protocol->rx_buffer_size)
1256                         break;
1257
1258                 /* spill because we filled our rx buffer */
1259 spill:
1260                 /*
1261                  * is this frame a control packet we should take care of at this
1262                  * layer?  If so service it and hide it from the user callback
1263                  */
1264
1265                 lwsl_parser("spill on %s\n", wsi->protocol->name);
1266
1267                 switch (wsi->u.ws.opcode) {
1268                 case LWSWSOPC_CLOSE:
1269
1270                         /* is this an acknowledgement of our close? */
1271                         if (wsi->state == LWSS_AWAITING_CLOSE_ACK) {
1272                                 /*
1273                                  * fine he has told us he is closing too, let's
1274                                  * finish our close
1275                                  */
1276                                 lwsl_parser("seen client close ack\n");
1277                                 return -1;
1278                         }
1279                         if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
1280                                 /* if he sends us 2 CLOSE, kill him */
1281                                 return -1;
1282
1283                         if (user_callback_handle_rxflow(
1284                                         wsi->protocol->callback, wsi,
1285                                         LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
1286                                         wsi->user_space,
1287                                         &wsi->u.ws.rx_ubuf[LWS_PRE],
1288                                         wsi->u.ws.rx_ubuf_head))
1289                                 return -1;
1290
1291                         lwsl_parser("server sees client close packet\n");
1292                         wsi->state = LWSS_RETURNED_CLOSE_ALREADY;
1293                         /* deal with the close packet contents as a PONG */
1294                         wsi->u.ws.payload_is_close = 1;
1295                         goto process_as_ping;
1296
1297                 case LWSWSOPC_PING:
1298                         lwsl_info("received %d byte ping, sending pong\n",
1299                                                  wsi->u.ws.rx_ubuf_head);
1300
1301                         if (wsi->u.ws.ping_pending_flag) {
1302                                 /*
1303                                  * there is already a pending ping payload
1304                                  * we should just log and drop
1305                                  */
1306                                 lwsl_parser("DROP PING since one pending\n");
1307                                 goto ping_drop;
1308                         }
1309 process_as_ping:
1310                         /* control packets can only be < 128 bytes long */
1311                         if (wsi->u.ws.rx_ubuf_head > 128 - 3) {
1312                                 lwsl_parser("DROP PING payload too large\n");
1313                                 goto ping_drop;
1314                         }
1315
1316                         /* stash the pong payload */
1317                         memcpy(wsi->u.ws.ping_payload_buf + LWS_PRE,
1318                                &wsi->u.ws.rx_ubuf[LWS_PRE],
1319                                 wsi->u.ws.rx_ubuf_head);
1320
1321                         wsi->u.ws.ping_payload_len = wsi->u.ws.rx_ubuf_head;
1322                         wsi->u.ws.ping_pending_flag = 1;
1323
1324                         /* get it sent as soon as possible */
1325                         lws_callback_on_writable(wsi);
1326 ping_drop:
1327                         wsi->u.ws.rx_ubuf_head = 0;
1328                         return 0;
1329
1330                 case LWSWSOPC_PONG:
1331                         lwsl_info("received pong\n");
1332                         lwsl_hexdump(&wsi->u.ws.rx_ubuf[LWS_PRE],
1333                                      wsi->u.ws.rx_ubuf_head);
1334
1335                         /* issue it */
1336                         callback_action = LWS_CALLBACK_RECEIVE_PONG;
1337                         break;
1338
1339                 case LWSWSOPC_TEXT_FRAME:
1340                 case LWSWSOPC_BINARY_FRAME:
1341                 case LWSWSOPC_CONTINUATION:
1342                         break;
1343
1344                 default:
1345                         lwsl_parser("passing opc %x up to exts\n",
1346                                     wsi->u.ws.opcode);
1347                         /*
1348                          * It's something special we can't understand here.
1349                          * Pass the payload up to the extension's parsing
1350                          * state machine.
1351                          */
1352
1353                         eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1354                         eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1355
1356                         if (lws_ext_cb_active(wsi, LWS_EXT_CB_EXTENDED_PAYLOAD_RX,
1357                                               &eff_buf, 0) <= 0)
1358                                 /* not handle or fail */
1359                                 lwsl_ext("ext opc opcode 0x%x unknown\n",
1360                                          wsi->u.ws.opcode);
1361
1362                         wsi->u.ws.rx_ubuf_head = 0;
1363                         return 0;
1364                 }
1365
1366                 /*
1367                  * No it's real payload, pass it up to the user callback.
1368                  * It's nicely buffered with the pre-padding taken care of
1369                  * so it can be sent straight out again using lws_write
1370                  */
1371
1372                 eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1373                 eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1374
1375 drain_extension:
1376                 lwsl_ext("%s: passing %d to ext\n", __func__, eff_buf.token_len);
1377
1378                 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
1379                     wsi->state == LWSS_AWAITING_CLOSE_ACK)
1380                         goto already_done;
1381
1382                 n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &eff_buf, 0);
1383                 if (n < 0) {
1384                         /*
1385                          * we may rely on this to get RX, just drop connection
1386                          */
1387                         wsi->socket_is_permanently_unusable = 1;
1388                         return -1;
1389                 }
1390
1391                 if (rx_draining_ext && eff_buf.token_len == 0)
1392                         goto already_done;
1393
1394                 if (n && eff_buf.token_len) {
1395                         /* extension had more... main loop will come back */
1396                         wsi->u.ws.rx_draining_ext = 1;
1397                         wsi->u.ws.rx_draining_ext_list = pt->rx_draining_ext_list;
1398                         pt->rx_draining_ext_list = wsi;
1399                 }
1400
1401                 if (eff_buf.token_len > 0 ||
1402                     callback_action == LWS_CALLBACK_RECEIVE_PONG) {
1403                         eff_buf.token[eff_buf.token_len] = '\0';
1404
1405                         if (wsi->protocol->callback) {
1406
1407                                 if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
1408                                         lwsl_info("Doing pong callback\n");
1409
1410                                 ret = user_callback_handle_rxflow(
1411                                                 wsi->protocol->callback,
1412                                                 wsi,
1413                                                 (enum lws_callback_reasons)callback_action,
1414                                                 wsi->user_space,
1415                                                 eff_buf.token,
1416                                                 eff_buf.token_len);
1417                         }
1418                         else
1419                                 lwsl_err("No callback on payload spill!\n");
1420                 }
1421
1422 already_done:
1423                 wsi->u.ws.rx_ubuf_head = 0;
1424                 break;
1425         }
1426
1427         return ret;
1428
1429 illegal_ctl_length:
1430
1431         lwsl_warn("Control frame with xtended length is illegal\n");
1432         /* kill the connection */
1433         return -1;
1434 }
1435
1436
1437 /**
1438  * lws_remaining_packet_payload() - Bytes to come before "overall"
1439  *                                            rx packet is complete
1440  * @wsi:                Websocket instance (available from user callback)
1441  *
1442  *      This function is intended to be called from the callback if the
1443  *  user code is interested in "complete packets" from the client.
1444  *  libwebsockets just passes through payload as it comes and issues a buffer
1445  *  additionally when it hits a built-in limit.  The LWS_CALLBACK_RECEIVE
1446  *  callback handler can use this API to find out if the buffer it has just
1447  *  been given is the last piece of a "complete packet" from the client --
1448  *  when that is the case lws_remaining_packet_payload() will return
1449  *  0.
1450  *
1451  *  Many protocols won't care becuse their packets are always small.
1452  */
1453
1454 LWS_VISIBLE size_t
1455 lws_remaining_packet_payload(struct lws *wsi)
1456 {
1457         return wsi->u.ws.rx_packet_length;
1458 }
1459
1460 /* Once we reach LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED, we know how much
1461  * to expect in that state and can deal with it in bulk more efficiently.
1462  */
1463
1464 void
1465 lws_payload_until_length_exhausted(struct lws *wsi, unsigned char **buf,
1466                                    size_t *len)
1467 {
1468         unsigned char *buffer = *buf, mask[4];
1469         int buffer_size, n;
1470         unsigned int avail;
1471         char *rx_ubuf;
1472
1473         if (wsi->protocol->rx_buffer_size)
1474                 buffer_size = wsi->protocol->rx_buffer_size;
1475         else
1476                 buffer_size = LWS_MAX_SOCKET_IO_BUF;
1477         avail = buffer_size - wsi->u.ws.rx_ubuf_head;
1478
1479         /* do not consume more than we should */
1480         if (avail > wsi->u.ws.rx_packet_length)
1481                 avail = wsi->u.ws.rx_packet_length;
1482
1483         /* do not consume more than what is in the buffer */
1484         if (avail > *len)
1485                 avail = *len;
1486
1487         /* we want to leave 1 byte for the parser to handle properly */
1488         if (avail <= 1)
1489                 return;
1490
1491         avail--;
1492         rx_ubuf = wsi->u.ws.rx_ubuf + LWS_PRE + wsi->u.ws.rx_ubuf_head;
1493         if (wsi->u.ws.all_zero_nonce)
1494                 memcpy(rx_ubuf, buffer, avail);
1495         else {
1496
1497                 for (n = 0; n < 4; n++)
1498                         mask[n] = wsi->u.ws.mask[(wsi->u.ws.mask_idx + n) & 3];
1499
1500                 /* deal with 4-byte chunks using unwrapped loop */
1501                 n = avail >> 2;
1502                 while (n--) {
1503                         *(rx_ubuf++) = *(buffer++) ^ mask[0];
1504                         *(rx_ubuf++) = *(buffer++) ^ mask[1];
1505                         *(rx_ubuf++) = *(buffer++) ^ mask[2];
1506                         *(rx_ubuf++) = *(buffer++) ^ mask[3];
1507                 }
1508                 /* and the remaining bytes bytewise */
1509                 for (n = 0; n < (int)(avail & 3); n++)
1510                         *(rx_ubuf++) = *(buffer++) ^ mask[n];
1511
1512                 wsi->u.ws.mask_idx = (wsi->u.ws.mask_idx + avail) & 3;
1513         }
1514
1515         (*buf) += avail;
1516         wsi->u.ws.rx_ubuf_head += avail;
1517         wsi->u.ws.rx_packet_length -= avail;
1518         *len -= avail;
1519 }