Lines Matching refs:ws
188 for (i = 0; i < sizeof(session->ws->key); i++) {
189 snprintf(&buf[i*3], 4, " %02x", session->ws->key[i]);
196 coap_ws_state_t *ws = session->ws;
200 data[i] ^= ws->mask_key[i%4];
211 if (!session->ws) {
212 session->ws = coap_malloc_type(COAP_STRING, sizeof(coap_ws_state_t));
213 if (!session->ws) {
217 memset(session->ws, 0, sizeof(coap_ws_state_t));
220 if (!session->ws->up) {
224 if (session->ws->sent_close)
247 if (session->ws->state == COAP_SESSION_TYPE_CLIENT) {
252 memcpy(session->ws->mask_key, &ws_header[hdr_len], 4);
260 if (session->ws->state == COAP_SESSION_TYPE_CLIENT) {
268 session->ws->data_size = datalen;
280 coap_log_debug("* %s: ws: sent %4zd bytes\n",
283 coap_log_debug("* %s: ws: sent %4zd of %4zd bytes\n",
290 char *cp = strchr((char *)session->ws->http_hdr, ' ');
293 cp = strchr((char *)session->ws->http_hdr, '\t');
307 coap_ws_state_t *ws = session->ws;
310 if (!ws->seen_first) {
311 if (strcasecmp((char *)ws->http_hdr,
313 coap_log_info("WS: Invalid GET request %s\n", (char *)ws->http_hdr);
316 ws->seen_first = 1;
324 if (strcasecmp((char *)ws->http_hdr, "Host:") == 0) {
325 if (ws->seen_host) {
329 ws->seen_host = 1;
330 } else if (strcasecmp((char *)ws->http_hdr, "Upgrade:") == 0) {
331 if (ws->seen_upg) {
339 ws->seen_upg = 1;
340 } else if (strcasecmp((char *)ws->http_hdr, "Connection:") == 0) {
341 if (ws->seen_conn) {
349 ws->seen_conn = 1;
350 } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Key:") == 0) {
353 if (ws->seen_key) {
357 if (!coap_base64_decode_buffer(value, &len, ws->key,
358 sizeof(ws->key)) ||
359 len != sizeof(ws->key)) {
364 ws->seen_key = 1;
365 } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Protocol:") == 0) {
366 if (ws->seen_proto) {
374 ws->seen_proto = 1;
375 } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Version:") == 0) {
376 if (ws->seen_ver) {
384 ws->seen_ver = 1;
399 if (!coap_base64_encode_buffer(session->ws->key, sizeof(session->ws->key),
421 coap_ws_state_t *ws = session->ws;
424 if (!ws->seen_first) {
427 if (strcmp((char *)ws->http_hdr, "HTTP/1.1") != 0 ||
429 coap_log_info("WS: Invalid GET response %s\n", (char *)ws->http_hdr);
432 ws->seen_first = 1;
440 if (strcasecmp((char *)ws->http_hdr, "Upgrade:") == 0) {
441 if (ws->seen_upg) {
449 ws->seen_upg = 1;
450 } else if (strcasecmp((char *)ws->http_hdr, "Connection:") == 0) {
451 if (ws->seen_conn) {
459 ws->seen_conn = 1;
460 } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Accept:") == 0) {
463 if (ws->seen_key) {
473 ws->seen_key = 1;
474 } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Protocol:") == 0) {
475 if (ws->seen_proto) {
483 ws->seen_proto = 1;
496 coap_ws_state_t *ws = session->ws;
501 while (!ws->up) {
506 rem = ws->http_ofs > (sizeof(ws->http_hdr) - 1 - COAP_MAX_FS) ?
507 sizeof(ws->http_hdr) - ws->http_ofs : COAP_MAX_FS;
509 &ws->http_hdr[ws->http_ofs],
516 ws->http_ofs += (uint32_t)bytes;
517 ws->http_hdr[ws->http_ofs] = '\000';
519 cp = (char *)ws->http_hdr;
521 cp = strchr((char *)ws->http_hdr, '\n');
525 if (cp != (char *)ws->http_hdr) {
530 coap_log_debug("WS: HTTP: %s\n", ws->http_hdr);
531 if (ws->http_hdr[0] != '\000') {
532 if (ws->state == COAP_SESSION_TYPE_SERVER) {
543 rem = ws->http_ofs - ((uint8_t *)cp + 1 - ws->http_hdr);
544 if (ws->http_hdr[0] == '\000') {
546 if (ws->state == COAP_SESSION_TYPE_SERVER) {
547 if (!(ws->seen_first && ws->seen_host && ws->seen_upg &&
548 ws->seen_conn && ws->seen_key && ws->seen_proto &&
549 ws->seen_ver)) {
554 if (!(ws->seen_first && ws->seen_upg && ws->seen_conn &&
555 ws->seen_key && ws->seen_proto)) {
560 ws->up = 1;
561 ws->hdr_ofs = (int)rem;
563 memcpy(ws->rd_header, cp + 1, rem);
566 ws->http_ofs = (uint32_t)rem;
567 memmove(ws->http_hdr, cp + 1, rem);
568 ws->http_hdr[ws->http_ofs] = '\000';
586 if (!session->ws) {
587 session->ws = coap_malloc_type(COAP_STRING, sizeof(coap_ws_state_t));
588 if (!session->ws) {
592 memset(session->ws, 0, sizeof(coap_ws_state_t));
595 if (!session->ws->up) {
609 if (!session->ws->up)
612 if (session->ws->state == COAP_SESSION_TYPE_SERVER) {
631 if (session->ws->hdr_ofs == 0)
636 if (!session->ws->all_hdr_in) {
638 &session->ws->rd_header[session->ws->hdr_ofs],
639 sizeof(session->ws->rd_header) - session->ws->hdr_ofs);
642 session->ws->hdr_ofs += (int)ret;
644 if (session->ws->hdr_ofs < 2)
647 if (session->ws->state == COAP_SESSION_TYPE_SERVER &&
648 !(session->ws->rd_header[1] & WS_B1_MASK_BIT)) {
650 session->ws->close_reason = 1002;
655 bytes_size = session->ws->rd_header[1] & WS_B1_LEN_MASK;
661 if (session->ws->rd_header[1] & WS_B1_MASK_BIT) {
662 memcpy(session->ws->mask_key, &session->ws->rd_header[2 + extra_hdr_len], 4);
665 if (session->ws->hdr_ofs < 2 + extra_hdr_len)
669 coap_ws_log_header(session, session->ws->rd_header);
671 op_code = session->ws->rd_header[0] & WS_B0_OP_MASK;
674 session->ws->close_reason = 1003;
680 session->ws->recv_close = 1;
685 session->ws->all_hdr_in = 1;
689 bytes_size = ((uint64_t)session->ws->rd_header[2] << 56) +
690 ((uint64_t)session->ws->rd_header[3] << 48) +
691 ((uint64_t)session->ws->rd_header[4] << 40) +
692 ((uint64_t)session->ws->rd_header[5] << 32) +
693 ((uint64_t)session->ws->rd_header[6] << 24) +
694 ((uint64_t)session->ws->rd_header[7] << 16) +
695 ((uint64_t)session->ws->rd_header[8] << 8) +
696 session->ws->rd_header[9];
698 bytes_size = ((uint16_t)session->ws->rd_header[2] << 8) +
699 session->ws->rd_header[3];
701 session->ws->data_size = bytes_size;
706 session->ws->close_reason = 1009;
714 ret = session->ws->hdr_ofs - 2 - extra_hdr_len;
716 assert(2 + extra_hdr_len < (ssize_t)sizeof(session->ws->rd_header));
720 memcpy(data, &session->ws->rd_header[2 + extra_hdr_len], ret);
721 session->ws->data_ofs = ret;
723 if (session->ws->state == COAP_SESSION_TYPE_SERVER) {
727 session->ws->all_hdr_in = 0;
728 session->ws->hdr_ofs = 0;
729 op_code = session->ws->rd_header[0] & WS_B0_OP_MASK;
731 session->ws->close_reason = (data[0] << 8) + data[1];
734 session->ws->close_reason);
735 session->ws->recv_close = 1;
736 if (!session->ws->sent_close)
744 memcpy(data, &session->ws->rd_header[2 + extra_hdr_len], bytes_size);
745 session->ws->data_ofs = bytes_size;
746 if (session->ws->state == COAP_SESSION_TYPE_SERVER) {
751 memmove(session->ws->rd_header,
752 &session->ws->rd_header[2 + extra_hdr_len + bytes_size],
754 session->ws->all_hdr_in = 0;
755 session->ws->hdr_ofs = (int)(ret - bytes_size);
759 session->ws->data_ofs = 0;
765 &data[session->ws->data_ofs],
766 session->ws->data_size - session->ws->data_ofs);
769 session->ws->data_ofs += ret;
770 if (session->ws->data_ofs == session->ws->data_size) {
771 if (session->ws->state == COAP_SESSION_TYPE_SERVER) {
773 coap_ws_mask_data(session, data, session->ws->data_size);
775 session->ws->all_hdr_in = 0;
776 session->ws->hdr_ofs = 0;
777 session->ws->data_ofs = 0;
778 coap_log_debug("* %s: ws: recv %4zd bytes\n",
779 coap_session_str(session), session->ws->data_size);
780 return session->ws->data_size;
784 session->ws->data_size, session->ws->data_ofs);
800 if (!session->ws) {
801 session->ws = coap_malloc_type(COAP_STRING, sizeof(coap_ws_state_t));
802 if (!session->ws) {
806 memset(session->ws, 0, sizeof(coap_ws_state_t));
814 session->ws->state = COAP_SESSION_TYPE_CLIENT;
820 coap_prng(session->ws->key, sizeof(session->ws->key));
822 if (!coap_base64_encode_buffer(session->ws->key, sizeof(session->ws->key),
850 session->ws->state = COAP_SESSION_TYPE_SERVER;
861 if (session->ws && session->ws->up) {
864 if (!session->ws->sent_close) {
871 if (session->ws->state == COAP_SESSION_TYPE_CLIENT) {
875 memcpy(session->ws->mask_key, &ws_header[hdr_len], 4);
879 if (session->ws->close_reason == 0)
880 session->ws->close_reason = 1000;
882 ws_header[hdr_len] = session->ws->close_reason >> 8;
883 ws_header[hdr_len+1] = session->ws->close_reason & 0xff;
884 if (session->ws->state == COAP_SESSION_TYPE_CLIENT) {
887 session->ws->sent_close = 1;
890 session->ws->close_reason);
897 while (!session->ws->recv_close && count > 0 && coap_netif_available(session)) {