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)
36 if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */
37 if ((lextable[pos] & 0x7f) != c)
41 if (lextable[pos] == FAIL_CHAR)
46 if (lextable[pos] == FAIL_CHAR)
49 /* b7 = 0, end or 3-byte */
50 if (lextable[pos] < FAIL_CHAR) /* terminal marker */
53 if (lextable[pos] == c) /* goto */
54 return pos + (lextable[pos + 1]) +
55 (lextable[pos + 2] << 8);
62 int lws_allocate_header_table(struct lws *wsi)
64 /* Be sure to free any existing header data to avoid mem leak: */
65 lws_free_header_table(wsi);
66 wsi->u.hdr.ah = lws_malloc(sizeof(*wsi->u.hdr.ah));
67 if (wsi->u.hdr.ah == NULL) {
68 lwsl_err("Out of memory\n");
71 memset(wsi->u.hdr.ah->frag_index, 0, sizeof(wsi->u.hdr.ah->frag_index));
72 wsi->u.hdr.ah->next_frag_index = 0;
73 wsi->u.hdr.ah->pos = 0;
78 int lws_free_header_table(struct lws *wsi)
80 lws_free2(wsi->u.hdr.ah);
85 LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h)
90 n = wsi->u.hdr.ah->frag_index[h];
94 len += wsi->u.hdr.ah->frags[n].len;
95 n = wsi->u.hdr.ah->frags[n].next_frag_index;
101 LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dest, int len,
102 enum lws_token_indexes h)
104 int toklen = lws_hdr_total_length(wsi, h);
110 n = wsi->u.hdr.ah->frag_index[h];
116 &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]);
117 dest += wsi->u.hdr.ah->frags[n].len;
118 n = wsi->u.hdr.ah->frags[n].next_frag_index;
124 char *lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h)
128 n = wsi->u.hdr.ah->frag_index[h];
132 return &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset];
135 int lws_hdr_simple_create(struct lws *wsi,
136 enum lws_token_indexes h, const char *s)
138 wsi->u.hdr.ah->next_frag_index++;
139 if (wsi->u.hdr.ah->next_frag_index ==
140 sizeof(wsi->u.hdr.ah->frags) / sizeof(wsi->u.hdr.ah->frags[0])) {
141 lwsl_warn("More hdr frags than we can deal with, dropping\n");
145 wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->next_frag_index;
147 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset =
149 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0;
150 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].next_frag_index =
154 if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
155 lwsl_err("Ran out of header data space\n");
158 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s;
160 wsi->u.hdr.ah->frags[
161 wsi->u.hdr.ah->next_frag_index].len++;
167 static signed char char_to_hex(const char c)
169 if (c >= '0' && c <= '9')
172 if (c >= 'a' && c <= 'f')
175 if (c >= 'A' && c <= 'F')
181 static int issue_char(struct lws *wsi, unsigned char c)
183 unsigned short frag_len;
184 if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
185 lwsl_warn("excessive header content\n");
190 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len;
191 /* If we haven't hit the token limit, just copy the character into
193 if( frag_len < wsi->u.hdr.current_token_limit ) {
194 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c;
196 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++;
200 /* Insert a null character when we *hit* the limit: */
201 if( frag_len == wsi->u.hdr.current_token_limit ) {
202 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
203 lwsl_warn("header %i exceeds limit\n", wsi->u.hdr.parser_state);
209 int lws_parse(struct lws_context *context, struct lws *wsi, unsigned char c)
211 static const unsigned char methods[] = {
214 WSI_TOKEN_OPTIONS_URI,
217 WSI_TOKEN_DELETE_URI,
219 unsigned int n, m, enc = 0;
221 switch (wsi->u.hdr.parser_state) {
224 lwsl_parser("WSI_TOK_(%d) '%c'\n", wsi->u.hdr.parser_state, c);
226 /* collect into malloc'd buffers */
227 /* optional initial space swallow */
228 if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->frag_index[
229 wsi->u.hdr.parser_state]].len && c == ' ')
232 for (m = 0; m < ARRAY_SIZE(methods); m++)
233 if (wsi->u.hdr.parser_state == methods[m])
235 if (m == ARRAY_SIZE(methods))
236 /* it was not any of the methods */
239 /* special URI processing... end at space */
242 /* enforce starting with / */
243 if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len)
244 if (issue_char(wsi, '/') < 0)
247 /* begin parsing HTTP version: */
248 if (issue_char(wsi, '\0') < 0)
250 wsi->u.hdr.parser_state = WSI_TOKEN_HTTP;
254 /* special URI processing... convert %xx */
256 switch (wsi->u.hdr.ues) {
259 wsi->u.hdr.ues = URIES_SEEN_PERCENT;
263 case URIES_SEEN_PERCENT:
264 if (char_to_hex(c) < 0) {
266 if (issue_char(wsi, '%') < 0)
268 wsi->u.hdr.ues = URIES_IDLE;
269 /* continue on to assess c */
272 wsi->u.hdr.esc_stash = c;
273 wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1;
276 case URIES_SEEN_PERCENT_H1:
277 if (char_to_hex(c) < 0) {
279 issue_char(wsi, '%');
280 wsi->u.hdr.ues = URIES_IDLE;
281 /* regurgitate + assess */
282 if (lws_parse(context, wsi, wsi->u.hdr.esc_stash) < 0)
284 /* continue on to assess c */
287 c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) |
290 wsi->u.hdr.ues = URIES_IDLE;
295 * special URI processing...
296 * convert /.. or /... or /../ etc to /
298 * convert // or /// etc to /
299 * leave /.dir or whatever alone
302 switch (wsi->u.hdr.ups) {
304 /* issue the first / always */
306 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
308 case URIPS_SEEN_SLASH:
309 /* swallow subsequent slashes */
312 /* track and swallow the first . after / */
314 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
317 wsi->u.hdr.ups = URIPS_IDLE;
319 case URIPS_SEEN_SLASH_DOT:
320 /* swallow second . */
323 * back up one dir level if possible
324 * safe against header fragmentation because
325 * the method URI can only be in 1 fragment
327 if (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 2) {
328 wsi->u.hdr.ah->pos--;
329 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
331 wsi->u.hdr.ah->pos--;
332 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
333 } while (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 1 &&
334 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos] != '/');
336 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
339 /* change /./ to / */
341 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
344 /* it was like /.dir ... regurgitate the . */
345 wsi->u.hdr.ups = URIPS_IDLE;
346 issue_char(wsi, '.');
349 case URIPS_SEEN_SLASH_DOT_DOT:
350 /* swallow prior .. chars and any subsequent . */
353 /* last issued was /, so another / == // */
356 /* last we issued was / so SEEN_SLASH */
357 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
359 case URIPS_ARGUMENTS:
360 /* leave them alone */
364 if (c == '?' && !enc) { /* start of URI arguments */
365 /* seal off uri header */
366 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
368 /* move to using WSI_TOKEN_HTTP_URI_ARGS */
369 wsi->u.hdr.ah->next_frag_index++;
370 wsi->u.hdr.ah->frags[
371 wsi->u.hdr.ah->next_frag_index].offset =
373 wsi->u.hdr.ah->frags[
374 wsi->u.hdr.ah->next_frag_index].len = 0;
375 wsi->u.hdr.ah->frags[
376 wsi->u.hdr.ah->next_frag_index].next_frag_index = 0;
378 wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] =
379 wsi->u.hdr.ah->next_frag_index;
381 /* defeat normal uri path processing */
382 wsi->u.hdr.ups = URIPS_ARGUMENTS;
389 if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
392 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
396 n = issue_char(wsi, c);
400 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
403 /* per-protocol end of headers management */
405 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
406 goto set_parsing_complete;
409 /* collecting and checking a name part */
410 case WSI_TOKEN_NAME_PART:
411 lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode);
413 wsi->u.hdr.lextable_pos =
414 lextable_decode(wsi->u.hdr.lextable_pos, c);
416 * Server needs to look out for unknown methods...
418 if (wsi->u.hdr.lextable_pos < 0 &&
419 wsi->mode == LWS_CONNMODE_HTTP_SERVING) {
420 /* this is not a header we know about */
421 for (m = 0; m < ARRAY_SIZE(methods); m++)
422 if (wsi->u.hdr.ah->frag_index[methods[m]]) {
424 * already had the method, no idea what
425 * this crap from the client is, ignore
427 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
431 * hm it's an unknown http method from a client in fact,
434 if (m == ARRAY_SIZE(methods)) {
435 lwsl_info("Unknown method - dropping\n");
441 * ...otherwise for a client, let him ignore unknown headers
442 * coming from the server
444 if (wsi->u.hdr.lextable_pos < 0) {
445 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
449 if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
452 n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) |
453 lextable[wsi->u.hdr.lextable_pos + 1];
455 lwsl_parser("known hdr %d\n", n);
456 for (m = 0; m < ARRAY_SIZE(methods); m++)
457 if (n == methods[m] &&
458 wsi->u.hdr.ah->frag_index[
460 lwsl_warn("Duplicated method\n");
465 * WSORIGIN is protocol equiv to ORIGIN,
466 * JWebSocket likes to send it, map to ORIGIN
468 if (n == WSI_TOKEN_SWORIGIN)
469 n = WSI_TOKEN_ORIGIN;
471 wsi->u.hdr.parser_state = (enum lws_token_indexes)
472 (WSI_TOKEN_GET_URI + n);
474 if (context->token_limits)
475 wsi->u.hdr.current_token_limit =
476 context->token_limits->token_limit[wsi->u.hdr.parser_state];
478 wsi->u.hdr.current_token_limit = sizeof(wsi->u.hdr.ah->data);
480 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
481 goto set_parsing_complete;
488 wsi->u.hdr.ah->next_frag_index++;
489 if (wsi->u.hdr.ah->next_frag_index ==
490 sizeof(wsi->u.hdr.ah->frags) /
491 sizeof(wsi->u.hdr.ah->frags[0])) {
492 lwsl_warn("More hdr frags than we can deal with\n");
496 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset =
498 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0;
499 wsi->u.hdr.ah->frags[
500 wsi->u.hdr.ah->next_frag_index].next_frag_index = 0;
502 n = wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state];
503 if (!n) { /* first fragment */
504 wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state] =
505 wsi->u.hdr.ah->next_frag_index;
509 while (wsi->u.hdr.ah->frags[n].next_frag_index)
510 n = wsi->u.hdr.ah->frags[n].next_frag_index;
511 wsi->u.hdr.ah->frags[n].next_frag_index =
512 wsi->u.hdr.ah->next_frag_index;
514 if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
515 lwsl_warn("excessive header content\n");
519 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = ' ';
520 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++;
523 /* skipping arg part of a name we didn't recognize */
524 case WSI_TOKEN_SKIPPING:
525 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
528 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
531 case WSI_TOKEN_SKIPPING_SAW_CR:
532 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
534 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
535 wsi->u.hdr.lextable_pos = 0;
537 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
539 /* we're done, ignore anything else */
541 case WSI_PARSING_COMPLETE:
542 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
548 set_parsing_complete:
550 if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
551 if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
552 wsi->ietf_spec_revision =
553 atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
555 lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
557 wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
558 wsi->hdr_parsing_completed = 1;
565 * lws_frame_is_binary: true if the current frame was sent in binary mode
567 * @wsi: the connection we are inquiring about
569 * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
570 * it's interested to see if the frame it's dealing with was sent in binary
574 LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
576 return wsi->u.ws.frame_is_binary;
580 lws_rx_sm(struct lws *wsi, unsigned char c)
582 struct lws_tokens eff_buf;
584 int callback_action = LWS_CALLBACK_RECEIVE;
586 switch (wsi->lws_rx_parse_state) {
589 switch (wsi->ietf_spec_revision) {
592 * no prepended frame key any more
594 wsi->u.ws.all_zero_nonce = 1;
598 lwsl_warn("lws_rx_sm: unknown spec version %d\n",
599 wsi->ietf_spec_revision);
603 case LWS_RXPS_04_MASK_NONCE_1:
604 wsi->u.ws.frame_masking_nonce_04[1] = c;
606 wsi->u.ws.all_zero_nonce = 0;
607 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_2;
609 case LWS_RXPS_04_MASK_NONCE_2:
610 wsi->u.ws.frame_masking_nonce_04[2] = c;
612 wsi->u.ws.all_zero_nonce = 0;
613 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_3;
615 case LWS_RXPS_04_MASK_NONCE_3:
616 wsi->u.ws.frame_masking_nonce_04[3] = c;
618 wsi->u.ws.all_zero_nonce = 0;
621 * start from the zero'th byte in the XOR key buffer since
622 * this is the start of a frame with a new key
625 wsi->u.ws.frame_mask_index = 0;
627 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
631 * 04 logical framing from the spec (all this is masked when incoming
632 * and has to be unmasked)
634 * We ignore the possibility of extension data because we don't
635 * negotiate any extensions at the moment.
638 * 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
639 * +-+-+-+-+-------+-+-------------+-------------------------------+
640 * |F|R|R|R| opcode|R| Payload len | Extended payload length |
641 * |I|S|S|S| (4) |S| (7) | (16/63) |
642 * |N|V|V|V| |V| | (if payload len==126/127) |
644 * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
645 * | Extended payload length continued, if payload len == 127 |
646 * + - - - - - - - - - - - - - - - +-------------------------------+
647 * | | Extension data |
648 * +-------------------------------+ - - - - - - - - - - - - - - - +
650 * +---------------------------------------------------------------+
651 * : Application data :
652 * +---------------------------------------------------------------+
654 * We pass payload through to userland as soon as we get it, ignoring
655 * FIN. It's up to userland to buffer it up if it wants to see a
656 * whole unfragmented block of the original size (which may be up to
660 case LWS_RXPS_04_FRAME_HDR_1:
663 wsi->u.ws.opcode = c & 0xf;
664 wsi->u.ws.rsv = c & 0x70;
665 wsi->u.ws.final = !!((c >> 7) & 1);
667 switch (wsi->u.ws.opcode) {
668 case LWS_WS_OPCODE_07__TEXT_FRAME:
669 case LWS_WS_OPCODE_07__BINARY_FRAME:
670 wsi->u.ws.frame_is_binary =
671 wsi->u.ws.opcode == LWS_WS_OPCODE_07__BINARY_FRAME;
674 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
677 case LWS_RXPS_04_FRAME_HDR_LEN:
679 wsi->u.ws.this_frame_masked = !!(c & 0x80);
683 /* control frames are not allowed to have big lengths */
684 if (wsi->u.ws.opcode & 8)
685 goto illegal_ctl_length;
687 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
690 /* control frames are not allowed to have big lengths */
691 if (wsi->u.ws.opcode & 8)
692 goto illegal_ctl_length;
694 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
697 wsi->u.ws.rx_packet_length = c & 0x7f;
698 if (wsi->u.ws.this_frame_masked)
699 wsi->lws_rx_parse_state =
700 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
702 if (wsi->u.ws.rx_packet_length)
703 wsi->lws_rx_parse_state =
704 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
706 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
713 case LWS_RXPS_04_FRAME_HDR_LEN16_2:
714 wsi->u.ws.rx_packet_length = c << 8;
715 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
718 case LWS_RXPS_04_FRAME_HDR_LEN16_1:
719 wsi->u.ws.rx_packet_length |= c;
720 if (wsi->u.ws.this_frame_masked)
721 wsi->lws_rx_parse_state =
722 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
724 wsi->lws_rx_parse_state =
725 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
728 case LWS_RXPS_04_FRAME_HDR_LEN64_8:
730 lwsl_warn("b63 of length must be zero\n");
731 /* kill the connection */
735 wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
737 wsi->u.ws.rx_packet_length = 0;
739 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
742 case LWS_RXPS_04_FRAME_HDR_LEN64_7:
744 wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
746 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
749 case LWS_RXPS_04_FRAME_HDR_LEN64_6:
751 wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
753 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
756 case LWS_RXPS_04_FRAME_HDR_LEN64_5:
758 wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
760 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
763 case LWS_RXPS_04_FRAME_HDR_LEN64_4:
764 wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
765 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
768 case LWS_RXPS_04_FRAME_HDR_LEN64_3:
769 wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
770 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
773 case LWS_RXPS_04_FRAME_HDR_LEN64_2:
774 wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
775 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
778 case LWS_RXPS_04_FRAME_HDR_LEN64_1:
779 wsi->u.ws.rx_packet_length |= ((size_t)c);
780 if (wsi->u.ws.this_frame_masked)
781 wsi->lws_rx_parse_state =
782 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
784 wsi->lws_rx_parse_state =
785 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
788 case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
789 wsi->u.ws.frame_masking_nonce_04[0] = c;
791 wsi->u.ws.all_zero_nonce = 0;
792 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
795 case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
796 wsi->u.ws.frame_masking_nonce_04[1] = c;
798 wsi->u.ws.all_zero_nonce = 0;
799 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
802 case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
803 wsi->u.ws.frame_masking_nonce_04[2] = c;
805 wsi->u.ws.all_zero_nonce = 0;
806 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
809 case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
810 wsi->u.ws.frame_masking_nonce_04[3] = c;
812 wsi->u.ws.all_zero_nonce = 0;
813 wsi->lws_rx_parse_state =
814 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
815 wsi->u.ws.frame_mask_index = 0;
816 if (wsi->u.ws.rx_packet_length == 0) {
817 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
823 case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
825 if (!wsi->u.ws.rx_user_buffer) {
826 lwsl_err("NULL user buffer...\n");
830 if (wsi->u.ws.all_zero_nonce)
831 wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
832 (wsi->u.ws.rx_user_buffer_head++)] = c;
834 wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
835 (wsi->u.ws.rx_user_buffer_head++)] =
836 c ^ wsi->u.ws.frame_masking_nonce_04[
837 (wsi->u.ws.frame_mask_index++) & 3];
839 if (--wsi->u.ws.rx_packet_length == 0) {
840 /* spill because we have the whole frame */
841 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
846 * if there's no protocol max frame size given, we are
847 * supposed to default to LWS_MAX_SOCKET_IO_BUF
850 if (!wsi->protocol->rx_buffer_size &&
851 wsi->u.ws.rx_user_buffer_head !=
852 LWS_MAX_SOCKET_IO_BUF)
855 if (wsi->protocol->rx_buffer_size &&
856 wsi->u.ws.rx_user_buffer_head !=
857 wsi->protocol->rx_buffer_size)
860 /* spill because we filled our rx buffer */
863 * is this frame a control packet we should take care of at this
864 * layer? If so service it and hide it from the user callback
867 lwsl_parser("spill on %s\n", wsi->protocol->name);
869 switch (wsi->u.ws.opcode) {
870 case LWS_WS_OPCODE_07__CLOSE:
871 /* is this an acknowledgement of our close? */
872 if (wsi->state == WSI_STATE_AWAITING_CLOSE_ACK) {
874 * fine he has told us he is closing too, let's
877 lwsl_parser("seen client close ack\n");
880 if (wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY)
881 /* if he sends us 2 CLOSE, kill him */
884 lwsl_parser("server sees client close packet\n");
885 wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY;
886 /* deal with the close packet contents as a PONG */
887 wsi->u.ws.payload_is_close = 1;
888 goto process_as_ping;
890 case LWS_WS_OPCODE_07__PING:
891 lwsl_info("received %d byte ping, sending pong\n",
892 wsi->u.ws.rx_user_buffer_head);
894 if (wsi->u.ws.ping_pending_flag) {
896 * there is already a pending ping payload
897 * we should just log and drop
899 lwsl_parser("DROP PING since one pending\n");
903 /* control packets can only be < 128 bytes long */
904 if (wsi->u.ws.rx_user_buffer_head > 128 - 4) {
905 lwsl_parser("DROP PING payload too large\n");
909 /* if existing buffer is too small, drop it */
910 if (wsi->u.ws.ping_payload_buf &&
911 wsi->u.ws.ping_payload_alloc < wsi->u.ws.rx_user_buffer_head) {
912 lws_free2(wsi->u.ws.ping_payload_buf);
915 /* if no buffer, allocate it */
916 if (!wsi->u.ws.ping_payload_buf) {
917 wsi->u.ws.ping_payload_buf = lws_malloc(wsi->u.ws.rx_user_buffer_head
918 + LWS_SEND_BUFFER_PRE_PADDING);
919 wsi->u.ws.ping_payload_alloc = wsi->u.ws.rx_user_buffer_head;
922 /* stash the pong payload */
923 memcpy(wsi->u.ws.ping_payload_buf + LWS_SEND_BUFFER_PRE_PADDING,
924 &wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
925 wsi->u.ws.rx_user_buffer_head);
927 wsi->u.ws.ping_payload_len = wsi->u.ws.rx_user_buffer_head;
928 wsi->u.ws.ping_pending_flag = 1;
930 /* get it sent as soon as possible */
931 lws_callback_on_writable(lws_get_ctx(wsi), wsi);
933 wsi->u.ws.rx_user_buffer_head = 0;
936 case LWS_WS_OPCODE_07__PONG:
937 lwsl_info("received pong\n");
938 lwsl_hexdump(&wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
939 wsi->u.ws.rx_user_buffer_head);
942 callback_action = LWS_CALLBACK_RECEIVE_PONG;
945 case LWS_WS_OPCODE_07__TEXT_FRAME:
946 case LWS_WS_OPCODE_07__BINARY_FRAME:
947 case LWS_WS_OPCODE_07__CONTINUATION:
951 lwsl_parser("passing opc %x up to exts\n",
954 * It's something special we can't understand here.
955 * Pass the payload up to the extension's parsing
959 eff_buf.token = &wsi->u.ws.rx_user_buffer[
960 LWS_SEND_BUFFER_PRE_PADDING];
961 eff_buf.token_len = wsi->u.ws.rx_user_buffer_head;
963 if (lws_ext_callback_for_each_active(wsi,
964 LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
965 &eff_buf, 0) <= 0) /* not handle or fail */
966 lwsl_ext("ext opc opcode 0x%x unknown\n",
969 wsi->u.ws.rx_user_buffer_head = 0;
974 * No it's real payload, pass it up to the user callback.
975 * It's nicely buffered with the pre-padding taken care of
976 * so it can be sent straight out again using lws_write
979 eff_buf.token = &wsi->u.ws.rx_user_buffer[
980 LWS_SEND_BUFFER_PRE_PADDING];
981 eff_buf.token_len = wsi->u.ws.rx_user_buffer_head;
983 if (lws_ext_callback_for_each_active(wsi,
984 LWS_EXT_CALLBACK_PAYLOAD_RX, &eff_buf, 0) < 0)
987 if (eff_buf.token_len > 0 ||
988 callback_action == LWS_CALLBACK_RECEIVE_PONG) {
989 eff_buf.token[eff_buf.token_len] = '\0';
991 if (wsi->protocol->callback) {
993 if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
994 lwsl_info("Doing pong callback\n");
996 ret = user_callback_handle_rxflow(
997 wsi->protocol->callback,
1000 (enum lws_callback_reasons)callback_action,
1006 lwsl_err("No callback on payload spill!\n");
1009 wsi->u.ws.rx_user_buffer_head = 0;
1017 lwsl_warn("Control frame with xtended length is illegal\n");
1018 /* kill the connection */
1024 * lws_remaining_packet_payload() - Bytes to come before "overall"
1025 * rx packet is complete
1026 * @wsi: Websocket instance (available from user callback)
1028 * This function is intended to be called from the callback if the
1029 * user code is interested in "complete packets" from the client.
1030 * libwebsockets just passes through payload as it comes and issues a buffer
1031 * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
1032 * callback handler can use this API to find out if the buffer it has just
1033 * been given is the last piece of a "complete packet" from the client --
1034 * when that is the case lws_remaining_packet_payload() will return
1037 * Many protocols won't care becuse their packets are always small.
1041 lws_remaining_packet_payload(struct lws *wsi)
1043 return wsi->u.ws.rx_packet_length;