2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
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.
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.
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,
22 #include "private-libwebsockets.h"
24 unsigned char lextable[] = {
28 #define FAIL_CHAR 0x08
30 int lextable_decode(int pos, char c)
35 if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */
36 if ((lextable[pos] & 0x7f) != c)
40 if (lextable[pos] == FAIL_CHAR)
45 if (lextable[pos] == FAIL_CHAR)
48 /* b7 = 0, end or 3-byte */
49 if (lextable[pos] < FAIL_CHAR) /* terminal marker */
52 if (lextable[pos] == c) /* goto */
53 return pos + (lextable[pos + 1]) +
54 (lextable[pos + 2] << 8);
61 int lws_allocate_header_table(struct lws *wsi)
63 struct lws_context *context = wsi->context;
66 lwsl_debug("%s: wsi %p: ah %p\n", __func__, (void *)wsi,
67 (void *)wsi->u.hdr.ah);
69 /* if we are already bound to one, just clear it down */
73 * server should have suppressed the accept of a new wsi before this
74 * became the case. If initiating multiple client connects, make sure
75 * the ah pool is big enough to cope, or be prepared to retry
77 if (context->ah_count_in_use == context->max_http_header_pool) {
78 lwsl_err("No free ah\n");
82 for (n = 0; n < context->max_http_header_pool; n++)
83 if (!context->ah_pool[n].in_use)
86 /* if the count of in use said something free... */
87 assert(n != context->max_http_header_pool);
89 wsi->u.hdr.ah = &context->ah_pool[n];
90 wsi->u.hdr.ah->in_use = 1;
92 context->ah_count_in_use++;
93 /* if we used up all the ah, defeat accepting new server connections */
94 if (context->ah_count_in_use == context->max_http_header_pool)
95 if (_lws_server_listen_accept_flow_control(context, 0))
98 lwsl_debug("%s: wsi %p: ah %p: count %d (on exit)\n",
99 __func__, (void *)wsi, (void *)wsi->u.hdr.ah,
100 context->ah_count_in_use);
103 /* init the ah to reflect no headers or data have appeared yet */
104 memset(wsi->u.hdr.ah->frag_index, 0, sizeof(wsi->u.hdr.ah->frag_index));
105 wsi->u.hdr.ah->nfrag = 0;
106 wsi->u.hdr.ah->pos = 0;
111 int lws_free_header_table(struct lws *wsi)
113 struct lws_context *context = wsi->context;
115 lwsl_debug("%s: wsi %p: ah %p (count = %d)\n", __func__, (void *)wsi,
116 (void *)wsi->u.hdr.ah, context->ah_count_in_use);
118 assert(wsi->u.hdr.ah);
122 /* if we think we're freeing one, there should be one to free */
123 assert(context->ah_count_in_use > 0);
125 assert(wsi->u.hdr.ah->in_use);
126 wsi->u.hdr.ah->in_use = 0;
128 /* if we just freed up one ah, allow new server connection */
129 if (context->ah_count_in_use == context->max_http_header_pool)
130 if (_lws_server_listen_accept_flow_control(context, 1))
133 context->ah_count_in_use--;
134 wsi->u.hdr.ah = NULL;
140 * lws_hdr_fragment_length: report length of a single fragment of a header
141 * The returned length does not include the space for a
144 * @wsi: websocket connection
145 * @h: which header index we are interested in
146 * @frag_idx: which fragment of @h we want to get the length of
150 lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx)
154 n = wsi->u.hdr.ah->frag_index[h];
159 return wsi->u.hdr.ah->frags[n].len;
160 n = wsi->u.hdr.ah->frags[n].nfrag;
161 } while (frag_idx-- && n);
167 * lws_hdr_total_length: report length of all fragments of a header totalled up
168 * The returned length does not include the space for a
171 * @wsi: websocket connection
172 * @h: which header index we are interested in
175 LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h)
180 n = wsi->u.hdr.ah->frag_index[h];
184 len += wsi->u.hdr.ah->frags[n].len;
185 n = wsi->u.hdr.ah->frags[n].nfrag;
192 * lws_hdr_copy_fragment: copy a single fragment of the given header to a buffer
193 * The buffer length @len must include space for an additional
194 * terminating '\0', or it will fail returning -1.
195 * If the requested fragment index is not present, it fails
198 * @wsi: websocket connection
199 * @dst: destination buffer
200 * @len: length of destination buffer
201 * @h: which header index we are interested in
202 * @frag_index: which fragment of @h we want to copy
205 LWS_VISIBLE int lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len,
206 enum lws_token_indexes h, int frag_idx)
209 int f = wsi->u.hdr.ah->frag_index[h];
211 while (n < frag_idx) {
212 f = wsi->u.hdr.ah->frags[f].nfrag;
218 if (wsi->u.hdr.ah->frags[f].len >= len)
221 memcpy(dst, wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[f].offset,
222 wsi->u.hdr.ah->frags[f].len);
223 dst[wsi->u.hdr.ah->frags[f].len] = '\0';
225 return wsi->u.hdr.ah->frags[f].len;
229 * lws_hdr_copy: copy a single fragment of the given header to a buffer
230 * The buffer length @len must include space for an additional
231 * terminating '\0', or it will fail returning -1.
233 * @wsi: websocket connection
234 * @dst: destination buffer
235 * @len: length of destination buffer
236 * @h: which header index we are interested in
239 LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dst, int len,
240 enum lws_token_indexes h)
242 int toklen = lws_hdr_total_length(wsi, h);
248 n = wsi->u.hdr.ah->frag_index[h];
253 strcpy(dst, &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]);
254 dst += wsi->u.hdr.ah->frags[n].len;
255 n = wsi->u.hdr.ah->frags[n].nfrag;
261 char *lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h)
265 n = wsi->u.hdr.ah->frag_index[h];
269 return wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[n].offset;
272 int lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h,
275 wsi->u.hdr.ah->nfrag++;
276 if (wsi->u.hdr.ah->nfrag == ARRAY_SIZE(wsi->u.hdr.ah->frags)) {
277 lwsl_warn("More hdr frags than we can deal with, dropping\n");
281 wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->nfrag;
283 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].offset = wsi->u.hdr.ah->pos;
284 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len = 0;
285 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].nfrag = 0;
288 if (wsi->u.hdr.ah->pos == wsi->context->max_http_header_data) {
289 lwsl_err("Ran out of header data space\n");
292 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s;
294 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
300 static signed char char_to_hex(const char c)
302 if (c >= '0' && c <= '9')
305 if (c >= 'a' && c <= 'f')
308 if (c >= 'A' && c <= 'F')
314 static int issue_char(struct lws *wsi, unsigned char c)
316 unsigned short frag_len;
318 if (wsi->u.hdr.ah->pos == wsi->context->max_http_header_data) {
319 lwsl_warn("excessive header content\n");
323 frag_len = wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len;
325 * If we haven't hit the token limit, just copy the character into
328 if (frag_len < wsi->u.hdr.current_token_limit) {
329 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c;
331 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
335 /* Insert a null character when we *hit* the limit: */
336 if (frag_len == wsi->u.hdr.current_token_limit) {
337 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
338 lwsl_warn("header %i exceeds limit %d\n",
339 wsi->u.hdr.parser_state, wsi->u.hdr.current_token_limit);
345 int lws_parse(struct lws *wsi, unsigned char c)
347 static const unsigned char methods[] = {
350 WSI_TOKEN_OPTIONS_URI,
353 WSI_TOKEN_DELETE_URI,
355 struct allocated_headers *ah = wsi->u.hdr.ah;
356 struct lws_context *context = wsi->context;
357 unsigned int n, m, enc = 0;
359 switch (wsi->u.hdr.parser_state) {
362 lwsl_parser("WSI_TOK_(%d) '%c'\n", wsi->u.hdr.parser_state, c);
364 /* collect into malloc'd buffers */
365 /* optional initial space swallow */
366 if (!ah->frags[ah->frag_index[
367 wsi->u.hdr.parser_state]].len && c == ' ')
370 for (m = 0; m < ARRAY_SIZE(methods); m++)
371 if (wsi->u.hdr.parser_state == methods[m])
373 if (m == ARRAY_SIZE(methods))
374 /* it was not any of the methods */
377 /* special URI processing... end at space */
380 /* enforce starting with / */
381 if (!ah->frags[ah->nfrag].len)
382 if (issue_char(wsi, '/') < 0)
385 /* begin parsing HTTP version: */
386 if (issue_char(wsi, '\0') < 0)
388 wsi->u.hdr.parser_state = WSI_TOKEN_HTTP;
392 /* special URI processing... convert %xx */
394 switch (wsi->u.hdr.ues) {
397 wsi->u.hdr.ues = URIES_SEEN_PERCENT;
401 case URIES_SEEN_PERCENT:
402 if (char_to_hex(c) < 0) {
404 if (issue_char(wsi, '%') < 0)
406 wsi->u.hdr.ues = URIES_IDLE;
407 /* continue on to assess c */
410 wsi->u.hdr.esc_stash = c;
411 wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1;
414 case URIES_SEEN_PERCENT_H1:
415 if (char_to_hex(c) < 0) {
417 issue_char(wsi, '%');
418 wsi->u.hdr.ues = URIES_IDLE;
419 /* regurgitate + assess */
420 if (lws_parse(wsi, wsi->u.hdr.esc_stash) < 0)
422 /* continue on to assess c */
425 c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) |
428 wsi->u.hdr.ues = URIES_IDLE;
433 * special URI processing...
434 * convert /.. or /... or /../ etc to /
436 * convert // or /// etc to /
437 * leave /.dir or whatever alone
440 switch (wsi->u.hdr.ups) {
442 /* genuine delimiter */
443 if ((c == '&' || c == ';') && !enc) {
445 /* swallow the terminator */
446 ah->frags[ah->nfrag].len--;
447 /* link to next fragment */
448 ah->frags[ah->nfrag].nfrag = ah->nfrag + 1;
450 if (ah->nfrag >= ARRAY_SIZE(ah->frags))
452 /* start next fragment after the & */
453 wsi->u.hdr.post_literal_equal = 0;
454 ah->frags[ah->nfrag].offset = ah->pos;
455 ah->frags[ah->nfrag].len = 0;
456 ah->frags[ah->nfrag].nfrag = 0;
459 /* uriencoded = in the name part, disallow */
460 if (c == '=' && enc && !wsi->u.hdr.post_literal_equal)
463 /* after the real =, we don't care how many = */
464 if (c == '=' && !enc)
465 wsi->u.hdr.post_literal_equal = 1;
468 if (c == '+' && !enc)
470 /* issue the first / always */
471 if (c == '/' && !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS])
472 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
474 case URIPS_SEEN_SLASH:
475 /* swallow subsequent slashes */
478 /* track and swallow the first . after / */
480 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
483 wsi->u.hdr.ups = URIPS_IDLE;
485 case URIPS_SEEN_SLASH_DOT:
486 /* swallow second . */
489 * back up one dir level if possible
490 * safe against header fragmentation because
491 * the method URI can only be in 1 fragment
493 if (ah->frags[ah->nfrag].len > 2) {
495 ah->frags[ah->nfrag].len--;
498 ah->frags[ah->nfrag].len--;
499 } while (ah->frags[ah->nfrag].len > 1 &&
500 ah->data[ah->pos] != '/');
502 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
505 /* change /./ to / */
507 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
510 /* it was like /.dir ... regurgitate the . */
511 wsi->u.hdr.ups = URIPS_IDLE;
512 if (issue_char(wsi, '.') < 0)
516 case URIPS_SEEN_SLASH_DOT_DOT:
517 /* swallow prior .. chars and any subsequent . */
520 /* last issued was /, so another / == // */
523 /* last we issued was / so SEEN_SLASH */
524 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
528 if (c == '?' && !enc &&
529 !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS]) { /* start of URI arguments */
530 /* seal off uri header */
531 ah->data[ah->pos++] = '\0';
533 /* move to using WSI_TOKEN_HTTP_URI_ARGS */
535 if (ah->nfrag >= ARRAY_SIZE(ah->frags))
537 ah->frags[ah->nfrag].offset = ah->pos;
538 ah->frags[ah->nfrag].len = 0;
539 ah->frags[ah->nfrag].nfrag = 0;
541 wsi->u.hdr.post_literal_equal = 0;
542 ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = ah->nfrag;
543 wsi->u.hdr.ups = URIPS_IDLE;
550 if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
553 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
557 n = issue_char(wsi, c);
561 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
564 /* per-protocol end of headers management */
566 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
567 goto set_parsing_complete;
570 /* collecting and checking a name part */
571 case WSI_TOKEN_NAME_PART:
572 lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode);
574 wsi->u.hdr.lextable_pos =
575 lextable_decode(wsi->u.hdr.lextable_pos, c);
577 * Server needs to look out for unknown methods...
579 if (wsi->u.hdr.lextable_pos < 0 &&
580 wsi->mode == LWSCM_HTTP_SERVING) {
581 /* this is not a header we know about */
582 for (m = 0; m < ARRAY_SIZE(methods); m++)
583 if (ah->frag_index[methods[m]]) {
585 * already had the method, no idea what
586 * this crap from the client is, ignore
588 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
592 * hm it's an unknown http method from a client in fact,
595 if (m == ARRAY_SIZE(methods)) {
596 lwsl_info("Unknown method - dropping\n");
602 * ...otherwise for a client, let him ignore unknown headers
603 * coming from the server
605 if (wsi->u.hdr.lextable_pos < 0) {
606 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
610 if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
613 n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) |
614 lextable[wsi->u.hdr.lextable_pos + 1];
616 lwsl_parser("known hdr %d\n", n);
617 for (m = 0; m < ARRAY_SIZE(methods); m++)
618 if (n == methods[m] &&
621 lwsl_warn("Duplicated method\n");
626 * WSORIGIN is protocol equiv to ORIGIN,
627 * JWebSocket likes to send it, map to ORIGIN
629 if (n == WSI_TOKEN_SWORIGIN)
630 n = WSI_TOKEN_ORIGIN;
632 wsi->u.hdr.parser_state = (enum lws_token_indexes)
633 (WSI_TOKEN_GET_URI + n);
635 if (context->token_limits)
636 wsi->u.hdr.current_token_limit =
637 context->token_limits->token_limit[
638 wsi->u.hdr.parser_state];
640 wsi->u.hdr.current_token_limit =
641 wsi->context->max_http_header_data;
643 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
644 goto set_parsing_complete;
653 if (ah->nfrag == ARRAY_SIZE(ah->frags)) {
654 lwsl_warn("More hdr frags than we can deal with\n");
658 ah->frags[ah->nfrag].offset = ah->pos;
659 ah->frags[ah->nfrag].len = 0;
660 ah->frags[ ah->nfrag].nfrag = 0;
662 n = ah->frag_index[wsi->u.hdr.parser_state];
663 if (!n) { /* first fragment */
664 ah->frag_index[wsi->u.hdr.parser_state] = ah->nfrag;
668 while (ah->frags[n].nfrag)
669 n = ah->frags[n].nfrag;
670 ah->frags[n].nfrag = ah->nfrag;
672 if (ah->pos == wsi->context->max_http_header_data) {
673 lwsl_warn("excessive header content\n");
677 ah->data[ah->pos++] = ' ';
678 ah->frags[ah->nfrag].len++;
681 /* skipping arg part of a name we didn't recognize */
682 case WSI_TOKEN_SKIPPING:
683 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
686 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
689 case WSI_TOKEN_SKIPPING_SAW_CR:
690 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
692 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
693 wsi->u.hdr.lextable_pos = 0;
695 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
697 /* we're done, ignore anything else */
699 case WSI_PARSING_COMPLETE:
700 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
706 set_parsing_complete:
708 if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
709 if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
710 wsi->ietf_spec_revision =
711 atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
713 lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
715 wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
716 wsi->hdr_parsing_completed = 1;
723 * lws_frame_is_binary: true if the current frame was sent in binary mode
725 * @wsi: the connection we are inquiring about
727 * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
728 * it's interested to see if the frame it's dealing with was sent in binary
732 LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
734 return wsi->u.ws.frame_is_binary;
738 lws_rx_sm(struct lws *wsi, unsigned char c)
740 struct lws_tokens eff_buf;
741 int ret = 0, n, rx_draining_ext = 0;
742 int callback_action = LWS_CALLBACK_RECEIVE;
743 if (wsi->socket_is_permanently_unusable)
746 switch (wsi->lws_rx_parse_state) {
748 if (wsi->u.ws.rx_draining_ext) {
749 struct lws **w = &wsi->context->rx_draining_ext_list;
751 eff_buf.token = NULL;
752 eff_buf.token_len = 0;
753 wsi->u.ws.rx_draining_ext = 0;
754 /* remove us from context draining ext list */
757 *w = wsi->u.ws.rx_draining_ext_list;
760 w = &((*w)->u.ws.rx_draining_ext_list);
762 wsi->u.ws.rx_draining_ext_list = NULL;
764 lwsl_err("%s: doing draining flow\n", __func__);
766 goto drain_extension;
768 switch (wsi->ietf_spec_revision) {
771 * no prepended frame key any more
773 wsi->u.ws.all_zero_nonce = 1;
777 lwsl_warn("lws_rx_sm: unknown spec version %d\n",
778 wsi->ietf_spec_revision);
782 case LWS_RXPS_04_mask_1:
783 wsi->u.ws.mask[1] = c;
785 wsi->u.ws.all_zero_nonce = 0;
786 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_2;
788 case LWS_RXPS_04_mask_2:
789 wsi->u.ws.mask[2] = c;
791 wsi->u.ws.all_zero_nonce = 0;
792 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_3;
794 case LWS_RXPS_04_mask_3:
795 wsi->u.ws.mask[3] = c;
797 wsi->u.ws.all_zero_nonce = 0;
800 * start from the zero'th byte in the XOR key buffer since
801 * this is the start of a frame with a new key
804 wsi->u.ws.mask_idx = 0;
806 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
810 * 04 logical framing from the spec (all this is masked when incoming
811 * and has to be unmasked)
813 * We ignore the possibility of extension data because we don't
814 * negotiate any extensions at the moment.
817 * 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
818 * +-+-+-+-+-------+-+-------------+-------------------------------+
819 * |F|R|R|R| opcode|R| Payload len | Extended payload length |
820 * |I|S|S|S| (4) |S| (7) | (16/63) |
821 * |N|V|V|V| |V| | (if payload len==126/127) |
823 * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
824 * | Extended payload length continued, if payload len == 127 |
825 * + - - - - - - - - - - - - - - - +-------------------------------+
826 * | | Extension data |
827 * +-------------------------------+ - - - - - - - - - - - - - - - +
829 * +---------------------------------------------------------------+
830 * : Application data :
831 * +---------------------------------------------------------------+
833 * We pass payload through to userland as soon as we get it, ignoring
834 * FIN. It's up to userland to buffer it up if it wants to see a
835 * whole unfragmented block of the original size (which may be up to
839 case LWS_RXPS_04_FRAME_HDR_1:
842 wsi->u.ws.opcode = c & 0xf;
843 wsi->u.ws.rsv = c & 0x70;
844 wsi->u.ws.final = !!((c >> 7) & 1);
846 switch (wsi->u.ws.opcode) {
847 case LWSWSOPC_TEXT_FRAME:
848 case LWSWSOPC_BINARY_FRAME:
849 wsi->u.ws.rsv_first_msg = (c & 0x70);
850 wsi->u.ws.frame_is_binary =
851 wsi->u.ws.opcode == LWSWSOPC_BINARY_FRAME;
863 lwsl_info("illegal opcode\n");
866 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
869 case LWS_RXPS_04_FRAME_HDR_LEN:
871 wsi->u.ws.this_frame_masked = !!(c & 0x80);
875 /* control frames are not allowed to have big lengths */
876 if (wsi->u.ws.opcode & 8)
877 goto illegal_ctl_length;
879 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
882 /* control frames are not allowed to have big lengths */
883 if (wsi->u.ws.opcode & 8)
884 goto illegal_ctl_length;
886 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
889 wsi->u.ws.rx_packet_length = c & 0x7f;
890 if (wsi->u.ws.this_frame_masked)
891 wsi->lws_rx_parse_state =
892 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
894 if (wsi->u.ws.rx_packet_length)
895 wsi->lws_rx_parse_state =
896 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
898 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
905 case LWS_RXPS_04_FRAME_HDR_LEN16_2:
906 wsi->u.ws.rx_packet_length = c << 8;
907 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
910 case LWS_RXPS_04_FRAME_HDR_LEN16_1:
911 wsi->u.ws.rx_packet_length |= c;
912 if (wsi->u.ws.this_frame_masked)
913 wsi->lws_rx_parse_state =
914 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
916 wsi->lws_rx_parse_state =
917 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
920 case LWS_RXPS_04_FRAME_HDR_LEN64_8:
922 lwsl_warn("b63 of length must be zero\n");
923 /* kill the connection */
927 wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
929 wsi->u.ws.rx_packet_length = 0;
931 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
934 case LWS_RXPS_04_FRAME_HDR_LEN64_7:
936 wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
938 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
941 case LWS_RXPS_04_FRAME_HDR_LEN64_6:
943 wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
945 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
948 case LWS_RXPS_04_FRAME_HDR_LEN64_5:
950 wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
952 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
955 case LWS_RXPS_04_FRAME_HDR_LEN64_4:
956 wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
957 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
960 case LWS_RXPS_04_FRAME_HDR_LEN64_3:
961 wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
962 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
965 case LWS_RXPS_04_FRAME_HDR_LEN64_2:
966 wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
967 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
970 case LWS_RXPS_04_FRAME_HDR_LEN64_1:
971 wsi->u.ws.rx_packet_length |= ((size_t)c);
972 if (wsi->u.ws.this_frame_masked)
973 wsi->lws_rx_parse_state =
974 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
976 wsi->lws_rx_parse_state =
977 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
980 case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
981 wsi->u.ws.mask[0] = c;
983 wsi->u.ws.all_zero_nonce = 0;
984 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
987 case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
988 wsi->u.ws.mask[1] = c;
990 wsi->u.ws.all_zero_nonce = 0;
991 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
994 case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
995 wsi->u.ws.mask[2] = c;
997 wsi->u.ws.all_zero_nonce = 0;
998 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
1001 case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
1002 wsi->u.ws.mask[3] = c;
1004 wsi->u.ws.all_zero_nonce = 0;
1005 wsi->lws_rx_parse_state =
1006 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1007 wsi->u.ws.mask_idx = 0;
1008 if (wsi->u.ws.rx_packet_length == 0) {
1009 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1015 case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
1017 assert(wsi->u.ws.rx_ubuf);
1019 if (wsi->u.ws.rx_ubuf_head + LWS_PRE + 4 >= wsi->u.ws.rx_ubuf_alloc) {
1020 lwsl_err("Attempted overflow\n");
1023 if (wsi->u.ws.all_zero_nonce)
1024 wsi->u.ws.rx_ubuf[LWS_PRE +
1025 (wsi->u.ws.rx_ubuf_head++)] = c;
1027 wsi->u.ws.rx_ubuf[LWS_PRE +
1028 (wsi->u.ws.rx_ubuf_head++)] =
1030 (wsi->u.ws.mask_idx++) & 3];
1032 if (--wsi->u.ws.rx_packet_length == 0) {
1033 /* spill because we have the whole frame */
1034 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1039 * if there's no protocol max frame size given, we are
1040 * supposed to default to LWS_MAX_SOCKET_IO_BUF
1043 if (!wsi->protocol->rx_buffer_size &&
1044 wsi->u.ws.rx_ubuf_head !=
1045 LWS_MAX_SOCKET_IO_BUF)
1048 if (wsi->protocol->rx_buffer_size &&
1049 wsi->u.ws.rx_ubuf_head !=
1050 wsi->protocol->rx_buffer_size)
1053 /* spill because we filled our rx buffer */
1056 * is this frame a control packet we should take care of at this
1057 * layer? If so service it and hide it from the user callback
1060 lwsl_parser("spill on %s\n", wsi->protocol->name);
1062 switch (wsi->u.ws.opcode) {
1063 case LWSWSOPC_CLOSE:
1065 /* is this an acknowledgement of our close? */
1066 if (wsi->state == LWSS_AWAITING_CLOSE_ACK) {
1068 * fine he has told us he is closing too, let's
1071 lwsl_parser("seen client close ack\n");
1074 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
1075 /* if he sends us 2 CLOSE, kill him */
1078 if (user_callback_handle_rxflow(
1079 wsi->protocol->callback, wsi,
1080 LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
1084 wsi->u.ws.rx_ubuf_head))
1087 lwsl_parser("server sees client close packet\n");
1088 wsi->state = LWSS_RETURNED_CLOSE_ALREADY;
1089 /* deal with the close packet contents as a PONG */
1090 wsi->u.ws.payload_is_close = 1;
1091 goto process_as_ping;
1094 lwsl_info("received %d byte ping, sending pong\n",
1095 wsi->u.ws.rx_ubuf_head);
1097 if (wsi->u.ws.ping_pending_flag) {
1099 * there is already a pending ping payload
1100 * we should just log and drop
1102 lwsl_parser("DROP PING since one pending\n");
1106 /* control packets can only be < 128 bytes long */
1107 if (wsi->u.ws.rx_ubuf_head > 128 - 3) {
1108 lwsl_parser("DROP PING payload too large\n");
1112 /* stash the pong payload */
1113 memcpy(wsi->u.ws.ping_payload_buf + LWS_PRE,
1114 &wsi->u.ws.rx_ubuf[LWS_PRE],
1115 wsi->u.ws.rx_ubuf_head);
1117 wsi->u.ws.ping_payload_len = wsi->u.ws.rx_ubuf_head;
1118 wsi->u.ws.ping_pending_flag = 1;
1120 /* get it sent as soon as possible */
1121 lws_callback_on_writable(wsi);
1123 wsi->u.ws.rx_ubuf_head = 0;
1127 lwsl_info("received pong\n");
1128 lwsl_hexdump(&wsi->u.ws.rx_ubuf[LWS_PRE],
1129 wsi->u.ws.rx_ubuf_head);
1132 callback_action = LWS_CALLBACK_RECEIVE_PONG;
1135 case LWSWSOPC_TEXT_FRAME:
1136 case LWSWSOPC_BINARY_FRAME:
1137 case LWSWSOPC_CONTINUATION:
1141 lwsl_parser("passing opc %x up to exts\n",
1144 * It's something special we can't understand here.
1145 * Pass the payload up to the extension's parsing
1149 eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1150 eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1152 if (lws_ext_cb_active(wsi, LWS_EXT_CB_EXTENDED_PAYLOAD_RX,
1153 &eff_buf, 0) <= 0) /* not handle or fail */
1154 lwsl_ext("ext opc opcode 0x%x unknown\n",
1157 wsi->u.ws.rx_ubuf_head = 0;
1162 * No it's real payload, pass it up to the user callback.
1163 * It's nicely buffered with the pre-padding taken care of
1164 * so it can be sent straight out again using lws_write
1167 eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1168 eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1171 lwsl_ext("%s: passing %d to ext\n", __func__, eff_buf.token_len);
1173 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
1174 wsi->state == LWSS_AWAITING_CLOSE_ACK)
1177 n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &eff_buf, 0);
1180 * we may rely on this to get RX, just drop connection
1182 wsi->socket_is_permanently_unusable = 1;
1186 if (rx_draining_ext && eff_buf.token_len == 0)
1189 if (n && eff_buf.token_len) {
1190 /* extension had more... main loop will come back */
1191 wsi->u.ws.rx_draining_ext = 1;
1192 wsi->u.ws.rx_draining_ext_list = wsi->context->rx_draining_ext_list;
1193 wsi->context->rx_draining_ext_list = wsi;
1196 if (eff_buf.token_len > 0 ||
1197 callback_action == LWS_CALLBACK_RECEIVE_PONG) {
1198 eff_buf.token[eff_buf.token_len] = '\0';
1200 if (wsi->protocol->callback) {
1202 if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
1203 lwsl_info("Doing pong callback\n");
1205 ret = user_callback_handle_rxflow(
1206 wsi->protocol->callback,
1208 (enum lws_callback_reasons)callback_action,
1214 lwsl_err("No callback on payload spill!\n");
1218 wsi->u.ws.rx_ubuf_head = 0;
1226 lwsl_warn("Control frame with xtended length is illegal\n");
1227 /* kill the connection */
1233 * lws_remaining_packet_payload() - Bytes to come before "overall"
1234 * rx packet is complete
1235 * @wsi: Websocket instance (available from user callback)
1237 * This function is intended to be called from the callback if the
1238 * user code is interested in "complete packets" from the client.
1239 * libwebsockets just passes through payload as it comes and issues a buffer
1240 * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
1241 * callback handler can use this API to find out if the buffer it has just
1242 * been given is the last piece of a "complete packet" from the client --
1243 * when that is the case lws_remaining_packet_payload() will return
1246 * Many protocols won't care becuse their packets are always small.
1250 lws_remaining_packet_payload(struct lws *wsi)
1252 return wsi->u.ws.rx_packet_length;