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 if (wsi->u.hdr.ah->pos == wsi->context->max_http_header_data) {
338 lwsl_warn("excessive header content 2\n");
341 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
342 lwsl_warn("header %i exceeds limit %d\n",
343 wsi->u.hdr.parser_state, wsi->u.hdr.current_token_limit);
349 int lws_parse(struct lws *wsi, unsigned char c)
351 static const unsigned char methods[] = {
354 WSI_TOKEN_OPTIONS_URI,
357 WSI_TOKEN_DELETE_URI,
359 struct allocated_headers *ah = wsi->u.hdr.ah;
360 struct lws_context *context = wsi->context;
361 unsigned int n, m, enc = 0;
363 switch (wsi->u.hdr.parser_state) {
366 lwsl_parser("WSI_TOK_(%d) '%c'\n", wsi->u.hdr.parser_state, c);
368 /* collect into malloc'd buffers */
369 /* optional initial space swallow */
370 if (!ah->frags[ah->frag_index[
371 wsi->u.hdr.parser_state]].len && c == ' ')
374 for (m = 0; m < ARRAY_SIZE(methods); m++)
375 if (wsi->u.hdr.parser_state == methods[m])
377 if (m == ARRAY_SIZE(methods))
378 /* it was not any of the methods */
381 /* special URI processing... end at space */
384 /* enforce starting with / */
385 if (!ah->frags[ah->nfrag].len)
386 if (issue_char(wsi, '/') < 0)
389 /* begin parsing HTTP version: */
390 if (issue_char(wsi, '\0') < 0)
392 wsi->u.hdr.parser_state = WSI_TOKEN_HTTP;
396 /* special URI processing... convert %xx */
398 switch (wsi->u.hdr.ues) {
401 wsi->u.hdr.ues = URIES_SEEN_PERCENT;
405 case URIES_SEEN_PERCENT:
406 if (char_to_hex(c) < 0) {
408 if (issue_char(wsi, '%') < 0)
410 wsi->u.hdr.ues = URIES_IDLE;
411 /* continue on to assess c */
414 wsi->u.hdr.esc_stash = c;
415 wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1;
418 case URIES_SEEN_PERCENT_H1:
419 if (char_to_hex(c) < 0) {
421 issue_char(wsi, '%');
422 wsi->u.hdr.ues = URIES_IDLE;
423 /* regurgitate + assess */
424 if (lws_parse(wsi, wsi->u.hdr.esc_stash) < 0)
426 /* continue on to assess c */
429 c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) |
432 wsi->u.hdr.ues = URIES_IDLE;
437 * special URI processing...
438 * convert /.. or /... or /../ etc to /
440 * convert // or /// etc to /
441 * leave /.dir or whatever alone
444 switch (wsi->u.hdr.ups) {
448 /* genuine delimiter */
449 if ((c == '&' || c == ';') && !enc) {
450 if (issue_char(wsi, c) < 0)
452 /* swallow the terminator */
453 ah->frags[ah->nfrag].len--;
454 /* link to next fragment */
455 ah->frags[ah->nfrag].nfrag = ah->nfrag + 1;
457 if (ah->nfrag >= ARRAY_SIZE(ah->frags))
459 /* start next fragment after the & */
460 wsi->u.hdr.post_literal_equal = 0;
461 ah->frags[ah->nfrag].offset = ah->pos;
462 ah->frags[ah->nfrag].len = 0;
463 ah->frags[ah->nfrag].nfrag = 0;
466 /* uriencoded = in the name part, disallow */
467 if (c == '=' && enc && !wsi->u.hdr.post_literal_equal)
470 /* after the real =, we don't care how many = */
471 if (c == '=' && !enc)
472 wsi->u.hdr.post_literal_equal = 1;
475 if (c == '+' && !enc)
477 /* issue the first / always */
478 if (c == '/' && !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS])
479 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
481 case URIPS_SEEN_SLASH:
482 /* swallow subsequent slashes */
485 /* track and swallow the first . after / */
487 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
490 wsi->u.hdr.ups = URIPS_IDLE;
492 case URIPS_SEEN_SLASH_DOT:
493 /* swallow second . */
496 * back up one dir level if possible
497 * safe against header fragmentation because
498 * the method URI can only be in 1 fragment
500 if (ah->frags[ah->nfrag].len > 2) {
502 ah->frags[ah->nfrag].len--;
505 ah->frags[ah->nfrag].len--;
506 } while (ah->frags[ah->nfrag].len > 1 &&
507 ah->data[ah->pos] != '/');
509 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
512 /* change /./ to / */
514 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
517 /* it was like /.dir ... regurgitate the . */
518 wsi->u.hdr.ups = URIPS_IDLE;
519 if (issue_char(wsi, '.') < 0)
523 case URIPS_SEEN_SLASH_DOT_DOT:
524 /* swallow prior .. chars and any subsequent . */
527 /* last issued was /, so another / == // */
530 /* last we issued was / so SEEN_SLASH */
531 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
535 if (c == '?' && !enc &&
536 !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS]) { /* start of URI arguments */
537 /* seal off uri header */
538 if (issue_char(wsi, '\0') < 0)
541 /* move to using WSI_TOKEN_HTTP_URI_ARGS */
543 if (ah->nfrag >= ARRAY_SIZE(ah->frags))
545 ah->frags[ah->nfrag].offset = ah->pos;
546 ah->frags[ah->nfrag].len = 0;
547 ah->frags[ah->nfrag].nfrag = 0;
549 wsi->u.hdr.post_literal_equal = 0;
550 ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = ah->nfrag;
551 wsi->u.hdr.ups = URIPS_IDLE;
558 if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
561 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
565 n = issue_char(wsi, c);
569 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
572 /* per-protocol end of headers management */
574 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
575 goto set_parsing_complete;
578 /* collecting and checking a name part */
579 case WSI_TOKEN_NAME_PART:
580 lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode);
582 wsi->u.hdr.lextable_pos =
583 lextable_decode(wsi->u.hdr.lextable_pos, c);
585 * Server needs to look out for unknown methods...
587 if (wsi->u.hdr.lextable_pos < 0 &&
588 wsi->mode == LWSCM_HTTP_SERVING) {
589 /* this is not a header we know about */
590 for (m = 0; m < ARRAY_SIZE(methods); m++)
591 if (ah->frag_index[methods[m]]) {
593 * already had the method, no idea what
594 * this crap from the client is, ignore
596 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
600 * hm it's an unknown http method from a client in fact,
603 if (m == ARRAY_SIZE(methods)) {
604 lwsl_info("Unknown method - dropping\n");
610 * ...otherwise for a client, let him ignore unknown headers
611 * coming from the server
613 if (wsi->u.hdr.lextable_pos < 0) {
614 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
618 if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
621 n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) |
622 lextable[wsi->u.hdr.lextable_pos + 1];
624 lwsl_parser("known hdr %d\n", n);
625 for (m = 0; m < ARRAY_SIZE(methods); m++)
626 if (n == methods[m] &&
629 lwsl_warn("Duplicated method\n");
634 * WSORIGIN is protocol equiv to ORIGIN,
635 * JWebSocket likes to send it, map to ORIGIN
637 if (n == WSI_TOKEN_SWORIGIN)
638 n = WSI_TOKEN_ORIGIN;
640 wsi->u.hdr.parser_state = (enum lws_token_indexes)
641 (WSI_TOKEN_GET_URI + n);
643 if (context->token_limits)
644 wsi->u.hdr.current_token_limit =
645 context->token_limits->token_limit[
646 wsi->u.hdr.parser_state];
648 wsi->u.hdr.current_token_limit =
649 wsi->context->max_http_header_data;
651 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
652 goto set_parsing_complete;
661 if (ah->nfrag == ARRAY_SIZE(ah->frags)) {
662 lwsl_warn("More hdr frags than we can deal with\n");
666 ah->frags[ah->nfrag].offset = ah->pos;
667 ah->frags[ah->nfrag].len = 0;
668 ah->frags[ ah->nfrag].nfrag = 0;
670 n = ah->frag_index[wsi->u.hdr.parser_state];
671 if (!n) { /* first fragment */
672 ah->frag_index[wsi->u.hdr.parser_state] = ah->nfrag;
676 while (ah->frags[n].nfrag)
677 n = ah->frags[n].nfrag;
678 ah->frags[n].nfrag = ah->nfrag;
680 if (issue_char(wsi, ' ') < 0)
684 /* skipping arg part of a name we didn't recognize */
685 case WSI_TOKEN_SKIPPING:
686 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
689 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
692 case WSI_TOKEN_SKIPPING_SAW_CR:
693 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
695 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
696 wsi->u.hdr.lextable_pos = 0;
698 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
700 /* we're done, ignore anything else */
702 case WSI_PARSING_COMPLETE:
703 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
709 set_parsing_complete:
711 if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
712 if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
713 wsi->ietf_spec_revision =
714 atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
716 lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
718 wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
719 wsi->hdr_parsing_completed = 1;
726 * lws_frame_is_binary: true if the current frame was sent in binary mode
728 * @wsi: the connection we are inquiring about
730 * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
731 * it's interested to see if the frame it's dealing with was sent in binary
735 LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
737 return wsi->u.ws.frame_is_binary;
741 lws_rx_sm(struct lws *wsi, unsigned char c)
743 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
744 struct lws_tokens eff_buf;
745 int ret = 0, n, rx_draining_ext = 0;
746 int callback_action = LWS_CALLBACK_RECEIVE;
747 if (wsi->socket_is_permanently_unusable)
750 switch (wsi->lws_rx_parse_state) {
752 if (wsi->u.ws.rx_draining_ext) {
753 struct lws **w = &pt->rx_draining_ext_list;
755 eff_buf.token = NULL;
756 eff_buf.token_len = 0;
757 wsi->u.ws.rx_draining_ext = 0;
758 /* remove us from context draining ext list */
761 *w = wsi->u.ws.rx_draining_ext_list;
764 w = &((*w)->u.ws.rx_draining_ext_list);
766 wsi->u.ws.rx_draining_ext_list = NULL;
768 lwsl_err("%s: doing draining flow\n", __func__);
770 goto drain_extension;
772 switch (wsi->ietf_spec_revision) {
775 * no prepended frame key any more
777 wsi->u.ws.all_zero_nonce = 1;
781 lwsl_warn("lws_rx_sm: unknown spec version %d\n",
782 wsi->ietf_spec_revision);
786 case LWS_RXPS_04_mask_1:
787 wsi->u.ws.mask[1] = c;
789 wsi->u.ws.all_zero_nonce = 0;
790 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_2;
792 case LWS_RXPS_04_mask_2:
793 wsi->u.ws.mask[2] = c;
795 wsi->u.ws.all_zero_nonce = 0;
796 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_3;
798 case LWS_RXPS_04_mask_3:
799 wsi->u.ws.mask[3] = c;
801 wsi->u.ws.all_zero_nonce = 0;
804 * start from the zero'th byte in the XOR key buffer since
805 * this is the start of a frame with a new key
808 wsi->u.ws.mask_idx = 0;
810 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
814 * 04 logical framing from the spec (all this is masked when incoming
815 * and has to be unmasked)
817 * We ignore the possibility of extension data because we don't
818 * negotiate any extensions at the moment.
821 * 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
822 * +-+-+-+-+-------+-+-------------+-------------------------------+
823 * |F|R|R|R| opcode|R| Payload len | Extended payload length |
824 * |I|S|S|S| (4) |S| (7) | (16/63) |
825 * |N|V|V|V| |V| | (if payload len==126/127) |
827 * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
828 * | Extended payload length continued, if payload len == 127 |
829 * + - - - - - - - - - - - - - - - +-------------------------------+
830 * | | Extension data |
831 * +-------------------------------+ - - - - - - - - - - - - - - - +
833 * +---------------------------------------------------------------+
834 * : Application data :
835 * +---------------------------------------------------------------+
837 * We pass payload through to userland as soon as we get it, ignoring
838 * FIN. It's up to userland to buffer it up if it wants to see a
839 * whole unfragmented block of the original size (which may be up to
843 case LWS_RXPS_04_FRAME_HDR_1:
846 wsi->u.ws.opcode = c & 0xf;
847 wsi->u.ws.rsv = c & 0x70;
848 wsi->u.ws.final = !!((c >> 7) & 1);
850 switch (wsi->u.ws.opcode) {
851 case LWSWSOPC_TEXT_FRAME:
852 case LWSWSOPC_BINARY_FRAME:
853 wsi->u.ws.rsv_first_msg = (c & 0x70);
854 wsi->u.ws.frame_is_binary =
855 wsi->u.ws.opcode == LWSWSOPC_BINARY_FRAME;
867 lwsl_info("illegal opcode\n");
870 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
873 case LWS_RXPS_04_FRAME_HDR_LEN:
875 wsi->u.ws.this_frame_masked = !!(c & 0x80);
879 /* control frames are not allowed to have big lengths */
880 if (wsi->u.ws.opcode & 8)
881 goto illegal_ctl_length;
883 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
886 /* control frames are not allowed to have big lengths */
887 if (wsi->u.ws.opcode & 8)
888 goto illegal_ctl_length;
890 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
893 wsi->u.ws.rx_packet_length = c & 0x7f;
894 if (wsi->u.ws.this_frame_masked)
895 wsi->lws_rx_parse_state =
896 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
898 if (wsi->u.ws.rx_packet_length)
899 wsi->lws_rx_parse_state =
900 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
902 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
909 case LWS_RXPS_04_FRAME_HDR_LEN16_2:
910 wsi->u.ws.rx_packet_length = c << 8;
911 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
914 case LWS_RXPS_04_FRAME_HDR_LEN16_1:
915 wsi->u.ws.rx_packet_length |= c;
916 if (wsi->u.ws.this_frame_masked)
917 wsi->lws_rx_parse_state =
918 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
920 wsi->lws_rx_parse_state =
921 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
924 case LWS_RXPS_04_FRAME_HDR_LEN64_8:
926 lwsl_warn("b63 of length must be zero\n");
927 /* kill the connection */
931 wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
933 wsi->u.ws.rx_packet_length = 0;
935 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
938 case LWS_RXPS_04_FRAME_HDR_LEN64_7:
940 wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
942 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
945 case LWS_RXPS_04_FRAME_HDR_LEN64_6:
947 wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
949 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
952 case LWS_RXPS_04_FRAME_HDR_LEN64_5:
954 wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
956 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
959 case LWS_RXPS_04_FRAME_HDR_LEN64_4:
960 wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
961 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
964 case LWS_RXPS_04_FRAME_HDR_LEN64_3:
965 wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
966 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
969 case LWS_RXPS_04_FRAME_HDR_LEN64_2:
970 wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
971 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
974 case LWS_RXPS_04_FRAME_HDR_LEN64_1:
975 wsi->u.ws.rx_packet_length |= ((size_t)c);
976 if (wsi->u.ws.this_frame_masked)
977 wsi->lws_rx_parse_state =
978 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
980 wsi->lws_rx_parse_state =
981 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
984 case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
985 wsi->u.ws.mask[0] = c;
987 wsi->u.ws.all_zero_nonce = 0;
988 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
991 case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
992 wsi->u.ws.mask[1] = c;
994 wsi->u.ws.all_zero_nonce = 0;
995 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
998 case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
999 wsi->u.ws.mask[2] = c;
1001 wsi->u.ws.all_zero_nonce = 0;
1002 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
1005 case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
1006 wsi->u.ws.mask[3] = c;
1008 wsi->u.ws.all_zero_nonce = 0;
1009 wsi->lws_rx_parse_state =
1010 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1011 wsi->u.ws.mask_idx = 0;
1012 if (wsi->u.ws.rx_packet_length == 0) {
1013 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1019 case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
1021 assert(wsi->u.ws.rx_ubuf);
1023 if (wsi->u.ws.rx_ubuf_head + LWS_PRE >=
1024 wsi->u.ws.rx_ubuf_alloc) {
1025 lwsl_err("Attempted overflow \n");
1028 if (wsi->u.ws.all_zero_nonce)
1029 wsi->u.ws.rx_ubuf[LWS_PRE +
1030 (wsi->u.ws.rx_ubuf_head++)] = c;
1032 wsi->u.ws.rx_ubuf[LWS_PRE +
1033 (wsi->u.ws.rx_ubuf_head++)] =
1035 (wsi->u.ws.mask_idx++) & 3];
1037 if (--wsi->u.ws.rx_packet_length == 0) {
1038 /* spill because we have the whole frame */
1039 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1044 * if there's no protocol max frame size given, we are
1045 * supposed to default to LWS_MAX_SOCKET_IO_BUF
1048 if (!wsi->protocol->rx_buffer_size &&
1049 wsi->u.ws.rx_ubuf_head !=
1050 LWS_MAX_SOCKET_IO_BUF)
1053 if (wsi->protocol->rx_buffer_size &&
1054 wsi->u.ws.rx_ubuf_head !=
1055 wsi->protocol->rx_buffer_size)
1058 /* spill because we filled our rx buffer */
1061 * is this frame a control packet we should take care of at this
1062 * layer? If so service it and hide it from the user callback
1065 lwsl_parser("spill on %s\n", wsi->protocol->name);
1067 switch (wsi->u.ws.opcode) {
1068 case LWSWSOPC_CLOSE:
1070 /* is this an acknowledgement of our close? */
1071 if (wsi->state == LWSS_AWAITING_CLOSE_ACK) {
1073 * fine he has told us he is closing too, let's
1076 lwsl_parser("seen client close ack\n");
1079 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
1080 /* if he sends us 2 CLOSE, kill him */
1083 if (user_callback_handle_rxflow(
1084 wsi->protocol->callback, wsi,
1085 LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
1087 &wsi->u.ws.rx_ubuf[LWS_PRE],
1088 wsi->u.ws.rx_ubuf_head))
1091 lwsl_parser("server sees client close packet\n");
1092 wsi->state = LWSS_RETURNED_CLOSE_ALREADY;
1093 /* deal with the close packet contents as a PONG */
1094 wsi->u.ws.payload_is_close = 1;
1095 goto process_as_ping;
1098 lwsl_info("received %d byte ping, sending pong\n",
1099 wsi->u.ws.rx_ubuf_head);
1101 if (wsi->u.ws.ping_pending_flag) {
1103 * there is already a pending ping payload
1104 * we should just log and drop
1106 lwsl_parser("DROP PING since one pending\n");
1110 /* control packets can only be < 128 bytes long */
1111 if (wsi->u.ws.rx_ubuf_head > 128 - 3) {
1112 lwsl_parser("DROP PING payload too large\n");
1116 /* stash the pong payload */
1117 memcpy(wsi->u.ws.ping_payload_buf + LWS_PRE,
1118 &wsi->u.ws.rx_ubuf[LWS_PRE],
1119 wsi->u.ws.rx_ubuf_head);
1121 wsi->u.ws.ping_payload_len = wsi->u.ws.rx_ubuf_head;
1122 wsi->u.ws.ping_pending_flag = 1;
1124 /* get it sent as soon as possible */
1125 lws_callback_on_writable(wsi);
1127 wsi->u.ws.rx_ubuf_head = 0;
1131 lwsl_info("received pong\n");
1132 lwsl_hexdump(&wsi->u.ws.rx_ubuf[LWS_PRE],
1133 wsi->u.ws.rx_ubuf_head);
1136 callback_action = LWS_CALLBACK_RECEIVE_PONG;
1139 case LWSWSOPC_TEXT_FRAME:
1140 case LWSWSOPC_BINARY_FRAME:
1141 case LWSWSOPC_CONTINUATION:
1145 lwsl_parser("passing opc %x up to exts\n",
1148 * It's something special we can't understand here.
1149 * Pass the payload up to the extension's parsing
1153 eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1154 eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1156 if (lws_ext_cb_active(wsi, LWS_EXT_CB_EXTENDED_PAYLOAD_RX,
1157 &eff_buf, 0) <= 0) /* not handle or fail */
1158 lwsl_ext("ext opc opcode 0x%x unknown\n",
1161 wsi->u.ws.rx_ubuf_head = 0;
1166 * No it's real payload, pass it up to the user callback.
1167 * It's nicely buffered with the pre-padding taken care of
1168 * so it can be sent straight out again using lws_write
1171 eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1172 eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1175 lwsl_ext("%s: passing %d to ext\n", __func__, eff_buf.token_len);
1177 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
1178 wsi->state == LWSS_AWAITING_CLOSE_ACK)
1181 n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &eff_buf, 0);
1184 * we may rely on this to get RX, just drop connection
1186 wsi->socket_is_permanently_unusable = 1;
1190 if (rx_draining_ext && eff_buf.token_len == 0)
1193 if (n && eff_buf.token_len) {
1194 /* extension had more... main loop will come back */
1195 wsi->u.ws.rx_draining_ext = 1;
1196 wsi->u.ws.rx_draining_ext_list = pt->rx_draining_ext_list;
1197 pt->rx_draining_ext_list = wsi;
1200 if (eff_buf.token_len > 0 ||
1201 callback_action == LWS_CALLBACK_RECEIVE_PONG) {
1202 eff_buf.token[eff_buf.token_len] = '\0';
1204 if (wsi->protocol->callback) {
1206 if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
1207 lwsl_info("Doing pong callback\n");
1209 ret = user_callback_handle_rxflow(
1210 wsi->protocol->callback,
1212 (enum lws_callback_reasons)callback_action,
1218 lwsl_err("No callback on payload spill!\n");
1222 wsi->u.ws.rx_ubuf_head = 0;
1230 lwsl_warn("Control frame with xtended length is illegal\n");
1231 /* kill the connection */
1237 * lws_remaining_packet_payload() - Bytes to come before "overall"
1238 * rx packet is complete
1239 * @wsi: Websocket instance (available from user callback)
1241 * This function is intended to be called from the callback if the
1242 * user code is interested in "complete packets" from the client.
1243 * libwebsockets just passes through payload as it comes and issues a buffer
1244 * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
1245 * callback handler can use this API to find out if the buffer it has just
1246 * been given is the last piece of a "complete packet" from the client --
1247 * when that is the case lws_remaining_packet_payload() will return
1250 * Many protocols won't care becuse their packets are always small.
1254 lws_remaining_packet_payload(struct lws *wsi)
1256 return wsi->u.ws.rx_packet_length;