2013-01-18 11:43:21 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2018-04-11 13:39:42 +08:00
|
|
|
* Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
|
2013-01-18 11:43:21 +08: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
|
|
|
|
*/
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
#include <private-libwebsockets.h>
|
2013-01-18 11:43:21 +08:00
|
|
|
|
|
|
|
#define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
|
2017-03-07 10:07:37 +08:00
|
|
|
|
2018-03-19 09:33:55 +08:00
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2017-03-07 10:07:37 +08:00
|
|
|
static int
|
|
|
|
lws_extension_server_handshake(struct lws *wsi, char **p, int budget)
|
2014-04-10 14:08:10 +08:00
|
|
|
{
|
2015-12-15 21:15:58 +08:00
|
|
|
struct lws_context *context = wsi->context;
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
2017-03-07 10:07:37 +08:00
|
|
|
char ext_name[64], *args, *end = (*p) + budget - 1;
|
|
|
|
const struct lws_ext_options *opts, *po;
|
2016-01-11 11:34:01 +08:00
|
|
|
const struct lws_extension *ext;
|
2017-03-07 10:07:37 +08:00
|
|
|
struct lws_ext_option_arg oa;
|
|
|
|
int n, m, more = 1;
|
2014-04-10 14:08:10 +08:00
|
|
|
int ext_count = 0;
|
2015-12-30 12:12:58 +08:00
|
|
|
char ignore;
|
2016-01-29 21:18:54 +08:00
|
|
|
char *c;
|
2014-04-10 14:08:10 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out which extensions the client has that we want to
|
|
|
|
* enable on this connection, and give him back the list
|
|
|
|
*/
|
|
|
|
if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* break down the list of client extensions
|
|
|
|
* and go through them
|
|
|
|
*/
|
|
|
|
|
2016-05-19 12:34:35 +08:00
|
|
|
if (lws_hdr_copy(wsi, (char *)pt->serv_buf, context->pt_serv_buf_size,
|
2016-01-19 03:34:24 +08:00
|
|
|
WSI_TOKEN_EXTENSIONS) < 0)
|
2014-04-10 14:08:10 +08:00
|
|
|
return 1;
|
|
|
|
|
2016-01-19 03:34:24 +08:00
|
|
|
c = (char *)pt->serv_buf;
|
2014-04-10 14:08:10 +08:00
|
|
|
lwsl_parser("WSI_TOKEN_EXTENSIONS = '%s'\n", c);
|
2016-01-11 11:34:01 +08:00
|
|
|
wsi->count_act_ext = 0;
|
2015-12-30 12:12:58 +08:00
|
|
|
ignore = 0;
|
2017-03-07 10:07:37 +08:00
|
|
|
n = 0;
|
|
|
|
args = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We may get a simple request
|
|
|
|
*
|
|
|
|
* Sec-WebSocket-Extensions: permessage-deflate
|
|
|
|
*
|
|
|
|
* or an elaborated one with requested options
|
|
|
|
*
|
|
|
|
* Sec-WebSocket-Extensions: permessage-deflate; \
|
|
|
|
* server_no_context_takeover; \
|
|
|
|
* client_no_context_takeover
|
|
|
|
*/
|
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
while (more) {
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if (*c && (*c != ',' && *c != '\t')) {
|
2017-03-07 10:07:37 +08:00
|
|
|
if (*c == ';') {
|
2015-12-30 12:12:58 +08:00
|
|
|
ignore = 1;
|
2017-03-07 10:07:37 +08:00
|
|
|
args = c + 1;
|
|
|
|
}
|
2016-01-11 11:34:01 +08:00
|
|
|
if (ignore || *c == ' ') {
|
2015-12-30 12:12:58 +08:00
|
|
|
c++;
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-10 14:08:10 +08:00
|
|
|
ext_name[n] = *c++;
|
2017-10-20 17:45:02 +08:00
|
|
|
if (n < (int)sizeof(ext_name) - 1)
|
2014-04-10 14:08:10 +08:00
|
|
|
n++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ext_name[n] = '\0';
|
2015-12-30 12:12:58 +08:00
|
|
|
|
|
|
|
ignore = 0;
|
2014-04-10 14:08:10 +08:00
|
|
|
if (!*c)
|
|
|
|
more = 0;
|
|
|
|
else {
|
|
|
|
c++;
|
|
|
|
if (!n)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-07 10:07:37 +08:00
|
|
|
while (args && *args && *args == ' ')
|
|
|
|
args++;
|
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
/* check a client's extension against our support */
|
|
|
|
|
2016-03-28 10:10:43 +08:00
|
|
|
ext = wsi->vhost->extensions;
|
2014-04-10 14:08:10 +08:00
|
|
|
|
|
|
|
while (ext && ext->callback) {
|
|
|
|
|
|
|
|
if (strcmp(ext_name, ext->name)) {
|
|
|
|
ext++;
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-07 10:07:37 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
/*
|
2015-12-14 08:52:03 +08:00
|
|
|
* oh, we do support this one he asked for... but let's
|
2017-03-07 10:07:37 +08:00
|
|
|
* confirm he only gave it once
|
|
|
|
*/
|
|
|
|
for (m = 0; m < wsi->count_act_ext; m++)
|
|
|
|
if (wsi->active_extensions[m] == ext) {
|
|
|
|
lwsl_info("extension mentioned twice\n");
|
|
|
|
return 1; /* shenanigans */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-12-14 08:52:03 +08:00
|
|
|
* ask user code if it's OK to apply it on this
|
2014-04-10 14:08:10 +08:00
|
|
|
* particular connection + protocol
|
|
|
|
*/
|
2017-05-27 22:51:58 +07:00
|
|
|
m = (wsi->protocol->callback)(wsi,
|
2015-12-17 07:54:44 +08:00
|
|
|
LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
|
|
|
|
wsi->user_space, ext_name, 0);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
|
|
|
/*
|
2015-12-14 08:52:03 +08:00
|
|
|
* zero return from callback means go ahead and allow
|
|
|
|
* the extension, it's what we get if the callback is
|
2014-04-10 14:08:10 +08:00
|
|
|
* unhandled
|
|
|
|
*/
|
2016-01-11 11:34:01 +08:00
|
|
|
if (m) {
|
2014-04-10 14:08:10 +08:00
|
|
|
ext++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* apply it */
|
|
|
|
|
|
|
|
ext_count++;
|
|
|
|
|
|
|
|
/* instantiate the extension on this conn */
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
wsi->active_extensions[wsi->count_act_ext] = ext;
|
2014-04-10 14:08:10 +08:00
|
|
|
|
|
|
|
/* allow him to construct his context */
|
|
|
|
|
2016-04-01 08:43:13 +08:00
|
|
|
if (ext->callback(lws_get_context(wsi), ext, wsi,
|
2017-03-07 10:07:37 +08:00
|
|
|
LWS_EXT_CB_CONSTRUCT,
|
|
|
|
(void *)&wsi->act_ext_user[
|
|
|
|
wsi->count_act_ext],
|
2017-07-15 19:02:04 +08:00
|
|
|
(void *)&opts, 0)) {
|
2017-09-20 10:37:59 +08:00
|
|
|
lwsl_info("ext %s failed construction\n",
|
2017-03-07 10:07:37 +08:00
|
|
|
ext_name);
|
2016-04-01 08:43:13 +08:00
|
|
|
ext_count--;
|
|
|
|
ext++;
|
2017-03-07 10:07:37 +08:00
|
|
|
|
2016-04-01 08:43:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext_count > 1)
|
|
|
|
*(*p)++ = ',';
|
|
|
|
else
|
2017-03-07 10:07:37 +08:00
|
|
|
LWS_CPYAPP(*p,
|
|
|
|
"\x0d\x0aSec-WebSocket-Extensions: ");
|
|
|
|
*p += lws_snprintf(*p, (end - *p), "%s", ext_name);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* go through the options trying to apply the
|
|
|
|
* recognized ones
|
|
|
|
*/
|
|
|
|
|
|
|
|
lwsl_debug("ext args %s", args);
|
|
|
|
|
|
|
|
while (args && *args && *args != ',') {
|
|
|
|
while (*args == ' ')
|
|
|
|
args++;
|
|
|
|
po = opts;
|
|
|
|
while (po->name) {
|
|
|
|
/* only support arg-less options... */
|
2017-11-06 10:05:04 +08:00
|
|
|
if (po->type != EXTARG_NONE ||
|
|
|
|
strncmp(args, po->name,
|
|
|
|
strlen(po->name))) {
|
|
|
|
po++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
oa.option_name = NULL;
|
|
|
|
oa.option_index = (int)(po - opts);
|
|
|
|
oa.start = NULL;
|
|
|
|
lwsl_debug("setting %s\n", po->name);
|
|
|
|
if (!ext->callback(
|
|
|
|
lws_get_context(wsi), ext, wsi,
|
|
|
|
LWS_EXT_CB_OPTION_SET,
|
|
|
|
wsi->act_ext_user[
|
|
|
|
wsi->count_act_ext],
|
|
|
|
&oa, (end - *p))) {
|
|
|
|
|
|
|
|
*p += lws_snprintf(*p, (end - *p),
|
|
|
|
"; %s", po->name);
|
|
|
|
lwsl_debug("adding option %s\n",
|
|
|
|
po->name);
|
2017-03-07 10:07:37 +08:00
|
|
|
}
|
|
|
|
po++;
|
|
|
|
}
|
|
|
|
while (*args && *args != ',' && *args != ';')
|
|
|
|
args++;
|
|
|
|
}
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
wsi->count_act_ext++;
|
2017-11-06 10:05:04 +08:00
|
|
|
lwsl_parser("cnt_act_ext <- %d\n", wsi->count_act_ext);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
|
|
|
ext++;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = 0;
|
2017-03-07 10:07:37 +08:00
|
|
|
args = NULL;
|
2014-04-10 14:08:10 +08:00
|
|
|
}
|
2014-12-04 23:59:35 +01:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-10 15:15:13 +08:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_process_ws_upgrade(struct lws *wsi)
|
|
|
|
{
|
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
|
|
|
char protocol_list[128], protocol_name[64], *p;
|
|
|
|
int protocol_len, hit, n = 0, non_space_char_found = 0;
|
|
|
|
|
|
|
|
if (!wsi->protocol)
|
|
|
|
lwsl_err("NULL protocol at lws_read\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's either websocket or h2->websocket
|
|
|
|
*
|
|
|
|
* Select the first protocol we support from the list
|
|
|
|
* the client sent us.
|
|
|
|
*
|
|
|
|
* Copy it to remove header fragmentation
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (lws_hdr_copy(wsi, protocol_list, sizeof(protocol_list) - 1,
|
|
|
|
WSI_TOKEN_PROTOCOL) < 0) {
|
|
|
|
lwsl_err("protocol list too long");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol_len = lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL);
|
|
|
|
protocol_list[protocol_len] = '\0';
|
|
|
|
p = protocol_list;
|
|
|
|
hit = 0;
|
|
|
|
|
|
|
|
while (*p && !hit) {
|
|
|
|
n = 0;
|
|
|
|
non_space_char_found = 0;
|
|
|
|
while (n < (int)sizeof(protocol_name) - 1 &&
|
|
|
|
*p && *p != ',') {
|
|
|
|
/* ignore leading spaces */
|
|
|
|
if (!non_space_char_found && *p == ' ') {
|
|
|
|
n++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
non_space_char_found = 1;
|
|
|
|
protocol_name[n++] = *p++;
|
|
|
|
}
|
|
|
|
protocol_name[n] = '\0';
|
|
|
|
if (*p)
|
|
|
|
p++;
|
|
|
|
|
|
|
|
lwsl_debug("checking %s\n", protocol_name);
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while (wsi->vhost->protocols[n].callback) {
|
|
|
|
lwsl_debug("try %s\n",
|
|
|
|
wsi->vhost->protocols[n].name);
|
|
|
|
|
|
|
|
if (wsi->vhost->protocols[n].name &&
|
|
|
|
!strcmp(wsi->vhost->protocols[n].name,
|
|
|
|
protocol_name)) {
|
|
|
|
wsi->protocol = &wsi->vhost->protocols[n];
|
|
|
|
hit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we didn't find a protocol he wanted? */
|
|
|
|
|
|
|
|
if (!hit) {
|
|
|
|
if (lws_hdr_simple_ptr(wsi, WSI_TOKEN_PROTOCOL)) {
|
|
|
|
lwsl_notice("No protocol from \"%s\" supported\n",
|
|
|
|
protocol_list);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* some clients only have one protocol and
|
|
|
|
* do not send the protocol list header...
|
|
|
|
* allow it and match to the vhost's default
|
|
|
|
* protocol (which itself defaults to zero)
|
|
|
|
*/
|
|
|
|
lwsl_info("defaulting to prot handler %d\n",
|
|
|
|
wsi->vhost->default_protocol_index);
|
|
|
|
n = wsi->vhost->default_protocol_index;
|
|
|
|
wsi->protocol = &wsi->vhost->protocols[
|
|
|
|
(int)wsi->vhost->default_protocol_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the ws struct for the wsi */
|
|
|
|
wsi->ws = lws_zalloc(sizeof(*wsi->ws), "ws struct");
|
|
|
|
if (!wsi->ws) {
|
|
|
|
lwsl_notice("OOM\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
|
|
|
|
wsi->ws->ietf_spec_revision =
|
|
|
|
atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
|
|
|
|
|
|
|
|
/* allocate wsi->user storage */
|
|
|
|
if (lws_ensure_user_space(wsi)) {
|
|
|
|
lwsl_notice("problem with user space\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give the user code a chance to study the request and
|
|
|
|
* have the opportunity to deny it
|
|
|
|
*/
|
|
|
|
if ((wsi->protocol->callback)(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");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform the handshake according to the protocol version the
|
|
|
|
* client announced
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (wsi->ws->ietf_spec_revision) {
|
|
|
|
default:
|
|
|
|
lwsl_notice("Unknown client spec version %d\n",
|
|
|
|
wsi->ws->ietf_spec_revision);
|
|
|
|
wsi->ws->ietf_spec_revision = 13;
|
|
|
|
//return 1;
|
|
|
|
/* fallthru */
|
|
|
|
case 13:
|
|
|
|
#if defined(LWS_WITH_HTTP2)
|
|
|
|
if (wsi->h2_stream_carries_ws) {
|
|
|
|
if (lws_h2_ws_handshake(wsi)) {
|
|
|
|
lwsl_notice("h2 ws handshake failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
lwsl_parser("lws_parse calling handshake_04\n");
|
|
|
|
if (handshake_0405(wsi->context, wsi)) {
|
|
|
|
lwsl_notice("hs0405 has failed the connection\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
lws_same_vh_protocol_insert(wsi, n);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are upgrading to ws, so http/1.1 + h2 and keepalive + pipelined
|
|
|
|
* header considerations about keeping the ah around no longer apply.
|
|
|
|
*
|
|
|
|
* However it's common for the first ws protocol data to have been
|
|
|
|
* coalesced with the browser upgrade request and to already be in the
|
|
|
|
* ah rx buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lwsl_debug("%s: %p: inheriting ws ah (rxpos:%d, rxlen:%d)\n",
|
|
|
|
__func__, wsi, wsi->ah->rxpos, wsi->ah->rxlen);
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
|
|
|
|
if (wsi->h2_stream_carries_ws)
|
|
|
|
lws_role_transition(wsi, LWSIFR_SERVER | LWSIFR_P_ENCAP_H2,
|
|
|
|
LRS_ESTABLISHED, &role_ops_ws);
|
|
|
|
else
|
|
|
|
lws_role_transition(wsi, LWSIFR_SERVER, LRS_ESTABLISHED,
|
|
|
|
&role_ops_ws);
|
|
|
|
/*
|
|
|
|
* Because rxpos/rxlen shows something in the ah, we will get
|
|
|
|
* service guaranteed next time around the event loop
|
|
|
|
*/
|
|
|
|
|
|
|
|
lws_pt_unlock(pt);
|
|
|
|
|
|
|
|
lws_server_init_wsi_for_ws(wsi);
|
|
|
|
lwsl_parser("accepted v%02d connection\n", wsi->ws->ietf_spec_revision);
|
|
|
|
|
|
|
|
/* !!! drop ah unreservedly after ESTABLISHED */
|
|
|
|
if (wsi->ah->rxpos == wsi->ah->rxlen) {
|
|
|
|
lwsl_info("%s: %p: dropping ah on ws upgrade\n", __func__, wsi);
|
|
|
|
lws_header_table_force_to_detachable_state(wsi);
|
|
|
|
lws_header_table_detach(wsi, 1);
|
|
|
|
} else
|
|
|
|
lwsl_info("%s: %p: unable to drop ah at ws upgrade %d vs %d\n",
|
|
|
|
__func__, wsi, wsi->ah->rxpos, wsi->ah->rxlen);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-18 11:43:21 +08:00
|
|
|
int
|
2015-12-04 11:08:32 +08:00
|
|
|
handshake_0405(struct lws_context *context, struct lws *wsi)
|
2013-01-18 11:43:21 +08:00
|
|
|
{
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
2017-09-21 21:51:04 +08:00
|
|
|
struct lws_process_html_args args;
|
2013-01-18 11:43:21 +08:00
|
|
|
unsigned char hash[20];
|
2016-01-29 21:18:54 +08:00
|
|
|
int n, accept_len;
|
2013-01-18 11:43:21 +08:00
|
|
|
char *response;
|
|
|
|
char *p;
|
|
|
|
|
replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.
It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure. It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).
The 3-level thing is all in one struct
- array indexed by the header enum, pointing to first "fragment" index
(ie, header type to fragment lookup, or 0 for none)
- array of fragments indexes, enough for 2 x the number of known headers
(fragment array... note that fragments can point to a "next"
fragment if the same header is spread across multiple entries)
- linear char array where the known header payload gets written
(fragments point into null-terminated strings stored in here,
only the known header content is stored)
http headers can legally be split over multiple headers of the same
name which should be concatenated. This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them. There are apis to get the total length and copy out a
linear, concatenated version to a buffer.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-10 18:02:31 +08:00
|
|
|
if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
|
2016-01-26 20:56:56 +08:00
|
|
|
!lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_info("handshake_04 missing pieces\n");
|
2013-01-18 11:43:21 +08:00
|
|
|
/* completed header processing, but missing some bits */
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2016-01-26 20:56:56 +08:00
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >= MAX_WEBSOCKET_04_KEY_LEN) {
|
2013-02-11 17:13:32 +08:00
|
|
|
lwsl_warn("Client key too long %d\n", MAX_WEBSOCKET_04_KEY_LEN);
|
2013-01-18 11:43:21 +08:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2013-02-12 10:07:22 +08:00
|
|
|
/*
|
|
|
|
* since key length is restricted above (currently 128), cannot
|
|
|
|
* overflow
|
|
|
|
*/
|
2016-01-19 03:34:24 +08:00
|
|
|
n = sprintf((char *)pt->serv_buf,
|
2016-01-26 20:56:56 +08:00
|
|
|
"%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
|
|
|
lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
|
2013-01-18 11:43:21 +08:00
|
|
|
|
2016-01-19 03:34:24 +08:00
|
|
|
lws_SHA1(pt->serv_buf, n, hash);
|
2013-01-18 11:43:21 +08:00
|
|
|
|
2017-03-07 10:07:37 +08:00
|
|
|
accept_len = lws_b64_encode_string((char *)hash, 20,
|
|
|
|
(char *)pt->serv_buf, context->pt_serv_buf_size);
|
2013-01-18 11:43:21 +08:00
|
|
|
if (accept_len < 0) {
|
|
|
|
lwsl_warn("Base64 encoded hash too long\n");
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the per-connection user memory (if any) */
|
2015-12-04 09:23:56 +08:00
|
|
|
if (lws_ensure_user_space(wsi))
|
2013-01-18 11:43:21 +08:00
|
|
|
goto bail;
|
|
|
|
|
|
|
|
/* create the response packet */
|
|
|
|
|
|
|
|
/* make a buffer big enough for everything */
|
|
|
|
|
2016-01-19 03:34:24 +08:00
|
|
|
response = (char *)pt->serv_buf + MAX_WEBSOCKET_04_KEY_LEN + LWS_PRE;
|
2013-01-18 11:43:21 +08:00
|
|
|
p = response;
|
|
|
|
LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
|
|
|
|
"Upgrade: WebSocket\x0d\x0a"
|
|
|
|
"Connection: Upgrade\x0d\x0a"
|
|
|
|
"Sec-WebSocket-Accept: ");
|
2016-01-19 03:34:24 +08:00
|
|
|
strcpy(p, (char *)pt->serv_buf);
|
2013-01-18 11:43:21 +08:00
|
|
|
p += accept_len;
|
|
|
|
|
2016-11-26 09:50:40 +08:00
|
|
|
/* we can only return the protocol header if:
|
|
|
|
* - one came in, and ... */
|
|
|
|
if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL) &&
|
|
|
|
/* - it is not an empty string */
|
|
|
|
wsi->protocol->name &&
|
|
|
|
wsi->protocol->name[0]) {
|
2013-01-18 11:43:21 +08:00
|
|
|
LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
|
2016-11-26 09:50:40 +08:00
|
|
|
p += lws_snprintf(p, 128, "%s", wsi->protocol->name);
|
2013-01-18 11:43:21 +08:00
|
|
|
}
|
|
|
|
|
2018-03-19 09:33:55 +08:00
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2013-01-18 11:43:21 +08:00
|
|
|
/*
|
|
|
|
* Figure out which extensions the client has that we want to
|
2017-03-07 10:07:37 +08:00
|
|
|
* enable on this connection, and give him back the list.
|
|
|
|
*
|
|
|
|
* Give him a limited write bugdet
|
2013-01-18 11:43:21 +08:00
|
|
|
*/
|
2017-03-07 10:07:37 +08:00
|
|
|
if (lws_extension_server_handshake(wsi, &p, 192))
|
2014-04-10 14:08:10 +08:00
|
|
|
goto bail;
|
2014-04-10 15:15:13 +08:00
|
|
|
#endif
|
2017-09-21 21:51:04 +08:00
|
|
|
LWS_CPYAPP(p, "\x0d\x0a");
|
2015-04-07 08:19:30 +08:00
|
|
|
|
2017-09-21 21:51:04 +08:00
|
|
|
args.p = p;
|
2017-10-25 08:00:23 +08:00
|
|
|
args.max_len = lws_ptr_diff((char *)pt->serv_buf +
|
|
|
|
context->pt_serv_buf_size, p);
|
2017-09-21 21:51:04 +08:00
|
|
|
if (user_callback_handle_rxflow(wsi->protocol->callback, wsi,
|
|
|
|
LWS_CALLBACK_ADD_HEADERS,
|
|
|
|
wsi->user_space, &args, 0))
|
|
|
|
goto bail;
|
|
|
|
|
|
|
|
p = args.p;
|
2015-04-07 08:19:30 +08:00
|
|
|
|
2013-01-18 11:43:21 +08:00
|
|
|
/* end of response packet */
|
|
|
|
|
2017-09-21 21:51:04 +08:00
|
|
|
LWS_CPYAPP(p, "\x0d\x0a");
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
if (!lws_any_extension_handled(wsi, LWS_EXT_CB_HANDSHAKE_REPLY_TX,
|
2015-12-15 21:15:58 +08:00
|
|
|
response, p - response)) {
|
2014-04-02 19:45:42 +08:00
|
|
|
|
2013-01-18 11:43:21 +08:00
|
|
|
/* okay send the handshake response accepting the connection */
|
|
|
|
|
2017-11-06 10:05:04 +08:00
|
|
|
lwsl_parser("issuing resp pkt %d len\n",
|
|
|
|
lws_ptr_diff(p, response));
|
2017-11-30 12:40:46 +08:00
|
|
|
#if defined(DEBUG)
|
2013-01-18 11:43:21 +08:00
|
|
|
fwrite(response, 1, p - response, stderr);
|
2014-04-10 14:08:10 +08:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
n = lws_write(wsi, (unsigned char *)response, p - response,
|
|
|
|
LWS_WRITE_HTTP_HEADERS);
|
2013-02-23 10:50:10 +08:00
|
|
|
if (n != (p - response)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_info("%s: ERROR writing to socket %d\n", __func__, n);
|
2013-01-18 11:43:21 +08:00
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* alright clean up and set ourselves into established state */
|
|
|
|
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsi_set_state(wsi, LRS_ESTABLISHED);
|
2013-01-18 11:43:21 +08:00
|
|
|
wsi->lws_rx_parse_state = LWS_RXPS_NEW;
|
|
|
|
|
2016-06-26 06:29:20 +08:00
|
|
|
{
|
|
|
|
const char * uri_ptr =
|
|
|
|
lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI);
|
|
|
|
int uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI);
|
|
|
|
const struct lws_http_mount *hit =
|
|
|
|
lws_find_mount(wsi, uri_ptr, uri_len);
|
|
|
|
if (hit && hit->cgienv &&
|
|
|
|
wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_PMO,
|
|
|
|
wsi->user_space, (void *)hit->cgienv, 0))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-18 11:43:21 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
bail:
|
2016-01-13 05:01:17 +08:00
|
|
|
/* caller will free up his parsing allocations */
|
2013-01-18 11:43:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
lws_interpret_incoming_packet(struct lws *wsi, unsigned char **buf, size_t len)
|
|
|
|
{
|
2018-04-13 16:01:38 +08:00
|
|
|
int m, draining_flow = 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-04-17 11:43:20 +08:00
|
|
|
if (lws_buflist_next_segment_len(&wsi->buflist_rxflow, NULL))
|
|
|
|
draining_flow = 1;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_parser("%s: received %d byte packet\n", __func__, (int)len);
|
|
|
|
|
|
|
|
/* let the rx protocol state machine have as much as it needs */
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
/*
|
|
|
|
* we were accepting input but now we stopped doing so
|
|
|
|
*/
|
|
|
|
if (wsi->rxflow_bitmap) {
|
|
|
|
lws_rxflow_cache(wsi, *buf, 0, (int)len);
|
|
|
|
lwsl_parser("%s: cached %ld\n", __func__, (long)len);
|
2018-04-13 16:01:38 +08:00
|
|
|
buf += len; /* stashing it is taking care of it */
|
2018-04-11 13:39:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wsi->ws->rx_draining_ext) {
|
|
|
|
m = lws_ws_rx_sm(wsi, 0);
|
|
|
|
if (m < 0)
|
|
|
|
return -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* consume payload bytes efficiently */
|
|
|
|
if (wsi->lws_rx_parse_state ==
|
|
|
|
LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED) {
|
|
|
|
m = lws_payload_until_length_exhausted(wsi, buf, &len);
|
2018-04-17 11:43:20 +08:00
|
|
|
if (draining_flow &&
|
|
|
|
!lws_buflist_use_segment(&wsi->buflist_rxflow, m))
|
|
|
|
lws_dll_lws_remove(&wsi->dll_rxflow);
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* process the byte */
|
|
|
|
m = lws_ws_rx_sm(wsi, *(*buf)++);
|
|
|
|
if (m < 0)
|
|
|
|
return -1;
|
|
|
|
len--;
|
|
|
|
|
2018-04-17 11:43:20 +08:00
|
|
|
/* account for what we're using in rxflow buffer */
|
|
|
|
if (draining_flow &&
|
|
|
|
!lws_buflist_use_segment(&wsi->buflist_rxflow, 1)) {
|
|
|
|
lws_dll_lws_remove(&wsi->dll_rxflow);
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_debug("%s: %p flow buf: drained\n", __func__, wsi);
|
2018-04-13 16:01:38 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
/* having drained the rxflow buffer, can rearm POLLIN */
|
|
|
|
#ifdef LWS_NO_SERVER
|
|
|
|
m =
|
|
|
|
#endif
|
|
|
|
__lws_rx_flow_control(wsi);
|
|
|
|
/* m ignored, needed for NO_SERVER case */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_parser("%s: exit with %d unused\n", __func__, (int)len);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|