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)
45 /* b7 = 0, end or 3-byte */
46 if (lextable[pos] < FAIL_CHAR) /* terminal marker */
49 if (lextable[pos] == c) /* goto */
50 return pos + (lextable[pos + 1]) +
51 (lextable[pos + 2] << 8);
58 int lws_allocate_header_table(struct libwebsocket *wsi)
60 wsi->u.hdr.ah = malloc(sizeof(*wsi->u.hdr.ah));
61 if (wsi->u.hdr.ah == NULL) {
62 lwsl_err("Out of memory\n");
65 memset(wsi->u.hdr.ah->frag_index, 0, sizeof(wsi->u.hdr.ah->frag_index));
66 wsi->u.hdr.ah->next_frag_index = 0;
67 wsi->u.hdr.ah->pos = 0;
72 LWS_VISIBLE int lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h)
77 n = wsi->u.hdr.ah->frag_index[h];
81 len += wsi->u.hdr.ah->frags[n].len;
82 n = wsi->u.hdr.ah->frags[n].next_frag_index;
88 LWS_VISIBLE int lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len,
89 enum lws_token_indexes h)
91 int toklen = lws_hdr_total_length(wsi, h);
97 n = wsi->u.hdr.ah->frag_index[h];
103 &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]);
104 dest += wsi->u.hdr.ah->frags[n].len;
105 n = wsi->u.hdr.ah->frags[n].next_frag_index;
111 char *lws_hdr_simple_ptr(struct libwebsocket *wsi, enum lws_token_indexes h)
115 n = wsi->u.hdr.ah->frag_index[h];
119 return &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset];
122 int lws_hdr_simple_create(struct libwebsocket *wsi,
123 enum lws_token_indexes h, const char *s)
125 wsi->u.hdr.ah->next_frag_index++;
126 if (wsi->u.hdr.ah->next_frag_index ==
127 sizeof(wsi->u.hdr.ah->frags) / sizeof(wsi->u.hdr.ah->frags[0])) {
128 lwsl_warn("More hdr frags than we can deal with, dropping\n");
132 wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->next_frag_index;
134 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset =
136 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0;
137 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].next_frag_index =
141 if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
142 lwsl_err("Ran out of header data space\n");
145 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s;
147 wsi->u.hdr.ah->frags[
148 wsi->u.hdr.ah->next_frag_index].len++;
154 static char char_to_hex(const char c)
156 if (c >= '0' && c <= '9')
159 if (c >= 'a' && c <= 'f')
162 if (c >= 'A' && c <= 'F')
168 static int issue_char(struct libwebsocket *wsi, unsigned char c)
170 if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
171 lwsl_warn("excessive header content\n");
175 if( wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len >=
176 wsi->u.hdr.current_token_limit) {
177 lwsl_warn("header %i exceeds limit\n", wsi->u.hdr.parser_state);
181 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c;
183 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++;
188 int libwebsocket_parse(
189 struct libwebsocket_context *context,
190 struct libwebsocket *wsi, unsigned char c)
194 switch (wsi->u.hdr.parser_state) {
195 case WSI_TOKEN_GET_URI:
196 case WSI_TOKEN_POST_URI:
197 case WSI_TOKEN_OPTIONS_URI:
199 case WSI_TOKEN_CONNECTION:
202 case WSI_TOKEN_PROTOCOL:
203 case WSI_TOKEN_UPGRADE:
204 case WSI_TOKEN_ORIGIN:
205 case WSI_TOKEN_SWORIGIN:
206 case WSI_TOKEN_DRAFT:
207 case WSI_TOKEN_CHALLENGE:
209 case WSI_TOKEN_VERSION:
210 case WSI_TOKEN_ACCEPT:
211 case WSI_TOKEN_NONCE:
212 case WSI_TOKEN_EXTENSIONS:
214 case WSI_TOKEN_HTTP_ACCEPT:
215 case WSI_TOKEN_HTTP_AC_REQUEST_HEADERS:
216 case WSI_TOKEN_HTTP_IF_MODIFIED_SINCE:
217 case WSI_TOKEN_HTTP_IF_NONE_MATCH:
218 case WSI_TOKEN_HTTP_ACCEPT_ENCODING:
219 case WSI_TOKEN_HTTP_ACCEPT_LANGUAGE:
220 case WSI_TOKEN_HTTP_PRAGMA:
221 case WSI_TOKEN_HTTP_CACHE_CONTROL:
222 case WSI_TOKEN_HTTP_AUTHORIZATION:
223 case WSI_TOKEN_HTTP_COOKIE:
224 case WSI_TOKEN_HTTP_CONTENT_LENGTH:
225 case WSI_TOKEN_HTTP_CONTENT_TYPE:
226 case WSI_TOKEN_HTTP_DATE:
227 case WSI_TOKEN_HTTP_RANGE:
228 case WSI_TOKEN_HTTP_REFERER:
231 lwsl_parser("WSI_TOK_(%d) '%c'\n", wsi->u.hdr.parser_state, c);
233 /* collect into malloc'd buffers */
234 /* optional initial space swallow */
235 if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->frag_index[
236 wsi->u.hdr.parser_state]].len && c == ' ')
239 if ((wsi->u.hdr.parser_state != WSI_TOKEN_GET_URI) &&
240 (wsi->u.hdr.parser_state != WSI_TOKEN_POST_URI) &&
241 (wsi->u.hdr.parser_state != WSI_TOKEN_OPTIONS_URI))
244 /* special URI processing... end at space */
247 /* enforce starting with / */
248 if (!wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len)
249 if (issue_char(wsi, '/') < 0)
252 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
256 /* special URI processing... convert %xx */
258 switch (wsi->u.hdr.ues) {
261 wsi->u.hdr.ues = URIES_SEEN_PERCENT;
265 case URIES_SEEN_PERCENT:
266 if (char_to_hex(c) < 0) {
268 if (issue_char(wsi, '%') < 0)
270 wsi->u.hdr.ues = URIES_IDLE;
271 /* continue on to assess c */
274 wsi->u.hdr.esc_stash = c;
275 wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1;
278 case URIES_SEEN_PERCENT_H1:
279 if (char_to_hex(c) < 0) {
281 issue_char(wsi, '%');
282 wsi->u.hdr.ues = URIES_IDLE;
283 /* regurgitate + assess */
284 if (libwebsocket_parse(context, wsi, wsi->u.hdr.esc_stash) < 0)
286 /* continue on to assess c */
289 c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) |
291 wsi->u.hdr.ues = URIES_IDLE;
296 * special URI processing...
297 * convert /.. or /... or /../ etc to /
299 * convert // or /// etc to /
300 * leave /.dir or whatever alone
303 switch (wsi->u.hdr.ups) {
305 /* issue the first / always */
307 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
309 case URIPS_SEEN_SLASH:
310 /* swallow subsequent slashes */
313 /* track and swallow the first . after / */
315 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
318 wsi->u.hdr.ups = URIPS_IDLE;
320 case URIPS_SEEN_SLASH_DOT:
321 /* swallow second . */
324 * back up one dir level if possible
325 * safe against header fragmentation because
326 * the method URI can only be in 1 fragment
328 if (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 2) {
329 wsi->u.hdr.ah->pos--;
330 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
332 wsi->u.hdr.ah->pos--;
333 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len--;
334 } while (wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len > 1 &&
335 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos] != '/');
337 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
340 /* change /./ to / */
342 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
345 /* it was like /.dir ... regurgitate the . */
346 wsi->u.hdr.ups = URIPS_IDLE;
347 issue_char(wsi, '.');
350 case URIPS_SEEN_SLASH_DOT_DOT:
351 /* swallow prior .. chars and any subsequent . */
354 /* last issued was /, so another / == // */
357 else /* last we issued was / so SEEN_SLASH */
358 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
360 case URIPS_ARGUMENTS:
361 /* leave them alone */
368 if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
371 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
375 if (c == '?') { /* start of URI arguments */
376 /* seal off uri header */
377 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
379 /* move to using WSI_TOKEN_HTTP_URI_ARGS */
380 wsi->u.hdr.ah->next_frag_index++;
381 wsi->u.hdr.ah->frags[
382 wsi->u.hdr.ah->next_frag_index].offset =
384 wsi->u.hdr.ah->frags[
385 wsi->u.hdr.ah->next_frag_index].len = 0;
386 wsi->u.hdr.ah->frags[
387 wsi->u.hdr.ah->next_frag_index].next_frag_index = 0;
389 wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] =
390 wsi->u.hdr.ah->next_frag_index;
392 /* defeat normal uri path processing */
393 wsi->u.hdr.ups = URIPS_ARGUMENTS;
399 int issue_result = issue_char(wsi, c);
400 if (issue_result < 0) {
403 else if(issue_result > 0) {
404 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
408 /* per-protocol end of headers management */
410 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
411 goto set_parsing_complete;
414 /* collecting and checking a name part */
415 case WSI_TOKEN_NAME_PART:
416 lwsl_parser("WSI_TOKEN_NAME_PART '%c'\n", c);
418 wsi->u.hdr.lextable_pos =
419 lextable_decode(wsi->u.hdr.lextable_pos, c);
421 if (wsi->u.hdr.lextable_pos < 0) {
422 /* this is not a header we know about */
423 if (wsi->u.hdr.ah->frag_index[WSI_TOKEN_GET_URI] ||
424 wsi->u.hdr.ah->frag_index[WSI_TOKEN_POST_URI] ||
425 wsi->u.hdr.ah->frag_index[WSI_TOKEN_OPTIONS_URI] ||
426 wsi->u.hdr.ah->frag_index[WSI_TOKEN_HTTP]) {
428 * altready had the method, no idea what
429 * this crap is, ignore
431 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
435 * hm it's an unknown http method in fact,
439 lwsl_info("Unknown method - dropping\n");
442 if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
446 n = (lextable[wsi->u.hdr.lextable_pos] << 8) | lextable[wsi->u.hdr.lextable_pos + 1];
448 lwsl_parser("known hdr %d\n", n);
449 if (n == WSI_TOKEN_GET_URI &&
450 wsi->u.hdr.ah->frag_index[WSI_TOKEN_GET_URI]) {
451 lwsl_warn("Duplicated GET\n");
453 } else if (n == WSI_TOKEN_POST_URI &&
454 wsi->u.hdr.ah->frag_index[WSI_TOKEN_POST_URI]) {
455 lwsl_warn("Duplicated POST\n");
457 } else if (n == WSI_TOKEN_OPTIONS_URI &&
458 wsi->u.hdr.ah->frag_index[WSI_TOKEN_OPTIONS_URI]) {
459 lwsl_warn("Duplicated OPTIONS\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];
478 wsi->u.hdr.current_token_limit = sizeof(wsi->u.hdr.ah->data);
481 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
482 goto set_parsing_complete;
489 wsi->u.hdr.ah->next_frag_index++;
490 if (wsi->u.hdr.ah->next_frag_index ==
491 sizeof(wsi->u.hdr.ah->frags) /
492 sizeof(wsi->u.hdr.ah->frags[0])) {
493 lwsl_warn("More hdr frags than we can deal with\n");
497 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].offset =
499 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len = 0;
500 wsi->u.hdr.ah->frags[
501 wsi->u.hdr.ah->next_frag_index].next_frag_index = 0;
503 n = wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state];
504 if (!n) { /* first fragment */
505 wsi->u.hdr.ah->frag_index[wsi->u.hdr.parser_state] =
506 wsi->u.hdr.ah->next_frag_index;
510 while (wsi->u.hdr.ah->frags[n].next_frag_index)
511 n = wsi->u.hdr.ah->frags[n].next_frag_index;
512 wsi->u.hdr.ah->frags[n].next_frag_index =
513 wsi->u.hdr.ah->next_frag_index;
515 if (wsi->u.hdr.ah->pos == sizeof(wsi->u.hdr.ah->data)) {
516 lwsl_warn("excessive header content\n");
520 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = ' ';
521 wsi->u.hdr.ah->frags[wsi->u.hdr.ah->next_frag_index].len++;
524 /* skipping arg part of a name we didn't recognize */
525 case WSI_TOKEN_SKIPPING:
526 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
529 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
532 case WSI_TOKEN_SKIPPING_SAW_CR:
533 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
535 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
536 wsi->u.hdr.lextable_pos = 0;
538 wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
540 /* we're done, ignore anything else */
542 case WSI_PARSING_COMPLETE:
543 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
546 default: /* keep gcc happy */
552 set_parsing_complete:
554 if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
555 if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
556 wsi->ietf_spec_revision =
557 atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
559 lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
561 wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
562 wsi->hdr_parsing_completed = 1;
569 * lws_frame_is_binary: true if the current frame was sent in binary mode
571 * @wsi: the connection we are inquiring about
573 * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
574 * it's interested to see if the frame it's dealing with was sent in binary
578 LWS_VISIBLE int lws_frame_is_binary(struct libwebsocket *wsi)
580 return wsi->u.ws.frame_is_binary;
584 libwebsocket_rx_sm(struct libwebsocket *wsi, unsigned char c)
587 struct lws_tokens eff_buf;
590 switch (wsi->lws_rx_parse_state) {
593 switch (wsi->ietf_spec_revision) {
596 * no prepended frame key any more
598 wsi->u.ws.all_zero_nonce = 1;
602 lwsl_warn("lws_rx_sm: unknown spec version %d\n",
603 wsi->ietf_spec_revision);
607 case LWS_RXPS_04_MASK_NONCE_1:
608 wsi->u.ws.frame_masking_nonce_04[1] = c;
610 wsi->u.ws.all_zero_nonce = 0;
611 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_2;
613 case LWS_RXPS_04_MASK_NONCE_2:
614 wsi->u.ws.frame_masking_nonce_04[2] = c;
616 wsi->u.ws.all_zero_nonce = 0;
617 wsi->lws_rx_parse_state = LWS_RXPS_04_MASK_NONCE_3;
619 case LWS_RXPS_04_MASK_NONCE_3:
620 wsi->u.ws.frame_masking_nonce_04[3] = c;
622 wsi->u.ws.all_zero_nonce = 0;
625 * start from the zero'th byte in the XOR key buffer since
626 * this is the start of a frame with a new key
629 wsi->u.ws.frame_mask_index = 0;
631 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
635 * 04 logical framing from the spec (all this is masked when incoming
636 * and has to be unmasked)
638 * We ignore the possibility of extension data because we don't
639 * negotiate any extensions at the moment.
642 * 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
643 * +-+-+-+-+-------+-+-------------+-------------------------------+
644 * |F|R|R|R| opcode|R| Payload len | Extended payload length |
645 * |I|S|S|S| (4) |S| (7) | (16/63) |
646 * |N|V|V|V| |V| | (if payload len==126/127) |
648 * +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
649 * | Extended payload length continued, if payload len == 127 |
650 * + - - - - - - - - - - - - - - - +-------------------------------+
651 * | | Extension data |
652 * +-------------------------------+ - - - - - - - - - - - - - - - +
654 * +---------------------------------------------------------------+
655 * : Application data :
656 * +---------------------------------------------------------------+
658 * We pass payload through to userland as soon as we get it, ignoring
659 * FIN. It's up to userland to buffer it up if it wants to see a
660 * whole unfragmented block of the original size (which may be up to
664 case LWS_RXPS_04_FRAME_HDR_1:
667 wsi->u.ws.opcode = c & 0xf;
668 wsi->u.ws.rsv = c & 0x70;
669 wsi->u.ws.final = !!((c >> 7) & 1);
671 switch (wsi->u.ws.opcode) {
672 case LWS_WS_OPCODE_07__TEXT_FRAME:
673 case LWS_WS_OPCODE_07__BINARY_FRAME:
674 wsi->u.ws.frame_is_binary =
675 wsi->u.ws.opcode == LWS_WS_OPCODE_07__BINARY_FRAME;
678 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
681 case LWS_RXPS_04_FRAME_HDR_LEN:
683 wsi->u.ws.this_frame_masked = !!(c & 0x80);
687 /* control frames are not allowed to have big lengths */
688 if (wsi->u.ws.opcode & 8)
689 goto illegal_ctl_length;
691 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
694 /* control frames are not allowed to have big lengths */
695 if (wsi->u.ws.opcode & 8)
696 goto illegal_ctl_length;
698 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
701 wsi->u.ws.rx_packet_length = c & 0x7f;
702 if (wsi->u.ws.this_frame_masked)
703 wsi->lws_rx_parse_state =
704 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
706 if (wsi->u.ws.rx_packet_length)
707 wsi->lws_rx_parse_state =
708 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
710 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
717 case LWS_RXPS_04_FRAME_HDR_LEN16_2:
718 wsi->u.ws.rx_packet_length = c << 8;
719 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
722 case LWS_RXPS_04_FRAME_HDR_LEN16_1:
723 wsi->u.ws.rx_packet_length |= c;
724 if (wsi->u.ws.this_frame_masked)
725 wsi->lws_rx_parse_state =
726 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
728 wsi->lws_rx_parse_state =
729 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
732 case LWS_RXPS_04_FRAME_HDR_LEN64_8:
734 lwsl_warn("b63 of length must be zero\n");
735 /* kill the connection */
739 wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
741 wsi->u.ws.rx_packet_length = 0;
743 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
746 case LWS_RXPS_04_FRAME_HDR_LEN64_7:
748 wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
750 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
753 case LWS_RXPS_04_FRAME_HDR_LEN64_6:
755 wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
757 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
760 case LWS_RXPS_04_FRAME_HDR_LEN64_5:
762 wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
764 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
767 case LWS_RXPS_04_FRAME_HDR_LEN64_4:
768 wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
769 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
772 case LWS_RXPS_04_FRAME_HDR_LEN64_3:
773 wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
774 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
777 case LWS_RXPS_04_FRAME_HDR_LEN64_2:
778 wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
779 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
782 case LWS_RXPS_04_FRAME_HDR_LEN64_1:
783 wsi->u.ws.rx_packet_length |= ((size_t)c);
784 if (wsi->u.ws.this_frame_masked)
785 wsi->lws_rx_parse_state =
786 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
788 wsi->lws_rx_parse_state =
789 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
792 case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
793 wsi->u.ws.frame_masking_nonce_04[0] = c;
795 wsi->u.ws.all_zero_nonce = 0;
796 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
799 case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
800 wsi->u.ws.frame_masking_nonce_04[1] = c;
802 wsi->u.ws.all_zero_nonce = 0;
803 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
806 case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
807 wsi->u.ws.frame_masking_nonce_04[2] = c;
809 wsi->u.ws.all_zero_nonce = 0;
810 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
813 case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
814 wsi->u.ws.frame_masking_nonce_04[3] = c;
816 wsi->u.ws.all_zero_nonce = 0;
817 wsi->lws_rx_parse_state =
818 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
819 wsi->u.ws.frame_mask_index = 0;
820 if (wsi->u.ws.rx_packet_length == 0) {
821 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
827 case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
829 if (!wsi->u.ws.rx_user_buffer)
830 lwsl_err("NULL user buffer...\n");
832 if (wsi->u.ws.all_zero_nonce)
833 wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
834 (wsi->u.ws.rx_user_buffer_head++)] = c;
836 wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING +
837 (wsi->u.ws.rx_user_buffer_head++)] =
838 c ^ wsi->u.ws.frame_masking_nonce_04[
839 (wsi->u.ws.frame_mask_index++) & 3];
841 if (--wsi->u.ws.rx_packet_length == 0) {
842 /* spill because we have the whole frame */
843 wsi->lws_rx_parse_state = LWS_RXPS_NEW;
848 * if there's no protocol max frame size given, we are
849 * supposed to default to LWS_MAX_SOCKET_IO_BUF
852 if (!wsi->protocol->rx_buffer_size &&
853 wsi->u.ws.rx_user_buffer_head !=
854 LWS_MAX_SOCKET_IO_BUF)
857 if (wsi->protocol->rx_buffer_size &&
858 wsi->u.ws.rx_user_buffer_head !=
859 wsi->protocol->rx_buffer_size)
862 /* spill because we filled our rx buffer */
865 * is this frame a control packet we should take care of at this
866 * layer? If so service it and hide it from the user callback
869 lwsl_parser("spill on %s\n", wsi->protocol->name);
871 switch (wsi->u.ws.opcode) {
872 case LWS_WS_OPCODE_07__CLOSE:
873 /* is this an acknowledgement of our close? */
874 if (wsi->state == WSI_STATE_AWAITING_CLOSE_ACK) {
876 * fine he has told us he is closing too, let's
879 lwsl_parser("seen client close ack\n");
882 lwsl_parser("server sees client close packet\n");
883 /* parrot the close packet payload back */
884 n = libwebsocket_write(wsi, (unsigned char *)
885 &wsi->u.ws.rx_user_buffer[
886 LWS_SEND_BUFFER_PRE_PADDING],
887 wsi->u.ws.rx_user_buffer_head,
890 lwsl_info("write of close ack failed %d\n", n);
891 wsi->state = WSI_STATE_RETURNED_CLOSE_ALREADY;
892 /* close the connection */
895 case LWS_WS_OPCODE_07__PING:
896 lwsl_info("received %d byte ping, sending pong\n",
897 wsi->u.ws.rx_user_buffer_head);
898 lwsl_hexdump(&wsi->u.ws.rx_user_buffer[
899 LWS_SEND_BUFFER_PRE_PADDING],
900 wsi->u.ws.rx_user_buffer_head);
901 /* parrot the ping packet payload back as a pong */
902 n = libwebsocket_write(wsi, (unsigned char *)
903 &wsi->u.ws.rx_user_buffer[LWS_SEND_BUFFER_PRE_PADDING],
904 wsi->u.ws.rx_user_buffer_head, LWS_WRITE_PONG);
907 /* ... then just drop it */
908 wsi->u.ws.rx_user_buffer_head = 0;
911 case LWS_WS_OPCODE_07__PONG:
912 /* ... then just drop it */
913 wsi->u.ws.rx_user_buffer_head = 0;
916 case LWS_WS_OPCODE_07__TEXT_FRAME:
917 case LWS_WS_OPCODE_07__BINARY_FRAME:
918 case LWS_WS_OPCODE_07__CONTINUATION:
922 lwsl_parser("passing opc %x up to exts\n",
925 * It's something special we can't understand here.
926 * Pass the payload up to the extension's parsing
930 eff_buf.token = &wsi->u.ws.rx_user_buffer[
931 LWS_SEND_BUFFER_PRE_PADDING];
932 eff_buf.token_len = wsi->u.ws.rx_user_buffer_head;
934 if (lws_ext_callback_for_each_active(wsi,
935 LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX,
936 &eff_buf, 0) <= 0) /* not handle or fail */
937 lwsl_ext("ext opc opcode 0x%x unknown\n",
940 wsi->u.ws.rx_user_buffer_head = 0;
945 * No it's real payload, pass it up to the user callback.
946 * It's nicely buffered with the pre-padding taken care of
947 * so it can be sent straight out again using libwebsocket_write
950 eff_buf.token = &wsi->u.ws.rx_user_buffer[
951 LWS_SEND_BUFFER_PRE_PADDING];
952 eff_buf.token_len = wsi->u.ws.rx_user_buffer_head;
954 if (lws_ext_callback_for_each_active(wsi,
955 LWS_EXT_CALLBACK_PAYLOAD_RX, &eff_buf, 0) < 0)
958 if (eff_buf.token_len > 0) {
959 eff_buf.token[eff_buf.token_len] = '\0';
961 if (wsi->protocol->callback)
962 ret = user_callback_handle_rxflow(
963 wsi->protocol->callback,
964 wsi->protocol->owning_server,
965 wsi, LWS_CALLBACK_RECEIVE,
970 lwsl_err("No callback on payload spill!\n");
973 wsi->u.ws.rx_user_buffer_head = 0;
981 lwsl_warn("Control frame with xtended length is illegal\n");
982 /* kill the connection */
988 * libwebsockets_remaining_packet_payload() - Bytes to come before "overall"
989 * rx packet is complete
990 * @wsi: Websocket instance (available from user callback)
992 * This function is intended to be called from the callback if the
993 * user code is interested in "complete packets" from the client.
994 * libwebsockets just passes through payload as it comes and issues a buffer
995 * additionally when it hits a built-in limit. The LWS_CALLBACK_RECEIVE
996 * callback handler can use this API to find out if the buffer it has just
997 * been given is the last piece of a "complete packet" from the client --
998 * when that is the case libwebsockets_remaining_packet_payload() will return
1001 * Many protocols won't care becuse their packets are always small.
1005 libwebsockets_remaining_packet_payload(struct libwebsocket *wsi)
1007 return wsi->u.ws.rx_packet_length;