2013-01-18 11:43:21 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
|
2013-01-18 11:43:21 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2013-01-18 11:43:21 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-01-18 11:43:21 +08:00
|
|
|
*
|
2019-08-14 10:44:14 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2013-01-18 11:43:21 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include <private-lib-core.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
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context *context = wsi->a.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
|
|
|
|
*/
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
if (lws_hdr_copy(wsi, (char *)pt->serv_buf, (int)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);
|
2018-04-25 06:53:30 +08:00
|
|
|
wsi->ws->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) {
|
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
if (c >= (char *)pt->serv_buf + 255)
|
|
|
|
return -1;
|
|
|
|
|
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;
|
2018-04-20 10:33:23 +08:00
|
|
|
if (!args)
|
|
|
|
args = c + 1;
|
2017-03-07 10:07:37 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-10 13:54:43 +08:00
|
|
|
while (args && *args == ' ')
|
2017-03-07 10:07:37 +08:00
|
|
|
args++;
|
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
/* check a client's extension against our support */
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
ext = wsi->a.vhost->ws.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
|
|
|
|
*/
|
2018-04-25 06:53:30 +08:00
|
|
|
for (m = 0; m < wsi->ws->count_act_ext; m++)
|
|
|
|
if (wsi->ws->active_extensions[m] == ext) {
|
2018-11-23 08:47:56 +08:00
|
|
|
lwsl_info("ext mentioned twice\n");
|
2017-03-07 10:07:37 +08:00
|
|
|
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
|
|
|
|
*/
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
m = (wsi->a.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 */
|
|
|
|
|
2018-04-25 06:53:30 +08:00
|
|
|
wsi->ws->active_extensions[wsi->ws->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,
|
2018-04-25 06:53:30 +08:00
|
|
|
(void *)&wsi->ws->act_ext_user[
|
|
|
|
wsi->ws->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: ");
|
2020-12-12 06:21:40 +00:00
|
|
|
*p += lws_snprintf(*p, lws_ptr_diff_size_t(end, *p), "%s", ext_name);
|
2017-03-07 10:07:37 +08:00
|
|
|
|
|
|
|
/*
|
2018-04-20 10:33:23 +08:00
|
|
|
* The client may send a bunch of different option
|
|
|
|
* sets for the same extension, we are supposed to
|
|
|
|
* pick one we like the look of. The option sets are
|
|
|
|
* separated by comma.
|
|
|
|
*
|
|
|
|
* Actually we just either accept the first one or
|
|
|
|
* nothing.
|
|
|
|
*
|
|
|
|
* Go through the options trying to apply the
|
2017-03-07 10:07:37 +08:00
|
|
|
* recognized ones
|
|
|
|
*/
|
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
lwsl_info("ext args %s\n", args);
|
2017-03-07 10:07:37 +08:00
|
|
|
|
|
|
|
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;
|
2018-04-20 10:33:23 +08:00
|
|
|
oa.len = 0;
|
|
|
|
lwsl_info("setting '%s'\n", po->name);
|
|
|
|
if (!ext->callback(lws_get_context(wsi),
|
2018-11-23 08:47:56 +08:00
|
|
|
ext, wsi,
|
|
|
|
LWS_EXT_CB_OPTION_SET,
|
|
|
|
wsi->ws->act_ext_user[
|
|
|
|
wsi->ws->count_act_ext],
|
2020-12-12 06:21:40 +00:00
|
|
|
&oa, lws_ptr_diff_size_t(end, *p))) {
|
2017-11-06 10:05:04 +08:00
|
|
|
|
2018-11-23 08:47:56 +08:00
|
|
|
*p += lws_snprintf(*p,
|
2020-12-12 06:21:40 +00:00
|
|
|
lws_ptr_diff_size_t(end, *p),
|
2018-11-23 08:47:56 +08:00
|
|
|
"; %s", po->name);
|
2017-11-06 10:05:04 +08:00
|
|
|
lwsl_debug("adding option %s\n",
|
2018-11-23 08:47:56 +08:00
|
|
|
po->name);
|
2017-03-07 10:07:37 +08:00
|
|
|
}
|
|
|
|
po++;
|
|
|
|
}
|
|
|
|
while (*args && *args != ',' && *args != ';')
|
|
|
|
args++;
|
2018-04-20 10:33:23 +08:00
|
|
|
|
|
|
|
if (*args == ';')
|
|
|
|
args++;
|
2017-03-07 10:07:37 +08:00
|
|
|
}
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2018-04-25 06:53:30 +08:00
|
|
|
wsi->ws->count_act_ext++;
|
2018-11-23 08:47:56 +08:00
|
|
|
lwsl_parser("cnt_act_ext <- %d\n",
|
|
|
|
wsi->ws->count_act_ext);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
if (args && *args == ',')
|
|
|
|
more = 0;
|
|
|
|
|
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
|
2019-03-19 11:53:57 +08:00
|
|
|
lws_process_ws_upgrade2(struct lws *wsi)
|
2018-04-11 13:39:42 +08:00
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
struct lws_context_per_thread *pt = &wsi->a.context->pt[(int)wsi->tsi];
|
2020-02-28 09:29:25 +00:00
|
|
|
#if defined(LWS_WITH_HTTP_BASIC_AUTH)
|
2018-11-28 12:16:01 +08:00
|
|
|
const struct lws_protocol_vhost_options *pvos = NULL;
|
|
|
|
const char *ws_prot_basic_auth = NULL;
|
2019-03-19 11:53:57 +08:00
|
|
|
|
2020-02-28 09:29:25 +00:00
|
|
|
|
2019-03-19 11:53:57 +08:00
|
|
|
/*
|
|
|
|
* Allow basic auth a look-in now we bound the wsi to the protocol.
|
|
|
|
*
|
|
|
|
* For vhost ws basic auth, it is "basic-auth": "path" as usual but
|
|
|
|
* applied to the protocol's entry in the vhost's "ws-protocols":
|
|
|
|
* section, as a pvo.
|
|
|
|
*/
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
pvos = lws_vhost_protocol_options(wsi->a.vhost, wsi->a.protocol->name);
|
2019-03-19 11:53:57 +08:00
|
|
|
if (pvos && pvos->options &&
|
|
|
|
!lws_pvo_get_str((void *)pvos->options, "basic-auth",
|
|
|
|
&ws_prot_basic_auth)) {
|
|
|
|
lwsl_info("%s: ws upgrade requires basic auth\n", __func__);
|
2019-12-12 18:45:00 +00:00
|
|
|
switch (lws_check_basic_auth(wsi, ws_prot_basic_auth, LWSAUTHM_DEFAULT
|
|
|
|
/* no callback based auth here */)) {
|
2019-03-19 11:53:57 +08:00
|
|
|
case LCBA_CONTINUE:
|
|
|
|
break;
|
|
|
|
case LCBA_FAILED_AUTH:
|
|
|
|
return lws_unauthorised_basic_auth(wsi);
|
|
|
|
case LCBA_END_TRANSACTION:
|
|
|
|
lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
|
|
|
|
return lws_http_transaction_completed(wsi);
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 09:29:25 +00:00
|
|
|
#endif
|
2019-03-19 11:53:57 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lws_pt_lock(pt, __func__);
|
|
|
|
|
2020-07-27 10:03:12 +01:00
|
|
|
/*
|
|
|
|
* Switch roles if we're upgrading away from http
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!wsi->h2_stream_carries_ws) {
|
2019-03-19 11:53:57 +08:00
|
|
|
lws_role_transition(wsi, LWSIFR_SERVER, LRS_ESTABLISHED,
|
|
|
|
&role_ops_ws);
|
|
|
|
|
2020-07-27 10:03:12 +01:00
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS) && defined(LWS_WITH_SERVER)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're a SS server object, we have to switch to ss-ws
|
|
|
|
* protocol handler too
|
|
|
|
*/
|
|
|
|
if (wsi->a.vhost->ss_handle) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s switching to ws protocol\n",
|
|
|
|
__func__, lws_ss_tag(wsi->a.vhost->ss_handle));
|
2020-07-27 10:03:12 +01:00
|
|
|
wsi->a.protocol = &protocol_secstream_ws;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* inform the SS user code that this has done a one-way
|
|
|
|
* upgrade to some other protocol... it will likely
|
|
|
|
* want to treat subsequent payloads differently
|
|
|
|
*/
|
|
|
|
|
2020-08-26 11:05:41 +01:00
|
|
|
(void)lws_ss_event_helper(wsi->a.vhost->ss_handle,
|
2020-07-27 10:03:12 +01:00
|
|
|
LWSSSCS_SERVER_UPGRADE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-19 11:53:57 +08:00
|
|
|
lws_pt_unlock(pt);
|
|
|
|
|
|
|
|
/* 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))
|
2020-12-12 06:21:40 +00:00
|
|
|
wsi->ws->ietf_spec_revision = (uint8_t)
|
2019-03-19 11:53:57 +08:00
|
|
|
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
|
|
|
|
*/
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if ((wsi->a.protocol->callback)(wsi,
|
2019-03-19 11:53:57 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
lws_role_transition(wsi,
|
|
|
|
LWSIFR_SERVER | LWSIFR_P_ENCAP_H2,
|
|
|
|
LRS_ESTABLISHED, &role_ops_ws);
|
2019-11-05 04:53:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be no validity checking since we
|
|
|
|
* are encapsulated in something else with its own
|
|
|
|
* validity checking
|
|
|
|
*/
|
|
|
|
|
2020-05-28 12:48:17 +01:00
|
|
|
lws_sul_cancel(&wsi->sul_validity);
|
2019-03-19 11:53:57 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
lwsl_parser("lws_parse calling handshake_04\n");
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (handshake_0405(wsi->a.context, wsi)) {
|
2019-03-19 11:53:57 +08:00
|
|
|
lwsl_notice("hs0405 has failed the connection\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-14 19:40:11 +01:00
|
|
|
if (lws_server_init_wsi_for_ws(wsi)) {
|
|
|
|
lwsl_notice("%s: user ESTABLISHED failed connection\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-03-19 11:53:57 +08:00
|
|
|
lwsl_parser("accepted v%02d connection\n", wsi->ws->ietf_spec_revision);
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_ACCESS_LOG)
|
|
|
|
{
|
2019-11-04 10:54:50 +00:00
|
|
|
char *uptr = "unknown method", combo[128], dotstar[64];
|
2019-09-11 14:43:09 +01:00
|
|
|
int l = 14, meth = lws_http_get_uri_and_method(wsi, &uptr, &l);
|
2019-03-19 11:53:57 +08:00
|
|
|
|
|
|
|
if (wsi->h2_stream_carries_ws)
|
|
|
|
wsi->http.request_version = HTTP_VERSION_2;
|
|
|
|
|
|
|
|
wsi->http.access_log.response = 101;
|
|
|
|
|
2019-11-04 10:54:50 +00:00
|
|
|
lws_strnncpy(dotstar, uptr, l, sizeof(dotstar));
|
|
|
|
l = lws_snprintf(combo, sizeof(combo), "%s (%s)", dotstar,
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
wsi->a.protocol->name);
|
2019-03-19 11:53:57 +08:00
|
|
|
|
2020-01-14 08:23:25 +00:00
|
|
|
if (meth < 0)
|
|
|
|
meth = 0;
|
2019-03-19 11:53:57 +08:00
|
|
|
lws_prepare_access_log_info(wsi, combo, l, meth);
|
|
|
|
lws_access_log(wsi);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: dropping ah on ws upgrade\n", __func__, lws_wsi_tag(wsi));
|
2019-03-19 11:53:57 +08:00
|
|
|
lws_header_table_detach(wsi, 1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lws_process_ws_upgrade(struct lws *wsi)
|
|
|
|
{
|
|
|
|
const struct lws_protocols *pcol = NULL;
|
2018-10-09 10:50:50 +08:00
|
|
|
char buf[128], name[64];
|
|
|
|
struct lws_tokenize ts;
|
|
|
|
lws_tokenize_elem e;
|
2020-01-11 14:04:50 +00:00
|
|
|
int n;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (!wsi->a.protocol)
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_err("NULL protocol at lws_read\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's either websocket or h2->websocket
|
|
|
|
*
|
2018-10-09 11:33:07 +08:00
|
|
|
* If we are on h1, confirm we got the required "connection: upgrade"
|
|
|
|
* header. h2 / ws-over-h2 does not have this.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_HTTP2)
|
2019-12-23 11:31:57 +00:00
|
|
|
if (!wsi->mux_substream) {
|
2018-10-09 11:33:07 +08:00
|
|
|
#endif
|
|
|
|
|
2019-02-20 07:47:48 +08:00
|
|
|
lws_tokenize_init(&ts, buf, LWS_TOKENIZE_F_COMMA_SEP_LIST |
|
|
|
|
LWS_TOKENIZE_F_DOT_NONTERM |
|
|
|
|
LWS_TOKENIZE_F_RFC7230_DELIMS |
|
|
|
|
LWS_TOKENIZE_F_MINUS_NONTERM);
|
2020-01-11 14:04:50 +00:00
|
|
|
n = lws_hdr_copy(wsi, buf, sizeof(buf) - 1, WSI_TOKEN_CONNECTION);
|
|
|
|
if (n <= 0)
|
2019-02-20 07:47:48 +08:00
|
|
|
goto bad_conn_format;
|
2020-12-12 06:21:40 +00:00
|
|
|
ts.len = (unsigned int)n;
|
2019-02-20 07:47:48 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
e = lws_tokenize(&ts);
|
|
|
|
switch (e) {
|
|
|
|
case LWS_TOKZE_TOKEN:
|
2019-07-31 05:08:14 +01:00
|
|
|
if (!strncasecmp(ts.token, "upgrade", ts.token_len))
|
2019-02-20 07:47:48 +08:00
|
|
|
e = LWS_TOKZE_ENDED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_TOKZE_DELIMITER:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* includes ENDED */
|
|
|
|
bad_conn_format:
|
|
|
|
lwsl_err("%s: malformed or absent conn hdr\n",
|
|
|
|
__func__);
|
2018-10-09 11:33:07 +08:00
|
|
|
|
2019-02-20 07:47:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} while (e > 0);
|
2018-10-09 11:33:07 +08:00
|
|
|
|
|
|
|
#if defined(LWS_WITH_HTTP2)
|
2019-02-20 07:47:48 +08:00
|
|
|
}
|
2018-10-09 11:33:07 +08:00
|
|
|
#endif
|
|
|
|
|
2019-03-19 11:53:57 +08:00
|
|
|
#if defined(LWS_WITH_HTTP_PROXY)
|
|
|
|
{
|
|
|
|
const struct lws_http_mount *hit;
|
|
|
|
int uri_len = 0, meth;
|
|
|
|
char *uri_ptr;
|
|
|
|
|
|
|
|
meth = lws_http_get_uri_and_method(wsi, &uri_ptr, &uri_len);
|
|
|
|
hit = lws_find_mount(wsi, uri_ptr, uri_len);
|
|
|
|
|
2020-01-04 15:28:42 +00:00
|
|
|
if (hit && (meth == LWSHUMETH_GET ||
|
|
|
|
meth == LWSHUMETH_CONNECT ||
|
|
|
|
meth == LWSHUMETH_COLON_PATH) &&
|
2019-03-19 11:53:57 +08:00
|
|
|
(hit->origin_protocol == LWSMPRO_HTTPS ||
|
|
|
|
hit->origin_protocol == LWSMPRO_HTTP))
|
|
|
|
/*
|
|
|
|
* We are an h1 ws upgrade on a urlpath that corresponds
|
|
|
|
* to a proxying mount. Don't try to deal with it
|
|
|
|
* locally, eg, we won't even have the right protocol
|
|
|
|
* handler since we're not the guy handling it, just a
|
|
|
|
* conduit.
|
|
|
|
*
|
|
|
|
* Instead open the related ongoing h1 connection
|
|
|
|
* according to the mount configuration and proxy
|
|
|
|
* whatever that has to say from now on.
|
|
|
|
*/
|
|
|
|
return lws_http_proxy_start(wsi, hit, uri_ptr, 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-09 11:33:07 +08:00
|
|
|
/*
|
2018-04-11 13:39:42 +08:00
|
|
|
* Select the first protocol we support from the list
|
|
|
|
* the client sent us.
|
|
|
|
*/
|
|
|
|
|
2018-10-09 10:50:50 +08:00
|
|
|
lws_tokenize_init(&ts, buf, LWS_TOKENIZE_F_COMMA_SEP_LIST |
|
2018-11-23 08:47:56 +08:00
|
|
|
LWS_TOKENIZE_F_MINUS_NONTERM |
|
2019-01-13 07:47:22 +08:00
|
|
|
LWS_TOKENIZE_F_DOT_NONTERM |
|
2018-11-23 08:47:56 +08:00
|
|
|
LWS_TOKENIZE_F_RFC7230_DELIMS);
|
2020-01-11 14:04:50 +00:00
|
|
|
n = lws_hdr_copy(wsi, buf, sizeof(buf) - 1, WSI_TOKEN_PROTOCOL);
|
|
|
|
if (n < 0) {
|
2018-10-09 10:50:50 +08:00
|
|
|
lwsl_err("%s: protocol list too long\n", __func__);
|
2018-04-11 13:39:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2020-12-12 06:21:40 +00:00
|
|
|
ts.len = (unsigned int)n;
|
2018-10-09 10:50:50 +08:00
|
|
|
if (!ts.len) {
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
int n = wsi->a.vhost->default_protocol_index;
|
2018-10-09 10:50:50 +08:00
|
|
|
/*
|
2019-02-22 14:27:21 +08:00
|
|
|
* Some clients only have one protocol and do not send the
|
2018-10-09 10:50:50 +08:00
|
|
|
* protocol list header... allow it and match to the vhost's
|
2019-02-22 14:27:21 +08:00
|
|
|
* default protocol (which itself defaults to zero).
|
|
|
|
*
|
|
|
|
* Setting the vhost default protocol index to -1 or anything
|
|
|
|
* more than the actual number of protocols on the vhost causes
|
|
|
|
* these "no protocol" ws connections to be rejected.
|
2018-10-09 10:50:50 +08:00
|
|
|
*/
|
2019-02-22 14:27:21 +08:00
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (n >= wsi->a.vhost->count_protocols) {
|
2019-02-22 14:27:21 +08:00
|
|
|
lwsl_notice("%s: rejecting ws upg with no protocol\n",
|
|
|
|
__func__);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:50:50 +08:00
|
|
|
lwsl_info("%s: defaulting to prot handler %d\n", __func__, n);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
lws_bind_protocol(wsi, &wsi->a.vhost->protocols[n],
|
2018-10-09 10:50:50 +08:00
|
|
|
"ws upgrade default pcol");
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-10-09 10:50:50 +08:00
|
|
|
goto alloc_ws;
|
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2020-07-27 10:03:12 +01:00
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS) && defined(LWS_WITH_SERVER)
|
|
|
|
if (wsi->a.vhost->ss_handle) {
|
|
|
|
lws_ss_handle_t *sssh = wsi->a.vhost->ss_handle;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At the moment, once we see it's a ss ws server, whatever
|
|
|
|
* he asked for we bind him to the ss-ws protocol handler.
|
|
|
|
*
|
|
|
|
* In the response subprotocol header, we need to name
|
|
|
|
*
|
|
|
|
* sssh->policy->u.http.u.ws.subprotocol
|
|
|
|
*
|
|
|
|
* though...
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sssh->policy->u.http.u.ws.subprotocol) {
|
|
|
|
pcol = lws_vhost_name_to_protocol(wsi->a.vhost,
|
|
|
|
"lws-secstream-ws");
|
|
|
|
if (pcol) {
|
|
|
|
lws_bind_protocol(wsi, pcol, "ss ws upg pcol");
|
|
|
|
|
|
|
|
goto alloc_ws;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-10-09 10:50:50 +08:00
|
|
|
/* otherwise go through the user-provided protocol list */
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-10-09 10:50:50 +08:00
|
|
|
do {
|
|
|
|
e = lws_tokenize(&ts);
|
|
|
|
switch (e) {
|
|
|
|
case LWS_TOKZE_TOKEN:
|
|
|
|
|
|
|
|
if (lws_tokenize_cstr(&ts, name, sizeof(name))) {
|
|
|
|
lwsl_err("%s: pcol name too long\n", __func__);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lwsl_debug("checking %s\n", name);
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
pcol = lws_vhost_name_to_protocol(wsi->a.vhost, name);
|
2018-10-09 10:50:50 +08:00
|
|
|
if (pcol) {
|
|
|
|
/* if we know it, bind to it and stop looking */
|
|
|
|
lws_bind_protocol(wsi, pcol, "ws upg pcol");
|
|
|
|
e = LWS_TOKZE_ENDED;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
2018-10-09 10:50:50 +08:00
|
|
|
break;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-10-09 10:50:50 +08:00
|
|
|
case LWS_TOKZE_DELIMITER:
|
|
|
|
case LWS_TOKZE_ENDED:
|
|
|
|
break;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-10-09 10:50:50 +08:00
|
|
|
default:
|
|
|
|
lwsl_err("%s: malformatted protocol list", __func__);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2018-10-09 10:50:50 +08:00
|
|
|
} while (e > 0);
|
|
|
|
|
|
|
|
/* we didn't find a protocol he wanted? */
|
|
|
|
|
|
|
|
if (!pcol) {
|
|
|
|
lwsl_notice("No supported protocol \"%s\"\n", buf);
|
|
|
|
|
|
|
|
return 1;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2018-10-09 10:50:50 +08:00
|
|
|
alloc_ws:
|
|
|
|
|
2019-03-19 11:53:57 +08:00
|
|
|
return lws_process_ws_upgrade2(wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-23 08:47: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
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
lws_SHA1(pt->serv_buf, (unsigned int)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,
|
2020-12-12 06:21:40 +00:00
|
|
|
(char *)pt->serv_buf, (int)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 */
|
|
|
|
|
2018-11-23 08:47:56 +08:00
|
|
|
response = (char *)pt->serv_buf + MAX_WEBSOCKET_04_KEY_LEN +
|
|
|
|
256 + 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 */
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
wsi->a.protocol->name &&
|
|
|
|
wsi->a.protocol->name[0]) {
|
|
|
|
const char *prot = wsi->a.protocol->name;
|
2019-03-19 11:53:57 +08:00
|
|
|
|
|
|
|
#if defined(LWS_WITH_HTTP_PROXY)
|
|
|
|
if (wsi->proxied_ws_parent && wsi->child_list)
|
|
|
|
prot = wsi->child_list->ws->actual_protocol;
|
|
|
|
#endif
|
|
|
|
|
2020-07-27 10:03:12 +01:00
|
|
|
#if defined(LWS_WITH_SECURE_STREAMS) && defined(LWS_WITH_SERVER)
|
|
|
|
{
|
|
|
|
lws_ss_handle_t *sssh = wsi->a.vhost->ss_handle;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At the moment, once we see it's a ss ws server, whatever
|
|
|
|
* he asked for we bind him to the ss-ws protocol handler.
|
|
|
|
*
|
|
|
|
* In the response subprotocol header, we need to name
|
|
|
|
*
|
|
|
|
* sssh->policy->u.http.u.ws.subprotocol
|
|
|
|
*
|
|
|
|
* though...
|
|
|
|
*/
|
|
|
|
|
2020-08-08 07:24:13 +01:00
|
|
|
if (sssh && sssh->policy &&
|
|
|
|
sssh->policy->u.http.u.ws.subprotocol)
|
2020-07-27 10:03:12 +01:00
|
|
|
prot = sssh->policy->u.http.u.ws.subprotocol;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-18 11:43:21 +08:00
|
|
|
LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
|
2019-03-19 11:53:57 +08:00
|
|
|
p += lws_snprintf(p, 128, "%s", prot);
|
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);
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (user_callback_handle_rxflow(wsi->a.protocol->callback, wsi,
|
2017-09-21 21:51:04 +08:00
|
|
|
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
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
/* okay send the handshake response accepting the connection */
|
2013-01-18 11:43:21 +08:00
|
|
|
|
2018-04-20 10:33:23 +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)
|
2018-04-20 10:33:23 +08:00
|
|
|
fwrite(response, 1, p - response, stderr);
|
2014-04-10 14:08:10 +08:00
|
|
|
#endif
|
2020-12-12 06:21:40 +00:00
|
|
|
n = lws_write(wsi, (unsigned char *)response, lws_ptr_diff_size_t(p, response),
|
2018-04-20 10:33:23 +08:00
|
|
|
LWS_WRITE_HTTP_HEADERS);
|
2020-12-12 06:21:40 +00:00
|
|
|
if (n != lws_ptr_diff(p, response)) {
|
2018-04-20 10:33:23 +08:00
|
|
|
lwsl_info("%s: ERROR writing to socket %d\n", __func__, n);
|
|
|
|
goto bail;
|
2013-01-18 11:43:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 &&
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
wsi->a.protocol->callback(wsi, LWS_CALLBACK_HTTP_PMO,
|
2016-06-26 06:29:20 +08:00
|
|
|
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
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Once we reach LWS_RXPS_WS_FRAME_PAYLOAD, we know how much
|
|
|
|
* to expect in that state and can deal with it in bulk more efficiently.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
lws_ws_frame_rest_is_payload(struct lws *wsi, uint8_t **buf, size_t len)
|
|
|
|
{
|
2019-05-04 13:19:12 +01:00
|
|
|
struct lws_ext_pm_deflate_rx_ebufs pmdrx;
|
2018-11-23 08:47:56 +08:00
|
|
|
unsigned int avail = (unsigned int)len;
|
2018-04-20 10:33:23 +08:00
|
|
|
uint8_t *buffer = *buf, mask[4];
|
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2020-12-12 06:21:40 +00:00
|
|
|
unsigned int old_packet_length = (unsigned int)wsi->ws->rx_packet_length;
|
2018-04-20 10:33:23 +08:00
|
|
|
#endif
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* With zlib, we can give it as much input as we like. The pmd
|
|
|
|
* extension will draw it down in chunks (default 1024).
|
|
|
|
*
|
|
|
|
* If we try to restrict how much we give it, because we must go
|
|
|
|
* back to the event loop each time, we will drop the remainder...
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2018-04-25 06:53:30 +08:00
|
|
|
if (!wsi->ws->count_act_ext)
|
2018-04-20 10:33:23 +08:00
|
|
|
#endif
|
|
|
|
{
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (wsi->a.protocol->rx_buffer_size)
|
2020-12-12 06:21:40 +00:00
|
|
|
avail = (unsigned int)wsi->a.protocol->rx_buffer_size;
|
2018-04-20 10:33:23 +08:00
|
|
|
else
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
avail = wsi->a.context->pt_serv_buf_size;
|
2018-04-20 10:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* do not consume more than we should */
|
|
|
|
if (avail > wsi->ws->rx_packet_length)
|
|
|
|
avail = (unsigned int)wsi->ws->rx_packet_length;
|
|
|
|
|
|
|
|
/* do not consume more than what is in the buffer */
|
|
|
|
if (avail > len)
|
|
|
|
avail = (unsigned int)len;
|
|
|
|
|
2018-10-10 13:54:43 +08:00
|
|
|
if (!avail)
|
2018-04-20 10:33:23 +08:00
|
|
|
return 0;
|
|
|
|
|
2019-05-30 08:21:33 +08:00
|
|
|
pmdrx.eb_in.token = buffer;
|
2020-12-12 06:21:40 +00:00
|
|
|
pmdrx.eb_in.len = (int)avail;
|
2019-05-30 08:21:33 +08:00
|
|
|
pmdrx.eb_out.token = buffer;
|
2020-12-12 06:21:40 +00:00
|
|
|
pmdrx.eb_out.len = (int)avail;
|
2018-04-20 10:33:23 +08:00
|
|
|
|
|
|
|
if (!wsi->ws->all_zero_nonce) {
|
|
|
|
|
|
|
|
for (n = 0; n < 4; n++)
|
|
|
|
mask[n] = wsi->ws->mask[(wsi->ws->mask_idx + n) & 3];
|
|
|
|
|
|
|
|
/* deal with 4-byte chunks using unwrapped loop */
|
2020-12-12 06:21:40 +00:00
|
|
|
n = (int)(avail >> 2);
|
2018-04-20 10:33:23 +08:00
|
|
|
while (n--) {
|
|
|
|
*(buffer) = *(buffer) ^ mask[0];
|
|
|
|
buffer++;
|
|
|
|
*(buffer) = *(buffer) ^ mask[1];
|
|
|
|
buffer++;
|
|
|
|
*(buffer) = *(buffer) ^ mask[2];
|
|
|
|
buffer++;
|
|
|
|
*(buffer) = *(buffer) ^ mask[3];
|
|
|
|
buffer++;
|
|
|
|
}
|
|
|
|
/* and the remaining bytes bytewise */
|
|
|
|
for (n = 0; n < (int)(avail & 3); n++) {
|
|
|
|
*(buffer) = *(buffer) ^ mask[n];
|
|
|
|
buffer++;
|
|
|
|
}
|
|
|
|
|
|
|
|
wsi->ws->mask_idx = (wsi->ws->mask_idx + avail) & 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_info("%s: using %d of raw input (total %d on offer)\n", __func__,
|
|
|
|
avail, (int)len);
|
|
|
|
|
|
|
|
(*buf) += avail;
|
|
|
|
len -= avail;
|
2019-05-04 13:19:12 +01:00
|
|
|
wsi->ws->rx_packet_length -= avail;
|
2018-04-20 10:33:23 +08:00
|
|
|
|
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2019-05-04 13:19:12 +01:00
|
|
|
n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &pmdrx, 0);
|
|
|
|
lwsl_info("%s: ext says %d / ebuf_out.len %d\n", __func__, n,
|
|
|
|
pmdrx.eb_out.len);
|
2019-07-30 19:12:17 +01:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
/*
|
|
|
|
* ebuf may be pointing somewhere completely different now,
|
|
|
|
* it's the output
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
/*
|
|
|
|
* we may rely on this to get RX, just drop connection
|
|
|
|
*/
|
|
|
|
lwsl_notice("%s: LWS_EXT_CB_PAYLOAD_RX blew out\n", __func__);
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
2019-05-04 13:19:12 +01:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we had an rx fragment right at the last compressed byte of the
|
|
|
|
* message, we can get a zero length inflated output, where no prior
|
|
|
|
* rx inflated output marked themselves with FIN, since there was
|
|
|
|
* raw ws payload still to drain at that time.
|
|
|
|
*
|
|
|
|
* Then we need to generate a zero length ws rx that can be understood
|
|
|
|
* as the message completion.
|
|
|
|
*/
|
|
|
|
|
2019-05-04 13:19:12 +01:00
|
|
|
if (!pmdrx.eb_out.len && /* zero-length inflation output */
|
|
|
|
n == PMDR_EMPTY_FINAL && /* nothing to drain from the inflator */
|
2018-04-20 10:33:23 +08:00
|
|
|
old_packet_length && /* we gave the inflator new input */
|
|
|
|
!wsi->ws->rx_packet_length && /* raw ws packet payload all gone */
|
|
|
|
wsi->ws->final && /* the raw ws packet is a FIN guy */
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
wsi->a.protocol->callback &&
|
2018-04-20 10:33:23 +08:00
|
|
|
!wsi->wsistate_pre_close) {
|
|
|
|
|
2019-05-04 13:19:12 +01:00
|
|
|
lwsl_ext("%s: issuing zero length FIN pkt\n", __func__);
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (user_callback_handle_rxflow(wsi->a.protocol->callback, wsi,
|
2018-04-20 10:33:23 +08:00
|
|
|
LWS_CALLBACK_RECEIVE,
|
|
|
|
wsi->user_space, NULL, 0))
|
|
|
|
return -1;
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
return (int)avail;
|
2018-04-20 10:33:23 +08:00
|
|
|
}
|
|
|
|
|
2019-07-30 19:12:17 +01:00
|
|
|
/*
|
|
|
|
* If doing permessage-deflate, above was the only way to get a zero
|
|
|
|
* length receive. Otherwise we're more willing.
|
|
|
|
*/
|
|
|
|
if (wsi->ws->count_act_ext && !pmdrx.eb_out.len)
|
2020-12-12 06:21:40 +00:00
|
|
|
return (int)avail;
|
2018-04-20 10:33:23 +08:00
|
|
|
|
2019-05-04 13:19:12 +01:00
|
|
|
if (n == PMDR_HAS_PENDING)
|
2018-04-20 10:33:23 +08:00
|
|
|
/* extension had more... main loop will come back */
|
|
|
|
lws_add_wsi_to_draining_ext_list(wsi);
|
|
|
|
else
|
|
|
|
lws_remove_wsi_from_draining_ext_list(wsi);
|
2019-05-04 13:19:12 +01:00
|
|
|
#endif
|
2018-04-20 10:33:23 +08:00
|
|
|
|
2019-07-30 19:12:17 +01:00
|
|
|
if (pmdrx.eb_out.len &&
|
|
|
|
wsi->ws->check_utf8 && !wsi->ws->defeat_check_utf8) {
|
2018-04-20 10:33:23 +08:00
|
|
|
if (lws_check_utf8(&wsi->ws->utf8,
|
2019-05-30 08:21:33 +08:00
|
|
|
pmdrx.eb_out.token,
|
2020-12-12 06:21:40 +00:00
|
|
|
(unsigned int)pmdrx.eb_out.len)) {
|
2018-04-20 10:33:23 +08:00
|
|
|
lws_close_reason(wsi, LWS_CLOSE_STATUS_INVALID_PAYLOAD,
|
|
|
|
(uint8_t *)"bad utf8", 8);
|
|
|
|
goto utf8_fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we are ending partway through utf-8 character? */
|
|
|
|
if (!wsi->ws->rx_packet_length && wsi->ws->final &&
|
|
|
|
wsi->ws->utf8 && !n) {
|
|
|
|
lwsl_info("FINAL utf8 error\n");
|
|
|
|
lws_close_reason(wsi, LWS_CLOSE_STATUS_INVALID_PAYLOAD,
|
|
|
|
(uint8_t *)"partial utf8", 12);
|
|
|
|
|
|
|
|
utf8_fail:
|
|
|
|
lwsl_info("utf8 error\n");
|
2020-12-12 06:21:40 +00:00
|
|
|
lwsl_hexdump_info(pmdrx.eb_out.token, (size_t)pmdrx.eb_out.len);
|
2018-04-20 10:33:23 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
fakewsi: replace with smaller substructure
Currently we always reserve a fakewsi per pt so events that don't have a related actual
wsi, like vhost-protocol-init or vhost cert init via protocol callback can make callbacks
that look reasonable to user protocol handler code expecting a valid wsi every time.
This patch splits out stuff that user callbacks often unconditionally expect to be in
a wsi, like context pointer, vhost pointer etc into a substructure, which is composed
into struct lws at the top of it. Internal references (struct lws is opaque, so there
are only internal references) are all updated to go via the substructre, the compiler
should make that a NOP.
Helpers are added when fakewsi is used and referenced.
If not PLAT_FREERTOS, we continue to provide a full fakewsi in the pt as before,
although the helpers improve consistency by zeroing down the substructure. There is
a huge amount of user code out there over the last 10 years that did not always have
the minimal examples to follow, some of it does some unexpected things.
If it is PLAT_FREERTOS, that is a newer thing in lws and users have the benefit of
being able to follow the minimal examples' approach. For PLAT_FREERTOS we don't
reserve the fakewsi in the pt any more, saving around 800 bytes. The helpers then
create a struct lws_a (the substructure) on the stack, zero it down (but it is only
like 4 pointers) and prepare it with whatever we know like the context.
Then we cast it to a struct lws * and use it in the user protocol handler call.
In this case, the remainder of the struct lws is undefined. However the amount of
old protocol handlers that might touch things outside of the substructure in
PLAT_FREERTOS is very limited compared to legacy lws user code and the saving is
significant on constrained devices.
User handlers should not be touching everything in a wsi every time anyway, there
are several cases where there is no valid wsi to do the call with. Dereference of
things outside the substructure should only happen when the callback reason shows
there is a valid wsi bound to the activity (as in all the minimal examples).
2020-07-19 08:33:46 +01:00
|
|
|
if (wsi->a.protocol->callback && !wsi->wsistate_pre_close)
|
|
|
|
if (user_callback_handle_rxflow(wsi->a.protocol->callback, wsi,
|
2018-04-20 10:33:23 +08:00
|
|
|
LWS_CALLBACK_RECEIVE,
|
|
|
|
wsi->user_space,
|
2019-05-04 13:19:12 +01:00
|
|
|
pmdrx.eb_out.token,
|
2020-12-12 06:21:40 +00:00
|
|
|
(unsigned int)pmdrx.eb_out.len))
|
2018-04-20 10:33:23 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
wsi->ws->first_fragment = 0;
|
|
|
|
|
2018-04-25 08:42:18 +08:00
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2018-04-20 10:33:23 +08:00
|
|
|
lwsl_info("%s: input used %d, output %d, rem len %d, rx_draining_ext %d\n",
|
2019-05-04 13:19:12 +01:00
|
|
|
__func__, avail, pmdrx.eb_out.len, (int)len,
|
|
|
|
wsi->ws->rx_draining_ext);
|
2018-04-25 08:42:18 +08:00
|
|
|
#endif
|
2018-04-20 10:33:23 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
return (int)avail; /* how much we used from the input */
|
2018-04-20 10:33:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
int
|
2018-04-20 10:33:23 +08:00
|
|
|
lws_parse_ws(struct lws *wsi, unsigned char **buf, size_t len)
|
2018-04-11 13:39:42 +08:00
|
|
|
{
|
2019-05-04 13:19:12 +01:00
|
|
|
unsigned char *bufin = *buf;
|
2018-04-20 10:33:23 +08:00
|
|
|
int m, bulk = 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
lwsl_debug("%s: received %d byte packet\n", __func__, (int)len);
|
2018-04-17 11:43:20 +08:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
//lwsl_hexdump_notice(*buf, len);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
/* 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) {
|
2019-04-19 07:13:40 +01:00
|
|
|
lwsl_info("%s: doing rxflow, caching %d\n", __func__,
|
2019-05-04 13:19:12 +01:00
|
|
|
(int)len);
|
|
|
|
/*
|
|
|
|
* Since we cached the remaining available input, we
|
|
|
|
* can say we "consumed" it.
|
|
|
|
*
|
|
|
|
* But what about the case where the available input
|
|
|
|
* came out of the rxflow cache already? If we are
|
|
|
|
* effectively "putting it back in the cache", we have
|
|
|
|
* leave it where it is, already pointed to by the head.
|
|
|
|
*/
|
2020-12-12 06:21:40 +00:00
|
|
|
if (lws_rxflow_cache(wsi, *buf, 0, len) ==
|
2019-05-04 13:19:12 +01:00
|
|
|
LWSRXFC_TRIMMED) {
|
|
|
|
/*
|
|
|
|
* We dealt with it by trimming the existing
|
|
|
|
* rxflow cache HEAD to account for what we used.
|
|
|
|
*
|
|
|
|
* so he doesn't do any consumed processing
|
|
|
|
*/
|
|
|
|
lwsl_info("%s: trimming inside rxflow cache\n",
|
|
|
|
__func__);
|
|
|
|
*buf = bufin;
|
|
|
|
} else
|
|
|
|
*buf += len;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2018-04-25 08:42:18 +08:00
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->ws->rx_draining_ext) {
|
2018-04-20 10:33:23 +08:00
|
|
|
lwsl_debug("%s: draining rx ext\n", __func__);
|
|
|
|
m = lws_ws_rx_sm(wsi, ALREADY_PROCESSED_IGNORE_CHAR, 0);
|
2018-04-11 13:39:42 +08:00
|
|
|
if (m < 0)
|
|
|
|
return -1;
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-25 08:42:18 +08:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
/* consume payload bytes efficiently */
|
2018-04-20 10:33:23 +08:00
|
|
|
while (wsi->lws_rx_parse_state == LWS_RXPS_WS_FRAME_PAYLOAD &&
|
|
|
|
(wsi->ws->opcode == LWSWSOPC_TEXT_FRAME ||
|
|
|
|
wsi->ws->opcode == LWSWSOPC_BINARY_FRAME ||
|
|
|
|
wsi->ws->opcode == LWSWSOPC_CONTINUATION) &&
|
|
|
|
len) {
|
|
|
|
uint8_t *bin = *buf;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
bulk = 1;
|
|
|
|
m = lws_ws_frame_rest_is_payload(wsi, buf, len);
|
|
|
|
assert((int)lws_ptr_diff(*buf, bin) <= (int)len);
|
2020-12-12 06:21:40 +00:00
|
|
|
len -= lws_ptr_diff_size_t(*buf, bin);
|
2018-04-20 10:33:23 +08:00
|
|
|
|
|
|
|
if (!m) {
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (m < 0) {
|
|
|
|
lwsl_info("%s: rest_is_payload bailed\n",
|
|
|
|
__func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bulk) {
|
|
|
|
/* process the byte */
|
|
|
|
m = lws_ws_rx_sm(wsi, 0, *(*buf)++);
|
|
|
|
len--;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We already handled this byte in bulk, just deal
|
|
|
|
* with the ramifications
|
|
|
|
*/
|
2018-04-25 08:42:18 +08:00
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2018-04-20 10:33:23 +08:00
|
|
|
lwsl_debug("%s: coming out of bulk with len %d, "
|
|
|
|
"wsi->ws->rx_draining_ext %d\n",
|
|
|
|
__func__, (int)len,
|
|
|
|
wsi->ws->rx_draining_ext);
|
2018-04-25 08:42:18 +08:00
|
|
|
#endif
|
2018-04-20 10:33:23 +08:00
|
|
|
m = lws_ws_rx_sm(wsi, ALREADY_PROCESSED_IGNORE_CHAR |
|
2023-11-22 23:08:07 +08:00
|
|
|
ALREADY_PROCESSED_NO_CB, 0);
|
2018-04-20 10:33:23 +08:00
|
|
|
}
|
2018-04-17 11:43:20 +08:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
if (m < 0) {
|
|
|
|
lwsl_info("%s: lws_ws_rx_sm bailed %d\n", __func__,
|
|
|
|
bulk);
|
2018-04-13 16:01:38 +08:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
return -1;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
2018-04-20 10:33:23 +08:00
|
|
|
|
|
|
|
bulk = 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
lwsl_debug("%s: exit with %d unused\n", __func__, (int)len);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|