2018-04-11 13:39:42 +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>
|
2018-04-11 13:39:42 +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:
|
2018-04-11 13:39:42 +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.
|
2018-04-11 13:39:42 +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.
|
2018-04-11 13:39:42 +08:00
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include <private-lib-core.h>
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These are the standardized defaults.
|
|
|
|
* Override what actually goes in the vhost settings in platform or user code.
|
|
|
|
* Leave these alone because they are used to determine "what is different
|
|
|
|
* from the protocol defaults".
|
|
|
|
*/
|
|
|
|
const struct http2_settings lws_h2_defaults = { {
|
|
|
|
1,
|
|
|
|
/* H2SET_HEADER_TABLE_SIZE */ 4096,
|
|
|
|
/* *** This controls how many entries in the dynamic table ***
|
|
|
|
* Allows the sender to inform the remote endpoint of the maximum
|
|
|
|
* size of the header compression table used to decode header
|
|
|
|
* blocks, in octets. The encoder can select any size equal to or
|
|
|
|
* less than this value by using signaling specific to the header
|
|
|
|
* compression format inside a header block (see [COMPRESSION]).
|
|
|
|
* The initial value is 4,096 octets.
|
|
|
|
*/
|
|
|
|
/* H2SET_ENABLE_PUSH */ 1,
|
|
|
|
/* H2SET_MAX_CONCURRENT_STREAMS */ 0x7fffffff,
|
|
|
|
/* H2SET_INITIAL_WINDOW_SIZE */ 65535,
|
|
|
|
/* H2SET_MAX_FRAME_SIZE */ 16384,
|
|
|
|
/* H2SET_MAX_HEADER_LIST_SIZE */ 0x7fffffff,
|
|
|
|
/*< This advisory setting informs a peer of the maximum size of
|
|
|
|
* header list that the sender is prepared to accept, in octets.
|
|
|
|
* The value is based on the uncompressed size of header fields,
|
|
|
|
* including the length of the name and value in octets plus an
|
|
|
|
* overhead of 32 octets for each header field.
|
|
|
|
*/
|
|
|
|
/* H2SET_RESERVED7 */ 0,
|
|
|
|
/* H2SET_ENABLE_CONNECT_PROTOCOL */ 0,
|
|
|
|
}};
|
|
|
|
|
|
|
|
/* these are the "lws defaults"... they can be overridden in plat */
|
|
|
|
|
|
|
|
const struct http2_settings lws_h2_stock_settings = { {
|
|
|
|
1,
|
|
|
|
/* H2SET_HEADER_TABLE_SIZE */ 65536, /* ffox */
|
|
|
|
/* *** This controls how many entries in the dynamic table ***
|
|
|
|
* Allows the sender to inform the remote endpoint of the maximum
|
|
|
|
* size of the header compression table used to decode header
|
|
|
|
* blocks, in octets. The encoder can select any size equal to or
|
|
|
|
* less than this value by using signaling specific to the header
|
|
|
|
* compression format inside a header block (see [COMPRESSION]).
|
|
|
|
* The initial value is 4,096 octets.
|
|
|
|
*
|
|
|
|
* Can't pass h2spec with less than 4096 here...
|
|
|
|
*/
|
2019-10-02 08:55:59 -07:00
|
|
|
/* H2SET_ENABLE_PUSH */ 0,
|
2018-04-11 13:39:42 +08:00
|
|
|
/* H2SET_MAX_CONCURRENT_STREAMS */ 24,
|
2019-12-26 02:35:41 +00:00
|
|
|
/* H2SET_INITIAL_WINDOW_SIZE */ 0,
|
|
|
|
/*< This is managed by explicit WINDOW_UPDATE. Because otherwise no
|
|
|
|
* way to precisely control it when we do want to.
|
|
|
|
*/
|
2018-04-11 13:39:42 +08:00
|
|
|
/* H2SET_MAX_FRAME_SIZE */ 16384,
|
|
|
|
/* H2SET_MAX_HEADER_LIST_SIZE */ 4096,
|
|
|
|
/*< This advisory setting informs a peer of the maximum size of
|
|
|
|
* header list that the sender is prepared to accept, in octets.
|
|
|
|
* The value is based on the uncompressed size of header fields,
|
|
|
|
* including the length of the name and value in octets plus an
|
|
|
|
* overhead of 32 octets for each header field.
|
|
|
|
*/
|
|
|
|
/* H2SET_RESERVED7 */ 0,
|
|
|
|
/* H2SET_ENABLE_CONNECT_PROTOCOL */ 1,
|
|
|
|
}};
|
|
|
|
|
2018-04-17 15:35:15 +08:00
|
|
|
/*
|
2021-01-18 14:20:37 +00:00
|
|
|
* The wsi at this level is normally the network wsi... we can get called on
|
|
|
|
* another path via lws_service_do_ripe_rxflow() on mux children too tho...
|
2018-04-17 15:35:15 +08:00
|
|
|
*/
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
static int
|
|
|
|
rops_handle_POLLIN_h2(struct lws_context_per_thread *pt, struct lws *wsi,
|
|
|
|
struct lws_pollfd *pollfd)
|
|
|
|
{
|
2018-04-17 15:35:15 +08:00
|
|
|
struct lws_tokens ebuf;
|
2018-04-11 13:39:42 +08:00
|
|
|
unsigned int pending = 0;
|
2018-04-17 15:35:15 +08:00
|
|
|
char buffered = 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
struct lws *wsi1;
|
2018-04-17 11:43:20 +08:00
|
|
|
int n, m;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
#ifdef LWS_WITH_CGI
|
2018-04-27 19:16:50 +08:00
|
|
|
if (wsi->http.cgi && (pollfd->revents & LWS_POLLOUT)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
if (lws_handle_POLLOUT_event(wsi, pollfd))
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-17 10:07:04 +01:00
|
|
|
lwsl_info("%s: %s wsistate 0x%x, events %d, revents %d, pollout %d\n", __func__,
|
|
|
|
wsi->lc.gutag, (unsigned int)wsi->wsistate,
|
|
|
|
pollfd->events, pollfd->revents,
|
|
|
|
pollfd->revents & LWS_POLLOUT);
|
|
|
|
|
|
|
|
/* !!! */
|
|
|
|
if (wsi->wsistate == 0x10000013) {
|
|
|
|
wsi->bugcatcher++;
|
|
|
|
if (wsi->bugcatcher == 250) {
|
|
|
|
lwsl_err("%s: BUGCATCHER\n", __func__);
|
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
wsi->bugcatcher = 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* something went wrong with parsing the handshake, and
|
|
|
|
* we ended up back in the event loop without completing it
|
|
|
|
*/
|
|
|
|
if (lwsi_state(wsi) == LRS_PRE_WS_SERVING_ACCEPT) {
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lwsi_state(wsi) == LRS_WAITING_CONNECT) {
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-04-11 13:39:42 +08:00
|
|
|
if ((pollfd->revents & LWS_POLLOUT) &&
|
|
|
|
lws_handle_POLLOUT_event(wsi, pollfd)) {
|
|
|
|
lwsl_debug("POLLOUT event closed it\n");
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2020-09-19 13:27:33 +01:00
|
|
|
n = lws_http_client_socket_service(wsi, pollfd);
|
2018-04-11 13:39:42 +08:00
|
|
|
if (n)
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_WSI_ALREADY_DIED;
|
2018-04-11 13:39:42 +08:00
|
|
|
#endif
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1: something requested a callback when it was OK to write */
|
|
|
|
|
|
|
|
if ((pollfd->revents & LWS_POLLOUT) &&
|
|
|
|
lwsi_state_can_handle_POLLOUT(wsi) &&
|
|
|
|
lws_handle_POLLOUT_event(wsi, pollfd)) {
|
|
|
|
if (lwsi_state(wsi) == LRS_RETURNED_CLOSE)
|
|
|
|
lwsi_set_state(wsi, LRS_FLUSHING_BEFORE_CLOSE);
|
|
|
|
/* the write failed... it's had it */
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lwsi_state(wsi) == LRS_RETURNED_CLOSE ||
|
|
|
|
lwsi_state(wsi) == LRS_WAITING_TO_SEND_CLOSE ||
|
|
|
|
lwsi_state(wsi) == LRS_AWAITING_CLOSE_ACK) {
|
|
|
|
/*
|
|
|
|
* we stopped caring about anything except control
|
|
|
|
* packets. Force flow control off, defeat tx
|
|
|
|
* draining.
|
|
|
|
*/
|
|
|
|
lws_rx_flow_control(wsi, 1);
|
2018-04-25 08:42:18 +08:00
|
|
|
#if defined(LWS_ROLE_WS) && !defined(LWS_WITHOUT_EXTENSIONS)
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->ws)
|
|
|
|
wsi->ws->tx_draining_ext = 0;
|
2018-04-25 08:42:18 +08:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->mux_substream || wsi->upgraded_to_http2) {
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi1 = lws_get_network_wsi(wsi);
|
2021-06-17 10:07:04 +01:00
|
|
|
if (wsi1 && lws_has_buffered_out(wsi1)) {
|
|
|
|
|
|
|
|
lwsl_info("%s: has buffered out\n", __func__);
|
2018-04-17 15:35:15 +08:00
|
|
|
/*
|
|
|
|
* We cannot deal with any kind of new RX
|
2018-04-11 13:39:42 +08:00
|
|
|
* because we are dealing with a partial send
|
|
|
|
* (new RX may trigger new http_action() that
|
|
|
|
* expect to be able to send)
|
|
|
|
*/
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
2021-06-17 10:07:04 +01:00
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2018-04-17 15:35:15 +08:00
|
|
|
read:
|
|
|
|
/* 3: network wsi buflist needs to be drained */
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-09-12 05:41:19 +01:00
|
|
|
// lws_buflist_describe(&wsi->buflist, wsi, __func__);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-04-17 15:35:15 +08:00
|
|
|
ebuf.len = (int)lws_buflist_next_segment_len(&wsi->buflist,
|
2019-05-30 08:21:33 +08:00
|
|
|
&ebuf.token);
|
2018-04-17 15:35:15 +08:00
|
|
|
if (ebuf.len) {
|
|
|
|
lwsl_info("draining buflist (len %d)\n", ebuf.len);
|
|
|
|
buffered = 1;
|
|
|
|
goto drain;
|
2021-01-18 14:20:37 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (wsi->mux_substream) {
|
|
|
|
lwsl_warn("%s: uh... %s mux child with nothing to drain\n", __func__, lws_wsi_tag(wsi));
|
|
|
|
// assert(0);
|
|
|
|
lws_dll2_remove(&wsi->dll_buflist);
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2018-04-17 15:35:15 +08:00
|
|
|
if (!lws_ssl_pending(wsi) &&
|
|
|
|
!(pollfd->revents & pollfd->events & LWS_POLLIN))
|
2018-04-11 13:39:42 +08:00
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
|
2021-06-17 10:07:04 +01:00
|
|
|
/* We have something to read... */
|
|
|
|
|
2018-04-17 15:35:15 +08:00
|
|
|
if (!(lwsi_role_client(wsi) &&
|
|
|
|
(lwsi_state(wsi) != LRS_ESTABLISHED &&
|
2021-06-17 10:07:04 +01:00
|
|
|
// lwsi_state(wsi) != LRS_H1C_ISSUE_HANDSHAKE2 &&
|
2018-04-17 15:35:15 +08:00
|
|
|
lwsi_state(wsi) != LRS_H2_WAITING_TO_SEND_HEADERS))) {
|
|
|
|
|
2025-01-16 10:59:48 +00:00
|
|
|
int scr_ret;
|
|
|
|
|
2019-05-30 08:21:33 +08:00
|
|
|
ebuf.token = pt->serv_buf;
|
2025-01-16 10:59:48 +00:00
|
|
|
scr_ret = lws_ssl_capable_read(wsi,
|
2019-05-30 08:21:33 +08:00
|
|
|
ebuf.token,
|
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.context->pt_serv_buf_size);
|
2025-01-16 10:59:48 +00:00
|
|
|
switch (scr_ret) {
|
2018-04-17 15:35:15 +08:00
|
|
|
case 0:
|
2018-04-20 10:33:23 +08:00
|
|
|
lwsl_info("%s: zero length read\n", __func__);
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
|
|
|
lwsl_info("SSL Capable more service\n");
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
2018-04-20 10:33:23 +08:00
|
|
|
lwsl_info("%s: LWS_SSL_CAPABLE_ERROR\n", __func__);
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
2018-04-17 15:35:15 +08:00
|
|
|
|
2025-01-16 10:59:48 +00:00
|
|
|
/*
|
|
|
|
* coverity is confused: it knows lws_ssl_capable_read may
|
|
|
|
* return < 0 and assigning that to ebuf.len is bad, but it
|
|
|
|
* doesn't understand this check below on scr_ret < 0
|
|
|
|
* removes that possibility
|
|
|
|
*/
|
|
|
|
|
|
|
|
ebuf.len = scr_ret;
|
|
|
|
if (ebuf.len < 0) /* ie, not usable data */ {
|
|
|
|
lwsl_info("%s: other error\n", __func__);
|
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
|
|
|
}
|
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
// lwsl_notice("%s: Actual RX %d\n", __func__, ebuf.len);
|
|
|
|
// if (ebuf.len > 0)
|
|
|
|
// lwsl_hexdump_notice(ebuf.token, ebuf.len);
|
2021-06-17 10:07:04 +01:00
|
|
|
} else
|
|
|
|
lwsl_info("%s: skipped read\n", __func__);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-04-20 10:33:23 +08:00
|
|
|
if (ebuf.len < 0)
|
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
drain:
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-04-11 13:39:42 +08:00
|
|
|
if (lwsi_role_http(wsi) && lwsi_role_client(wsi) &&
|
|
|
|
wsi->hdr_parsing_completed && !wsi->told_user_closed) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In SSL mode we get POLLIN notification about
|
|
|
|
* encrypted data in.
|
|
|
|
*
|
|
|
|
* But that is not necessarily related to decrypted
|
|
|
|
* data out becoming available; in may need to perform
|
|
|
|
* other in or out before that happens.
|
|
|
|
*
|
|
|
|
* simply mark ourselves as having readable data
|
|
|
|
* and turn off our POLLIN
|
|
|
|
*/
|
|
|
|
wsi->client_rx_avail = 1;
|
2019-07-13 12:06:33 -07:00
|
|
|
if (lws_change_pollfd(wsi, LWS_POLLIN, 0))
|
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
/* let user code know, he'll usually ask for writeable
|
|
|
|
* callback and drain / re-enable it there
|
|
|
|
*/
|
|
|
|
if (user_callback_handle_rxflow(
|
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-11 13:39:42 +08:00
|
|
|
wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
|
|
|
|
wsi->user_space, NULL, 0)) {
|
|
|
|
lwsl_info("RECEIVE_CLIENT_HTTP closed it\n");
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* service incoming data */
|
|
|
|
|
2018-04-17 15:35:15 +08:00
|
|
|
if (ebuf.len) {
|
|
|
|
n = 0;
|
2019-07-05 06:07:03 +01:00
|
|
|
if (lwsi_role_h2(wsi) && lwsi_state(wsi) != LRS_BODY &&
|
|
|
|
lwsi_state(wsi) != LRS_DISCARD_BODY)
|
2020-12-12 06:21:40 +00:00
|
|
|
n = lws_read_h2(wsi, ebuf.token, (unsigned int)ebuf.len);
|
2018-04-25 08:42:18 +08:00
|
|
|
else
|
2020-12-12 06:21:40 +00:00
|
|
|
n = lws_read_h1(wsi, ebuf.token, (unsigned int)ebuf.len);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
if (n < 0) {
|
|
|
|
/* we closed wsi */
|
2018-04-17 15:35:15 +08:00
|
|
|
return LWS_HPI_RET_WSI_ALREADY_DIED;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
2018-04-17 11:43:20 +08:00
|
|
|
|
2019-07-01 05:53:08 +01:00
|
|
|
if (n && buffered) {
|
2019-08-27 06:06:13 +01:00
|
|
|
// lwsl_notice("%s: h2 use %d\n", __func__, n);
|
2020-01-11 14:04:50 +00:00
|
|
|
m = (int)lws_buflist_use_segment(&wsi->buflist, (size_t)n);
|
2018-04-17 11:43:20 +08:00
|
|
|
lwsl_info("%s: draining rxflow: used %d, next %d\n",
|
2018-04-17 15:35:15 +08:00
|
|
|
__func__, n, m);
|
2018-04-17 11:43:20 +08:00
|
|
|
if (!m) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_notice("%s: removed %s from dll_buflist\n",
|
|
|
|
__func__, lws_wsi_tag(wsi));
|
2019-04-21 06:24:05 +01:00
|
|
|
lws_dll2_remove(&wsi->dll_buflist);
|
2018-04-17 11:43:20 +08:00
|
|
|
}
|
2018-04-17 15:35:15 +08:00
|
|
|
} else
|
2025-01-16 10:59:48 +00:00
|
|
|
/* cov: both n and ebuf.len are int */
|
|
|
|
if (n > 0 && n < ebuf.len && ebuf.len > 0) {
|
2019-08-27 06:06:13 +01:00
|
|
|
// lwsl_notice("%s: h2 append seg %d\n", __func__, ebuf.len - n);
|
2018-04-30 19:17:32 +08:00
|
|
|
m = lws_buflist_append_segment(&wsi->buflist,
|
2019-05-30 08:21:33 +08:00
|
|
|
ebuf.token + n,
|
2020-12-12 06:21:40 +00:00
|
|
|
(unsigned int)(ebuf.len - n));
|
2018-04-30 19:17:32 +08:00
|
|
|
if (m < 0)
|
|
|
|
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
|
|
|
if (m) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_debug("%s: added %s to rxflow list\n",
|
|
|
|
__func__, lws_wsi_tag(wsi));
|
2019-09-11 14:43:09 +01:00
|
|
|
if (lws_dll2_is_detached(&wsi->dll_buflist))
|
|
|
|
lws_dll2_add_head(&wsi->dll_buflist,
|
2019-04-21 06:24:05 +01:00
|
|
|
&pt->dll_buflist_owner);
|
2018-04-30 19:17:32 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2019-09-12 05:41:19 +01:00
|
|
|
// lws_buflist_describe(&wsi->buflist, wsi, __func__);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-05-12 08:01:50 +01:00
|
|
|
#if 0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This seems to be too aggressive... we don't want the ah stuck
|
|
|
|
* there but eg, WINDOW_UPDATE may come and detach it if we leave
|
|
|
|
* it like that... it will get detached at stream close
|
|
|
|
*/
|
|
|
|
|
2018-04-27 15:20:56 +08:00
|
|
|
if (wsi->http.ah
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-04-11 13:39:42 +08:00
|
|
|
&& !wsi->client_h2_alpn
|
|
|
|
#endif
|
2019-05-12 08:01:50 +01:00
|
|
|
) {
|
|
|
|
lwsl_err("xxx\n");
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
lws_header_table_detach(wsi, 0);
|
2019-05-12 08:01:50 +01:00
|
|
|
}
|
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
pending = (unsigned int)lws_ssl_pending(wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
if (pending) {
|
2018-04-26 15:27:02 +08:00
|
|
|
// lwsl_info("going around\n");
|
2018-04-11 13:39:42 +08:00
|
|
|
goto read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LWS_HPI_RET_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rops_handle_POLLOUT_h2(struct lws *wsi)
|
|
|
|
{
|
|
|
|
// lwsl_notice("%s\n", __func__);
|
|
|
|
|
|
|
|
if (lwsi_state(wsi) == LRS_ISSUE_HTTP_BODY)
|
|
|
|
return LWS_HP_RET_USER_SERVICE;
|
|
|
|
|
|
|
|
/*
|
2019-12-26 02:35:41 +00:00
|
|
|
* Priority 1: H2 protocol packets
|
2018-04-11 13:39:42 +08:00
|
|
|
*/
|
|
|
|
if ((wsi->upgraded_to_http2
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-04-11 13:39:42 +08:00
|
|
|
|| wsi->client_h2_alpn
|
|
|
|
#endif
|
|
|
|
) && wsi->h2.h2n->pps) {
|
|
|
|
lwsl_info("servicing pps\n");
|
2018-04-17 11:43:20 +08:00
|
|
|
/*
|
|
|
|
* this is called on the network connection, but may close
|
|
|
|
* substreams... that may affect callers
|
|
|
|
*/
|
2018-04-11 13:39:42 +08:00
|
|
|
if (lws_h2_do_pps_send(wsi)) {
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
|
|
|
return LWS_HP_RET_BAIL_DIE;
|
|
|
|
}
|
|
|
|
if (wsi->h2.h2n->pps)
|
|
|
|
return LWS_HP_RET_BAIL_OK;
|
|
|
|
|
|
|
|
/* we can resume whatever we were doing */
|
|
|
|
lws_rx_flow_control(wsi, LWS_RXFLOW_REASON_APPLIES_ENABLE |
|
|
|
|
LWS_RXFLOW_REASON_H2_PPS_PENDING);
|
|
|
|
|
|
|
|
return LWS_HP_RET_BAIL_OK; /* leave POLLOUT active */
|
|
|
|
}
|
|
|
|
|
2019-12-26 02:35:41 +00:00
|
|
|
/* Priority 2: if we are closing, not allowed to send more data frags
|
2018-04-11 13:39:42 +08:00
|
|
|
* which means user callback or tx ext flush banned now
|
|
|
|
*/
|
|
|
|
if (lwsi_state(wsi) == LRS_RETURNED_CLOSE)
|
|
|
|
return LWS_HP_RET_USER_SERVICE;
|
|
|
|
|
|
|
|
return LWS_HP_RET_USER_SERVICE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rops_write_role_protocol_h2(struct lws *wsi, unsigned char *buf, size_t len,
|
|
|
|
enum lws_write_protocol *wp)
|
|
|
|
{
|
2018-04-25 08:42:18 +08:00
|
|
|
unsigned char flags = 0, base = (*wp) & 0x1f;
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
size_t olen = len;
|
2018-04-11 13:39:42 +08:00
|
|
|
int n;
|
2018-08-23 11:48:17 +08:00
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
2018-09-02 14:35:37 +08:00
|
|
|
unsigned char mtubuf[4096 + LWS_PRE];
|
2018-08-23 11:48:17 +08:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
/* if not in a state to send stuff, then just send nothing */
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
if (!lwsi_role_ws(wsi) && !wsi->mux_stream_immortal &&
|
2018-04-25 08:42:18 +08:00
|
|
|
base != LWS_WRITE_HTTP &&
|
|
|
|
base != LWS_WRITE_HTTP_FINAL &&
|
|
|
|
base != LWS_WRITE_HTTP_HEADERS_CONTINUATION &&
|
2020-01-02 08:32:23 +00:00
|
|
|
base != LWS_WRITE_HTTP_HEADERS && lwsi_state(wsi) != LRS_BODY &&
|
2018-04-11 13:39:42 +08:00
|
|
|
((lwsi_state(wsi) != LRS_RETURNED_CLOSE &&
|
|
|
|
lwsi_state(wsi) != LRS_WAITING_TO_SEND_CLOSE &&
|
2019-09-13 10:33:24 +01:00
|
|
|
lwsi_state(wsi) != LRS_ESTABLISHED &&
|
2018-04-25 08:42:18 +08:00
|
|
|
lwsi_state(wsi) != LRS_AWAITING_CLOSE_ACK)
|
|
|
|
#if defined(LWS_ROLE_WS)
|
|
|
|
|| base != LWS_WRITE_CLOSE
|
|
|
|
#endif
|
|
|
|
)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
//assert(0);
|
2019-10-17 11:08:47 +01:00
|
|
|
lwsl_notice("%s: binning wsistate 0x%x %d: %s\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
|
|
|
(unsigned int)wsi->wsistate, *wp, wsi->a.protocol ?
|
|
|
|
wsi->a.protocol->name : "no protocol");
|
2019-10-17 11:08:47 +01:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
/* compression transform... */
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
|
|
|
if (wsi->http.lcs) {
|
2018-08-23 11:48:17 +08:00
|
|
|
unsigned char *out = mtubuf + LWS_PRE;
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
size_t o = sizeof(mtubuf) - LWS_PRE;
|
|
|
|
|
|
|
|
n = lws_http_compression_transform(wsi, buf, len, wp, &out, &o);
|
|
|
|
if (n)
|
|
|
|
return n;
|
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: transformed %d bytes to %d "
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
"(wp 0x%x, more %d)\n", __func__,
|
2020-12-25 05:54:19 +00:00
|
|
|
lws_wsi_tag(wsi), (int)len, (int)o, (int)*wp,
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
wsi->http.comp_ctx.may_have_more);
|
|
|
|
|
|
|
|
buf = out;
|
|
|
|
len = o;
|
|
|
|
base = (*wp) & 0x1f;
|
|
|
|
|
|
|
|
if (!len)
|
2020-12-12 06:21:40 +00:00
|
|
|
return (int)olen;
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
/*
|
|
|
|
* ws-over-h2 also ends up here after the ws framing applied
|
|
|
|
*/
|
|
|
|
|
|
|
|
n = LWS_H2_FRAME_TYPE_DATA;
|
2018-04-25 08:42:18 +08:00
|
|
|
if (base == LWS_WRITE_HTTP_HEADERS) {
|
2018-04-11 13:39:42 +08:00
|
|
|
n = LWS_H2_FRAME_TYPE_HEADERS;
|
|
|
|
if (!((*wp) & LWS_WRITE_NO_FIN))
|
|
|
|
flags = LWS_H2_FLAG_END_HEADERS;
|
|
|
|
if (wsi->h2.send_END_STREAM ||
|
|
|
|
((*wp) & LWS_WRITE_H2_STREAM_END)) {
|
|
|
|
flags |= LWS_H2_FLAG_END_STREAM;
|
|
|
|
wsi->h2.send_END_STREAM = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 08:42:18 +08:00
|
|
|
if (base == LWS_WRITE_HTTP_HEADERS_CONTINUATION) {
|
2018-04-11 13:39:42 +08:00
|
|
|
n = LWS_H2_FRAME_TYPE_CONTINUATION;
|
|
|
|
if (!((*wp) & LWS_WRITE_NO_FIN))
|
|
|
|
flags = LWS_H2_FLAG_END_HEADERS;
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
if (wsi->h2.send_END_STREAM ||
|
|
|
|
((*wp) & LWS_WRITE_H2_STREAM_END)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
flags |= LWS_H2_FLAG_END_STREAM;
|
|
|
|
wsi->h2.send_END_STREAM = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 08:42:18 +08:00
|
|
|
if ((base == LWS_WRITE_HTTP ||
|
|
|
|
base == LWS_WRITE_HTTP_FINAL) &&
|
|
|
|
wsi->http.tx_content_length) {
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi->http.tx_content_remain -= len;
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: tx_content_rem = %llu\n", __func__,
|
|
|
|
lws_wsi_tag(wsi),
|
2018-04-11 13:39:42 +08:00
|
|
|
(unsigned long long)wsi->http.tx_content_remain);
|
|
|
|
if (!wsi->http.tx_content_remain) {
|
2018-04-25 08:42:18 +08:00
|
|
|
lwsl_info("%s: selecting final write mode\n", __func__);
|
|
|
|
base = *wp = LWS_WRITE_HTTP_FINAL;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 08:42:18 +08:00
|
|
|
if (base == LWS_WRITE_HTTP_FINAL || ((*wp) & LWS_WRITE_H2_STREAM_END)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
flags |= LWS_H2_FLAG_END_STREAM;
|
2021-06-17 10:07:04 +01:00
|
|
|
lwsl_info("%s: %s: setting END_STREAM, 0x%x\n", __func__,
|
|
|
|
lws_wsi_tag(wsi), flags);
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi->h2.send_END_STREAM = 1;
|
|
|
|
}
|
|
|
|
|
2020-12-12 06:21:40 +00:00
|
|
|
n = lws_h2_frame_write(wsi, n, flags, wsi->mux.my_sid, (unsigned int)len, buf);
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
if (n < 0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
/* hide it may have been compressed... */
|
|
|
|
|
2018-08-23 11:29:45 +08:00
|
|
|
return (int)olen;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2020-04-16 06:53:10 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2018-04-11 13:39:42 +08:00
|
|
|
static int
|
|
|
|
rops_check_upgrades_h2(struct lws *wsi)
|
|
|
|
{
|
|
|
|
#if defined(LWS_ROLE_WS)
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* with H2 there's also a way to upgrade a stream to something
|
|
|
|
* else... :method is CONNECT and :protocol says the name of
|
|
|
|
* the new protocol we want to carry. We have to have sent a
|
|
|
|
* SETTINGS saying that we support it though.
|
|
|
|
*/
|
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_METHOD);
|
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.vhost->h2.set.s[H2SET_ENABLE_CONNECT_PROTOCOL] ||
|
2019-12-23 11:31:57 +00:00
|
|
|
!wsi->mux_substream || !p || strcmp(p, "CONNECT"))
|
2018-04-11 13:39:42 +08:00
|
|
|
return LWS_UPG_RET_CONTINUE;
|
|
|
|
|
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_COLON_PROTOCOL);
|
|
|
|
if (!p || strcmp(p, "websocket"))
|
|
|
|
return LWS_UPG_RET_CONTINUE;
|
|
|
|
|
|
|
|
lwsl_info("Upgrade h2 to ws\n");
|
2019-12-23 11:31:57 +00:00
|
|
|
lws_mux_mark_immortal(wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi->h2_stream_carries_ws = 1;
|
2019-09-13 10:33:24 +01:00
|
|
|
|
2021-01-06 15:08:22 +00:00
|
|
|
lws_metrics_tag_wsi_add(wsi, "upg", "ws_over_h2");
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (lws_process_ws_upgrade(wsi))
|
|
|
|
return LWS_UPG_RET_BAIL;
|
|
|
|
|
|
|
|
lwsl_info("Upgraded h2 to ws OK\n");
|
|
|
|
|
|
|
|
return LWS_UPG_RET_DONE;
|
|
|
|
#else
|
|
|
|
return LWS_UPG_RET_CONTINUE;
|
|
|
|
#endif
|
|
|
|
}
|
2020-04-16 06:53:10 +01:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
static int
|
|
|
|
rops_init_vhost_h2(struct lws_vhost *vh,
|
2018-04-27 08:27:16 +08:00
|
|
|
const struct lws_context_creation_info *info)
|
2018-04-11 13:39:42 +08:00
|
|
|
{
|
2018-04-25 08:42:18 +08:00
|
|
|
vh->h2.set = vh->context->set;
|
2018-10-10 13:54:43 +08:00
|
|
|
if (info->http2_settings[0]) {
|
|
|
|
int n;
|
|
|
|
|
2018-04-25 08:42:18 +08:00
|
|
|
for (n = 1; n < LWS_H2_SETTINGS_LEN; n++)
|
|
|
|
vh->h2.set.s[n] = info->http2_settings[n];
|
2018-10-10 13:54:43 +08:00
|
|
|
}
|
2018-04-25 08:42:18 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-18 13:09:32 +01:00
|
|
|
int
|
|
|
|
rops_pt_init_destroy_h2(struct lws_context *context,
|
|
|
|
const struct lws_context_creation_info *info,
|
|
|
|
struct lws_context_per_thread *pt, int destroy)
|
2018-04-11 13:39:42 +08:00
|
|
|
{
|
2021-08-17 10:36:37 +01:00
|
|
|
/* if not already set by plat, use lws default SETTINGS */
|
|
|
|
if (!context->set.s[0])
|
|
|
|
context->set = lws_h2_stock_settings;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-08-09 10:12:09 +01:00
|
|
|
/*
|
|
|
|
* We only want to do this once... we will do it if we are built
|
|
|
|
* otherwise h1 ops will do it (or nobody if no http at all)
|
|
|
|
*/
|
2019-09-18 13:09:32 +01:00
|
|
|
#if !defined(LWS_ROLE_H2) && defined(LWS_WITH_SERVER)
|
|
|
|
if (!destroy) {
|
2019-08-09 10:12:09 +01:00
|
|
|
|
|
|
|
pt->sul_ah_lifecheck.cb = lws_sul_http_ah_lifecheck;
|
|
|
|
|
2020-08-22 18:52:13 +01:00
|
|
|
__lws_sul_insert_us(&pt->pt_sul_owner[LWSSULLI_MISS_IF_SUSPENDED],
|
|
|
|
&pt->sul_ah_lifecheck, 30 * LWS_US_PER_SEC);
|
2019-09-18 13:09:32 +01:00
|
|
|
} else
|
|
|
|
lws_dll2_remove(&pt->sul_ah_lifecheck.list);
|
2019-08-18 05:04:15 +01:00
|
|
|
#endif
|
2019-08-09 10:12:09 +01:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-18 13:09:32 +01:00
|
|
|
|
2019-12-26 02:35:41 +00:00
|
|
|
static int
|
|
|
|
rops_tx_credit_h2(struct lws *wsi, char peer_to_us, int add)
|
2018-04-11 13:39:42 +08:00
|
|
|
{
|
2019-12-26 02:35:41 +00:00
|
|
|
struct lws *nwsi = lws_get_network_wsi(wsi);
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (add) {
|
|
|
|
if (peer_to_us == LWSTXCR_PEER_TO_US) {
|
|
|
|
/*
|
|
|
|
* We want to tell the peer they can write an additional
|
|
|
|
* "add" bytes to us
|
|
|
|
*/
|
2020-12-12 06:21:40 +00:00
|
|
|
return lws_h2_update_peer_txcredit(wsi, (unsigned int)-1, add);
|
2019-12-26 02:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're being told we can write an additional "add" bytes
|
|
|
|
* to the peer
|
|
|
|
*/
|
|
|
|
|
|
|
|
wsi->txc.tx_cr += add;
|
|
|
|
nwsi->txc.tx_cr += add;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer_to_us == LWSTXCR_US_TO_PEER)
|
|
|
|
return lws_h2_tx_cr_get(wsi);
|
|
|
|
|
|
|
|
n = wsi->txc.peer_tx_cr_est;
|
|
|
|
if (n > nwsi->txc.peer_tx_cr_est)
|
|
|
|
n = nwsi->txc.peer_tx_cr_est;
|
|
|
|
|
|
|
|
return n;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rops_destroy_role_h2(struct lws *wsi)
|
|
|
|
{
|
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];
|
2018-05-02 08:46:16 +08:00
|
|
|
struct allocated_headers *ah;
|
|
|
|
|
|
|
|
/* we may not have an ah, but may be on the waiting list... */
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: ah det due to close\n", __func__, lws_wsi_tag(wsi));
|
2018-05-02 08:46:16 +08:00
|
|
|
__lws_header_table_detach(wsi, 0);
|
|
|
|
|
|
|
|
ah = pt->http.ah_list;
|
|
|
|
|
|
|
|
while (ah) {
|
|
|
|
if (ah->in_use && ah->wsi == wsi) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_err("%s: ah leak: %s\n", __func__, lws_wsi_tag(wsi));
|
2018-05-02 08:46:16 +08:00
|
|
|
ah->in_use = 0;
|
|
|
|
ah->wsi = NULL;
|
|
|
|
pt->http.ah_count_in_use--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ah = ah->next;
|
|
|
|
}
|
|
|
|
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
|
|
|
lws_http_compression_destroy(wsi);
|
|
|
|
#endif
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->upgraded_to_http2 || wsi->mux_substream) {
|
2018-04-11 13:39:42 +08:00
|
|
|
lws_hpack_destroy_dynamic_header(wsi);
|
|
|
|
|
|
|
|
if (wsi->h2.h2n)
|
|
|
|
lws_free_set_NULL(wsi->h2.h2n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rops_close_kill_connection_h2(struct lws *wsi, enum lws_close_status reason)
|
|
|
|
{
|
|
|
|
|
2018-09-04 08:06:46 +08:00
|
|
|
#if defined(LWS_WITH_HTTP_PROXY)
|
|
|
|
if (wsi->http.proxy_clientside) {
|
|
|
|
|
|
|
|
wsi->http.proxy_clientside = 0;
|
|
|
|
|
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,
|
2020-01-30 13:19:11 +00:00
|
|
|
wsi,
|
2018-11-23 08:47:56 +08:00
|
|
|
LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
|
2020-01-30 13:19:11 +00:00
|
|
|
wsi->user_space, NULL, 0))
|
2018-09-04 08:06:46 +08:00
|
|
|
wsi->http.proxy_clientside = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->mux_substream && wsi->h2_stream_carries_ws)
|
2018-04-11 13:39:42 +08:00
|
|
|
lws_h2_rst_stream(wsi, 0, "none");
|
2019-08-26 18:41:40 +01:00
|
|
|
/* else
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->mux_substream)
|
2019-08-26 18:41:40 +01:00
|
|
|
lws_h2_rst_stream(wsi, H2_ERR_STREAM_CLOSED, "swsi got closed");
|
|
|
|
*/
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info(" %s, his parent %s: siblings:\n", lws_wsi_tag(wsi), lws_wsi_tag(wsi->mux.parent_wsi));
|
2019-12-23 11:31:57 +00:00
|
|
|
lws_wsi_mux_dump_children(wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->upgraded_to_http2 || wsi->mux_substream
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-12-23 11:31:57 +00:00
|
|
|
|| wsi->client_mux_substream
|
2018-11-12 15:24:42 +08:00
|
|
|
#endif
|
|
|
|
) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("closing %s: parent %s\n", lws_wsi_tag(wsi),
|
|
|
|
lws_wsi_tag(wsi->mux.parent_wsi));
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->mux.child_list && lwsl_visible(LLL_INFO)) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info(" parent %s: closing children: list:\n", lws_wsi_tag(wsi));
|
2019-12-23 11:31:57 +00:00
|
|
|
lws_wsi_mux_dump_children(wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
2020-12-12 06:21:40 +00:00
|
|
|
lws_wsi_mux_close_children(wsi, (int)reason);
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wsi->upgraded_to_http2) {
|
|
|
|
/* remove pps */
|
|
|
|
struct lws_h2_protocol_send *w = wsi->h2.h2n->pps, *w1;
|
2018-04-17 11:43:20 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
while (w) {
|
|
|
|
w1 = w->next;
|
|
|
|
free(w);
|
|
|
|
w = w1;
|
|
|
|
}
|
|
|
|
wsi->h2.h2n->pps = NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-12 15:24:42 +08:00
|
|
|
if ((
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2019-12-23 11:31:57 +00:00
|
|
|
wsi->client_mux_substream ||
|
2018-11-12 15:24:42 +08:00
|
|
|
#endif
|
2019-12-23 11:31:57 +00:00
|
|
|
wsi->mux_substream) &&
|
|
|
|
wsi->mux.parent_wsi) {
|
|
|
|
lws_wsi_mux_sibling_disconnect(wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->h2.pending_status_body)
|
|
|
|
lws_free_set_NULL(wsi->h2.pending_status_body);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rops_callback_on_writable_h2(struct lws *wsi)
|
|
|
|
{
|
2019-12-23 11:31:57 +00:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
|
|
|
struct lws *network_wsi;
|
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
int already;
|
|
|
|
|
|
|
|
// if (!lwsi_role_h2(wsi) && !lwsi_role_h2_ENCAPSULATION(wsi))
|
|
|
|
// return 0;
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->mux.requested_POLLOUT
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-04-11 13:39:42 +08:00
|
|
|
&& !wsi->client_h2_alpn
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
lwsl_debug("already pending writable\n");
|
2020-03-06 07:25:00 +00:00
|
|
|
// return 1;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* is this for DATA or for control messages? */
|
2019-12-26 02:35:41 +00:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->upgraded_to_http2 && !wsi->h2.h2n->pps &&
|
2020-01-30 13:19:11 +00:00
|
|
|
lws_wsi_txc_check_skint(&wsi->txc, lws_h2_tx_cr_get(wsi))) {
|
2018-04-11 13:39:42 +08:00
|
|
|
/*
|
2019-12-26 02:35:41 +00:00
|
|
|
* refuse his efforts to get WRITABLE if we have no credit and
|
|
|
|
* no non-DATA pps to send
|
2018-04-11 13:39:42 +08:00
|
|
|
*/
|
2020-01-30 13:19:11 +00:00
|
|
|
lwsl_err("%s: skint\n", __func__);
|
2018-04-11 13:39:42 +08:00
|
|
|
return 0;
|
2020-01-30 13:19:11 +00:00
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-04-11 13:39:42 +08:00
|
|
|
network_wsi = lws_get_network_wsi(wsi);
|
2019-12-23 11:31:57 +00:00
|
|
|
#endif
|
|
|
|
already = lws_wsi_mux_mark_parents_needing_writeable(wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
/* for network action, act only on the network wsi */
|
|
|
|
|
2018-11-12 15:24:42 +08:00
|
|
|
if (already
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-11-19 07:40:47 +08:00
|
|
|
&& !network_wsi->client_h2_alpn
|
2019-12-23 11:31:57 +00:00
|
|
|
&& !network_wsi->client_mux_substream
|
2018-04-11 13:39:42 +08:00
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2018-04-17 11:43:20 +08:00
|
|
|
static int
|
|
|
|
lws_h2_bind_for_post_before_action(struct lws *wsi)
|
|
|
|
{
|
2021-01-18 14:20:37 +00:00
|
|
|
const struct lws_http_mount *hit;
|
2022-04-11 08:20:28 +01:00
|
|
|
int uri_len = 0, methidx;
|
2021-01-18 14:20:37 +00:00
|
|
|
char *uri_ptr = NULL;
|
2021-01-02 05:31:31 +00:00
|
|
|
uint8_t *buffered;
|
2018-04-17 11:43:20 +08:00
|
|
|
const char *p;
|
2021-01-02 05:31:31 +00:00
|
|
|
size_t blen;
|
2018-04-17 11:43:20 +08:00
|
|
|
|
|
|
|
p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_METHOD);
|
2021-01-18 14:20:37 +00:00
|
|
|
if (!p || strcmp(p, "POST"))
|
|
|
|
return 0;
|
2020-08-14 06:35:31 +01:00
|
|
|
|
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COLON_PATH) ||
|
|
|
|
!lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_PATH))
|
|
|
|
/*
|
|
|
|
* There must be a path. Actually this is checked at
|
|
|
|
* http2.c along with the other required header
|
|
|
|
* presence before we can get here.
|
|
|
|
*
|
|
|
|
* But Coverity insists to see us check it.
|
|
|
|
*/
|
|
|
|
return 1;
|
2018-04-17 11:43:20 +08:00
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
hit = lws_find_mount(wsi,
|
|
|
|
lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_PATH),
|
|
|
|
lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COLON_PATH));
|
2018-10-10 13:54:43 +08:00
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
lwsl_debug("%s: %s: hit %p: %s\n", __func__,
|
|
|
|
lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_COLON_PATH),
|
|
|
|
hit, hit ? hit->origin : "null");
|
|
|
|
if (hit) {
|
|
|
|
const struct lws_protocols *pp;
|
|
|
|
const char *name = hit->origin;
|
2018-04-17 11:43:20 +08:00
|
|
|
|
2021-03-19 20:01:33 +00:00
|
|
|
if (hit->origin_protocol == LWSMPRO_CGI ||
|
|
|
|
hit->origin_protocol == LWSMPRO_HTTP ||
|
|
|
|
hit->origin_protocol == LWSMPRO_HTTPS)
|
|
|
|
return 0;
|
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
if (hit->protocol)
|
|
|
|
name = hit->protocol;
|
2021-11-17 14:48:59 +00:00
|
|
|
else
|
|
|
|
if (hit->origin_protocol == LWSMPRO_FILE)
|
|
|
|
return 0;
|
2018-04-17 11:43:20 +08:00
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
pp = lws_vhost_name_to_protocol(wsi->a.vhost, name);
|
|
|
|
if (!pp) {
|
|
|
|
lwsl_info("Unable to find protocol '%s'\n", name);
|
|
|
|
return 1;
|
2018-04-17 11:43:20 +08:00
|
|
|
}
|
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
if (lws_bind_protocol(wsi, pp, __func__))
|
|
|
|
return 1;
|
|
|
|
}
|
2022-04-11 08:20:28 +01:00
|
|
|
|
|
|
|
methidx = lws_http_get_uri_and_method(wsi, &uri_ptr, &uri_len);
|
|
|
|
|
|
|
|
if (methidx >= 0)
|
2021-02-01 17:29:00 +00:00
|
|
|
if (wsi->a.protocol->callback(wsi, LWS_CALLBACK_HTTP,
|
|
|
|
wsi->user_space,
|
|
|
|
hit ? uri_ptr +
|
|
|
|
hit->mountpoint_len : uri_ptr,
|
|
|
|
(size_t)(hit ? uri_len -
|
|
|
|
hit->mountpoint_len :
|
|
|
|
uri_len)))
|
|
|
|
return 1;
|
2021-01-02 05:31:31 +00:00
|
|
|
|
2022-04-11 08:20:28 +01:00
|
|
|
#if defined(LWS_WITH_ACCESS_LOG)
|
|
|
|
lws_prepare_access_log_info(wsi, uri_ptr, uri_len, methidx);
|
|
|
|
#endif
|
|
|
|
|
2021-01-18 16:04:08 +00:00
|
|
|
lwsl_info("%s: setting LRS_BODY from 0x%x (%s)\n", __func__,
|
2021-01-18 14:20:37 +00:00
|
|
|
(int)wsi->wsistate, wsi->a.protocol->name);
|
2021-01-02 05:31:31 +00:00
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
lwsi_set_state(wsi, LRS_BODY);
|
2021-01-02 05:31:31 +00:00
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
if (wsi->http.content_length_explicitly_zero)
|
|
|
|
return 0;
|
2021-01-02 05:31:31 +00:00
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
/*
|
|
|
|
* Dump any stashed body
|
|
|
|
*/
|
2021-01-02 05:31:31 +00:00
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
while (((!wsi->http.content_length_given) ||
|
|
|
|
wsi->http.rx_content_length) &&
|
|
|
|
(blen = lws_buflist_next_segment_len(&wsi->buflist, &buffered))) {
|
|
|
|
|
|
|
|
if ((size_t)wsi->http.rx_content_length < blen)
|
|
|
|
blen = (size_t)wsi->http.rx_content_length;
|
|
|
|
|
|
|
|
if (wsi->a.protocol->callback(wsi, LWS_CALLBACK_HTTP_BODY,
|
|
|
|
wsi->user_space, buffered, blen))
|
|
|
|
return 1;
|
|
|
|
lws_buflist_use_segment(&wsi->buflist, blen);
|
|
|
|
|
|
|
|
wsi->http.rx_content_length -= blen;
|
2018-04-17 11:43:20 +08:00
|
|
|
}
|
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
if (!wsi->buflist)
|
|
|
|
/* Take us off the pt's "wsi holding input buflist" list */
|
|
|
|
lws_dll2_remove(&wsi->dll_buflist);
|
|
|
|
|
|
|
|
if (wsi->http.content_length_given && wsi->http.rx_content_length)
|
|
|
|
/* still a-ways to go */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!wsi->http.content_length_given && !wsi->h2.END_STREAM)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (wsi->a.protocol->callback(wsi, LWS_CALLBACK_HTTP_BODY_COMPLETION,
|
|
|
|
wsi->user_space, NULL, 0))
|
|
|
|
return 1;
|
|
|
|
|
2018-04-17 11:43:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2019-08-18 10:35:43 +01:00
|
|
|
#endif
|
2018-04-17 11:43:20 +08:00
|
|
|
|
2018-04-13 16:01:38 +08:00
|
|
|
/*
|
|
|
|
* we are the 'network wsi' for potentially many muxed child wsi with
|
|
|
|
* no network connection of their own, who have to use us for all their
|
|
|
|
* network actions. So we use a round-robin scheme to share out the
|
|
|
|
* POLLOUT notifications to our children.
|
|
|
|
*
|
|
|
|
* But because any child could exhaust the socket's ability to take
|
|
|
|
* writes, we can only let one child get notified each time.
|
|
|
|
*
|
|
|
|
* In addition children may be closed / deleted / added between POLLOUT
|
|
|
|
* notifications, so we can't hold pointers
|
|
|
|
*/
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
static int
|
|
|
|
rops_perform_user_POLLOUT_h2(struct lws *wsi)
|
|
|
|
{
|
2019-12-23 11:31:57 +00:00
|
|
|
struct lws **wsi2;
|
2018-04-25 08:42:18 +08:00
|
|
|
#if defined(LWS_ROLE_WS)
|
|
|
|
int write_type = LWS_WRITE_PONG;
|
|
|
|
#endif
|
|
|
|
int n;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
wsi = lws_get_network_wsi(wsi);
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
wsi->mux.requested_POLLOUT = 0;
|
2019-12-26 02:35:41 +00:00
|
|
|
// if (!wsi->h2.initialized) {
|
|
|
|
// lwsl_info("pollout on uninitialized http2 conn\n");
|
|
|
|
// return 0;
|
|
|
|
// }
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
lws_wsi_mux_dump_waiting_children(wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
wsi2 = &wsi->mux.child_list;
|
2018-04-11 13:39:42 +08:00
|
|
|
if (!*wsi2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
struct lws *w, **wa;
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &(*wsi2)->mux.sibling_list;
|
|
|
|
if (!(*wsi2)->mux.requested_POLLOUT)
|
2018-04-11 13:39:42 +08:00
|
|
|
goto next_child;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we're going to do writable callback for this child.
|
|
|
|
* move him to be the last child
|
|
|
|
*/
|
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_debug("servicing child %s\n", lws_wsi_tag(*wsi2));
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
w = lws_wsi_mux_move_child_to_tail(wsi2);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-10-10 13:54:43 +08:00
|
|
|
if (!w) {
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
2018-10-10 13:54:43 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: child %s, sid %d, (wsistate 0x%x)\n",
|
|
|
|
__func__, lws_wsi_tag(w), w->mux.my_sid,
|
|
|
|
(unsigned int)w->wsistate);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
/* priority 1: post compression-transform buffered output */
|
|
|
|
|
|
|
|
if (lws_has_buffered_out(w)) {
|
|
|
|
lwsl_debug("%s: completing partial\n", __func__);
|
|
|
|
if (lws_issue_raw(w, NULL, 0) < 0) {
|
|
|
|
lwsl_info("%s signalling to close\n", __func__);
|
|
|
|
lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"h2 end stream 1");
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
lws_callback_on_writable(w);
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* priority 2: pre compression-transform buffered output */
|
|
|
|
|
|
|
|
#if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
|
|
|
|
if (w->http.comp_ctx.buflist_comp ||
|
|
|
|
w->http.comp_ctx.may_have_more) {
|
|
|
|
enum lws_write_protocol wp = LWS_WRITE_HTTP;
|
|
|
|
|
2018-09-02 14:35:37 +08:00
|
|
|
lwsl_info("%s: completing comp partial"
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
"(buflist_comp %p, may %d)\n",
|
|
|
|
__func__, w->http.comp_ctx.buflist_comp,
|
|
|
|
w->http.comp_ctx.may_have_more);
|
|
|
|
|
|
|
|
if (rops_write_role_protocol_h2(w, NULL, 0, &wp) < 0) {
|
|
|
|
lwsl_info("%s signalling to close\n", __func__);
|
|
|
|
lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"comp write fail");
|
|
|
|
}
|
|
|
|
lws_callback_on_writable(w);
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* priority 3: if no buffered out and waiting for that... */
|
|
|
|
|
|
|
|
if (lwsi_state(w) == LRS_FLUSHING_BEFORE_CLOSE) {
|
|
|
|
w->socket_is_permanently_unusable = 1;
|
|
|
|
lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"h2 end stream 1");
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
http: compression methods
Add generic http compression layer eanbled at cmake with LWS_WITH_HTTP_STREAM_COMPRESSION.
This is wholly a feature of the HTTP role (used by h1 and h2 roles) and doesn't exist
outside that context.
Currently provides 'deflate' and 'br' compression methods for server side only.
'br' requires also -DLWS_WITH_HTTP_BROTLI=1 at cmake and the brotli libraries (available in
your distro already) and dev package.
Other compression methods can be added nicely using an ops struct.
The built-in file serving stuff will use this is the client says he can handle it, and the
mimetype of the file either starts with "text/" (html and css etc) or is the mimetype of
Javascript.
zlib allocates quite a bit while in use, it seems to be around 256KiB per stream. So this
is only useful on relatively strong servers with lots of memory. However for some usecases
where you are serving a lot of css and js assets, it's a nice help.
The patch performs special treatment for http/1.1 pipelining, since the compression is
performed on the fly the compressed content-length is not known until the end. So for h1
only, chunked transfer-encoding is automatically added so pipelining can continue of the
connection.
For h2 the chunking is neither supported nor required, so it "just works".
User code can also request to add a compression transform before the reply headers were
sent using the new api
LWS_VISIBLE int
lws_http_compression_apply(struct lws *wsi, const char *name,
unsigned char **p, unsigned char *end, char decomp);
... this allows transparent compression of dynamically generated HTTP. The requested
compression (eg, "deflate") is only applied if the client headers indicated it was
supported, otherwise it's a NOP.
Name may be NULL in which case the first compression method in the internal table at
stream.c that is mentioned as acceptable by the client will be used.
NOTE: the compression translation, same as h2 support, relies on the user code using
LWS_WRITE_HTTP and then LWS_WRITE_HTTP_FINAL on the last part written. The internal
lws fileserving code already does this.
2018-09-02 14:43:05 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
/* if we arrived here, even by looping, we checked choked */
|
|
|
|
w->could_have_pending = 0;
|
|
|
|
wsi->could_have_pending = 0;
|
|
|
|
|
|
|
|
if (w->h2.pending_status_body) {
|
|
|
|
w->h2.send_END_STREAM = 1;
|
|
|
|
n = lws_write(w, (uint8_t *)w->h2.pending_status_body +
|
|
|
|
LWS_PRE,
|
|
|
|
strlen(w->h2.pending_status_body +
|
|
|
|
LWS_PRE), LWS_WRITE_HTTP_FINAL);
|
|
|
|
lws_free_set_NULL(w->h2.pending_status_body);
|
2018-04-13 16:01:38 +08:00
|
|
|
lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"h2 end stream 1");
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
2018-04-11 13:39:42 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
|
2019-10-03 08:31:34 -07:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-04-11 13:39:42 +08:00
|
|
|
if (lwsi_state(w) == LRS_H2_WAITING_TO_SEND_HEADERS) {
|
|
|
|
if (lws_h2_client_handshake(w))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
goto next_child;
|
|
|
|
}
|
2019-10-03 08:31:34 -07:00
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
2018-04-11 13:39:42 +08:00
|
|
|
if (lwsi_state(w) == LRS_DEFERRING_ACTION) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we had to defer the http_action to the POLLOUT
|
|
|
|
* handler, because we know it will send something and
|
|
|
|
* only in the POLLOUT handler do we know for sure
|
|
|
|
* that there is no partial pending on the network wsi.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lwsi_set_state(w, LRS_ESTABLISHED);
|
|
|
|
|
2021-01-18 14:20:37 +00:00
|
|
|
if (w->buflist) {
|
|
|
|
struct lws_context_per_thread *pt;
|
|
|
|
|
|
|
|
pt = &w->a.context->pt[(int)w->tsi];
|
|
|
|
lwsl_debug("%s: added %s to rxflow list\n",
|
|
|
|
__func__, lws_wsi_tag(w));
|
|
|
|
lws_dll2_add_head(
|
|
|
|
&w->dll_buflist,
|
|
|
|
&pt->dll_buflist_owner);
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:29:00 +00:00
|
|
|
if (lws_h2_bind_for_post_before_action(w))
|
|
|
|
return -1;
|
2018-04-17 11:43:20 +08:00
|
|
|
|
2020-12-19 09:13:24 +00:00
|
|
|
/*
|
|
|
|
* Well, we could be getting a POST from the client, it
|
|
|
|
* may not have any content-length. In that case, we
|
|
|
|
* will be in LRS_BODY state, we can't actually start
|
|
|
|
* the action until we had the body and the stream is
|
|
|
|
* half-closed, indicating that we can reply
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (lwsi_state(w) == LRS_BODY &&
|
|
|
|
w->h2.h2_state != LWS_H2_STATE_HALF_CLOSED_REMOTE)
|
|
|
|
goto next_child;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_info(" h2 action start...\n");
|
|
|
|
n = lws_http_action(w);
|
2019-03-22 06:22:40 +08:00
|
|
|
if (n < 0)
|
|
|
|
lwsl_info (" h2 action result %d\n", n);
|
|
|
|
else
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_info(" h2 action result %d "
|
|
|
|
"(wsi->http.rx_content_remain %lld)\n",
|
|
|
|
n, w->http.rx_content_remain);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Commonly we only managed to start a larger transfer
|
|
|
|
* that will complete asynchronously under its own wsi
|
|
|
|
* states. In those cases we will hear about
|
|
|
|
* END_STREAM going out in the POLLOUT handler.
|
|
|
|
*/
|
2019-03-22 06:22:40 +08:00
|
|
|
if (n >= 0 && !w->h2.pending_status_body &&
|
2018-09-04 08:06:46 +08:00
|
|
|
(n || w->h2.send_END_STREAM)) {
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_info("closing stream after h2 action\n");
|
2018-04-13 16:01:38 +08:00
|
|
|
lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"h2 end stream");
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
2019-03-22 06:22:40 +08:00
|
|
|
if (n < 0)
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
2019-03-22 06:22:40 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_WITH_FILE_OPS)
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (lwsi_state(w) == LRS_ISSUING_FILE) {
|
|
|
|
|
2019-12-26 02:35:41 +00:00
|
|
|
if (lws_wsi_txc_check_skint(&w->txc,
|
|
|
|
lws_h2_tx_cr_get(w))) {
|
|
|
|
wa = &wsi->mux.child_list;
|
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
((volatile struct lws *)w)->leave_pollout_active = 0;
|
|
|
|
|
|
|
|
/* >0 == completion, <0 == error
|
|
|
|
*
|
|
|
|
* We'll get a LWS_CALLBACK_HTTP_FILE_COMPLETION
|
|
|
|
* callback when it's done. That's the case even if we
|
|
|
|
* just completed the send, so wait for that.
|
|
|
|
*/
|
|
|
|
n = lws_serve_http_file_fragment(w);
|
|
|
|
lwsl_debug("lws_serve_http_file_fragment says %d\n", n);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We will often hear about out having sent the final
|
|
|
|
* DATA here... if so close the actual wsi
|
|
|
|
*/
|
|
|
|
if (n < 0 || w->h2.send_END_STREAM) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_debug("Closing POLLOUT child %s\n",
|
|
|
|
lws_wsi_tag(w));
|
2018-04-13 16:01:38 +08:00
|
|
|
lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"h2 end stream file");
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
2018-04-11 13:39:42 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
if (n > 0)
|
|
|
|
if (lws_http_transaction_completed(w))
|
|
|
|
return -1;
|
|
|
|
if (!n) {
|
|
|
|
lws_callback_on_writable(w);
|
2019-12-23 11:31:57 +00:00
|
|
|
(w)->mux.requested_POLLOUT = 1;
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
goto next_child;
|
|
|
|
}
|
2019-08-18 10:35:43 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-04-25 08:42:18 +08:00
|
|
|
#if defined(LWS_ROLE_WS)
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
/* Notify peer that we decided to close */
|
|
|
|
|
2018-11-23 08:47:56 +08:00
|
|
|
if (lwsi_role_ws(w) &&
|
|
|
|
lwsi_state(w) == LRS_WAITING_TO_SEND_CLOSE) {
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_debug("sending close packet\n");
|
|
|
|
w->waiting_to_send_close_frame = 0;
|
|
|
|
n = lws_write(w, &w->ws->ping_payload_buf[LWS_PRE],
|
|
|
|
w->ws->close_in_ping_buffer_len,
|
|
|
|
LWS_WRITE_CLOSE);
|
|
|
|
if (n >= 0) {
|
|
|
|
lwsi_set_state(w, LRS_AWAITING_CLOSE_ACK);
|
|
|
|
lws_set_timeout(w, PENDING_TIMEOUT_CLOSE_ACK, 5);
|
2018-04-13 16:01:38 +08:00
|
|
|
lwsl_debug("sent close frame, awaiting ack\n");
|
2018-04-11 13:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
|
2018-04-13 16:01:38 +08:00
|
|
|
/*
|
|
|
|
* Acknowledge receipt of peer's notification he closed,
|
|
|
|
* then logically close ourself
|
|
|
|
*/
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2021-09-13 17:57:08 +03:00
|
|
|
if ((lwsi_role_ws(w) && w->ws->pong_pending_flag) ||
|
2018-04-11 13:39:42 +08:00
|
|
|
(lwsi_state(w) == LRS_RETURNED_CLOSE &&
|
|
|
|
w->ws->payload_is_close)) {
|
|
|
|
|
|
|
|
if (w->ws->payload_is_close)
|
|
|
|
write_type = LWS_WRITE_CLOSE |
|
|
|
|
LWS_WRITE_H2_STREAM_END;
|
|
|
|
|
2021-09-13 17:57:08 +03:00
|
|
|
n = lws_write(w, &w->ws->pong_payload_buf[LWS_PRE],
|
|
|
|
w->ws->pong_payload_len, (enum lws_write_protocol)write_type);
|
2018-04-11 13:39:42 +08:00
|
|
|
if (n < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* well he is sent, mark him done */
|
2021-09-13 17:57:08 +03:00
|
|
|
w->ws->pong_pending_flag = 0;
|
2018-04-11 13:39:42 +08:00
|
|
|
if (w->ws->payload_is_close) {
|
2018-04-13 16:01:38 +08:00
|
|
|
/* oh... a close frame... then we are done */
|
2018-11-23 08:47:56 +08:00
|
|
|
lwsl_debug("Ack'd peer's close packet\n");
|
2018-04-11 13:39:42 +08:00
|
|
|
w->ws->payload_is_close = 0;
|
|
|
|
lwsi_set_state(w, LRS_RETURNED_CLOSE);
|
2018-04-13 16:01:38 +08:00
|
|
|
lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"returned close packet");
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
2018-04-11 13:39:42 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
|
|
|
|
lws_callback_on_writable(w);
|
2019-12-23 11:31:57 +00:00
|
|
|
(w)->mux.requested_POLLOUT = 1;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2018-11-23 08:47:56 +08:00
|
|
|
/* otherwise for PING, leave POLLOUT active both ways */
|
2018-04-11 13:39:42 +08:00
|
|
|
goto next_child;
|
|
|
|
}
|
2018-04-25 08:42:18 +08:00
|
|
|
#endif
|
2019-09-13 10:33:24 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* set client wsi to immortal long-poll mode; send END_STREAM
|
|
|
|
* flag on headers to indicate to a server, that allows
|
|
|
|
* it, that you want them to leave the stream in a long poll
|
|
|
|
* ro immortal state. We have to send headers so the client
|
|
|
|
* understands the http connection is ongoing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (w->h2.send_END_STREAM && w->h2.long_poll) {
|
|
|
|
uint8_t buf[LWS_PRE + 1];
|
|
|
|
enum lws_write_protocol wp = 0;
|
|
|
|
|
|
|
|
if (!rops_write_role_protocol_h2(w, buf + LWS_PRE, 0,
|
|
|
|
&wp)) {
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: entering ro long poll\n",
|
|
|
|
__func__, lws_wsi_tag(w));
|
2019-12-23 11:31:57 +00:00
|
|
|
lws_mux_mark_immortal(w);
|
2019-09-13 10:33:24 +01:00
|
|
|
} else
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_err("%s: %s: failed to set long poll\n",
|
|
|
|
__func__, lws_wsi_tag(w));
|
2019-09-13 10:33:24 +01:00
|
|
|
goto next_child;
|
|
|
|
}
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (lws_callback_as_writeable(w)) {
|
2018-04-13 16:01:38 +08:00
|
|
|
lwsl_info("Closing POLLOUT child (end stream %d)\n",
|
|
|
|
w->h2.send_END_STREAM);
|
|
|
|
lws_close_free_wsi(w, LWS_CLOSE_STATUS_NOSTATUS,
|
|
|
|
"h2 pollout handle");
|
2019-12-23 11:31:57 +00:00
|
|
|
wa = &wsi->mux.child_list;
|
2018-04-11 13:39:42 +08:00
|
|
|
} else
|
|
|
|
if (w->h2.send_END_STREAM)
|
|
|
|
lws_h2_state(w, LWS_H2_STATE_HALF_CLOSED_LOCAL);
|
|
|
|
|
|
|
|
next_child:
|
|
|
|
wsi2 = wa;
|
|
|
|
} while (wsi2 && *wsi2 && !lws_send_pipe_choked(wsi));
|
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
// lws_wsi_mux_dump_waiting_children(wsi);
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2019-12-23 11:31:57 +00:00
|
|
|
if (lws_wsi_mux_action_pending_writeable_reqs(wsi))
|
|
|
|
return -1;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct lws *
|
|
|
|
rops_encapsulation_parent_h2(struct lws *wsi)
|
|
|
|
{
|
2019-12-23 11:31:57 +00:00
|
|
|
if (wsi->mux.parent_wsi)
|
|
|
|
return wsi->mux.parent_wsi;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-12 15:56:38 +08:00
|
|
|
static int
|
|
|
|
rops_alpn_negotiated_h2(struct lws *wsi, const char *alpn)
|
|
|
|
{
|
|
|
|
struct allocated_headers *ah;
|
|
|
|
|
|
|
|
lwsl_debug("%s: client %d\n", __func__, lwsi_role_client(wsi));
|
2019-08-18 05:04:15 +01:00
|
|
|
#if defined(LWS_WITH_CLIENT)
|
2018-04-12 15:56:38 +08:00
|
|
|
if (lwsi_role_client(wsi)) {
|
|
|
|
lwsl_info("%s: upgraded to H2\n", __func__);
|
|
|
|
wsi->client_h2_alpn = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-12 08:01:50 +01:00
|
|
|
wsi->upgraded_to_http2 = 1;
|
2018-04-12 15:56:38 +08:00
|
|
|
|
2019-05-12 08:01:50 +01:00
|
|
|
/* adopt the header info */
|
2018-04-12 15:56:38 +08:00
|
|
|
|
2019-05-12 08:01:50 +01:00
|
|
|
ah = wsi->http.ah;
|
2018-04-12 15:56:38 +08:00
|
|
|
|
2020-12-17 22:16:57 +00:00
|
|
|
lws_role_transition(wsi, lwsi_role_client(wsi) ? LWSIFR_CLIENT : LWSIFR_SERVER, LRS_H2_AWAIT_PREFACE,
|
2019-05-12 08:01:50 +01:00
|
|
|
&role_ops_h2);
|
2018-04-12 15:56:38 +08:00
|
|
|
|
2019-05-12 08:01:50 +01:00
|
|
|
/* http2 union member has http union struct at start */
|
|
|
|
wsi->http.ah = ah;
|
2018-04-12 15:56:38 +08:00
|
|
|
|
2019-05-12 08:01:50 +01:00
|
|
|
if (!wsi->h2.h2n)
|
|
|
|
wsi->h2.h2n = lws_zalloc(sizeof(*wsi->h2.h2n), "h2n");
|
|
|
|
if (!wsi->h2.h2n)
|
|
|
|
return 1;
|
2018-04-12 15:56:38 +08:00
|
|
|
|
2019-05-12 08:01:50 +01:00
|
|
|
lws_h2_init(wsi);
|
2018-04-12 15:56:38 +08:00
|
|
|
|
2019-05-12 08:01:50 +01:00
|
|
|
/* HTTP2 union */
|
2018-04-12 15:56:38 +08:00
|
|
|
|
2021-06-04 14:51:45 +01:00
|
|
|
if (lws_hpack_dynamic_size(wsi,
|
|
|
|
(int)wsi->h2.h2n->our_set.s[H2SET_HEADER_TABLE_SIZE]))
|
|
|
|
return 1;
|
2019-12-26 02:35:41 +00:00
|
|
|
wsi->txc.tx_cr = 65535;
|
2018-04-12 15:56:38 +08:00
|
|
|
|
2020-12-25 05:54:19 +00:00
|
|
|
lwsl_info("%s: %s: configured for h2\n", __func__, lws_wsi_tag(wsi));
|
2018-04-12 15:56:38 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-18 13:09:32 +01:00
|
|
|
static int
|
|
|
|
rops_issue_keepalive_h2(struct lws *wsi, int isvalid)
|
|
|
|
{
|
|
|
|
struct lws *nwsi = lws_get_network_wsi(wsi);
|
|
|
|
struct lws_h2_protocol_send *pps;
|
2020-12-12 06:21:40 +00:00
|
|
|
uint64_t us = (uint64_t)lws_now_usecs();
|
2019-09-18 13:09:32 +01:00
|
|
|
|
|
|
|
if (isvalid) {
|
|
|
|
_lws_validity_confirmed_role(nwsi);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can only send these frames on the network connection itself...
|
|
|
|
* we shouldn't be tracking validity on anything else
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert(wsi == nwsi);
|
|
|
|
|
|
|
|
pps = lws_h2_new_pps(LWS_H2_PPS_PING);
|
|
|
|
if (!pps)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The peer is defined to copy us back the unchanged payload in another
|
|
|
|
* PING frame this time with ACK set. So by sending that out with the
|
|
|
|
* current time, it's an interesting opportunity to learn the effective
|
|
|
|
* RTT on the link when the PONG comes in, plus or minus the time to
|
|
|
|
* schedule the PPS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
memcpy(pps->u.ping.ping_payload, &us, 8);
|
|
|
|
lws_pps_schedule(nwsi, pps);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
static const lws_rops_t rops_table_h2[] = {
|
|
|
|
#if defined(LWS_WITH_SERVER)
|
|
|
|
/* 1 */ { .check_upgrades = rops_check_upgrades_h2 },
|
|
|
|
#else
|
2021-02-01 11:48:04 +00:00
|
|
|
/* 1 */ { .check_upgrades = NULL },
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
#endif
|
|
|
|
/* 2 */ { .pt_init_destroy = rops_pt_init_destroy_h2 },
|
|
|
|
/* 3 */ { .init_vhost = rops_init_vhost_h2 },
|
|
|
|
/* 4 */ { .handle_POLLIN = rops_handle_POLLIN_h2 },
|
|
|
|
/* 5 */ { .handle_POLLOUT = rops_handle_POLLOUT_h2 },
|
|
|
|
/* 6 */ { .perform_user_POLLOUT = rops_perform_user_POLLOUT_h2 },
|
|
|
|
/* 7 */ { .callback_on_writable = rops_callback_on_writable_h2 },
|
|
|
|
/* 8 */ { .tx_credit = rops_tx_credit_h2 },
|
|
|
|
/* 9 */ { .write_role_protocol = rops_write_role_protocol_h2 },
|
|
|
|
/* 10 */ { .encapsulation_parent = rops_encapsulation_parent_h2 },
|
|
|
|
/* 11 */ { .alpn_negotiated = rops_alpn_negotiated_h2 },
|
|
|
|
/* 12 */ { .close_kill_connection = rops_close_kill_connection_h2 },
|
|
|
|
/* 13 */ { .destroy_role = rops_destroy_role_h2 },
|
|
|
|
/* 14 */ { .issue_keepalive = rops_issue_keepalive_h2 },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-01-15 06:31:19 +00:00
|
|
|
const struct lws_role_ops role_ops_h2 = {
|
2018-04-12 15:56:38 +08:00
|
|
|
/* role name */ "h2",
|
|
|
|
/* alpn id */ "h2",
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
|
|
|
|
/* rops_table */ rops_table_h2,
|
|
|
|
/* rops_idx */ {
|
|
|
|
/* LWS_ROPS_check_upgrades */
|
2020-04-16 06:53:10 +01:00
|
|
|
#if defined(LWS_WITH_SERVER)
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
/* LWS_ROPS_pt_init_destroy */ 0x12,
|
2020-04-16 06:53:10 +01:00
|
|
|
#else
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
/* LWS_ROPS_pt_init_destroy */ 0x02,
|
2020-04-16 06:53:10 +01:00
|
|
|
#endif
|
roles: compress role ops structs
role ops are usually only sparsely filled, there are currently 20
function pointers but several roles only fill in two. No single
role has more than 14 of the ops. On a 32/64 bit build this part
of the ops struct takes a fixed 80 / 160 bytes then.
First reduce the type of the callback reason part from uint16_t to
uint8_t, this saves 12 bytes unconditionally.
Change to a separate function pointer array with a nybble index
array, it costs 10 bytes for the index and a pointer to the
separate array, for 32-bit the cost is
2 + (4 x ops_used)
and for 64-bit
6 + (8 x ops_used)
for 2 x ops_used it means 32-bit: 10 vs 80 / 64-bit: 22 vs 160
For a typical system with h1 (9), h2 (14), listen (2), netlink (2),
pipe (1), raw_skt (3), ws (12), == 43 ops_used out of 140, it means
the .rodata for this reduced from 32-bit: 560 -> 174 (386 byte
saving) and 64-bit: 1120 -> 350 (770 byte saving)
This doesn't account for the changed function ops calling code, two
ways were tried, a preprocessor macro and explicit functions
For an x86_64 gcc 10 build with most options, release mode,
.text + .rodata
before patch: 553282
accessor macro: 552714 (568 byte saving)
accessor functions: 553674 (392 bytes worse than without patch)
therefore we went with the macros
2020-10-19 13:55:21 +01:00
|
|
|
/* LWS_ROPS_init_vhost */
|
|
|
|
/* LWS_ROPS_destroy_vhost */ 0x30,
|
|
|
|
/* LWS_ROPS_service_flag_pending */
|
|
|
|
/* LWS_ROPS_handle_POLLIN */ 0x04,
|
|
|
|
/* LWS_ROPS_handle_POLLOUT */
|
|
|
|
/* LWS_ROPS_perform_user_POLLOUT */ 0x56,
|
|
|
|
/* LWS_ROPS_callback_on_writable */
|
|
|
|
/* LWS_ROPS_tx_credit */ 0x78,
|
|
|
|
/* LWS_ROPS_write_role_protocol */
|
|
|
|
/* LWS_ROPS_encapsulation_parent */ 0x9a,
|
|
|
|
/* LWS_ROPS_alpn_negotiated */
|
|
|
|
/* LWS_ROPS_close_via_role_protocol */ 0xb0,
|
|
|
|
/* LWS_ROPS_close_role */
|
|
|
|
/* LWS_ROPS_close_kill_connection */ 0x0c,
|
|
|
|
/* LWS_ROPS_destroy_role */
|
|
|
|
/* LWS_ROPS_adoption_bind */ 0xd0,
|
|
|
|
/* LWS_ROPS_client_bind */
|
|
|
|
/* LWS_ROPS_issue_keepalive */ 0x0e,
|
|
|
|
},
|
2018-11-29 08:29:48 +08:00
|
|
|
/* adoption_cb clnt, srv */ { LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED,
|
|
|
|
LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED },
|
|
|
|
/* rx cb clnt, srv */ { LWS_CALLBACK_RECEIVE_CLIENT_HTTP,
|
|
|
|
0 /* may be POST, etc */ },
|
2018-04-11 13:39:42 +08:00
|
|
|
/* writeable cb clnt, srv */ { LWS_CALLBACK_CLIENT_HTTP_WRITEABLE,
|
|
|
|
LWS_CALLBACK_HTTP_WRITEABLE },
|
|
|
|
/* close cb clnt, srv */ { LWS_CALLBACK_CLOSED_CLIENT_HTTP,
|
|
|
|
LWS_CALLBACK_CLOSED_HTTP },
|
2018-08-18 14:11:29 +08:00
|
|
|
/* protocol_bind cb c, srv */ { LWS_CALLBACK_CLIENT_HTTP_BIND_PROTOCOL,
|
|
|
|
LWS_CALLBACK_HTTP_BIND_PROTOCOL },
|
|
|
|
/* protocol_unbind cb c, srv */ { LWS_CALLBACK_CLIENT_HTTP_DROP_PROTOCOL,
|
|
|
|
LWS_CALLBACK_HTTP_DROP_PROTOCOL },
|
2018-04-29 10:44:36 +08:00
|
|
|
/* file_handle */ 0,
|
2018-04-11 13:39:42 +08:00
|
|
|
};
|