int lws_handshake_client(struct libwebsocket *wsi, unsigned char **buf, size_t len) { int n; switch (wsi->mode) { case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY: case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE: case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY: case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT: case LWS_CONNMODE_WS_CLIENT: for (n = 0; n < len; n++) if (libwebsocket_client_rx_sm(wsi, *(*buf)++)) { lwsl_debug("client_rx_sm failed\n"); return 1; } return 0; default: break; } return 0; }
int libwebsocket_read(struct libwebsocket_context *context, struct libwebsocket *wsi, unsigned char * buf, size_t len) { size_t n; switch (wsi->state) { case WSI_STATE_HTTP: wsi->state = WSI_STATE_HTTP_HEADERS; wsi->parser_state = WSI_TOKEN_NAME_PART; /* fallthru */ case WSI_STATE_HTTP_HEADERS: debug("issuing %d bytes to parser\n", (int)len); #ifdef DEBUG fwrite(buf, 1, len, stderr); #endif switch (wsi->mode) { case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY: case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE: case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY: case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT: case LWS_CONNMODE_WS_CLIENT: for (n = 0; n < len; n++) libwebsocket_client_rx_sm(wsi, *buf++); return 0; default: break; } /* LWS_CONNMODE_WS_SERVING */ for (n = 0; n < len; n++) libwebsocket_parse(wsi, *buf++); if (wsi->parser_state != WSI_PARSING_COMPLETE) break; fprintf(stderr, "seem to be serving, mode is %d\n", wsi->mode); fprintf(stderr, "libwebsocket_parse sees parsing complete\n"); /* is this websocket protocol or normal http 1.0? */ if (!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len || !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len) { if (wsi->protocol->callback) (wsi->protocol->callback)(context, wsi, LWS_CALLBACK_HTTP, wsi->user_space, wsi->utf8_token[WSI_TOKEN_GET_URI].token, 0); wsi->state = WSI_STATE_HTTP; return 0; } if (!wsi->protocol) { fprintf(stderr, "NULL protocol coming on libwebsocket_read\n"); } /* * It's websocket * * Make sure user side is happy about protocol */ while (wsi->protocol->callback) { if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL) { if (wsi->protocol->name == NULL) break; } else if (strcmp( wsi->utf8_token[WSI_TOKEN_PROTOCOL].token, wsi->protocol->name) == 0) break; wsi->protocol++; } /* we didn't find a protocol he wanted? */ if (wsi->protocol->callback == NULL) { if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL) fprintf(stderr, "[no protocol] " "not supported (use NULL .name)\n"); else fprintf(stderr, "Requested protocol %s " "not supported\n", wsi->utf8_token[WSI_TOKEN_PROTOCOL].token); goto bail; } /* * find out which spec version the client is using * if this header is not given, we default to 00 (aka 76) */ if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len) wsi->ietf_spec_revision = atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token); /* * Give the user code a chance to study the request and * have the opportunity to deny it */ if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi, LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION, &wsi->utf8_token[0], NULL, 0)) { fprintf(stderr, "User code denied connection\n"); goto bail; } /* * Perform the handshake according to the protocol version the * client announced */ switch (wsi->ietf_spec_revision) { case 0: /* applies to 76 and 00 */ wsi->xor_mask = xor_no_mask; if (handshake_00(context, wsi)) goto bail; break; case 4: /* 04 */ wsi->xor_mask = xor_mask_04; debug("libwebsocket_parse calling handshake_04\n"); if (handshake_0405(context, wsi)) goto bail; break; case 5: case 6: case 7: case 8: case 13: wsi->xor_mask = xor_mask_05; debug("libwebsocket_parse calling handshake_04\n"); if (handshake_0405(context, wsi)) goto bail; break; default: fprintf(stderr, "Unknown client spec version %d\n", wsi->ietf_spec_revision); goto bail; } fprintf(stderr, "accepted v%02d connection\n", wsi->ietf_spec_revision); break; case WSI_STATE_AWAITING_CLOSE_ACK: case WSI_STATE_ESTABLISHED: switch (wsi->mode) { case LWS_CONNMODE_WS_CLIENT: for (n = 0; n < len; n++) if (libwebsocket_client_rx_sm(wsi, *buf++) < 0) goto bail; return 0; default: break; } /* LWS_CONNMODE_WS_SERVING */ if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) goto bail; break; default: break; } return 0; bail: libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS); return -1; }
LWS_VISIBLE int libwebsocket_read(struct libwebsocket_context *context, struct libwebsocket *wsi, unsigned char *buf, size_t len) { size_t n; struct allocated_headers *ah; char *uri_ptr; int uri_len; switch (wsi->state) { case WSI_STATE_HTTP_ISSUING_FILE: case WSI_STATE_HTTP: wsi->state = WSI_STATE_HTTP_HEADERS; wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART; wsi->u.hdr.lextable_pos = 0; /* fallthru */ case WSI_STATE_HTTP_HEADERS: lwsl_parser("issuing %d bytes to parser\n", (int)len); #ifndef LWS_NO_CLIENT switch (wsi->mode) { case LWS_CONNMODE_WS_CLIENT_WAITING_PROXY_REPLY: case LWS_CONNMODE_WS_CLIENT_ISSUE_HANDSHAKE: case LWS_CONNMODE_WS_CLIENT_WAITING_SERVER_REPLY: case LWS_CONNMODE_WS_CLIENT_WAITING_EXTENSION_CONNECT: case LWS_CONNMODE_WS_CLIENT: for (n = 0; n < len; n++) if (libwebsocket_client_rx_sm(wsi, *buf++)) { lwsl_info("client_rx_sm failed\n"); goto bail; } return 0; default: break; } #endif #ifndef LWS_NO_SERVER /* LWS_CONNMODE_WS_SERVING */ for (n = 0; n < len; n++) if (libwebsocket_parse(wsi, *buf++)) { lwsl_info("libwebsocket_parse failed\n"); goto bail_nuke_ah; } if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE) break; lwsl_parser("libwebsocket_parse sees parsing complete\n"); wsi->mode = LWS_CONNMODE_PRE_WS_SERVING_ACCEPT; /* is this websocket protocol or normal http 1.0? */ if (!lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE) || !lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION)) { /* it's not websocket.... shall we accept it as http? */ if (!lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI)) { lwsl_warn("Missing URI in HTTP request\n"); goto bail_nuke_ah; } lwsl_info("HTTP request for '%s'\n", lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI)); if (libwebsocket_ensure_user_space(wsi)) goto bail_nuke_ah; /* * Hm we still need the headers so the * callback can look at leaders like the URI, but we * need to transition to http union state.... hold a * copy of u.hdr.ah and deallocate afterwards */ ah = wsi->u.hdr.ah; uri_ptr = lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI); uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI); /* union transition */ memset(&wsi->u, 0, sizeof(wsi->u)); wsi->mode = LWS_CONNMODE_HTTP_SERVING_ACCEPTED; wsi->state = WSI_STATE_HTTP; n = 0; if (wsi->protocol->callback) n = wsi->protocol->callback(context, wsi, LWS_CALLBACK_HTTP, wsi->user_space, uri_ptr, uri_len); /* now drop the header info we kept a pointer to */ if (ah) free(ah); if (n) { lwsl_info("LWS_CALLBACK_HTTP closing\n"); goto bail; /* struct ah ptr already nuked */ } return 0; } if (!wsi->protocol) lwsl_err("NULL protocol at libwebsocket_read\n"); /* * It's websocket * * Make sure user side is happy about protocol */ while (wsi->protocol->callback) { if (!lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL)) { if (wsi->protocol->name == NULL) break; } else if (wsi->protocol->name && strcmp( lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), wsi->protocol->name) == 0) break; wsi->protocol++; } /* we didn't find a protocol he wanted? */ if (wsi->protocol->callback == NULL) { if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL) == NULL) { lwsl_info("no protocol -> prot 0 handler\n"); wsi->protocol = &context->protocols[0]; } else { lwsl_err("Req protocol %s not supported\n", lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL)); goto bail_nuke_ah; } } /* allocate wsi->user storage */ if (libwebsocket_ensure_user_space(wsi)) goto bail_nuke_ah; /* * Give the user code a chance to study the request and * have the opportunity to deny it */ if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi, LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION, wsi->user_space, lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL), 0)) { lwsl_warn("User code denied connection\n"); goto bail_nuke_ah; } /* * Perform the handshake according to the protocol version the * client announced */ switch (wsi->ietf_spec_revision) { case 13: lwsl_parser("lws_parse calling handshake_04\n"); if (handshake_0405(context, wsi)) { lwsl_info("hs0405 has failed the connection\n"); goto bail_nuke_ah; } break; default: lwsl_warn("Unknown client spec version %d\n", wsi->ietf_spec_revision); goto bail_nuke_ah; } /* drop the header info -- no bail_nuke_ah after this */ if (wsi->u.hdr.ah) free(wsi->u.hdr.ah); wsi->mode = LWS_CONNMODE_WS_SERVING; /* union transition */ memset(&wsi->u, 0, sizeof(wsi->u)); wsi->u.ws.rxflow_change_to = LWS_RXFLOW_ALLOW; /* * create the frame buffer for this connection according to the * size mentioned in the protocol definition. If 0 there, use * a big default for compatibility */ n = wsi->protocol->rx_buffer_size; if (!n) n = LWS_MAX_SOCKET_IO_BUF; n += LWS_SEND_BUFFER_PRE_PADDING + LWS_SEND_BUFFER_POST_PADDING; wsi->u.ws.rx_user_buffer = malloc(n); if (!wsi->u.ws.rx_user_buffer) { lwsl_err("Out of Mem allocating rx buffer %d\n", n); goto bail; } lwsl_info("Allocating RX buffer %d\n", n); if (setsockopt(wsi->sock, SOL_SOCKET, SO_SNDBUF, (const void*) &n, sizeof n)) { lwsl_warn("Failed to set SNDBUF to %d", n); goto bail; } lwsl_parser("accepted v%02d connection\n", wsi->ietf_spec_revision); #endif break; case WSI_STATE_AWAITING_CLOSE_ACK: case WSI_STATE_ESTABLISHED: #ifndef LWS_NO_CLIENT switch (wsi->mode) { case LWS_CONNMODE_WS_CLIENT: for (n = 0; n < len; n++) if (libwebsocket_client_rx_sm( wsi, *buf++) < 0) { lwsl_info("client rx has bailed\n"); goto bail; } return 0; default: break; } #endif #ifndef LWS_NO_SERVER /* LWS_CONNMODE_WS_SERVING */ if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) { lwsl_info("interpret_incoming_packet has bailed\n"); goto bail; } #endif break; default: lwsl_err("libwebsocket_read: Unhandled state\n"); break; } return 0; bail_nuke_ah: /* drop the header info */ if (wsi->u.hdr.ah) free(wsi->u.hdr.ah); bail: lwsl_info("closing connection at libwebsocket_read bail:\n"); libwebsocket_close_and_free_session(context, wsi, LWS_CLOSE_STATUS_NOSTATUS); return -1; }