2010-11-08 20:20:42 +00:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
2010-11-13 10:03:47 +00:00
|
|
|
*
|
2013-01-18 11:43:21 +08:00
|
|
|
* Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
|
2010-11-08 20:20:42 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "private-libwebsockets.h"
|
|
|
|
|
2011-01-18 15:39:02 +00:00
|
|
|
/*
|
|
|
|
* -04 of the protocol (actually the 80th version) has a radically different
|
|
|
|
* handshake. The 04 spec gives the following idea
|
|
|
|
*
|
|
|
|
* The handshake from the client looks as follows:
|
|
|
|
*
|
|
|
|
* GET /chat HTTP/1.1
|
|
|
|
* Host: server.example.com
|
|
|
|
* Upgrade: websocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
|
|
|
|
* Sec-WebSocket-Origin: http://example.com
|
|
|
|
* Sec-WebSocket-Protocol: chat, superchat
|
2011-01-18 17:14:03 +00:00
|
|
|
* Sec-WebSocket-Version: 4
|
2011-01-18 15:39:02 +00:00
|
|
|
*
|
|
|
|
* The handshake from the server looks as follows:
|
|
|
|
*
|
|
|
|
* HTTP/1.1 101 Switching Protocols
|
|
|
|
* Upgrade: websocket
|
|
|
|
* Connection: Upgrade
|
|
|
|
* Sec-WebSocket-Accept: me89jWimTRKTWwrS3aRrL53YZSo=
|
|
|
|
* Sec-WebSocket-Nonce: AQIDBAUGBwgJCgsMDQ4PEC==
|
|
|
|
* Sec-WebSocket-Protocol: chat
|
|
|
|
*/
|
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/*
|
|
|
|
* We have to take care about parsing because the headers may be split
|
|
|
|
* into multiple fragments. They may contain unknown headers with arbitrary
|
|
|
|
* argument lengths. So, we parse using a single-character at a time state
|
|
|
|
* machine that is completely independent of packet size.
|
|
|
|
*/
|
|
|
|
|
2010-11-13 10:03:47 +00:00
|
|
|
int
|
2012-04-09 15:09:01 +08:00
|
|
|
libwebsocket_read(struct libwebsocket_context *context,
|
|
|
|
struct libwebsocket *wsi, unsigned char * buf, size_t len)
|
2010-11-08 20:20:42 +00:00
|
|
|
{
|
|
|
|
size_t n;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
switch (wsi->state) {
|
2013-01-15 13:40:23 +08:00
|
|
|
case WSI_STATE_HTTP_ISSUING_FILE:
|
2010-11-08 20:20:42 +00:00
|
|
|
case WSI_STATE_HTTP:
|
|
|
|
wsi->state = WSI_STATE_HTTP_HEADERS;
|
2013-01-21 11:04:23 +08:00
|
|
|
wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
|
|
|
|
wsi->u.hdr.lextable_pos = 0;
|
2010-11-08 20:20:42 +00:00
|
|
|
/* fallthru */
|
|
|
|
case WSI_STATE_HTTP_HEADERS:
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2013-01-10 19:50:35 +08:00
|
|
|
lwsl_parser("issuing %d bytes to parser\n", (int)len);
|
2013-01-10 12:35:18 +08:00
|
|
|
#ifdef _DEBUG
|
2013-01-10 19:50:35 +08:00
|
|
|
//fwrite(buf, 1, len, stderr);
|
2010-11-08 20:20:42 +00:00
|
|
|
#endif
|
2011-01-22 12:51:57 +00:00
|
|
|
|
2013-01-16 11:47:40 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2013-01-16 14:35:12 +08:00
|
|
|
|
|
|
|
// lwsl_info("mode=%d\n", wsi->mode);
|
|
|
|
|
2011-02-09 07:16:34 +00:00
|
|
|
switch (wsi->mode) {
|
2011-05-23 10:00:03 +01:00
|
|
|
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:
|
2011-02-09 07:16:34 +00:00
|
|
|
case LWS_CONNMODE_WS_CLIENT:
|
2011-01-22 12:51:57 +00:00
|
|
|
for (n = 0; n < len; n++)
|
|
|
|
libwebsocket_client_rx_sm(wsi, *buf++);
|
|
|
|
|
|
|
|
return 0;
|
2011-02-09 07:16:34 +00:00
|
|
|
default:
|
|
|
|
break;
|
2011-01-22 12:51:57 +00:00
|
|
|
}
|
2013-01-16 11:47:40 +08:00
|
|
|
#endif
|
2013-01-18 11:43:21 +08:00
|
|
|
#ifndef LWS_NO_SERVER
|
2011-02-09 07:16:34 +00:00
|
|
|
/* LWS_CONNMODE_WS_SERVING */
|
2011-01-22 12:51:57 +00:00
|
|
|
|
2013-01-18 11:43:21 +08:00
|
|
|
extern int handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi);
|
|
|
|
|
2010-11-13 10:03:47 +00:00
|
|
|
for (n = 0; n < len; n++)
|
2010-11-08 20:20:42 +00:00
|
|
|
libwebsocket_parse(wsi, *buf++);
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2013-01-21 11:04:23 +08:00
|
|
|
if (wsi->u.hdr.parser_state != WSI_PARSING_COMPLETE)
|
2010-11-08 20:20:42 +00:00
|
|
|
break;
|
|
|
|
|
2013-01-10 19:50:35 +08:00
|
|
|
lwsl_parser("seem to be serving, mode is %d\n", wsi->mode);
|
2011-05-23 10:00:03 +01:00
|
|
|
|
2013-01-10 19:50:35 +08:00
|
|
|
lwsl_parser("libwebsocket_parse sees parsing complete\n");
|
2011-01-22 12:51:57 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
/* is this websocket protocol or normal http 1.0? */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
if (!wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
|
|
|
|
!wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len) {
|
2012-04-05 10:29:29 +08:00
|
|
|
wsi->state = WSI_STATE_HTTP;
|
2010-11-12 10:44:16 +00:00
|
|
|
if (wsi->protocol->callback)
|
2013-01-10 10:37:29 +08:00
|
|
|
if (wsi->protocol->callback(context, wsi,
|
|
|
|
LWS_CALLBACK_HTTP, wsi->user_space,
|
|
|
|
wsi->utf8_token[WSI_TOKEN_GET_URI].token,
|
2013-01-20 17:08:31 +08:00
|
|
|
wsi->utf8_token[WSI_TOKEN_GET_URI].token_len)) {
|
|
|
|
lwsl_info("LWS_CALLBACK_HTTP wanted to close\n");
|
2013-01-10 10:37:29 +08:00
|
|
|
goto bail;
|
2013-01-20 17:08:31 +08:00
|
|
|
}
|
2010-11-08 20:20:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-07 19:53:23 +08:00
|
|
|
if (!wsi->protocol)
|
2013-01-10 19:50:35 +08:00
|
|
|
lwsl_err("NULL protocol at libwebsocket_read\n");
|
2011-05-23 10:00:03 +01:00
|
|
|
|
2013-01-20 17:08:31 +08:00
|
|
|
|
2011-01-18 17:14:03 +00:00
|
|
|
/*
|
|
|
|
* It's websocket
|
|
|
|
*
|
|
|
|
* Make sure user side is happy about protocol
|
|
|
|
*/
|
2010-11-11 12:28:29 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
while (wsi->protocol->callback) {
|
|
|
|
|
|
|
|
if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL) {
|
|
|
|
if (wsi->protocol->name == NULL)
|
|
|
|
break;
|
|
|
|
} else
|
2012-04-03 17:22:19 +01:00
|
|
|
if (wsi->protocol->name && strcmp(
|
2010-11-12 10:44:16 +00:00
|
|
|
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token,
|
|
|
|
wsi->protocol->name) == 0)
|
|
|
|
break;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
wsi->protocol++;
|
|
|
|
}
|
2010-11-12 11:15:49 +00:00
|
|
|
|
|
|
|
/* we didn't find a protocol he wanted? */
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-12 10:44:16 +00:00
|
|
|
if (wsi->protocol->callback == NULL) {
|
2013-01-30 12:26:14 +08:00
|
|
|
if (wsi->utf8_token[WSI_TOKEN_PROTOCOL].token == NULL) {
|
|
|
|
lwsl_info("[no protocol] "
|
|
|
|
"mapped to protocol 0 handler\n");
|
|
|
|
wsi->protocol = &context->protocols[0];
|
|
|
|
} else {
|
2013-01-10 19:50:35 +08:00
|
|
|
lwsl_err("Requested protocol %s "
|
2010-11-12 10:44:16 +00:00
|
|
|
"not supported\n",
|
|
|
|
wsi->utf8_token[WSI_TOKEN_PROTOCOL].token);
|
2013-01-30 12:26:14 +08:00
|
|
|
goto bail;
|
|
|
|
}
|
2010-11-12 10:44:16 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 10:03:47 +00:00
|
|
|
/*
|
2011-01-18 17:14:03 +00:00
|
|
|
* find out which spec version the client is using
|
|
|
|
* if this header is not given, we default to 00 (aka 76)
|
2010-11-08 20:20:42 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-18 17:14:03 +00:00
|
|
|
if (wsi->utf8_token[WSI_TOKEN_VERSION].token_len)
|
|
|
|
wsi->ietf_spec_revision =
|
|
|
|
atoi(wsi->utf8_token[WSI_TOKEN_VERSION].token);
|
2010-11-08 20:20:42 +00:00
|
|
|
|
2011-02-13 08:25:26 +00:00
|
|
|
/*
|
|
|
|
* Give the user code a chance to study the request and
|
|
|
|
* have the opportunity to deny it
|
|
|
|
*/
|
|
|
|
|
2011-02-14 09:14:25 +00:00
|
|
|
if ((wsi->protocol->callback)(wsi->protocol->owning_server, wsi,
|
2011-02-13 08:25:26 +00:00
|
|
|
LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION,
|
|
|
|
&wsi->utf8_token[0], NULL, 0)) {
|
2013-01-10 19:50:35 +08:00
|
|
|
lwsl_warn("User code denied connection\n");
|
2011-02-13 08:25:26 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 17:14:03 +00:00
|
|
|
/*
|
2011-01-19 12:20:27 +00:00
|
|
|
* Perform the handshake according to the protocol version the
|
|
|
|
* client announced
|
2011-01-18 17:14:03 +00:00
|
|
|
*/
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2011-01-18 17:14:03 +00:00
|
|
|
switch (wsi->ietf_spec_revision) {
|
2011-09-25 09:32:54 +01:00
|
|
|
case 13:
|
2013-01-10 19:50:35 +08:00
|
|
|
lwsl_parser("libwebsocket_parse calling handshake_04\n");
|
2013-01-20 17:08:31 +08:00
|
|
|
if (handshake_0405(context, wsi)) {
|
|
|
|
lwsl_info("handshake_0405 xor 05 has failed the connection\n");
|
2011-01-18 17:14:03 +00:00
|
|
|
goto bail;
|
2013-01-20 17:08:31 +08:00
|
|
|
}
|
2011-01-18 17:14:03 +00:00
|
|
|
break;
|
2011-02-09 08:49:14 +00:00
|
|
|
|
2011-01-18 17:14:03 +00:00
|
|
|
default:
|
2013-01-10 19:50:35 +08:00
|
|
|
lwsl_warn("Unknown client spec version %d\n",
|
2011-01-18 17:14:03 +00:00
|
|
|
wsi->ietf_spec_revision);
|
2010-11-08 20:20:42 +00:00
|
|
|
goto bail;
|
|
|
|
}
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2013-01-15 13:40:23 +08:00
|
|
|
wsi->mode = LWS_CONNMODE_WS_SERVING;
|
|
|
|
|
2013-01-21 11:04:23 +08:00
|
|
|
/* union transition */
|
|
|
|
memset(&wsi->u, 0, sizeof wsi->u);
|
|
|
|
|
2013-01-10 19:50:35 +08:00
|
|
|
lwsl_parser("accepted v%02d connection\n",
|
2011-02-09 08:49:14 +00:00
|
|
|
wsi->ietf_spec_revision);
|
2013-01-18 11:43:21 +08:00
|
|
|
#endif
|
2010-11-08 20:20:42 +00:00
|
|
|
break;
|
|
|
|
|
2011-03-07 07:08:12 +00:00
|
|
|
case WSI_STATE_AWAITING_CLOSE_ACK:
|
2010-11-08 20:20:42 +00:00
|
|
|
case WSI_STATE_ESTABLISHED:
|
2013-01-16 11:47:40 +08:00
|
|
|
#ifndef LWS_NO_CLIENT
|
2011-02-09 07:16:34 +00:00
|
|
|
switch (wsi->mode) {
|
|
|
|
case LWS_CONNMODE_WS_CLIENT:
|
2011-01-22 12:51:57 +00:00
|
|
|
for (n = 0; n < len; n++)
|
2013-01-20 17:08:31 +08:00
|
|
|
if (libwebsocket_client_rx_sm(wsi, *buf++) < 0) {
|
|
|
|
lwsl_info("client rx has bailed\n");
|
2011-05-24 22:07:45 +01:00
|
|
|
goto bail;
|
2013-01-20 17:08:31 +08:00
|
|
|
}
|
2011-01-22 12:51:57 +00:00
|
|
|
|
|
|
|
return 0;
|
2011-02-09 07:16:34 +00:00
|
|
|
default:
|
|
|
|
break;
|
2011-01-22 12:51:57 +00:00
|
|
|
}
|
2013-01-16 11:47:40 +08:00
|
|
|
#endif
|
2013-01-18 11:43:21 +08:00
|
|
|
#ifndef LWS_NO_SERVER
|
2011-02-09 07:16:34 +00:00
|
|
|
/* LWS_CONNMODE_WS_SERVING */
|
|
|
|
|
2013-01-20 17:08:31 +08:00
|
|
|
if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) {
|
|
|
|
lwsl_info("interpret_incoming_packet has bailed\n");
|
2010-11-08 20:20:42 +00:00
|
|
|
goto bail;
|
2013-01-20 17:08:31 +08:00
|
|
|
}
|
2013-01-18 11:43:21 +08:00
|
|
|
#endif
|
2010-11-08 20:20:42 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-01-15 13:40:23 +08:00
|
|
|
lwsl_err("libwebsocket_read: Unhandled state\n");
|
2010-11-08 20:20:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
return 0;
|
2010-11-13 10:03:47 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
bail:
|
2013-01-20 17:08:31 +08:00
|
|
|
lwsl_info("closing connection at libwebsocket_read bail:\n");
|
2011-03-02 22:03:47 +00:00
|
|
|
libwebsocket_close_and_free_session(context, wsi,
|
2011-02-26 11:04:01 +00:00
|
|
|
LWS_CLOSE_STATUS_NOSTATUS);
|
2011-01-23 16:50:33 +00:00
|
|
|
|
2010-11-08 20:20:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|