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,
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) |
289 wsi->u.hdr.ues = URIES_IDLE;
294 * special URI processing...
295 * convert /.. or /... or /../ etc to /
297 * convert // or /// etc to /
298 * leave /.dir or whatever alone
301 switch (wsi->u.hdr.ups) {
303 /* issue the first / always */
305 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
307 case URIPS_SEEN_SLASH:
308 /* swallow subsequent slashes */
311 /* track and swallow the first . after / */
313 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
316 wsi->u.hdr.ups = URIPS_IDLE;
318 case URIPS_SEEN_SLASH_DOT:
319 /* swallow second . */
322 * back up one dir level if possible
323 * safe against header fragmentation because
324 * the method URI can only be in 1 fragment
326 if (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 2) {
327 wsi->u.hdr.ah->pos--;
328 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
330 wsi->u.hdr.ah->pos--;
331 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
332 } while (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 1 &&
333 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos] != '/');
335 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
338 /* change /./ to / */
340 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
343 /* it was like /.dir ... regurgitate the . */
344 wsi->u.hdr.ups = URIPS_IDLE;
345 issue_char(wsi, '.');
348 case URIPS_SEEN_SLASH_DOT_DOT:
349 /* swallow prior .. chars and any subsequent . */
352 /* last issued was /, so another / == // */
355 /* last we issued was / so SEEN_SLASH */
356 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
358 case URIPS_ARGUMENTS:
359 /* leave them alone */
363 if (c == '?') { /* start of URI arguments */
364 /* seal off uri header */
365 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
367 /* move to using WSI_TOKEN_HTTP_URI_ARGS */
368 wsi->u.hdr.ah->next_frag_index++;
369 wsi->u.hdr.ah->frags[
370 wsi->u.hdr.ah->next_frag_index].offset =
372 wsi->u.hdr.ah->frags[
373 wsi->u.hdr.ah->next_frag_index].len = 0;
374 wsi->u.hdr.ah->frags[
375 wsi->u.hdr.ah->next_frag_index].next_frag_index = 0;
377 wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] =
378 wsi->u.hdr.ah->next_frag_index;
380 /* defeat normal uri path processing */
381 wsi->u.hdr.ups = URIPS_ARGUMENTS;
388 if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
391 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
395 n = issue_char(wsi, c);
399 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
402 /* per-protocol end of headers management */
404 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
405 goto set_parsing_complete;
408 /* collecting and checking a name part */
409 case WSI_TOKEN_NAME_PART:
410 lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode);
412 wsi->u.hdr.lextable_pos =
413 lextable_decode(wsi->u.hdr.lextable_pos, c);
415 * Server needs to look out for unknown methods...
417 if (wsi->u.hdr.lextable_pos < 0 &&
418 wsi->mode == LWS_CONNMODE_HTTP_SERVING) {
419 /* this is not a header we know about */
420 for (m = 0; m < ARRAY_SIZE(methods); m++)
421 if (wsi->u.hdr.ah->frag_index[methods[m]]) {
423 * already had the method, no idea what
424 * this crap from the client is, ignore
426 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
430 * hm it's an unknown http method from a client in fact,
433 if (m == ARRAY_SIZE(methods)) {
434 lwsl_info("Unknown method - dropping\n");
440 * ...otherwise for a client, let him ignore unknown headers
441 * coming from the server
443 if (wsi->u.hdr.lextable_pos < 0) {
444 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
448 if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
451 n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) |
452 lextable[wsi->u.hdr.lextable_pos + 1];
454 lwsl_parser("known hdr %d\n", n);
455 for (m = 0; m < ARRAY_SIZE(methods); m++)
456 if (n == methods[m] &&
457 wsi->u.hdr.ah->frag_index[
459 lwsl_warn("Duplicated method\n");
464 * WSORIGIN is protocol equiv to ORIGIN,
465 * JWebSocket likes to send it, map to ORIGIN
467 if (n == WSI_TOKEN_SWORIGIN)
468 n = WSI_TOKEN_ORIGIN;
470 wsi->u.hdr.parser_state = (enum lws_token_indexes)
471 (WSI_TOKEN_GET_URI + n);
473 if (context->token_limits)
474 wsi->u.hdr.current_token_limit =
475 context->token_limits->token_limit[wsi->u.hdr.parser_state];
477 wsi->u.hdr.current_token_limit = sizeof(wsi->u.hdr.ah->data);
479 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
480 goto set_parsing_complete;
487 wsi->u.hdr.ah->next_frag_index++;
488 if (wsi->u.hdr.ah->next_frag_index ==
489 sizeof(wsi->u.hdr.ah->frags) /
490 sizeof(wsi->u.hdr.ah->frags[0])) {
491 lwsl_warn("More hdr frags than we can deal with\n");
495 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset =
497 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0;
498 wsi->u.hdr.ah->frags[
499 wsi->u.hdr.ah->next_frag_index].next_frag_index = 0;
501 n = wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state];
502 if (!n) { /* first fragment */
503 wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state] =
504 wsi->u.hdr.ah->next_frag_index;
508 while (wsi->u.hdr.ah->frags[n].next_frag_index)
509 n = wsi->u.hdr.ah->frags[n].next_frag_index;
510 wsi->u.hdr.ah->frags[n].next_frag_index =
511 wsi->u.hdr.ah->next_frag_index;
513 if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
514 lwsl_warn("excessive header content\n");
518 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = ' ';
519 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++;
522 /* skipping arg part of a name we didn't recognize */
523 case WSI_TOKEN_SKIPPING:
524 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
527 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
530 case WSI_TOKEN_SKIPPING_SAW_CR:
531 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
533 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
534 wsi->u.hdr.lextable_pos = 0;
536 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
538 /* we're done, ignore anything else */
540 case WSI_PARSING_COMPLETE:
541 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
547 set_parsing_complete:
549 if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
550 if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
551 wsi->ietf_spec_revision =
552 atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
554 lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
556 wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
557 wsi->hdr_parsing_completed = 1;
564 * lws_frame_is_binary: true if the current frame was sent in binary mode
566 * @wsi: the connection we are inquiring about
568 * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
569 * it's interested to see if the frame it's dealing with was sent in binary
573 LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
575 return wsi->u.ws.frame_is_binary;
579 lws_rx_sm(struct lws *wsi, unsigned char c)
581 struct lws_tokens eff_buf;
583 int callback_action = LWS_CALLBACK_RECEIVE;
585 switch (wsi->lws_rx_parse_state) {
588 switch (wsi->ietf_spec_revision) {
591 * no prepended frame key any more
593 wsi->u.ws.all_zero_nonce = 1;
597 lwsl_warn("lws_rx_sm: unknown spec version %d\n",
598 wsi->ietf_spec_revision);
602 case LWS_RXPS_04_MASK_NONCE_1:
603 wsi->u.ws.frame_masking_nonce_04[1] = c;
605 wsi->u.ws.all_zero_nonce = 0;
606 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_2;
608 case LWS_RXPS_04_MASK_NONCE_2:
609 wsi->u.ws.frame_masking_nonce_04[2] = c;
611 wsi->u.ws.all_zero_nonce = 0;
612 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_3;
614 case LWS_RXPS_04_MASK_NONCE_3:
615 wsi->u.ws.frame_masking_nonce_04[3] = c;
617 wsi->u.ws.all_zero_nonce = 0;
620 * start from the zero'th byte in the XOR key buffer since
621 * this is the start of a frame with a new key
624 wsi->u.ws.frame_mask_index = 0;
626 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
630 * 04 logical framing from the spec (all this is masked when incoming
631 * and has to be unmasked)
633 * We ignore the possibility of extension data because we don't
634 * negotiate any extensions at the moment.
637 * 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
638 * +-+-+-+-+-------+-+-------------+-------------------------------+
639 * |F|R|R|R| opcode|R| Payload len | Extended payload length |
640 * |I|S|S|S| (4) |S| (7) | (16/63) |
641 * |N|V|V|V| |V| | (if payload len==126/127) |
643 * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
644 * | Extended payload length continued, if payload len == 127 |
645 * + - - - - - - - - - - - - - - - +-------------------------------+
646 * | | Extension data |
647 * +-------------------------------+ - - - - - - - - - - - - - - - +
649 * +---------------------------------------------------------------+
650 * : Application data :
651 * +---------------------------------------------------------------+
653 * We pass payload through to userland as soon as we get it, ignoring
654 * FIN. It's up to userland to buffer it up if it wants to see a
655 * whole unfragmented block of the original size (which may be up to
659 case LWS_RXPS_04_FRAME_HDR_1:
662 wsi->u.ws.opcode = c & 0xf;
663 wsi->u.ws.rsv = c & 0x70;
664 wsi->u.ws.final = !!((c >> 7) & 1);
666 switch (wsi->u.ws.opcode) {
667 case LWS_WS_OPCODE_07__TEXT_FRAME:
668 case LWS_WS_OPCODE_07__BINARY_FRAME:
669 wsi->u.ws.frame_is_binary =
670 wsi->u.ws.opcode == LWS_WS_OPCODE_07__BINARY_FRAME;
673 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
676 case LWS_RXPS_04_FRAME_HDR_LEN:
678 wsi->u.ws.this_frame_masked = !!(c & 0x80);
682 /* control frames are not allowed to have big lengths */
683 if (wsi->u.ws.opcode & 8)
684 goto illegal_ctl_length;
686 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
689 /* control frames are not allowed to have big lengths */
690 if (wsi->u.ws.opcode & 8)
691 goto illegal_ctl_length;
693 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
696 wsi->u.ws.rx_packet_length = c & 0x7f;
697 if (wsi->u.ws.this_frame_masked)
698 wsi->lws_rx_parse_state =
699 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
701 if (wsi->u.ws.rx_packet_length)
702 wsi->lws_rx_parse_state =
703 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
705 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
712 case LWS_RXPS_04_FRAME_HDR_LEN16_2:
713 wsi->u.ws.rx_packet_length = c << 8;
714 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
717 case LWS_RXPS_04_FRAME_HDR_LEN16_1:
718 wsi->u.ws.rx_packet_length |= c;
719 if (wsi->u.ws.this_frame_masked)
720 wsi->lws_rx_parse_state =
721 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
723 wsi->lws_rx_parse_state =
724 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
727 case LWS_RXPS_04_FRAME_HDR_LEN64_8:
729 lwsl_warn("b63 of length must be zero\n");
730 /* kill the connection */
734 wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
736 wsi->u.ws.rx_packet_length = 0;
738 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
741 case LWS_RXPS_04_FRAME_HDR_LEN64_7:
743 wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
745 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
748 case LWS_RXPS_04_FRAME_HDR_LEN64_6:
750 wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
752 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
755 case LWS_RXPS_04_FRAME_HDR_LEN64_5:
757 wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
759 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
762 case LWS_RXPS_04_FRAME_HDR_LEN64_4:
763 wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
764 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
767 case LWS_RXPS_04_FRAME_HDR_LEN64_3:
768 wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
769 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
772 case LWS_RXPS_04_FRAME_HDR_LEN64_2:
773 wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
774 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
777 case LWS_RXPS_04_FRAME_HDR_LEN64_1:
778 wsi->u.ws.rx_packet_length |= ((size_t)c);
779 if (wsi->u.ws.this_frame_masked)
780 wsi->lws_rx_parse_state =
781 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
783 wsi->lws_rx_parse_state =
784 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
787 case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
788 wsi->u.ws.frame_masking_nonce_04[0] = c;
790 wsi->u.ws.all_zero_nonce = 0;
791 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
794 case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
795 wsi->u.ws.frame_masking_nonce_04[1] = c;
797 wsi->u.ws.all_zero_nonce = 0;
798 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
801 case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
802 wsi->u.ws.frame_masking_nonce_04[2] = c;
804 wsi->u.ws.all_zero_nonce = 0;
805 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
808 case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
809 wsi->u.ws.frame_masking_nonce_04[3] = c;
811 wsi->u.ws.all_zero_nonce = 0;
812 wsi->lws_rx_parse_state =
813 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
814 wsi->u.ws.frame_mask_index = 0;
815 if (wsi->u.ws.rx_packet_length == 0) {
816 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
822 case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
824 if (!wsi->u.ws.rx_user_buffer) {
825 lwsl_err("NULL user buffer...\n");
829 if (wsi->u.ws.all_zero_nonce)
830 wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
831 (wsi->u.ws.rx_user_buffer_head++)] = c;
833 wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
834 (wsi->u.ws.rx_user_buffer_head++)] =
835 c ^ wsi->u.ws.frame_masking_nonce_04[
836 (wsi->u.ws.frame_mask_index++) & 3];
838 if (--wsi->u.ws.rx_packet_length == 0) {
839 /* spill because we have the whole frame */
840 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
845 * if there's no protocol max frame size given, we are
846 * supposed to default to LWS_MAX_SOCKET_IO_BUF
849 if (!wsi->protocol->rx_buffer_size &&
850 wsi->u.ws.rx_user_buffer_head !=
851 LWS_MAX_SOCKET_IO_BUF)
854 if (wsi->protocol->rx_buffer_size &&
855 wsi->u.ws.rx_user_buffer_head !=
856 wsi->protocol->rx_buffer_size)
859 /* spill because we filled our rx buffer */
862 * is this frame a control packet we should take care of at this
863 * layer? If so service it and hide it from the user callback
866 lwsl_parser("spill on %s\n", wsi->protocol->name);
868 switch (wsi->u.ws.opcode) {
869 case LWS_WS_OPCODE_07__CLOSE:
870 /* is this an acknowledgement of our close? */
871 if (wsi->state == WSI_STATE_AWAITING_CLOSE_ACK) {
873 * fine he has told us he is closing too, let's
876 lwsl_parser("seen client close ack\n");
879 if (wsi->state == WSI_STATE_RETURNED_CLOSE_ALREADY)
880 /* if he sends us 2 CLOSE, kill him */
883 lwsl_parser("server sees client close packet\n");
884 wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY;
885 /* deal with the close packet contents as a PONG */
886 wsi->u.ws.payload_is_close = 1;
887 goto process_as_ping;
889 case LWS_WS_OPCODE_07__PING:
890 lwsl_info("received %d byte ping, sending pong\n",
891 wsi->u.ws.rx_user_buffer_head);
893 if (wsi->u.ws.ping_pending_flag) {
895 * there is already a pending ping payload
896 * we should just log and drop
898 lwsl_parser("DROP PING since one pending\n");
902 /* control packets can only be < 128 bytes long */
903 if (wsi->u.ws.rx_user_buffer_head > 128 - 4) {
904 lwsl_parser("DROP PING payload too large\n");
908 /* if existing buffer is too small, drop it */
909 if (wsi->u.ws.ping_payload_buf &&
910 wsi->u.ws.ping_payload_alloc < wsi->u.ws.rx_user_buffer_head) {
911 lws_free2(wsi->u.ws.ping_payload_buf);
914 /* if no buffer, allocate it */
915 if (!wsi->u.ws.ping_payload_buf) {
916 wsi->u.ws.ping_payload_buf = lws_malloc(wsi->u.ws.rx_user_buffer_head
917 + LWS_SEND_BUFFER_PRE_PADDING);
918 wsi->u.ws.ping_payload_alloc = wsi->u.ws.rx_user_buffer_head;
921 /* stash the pong payload */
922 memcpy(wsi->u.ws.ping_payload_buf + LWS_SEND_BUFFER_PRE_PADDING,
923 &wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
924 wsi->u.ws.rx_user_buffer_head);
926 wsi->u.ws.ping_payload_len = wsi->u.ws.rx_user_buffer_head;
927 wsi->u.ws.ping_pending_flag = 1;
929 /* get it sent as soon as possible */
930 lws_callback_on_writable(wsi->protocol->owning_server, wsi);
932 wsi->u.ws.rx_user_buffer_head = 0;
935 case LWS_WS_OPCODE_07__PONG:
936 lwsl_info("received pong\n");
937 lwsl_hexdump(&wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
938 wsi->u.ws.rx_user_buffer_head);
941 callback_action = LWS_CALLBACK_RECEIVE_PONG;
944 case LWS_WS_OPCODE_07__TEXT_FRAME:
945 case LWS_WS_OPCODE_07__BINARY_FRAME:
946 case LWS_WS_OPCODE_07__CONTINUATION:
950 lwsl_parser("passing opc %x up to exts\n",
953 * It's something special we can't understand here.
954 * Pass the payload up to the extension's parsing
958 eff_buf.token = &wsi->u.ws.rx_user_buffer[
959 LWS_SEND_BUFFER_PRE_PADDING];
960 eff_buf.token_len = wsi->u.ws.rx_user_buffer_head;
962 if (lws_ext_callback_for_each_active(wsi,
963 LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
964 &eff_buf, 0) <= 0) /* not handle or fail */
965 lwsl_ext("ext opc opcode 0x%x unknown\n",
968 wsi->u.ws.rx_user_buffer_head = 0;
973 * No it's real payload, pass it up to the user callback.
974 * It's nicely buffered with the pre-padding taken care of
975 * so it can be sent straight out again using lws_write
978 eff_buf.token = &wsi->u.ws.rx_user_buffer[
979 LWS_SEND_BUFFER_PRE_PADDING];
980 eff_buf.token_len = wsi->u.ws.rx_user_buffer_head;
982 if (lws_ext_callback_for_each_active(wsi,
983 LWS_EXT_CALLBACK_PAYLOAD_RX, &eff_buf, 0) < 0)
986 if (eff_buf.token_len > 0 ||
987 callback_action == LWS_CALLBACK_RECEIVE_PONG) {
988 eff_buf.token[eff_buf.token_len] = '\0';
990 if (wsi->protocol->callback) {
992 if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
993 lwsl_info("Doing pong callback\n");
995 ret = user_callback_handle_rxflow(
996 wsi->protocol->callback,
997 wsi->protocol->owning_server,
999 (enum lws_callback_reasons)callback_action,
1005 lwsl_err("No callback on payload spill!\n");
1008 wsi->u.ws.rx_user_buffer_head = 0;
1016 lwsl_warn("Control frame with xtended length is illegal\n");
1017 /* kill the connection */
1023 * lws_remaining_packet_payload() - Bytes to come before "overall"
1024 * rx packet is complete
1025 * @wsi: Websocket instance (available from user callback)
1027 * This function is intended to be called from the callback if the
1028 * user code is interested in "complete packets" from the client.
1029 * libwebsockets just passes through payload as it comes and issues a buffer
1030 * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
1031 * callback handler can use this API to find out if the buffer it has just
1032 * been given is the last piece of a "complete packet" from the client --
1033 * when that is the case lws_remaining_packet_payload() will return
1036 * Many protocols won't care becuse their packets are always small.
1040 lws_remaining_packet_payload(struct lws *wsi)
1042 return wsi->u.ws.rx_packet_length;