1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-09 00:00:04 +01:00
libwebsockets/lib/roles/cgi/ops-cgi.c

186 lines
5.2 KiB
C
Raw Permalink Normal View History

/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
#include <private-lib-core.h>
static int
rops_handle_POLLIN_cgi(struct lws_context_per_thread *pt, struct lws *wsi,
struct lws_pollfd *pollfd)
{
struct lws_cgi_args args;
assert(wsi->role_ops == &role_ops_cgi);
if (wsi->lsp_channel >= LWS_STDOUT &&
!(pollfd->revents & pollfd->events & LWS_POLLIN))
return LWS_HPI_RET_HANDLED;
if (wsi->lsp_channel == LWS_STDIN &&
!(pollfd->revents & pollfd->events & LWS_POLLOUT))
return LWS_HPI_RET_HANDLED;
if (wsi->lsp_channel == LWS_STDIN &&
lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
lwsl_info("failed at set pollfd\n");
return LWS_HPI_RET_WSI_ALREADY_DIED;
}
2020-07-17 15:35:28 +01:00
if (!wsi->parent) {
2020-11-25 07:42:07 +00:00
lwsl_debug("%s: stdwsi content with parent\n",
2020-07-17 15:35:28 +01:00
__func__);
return LWS_HPI_RET_HANDLED;
}
if (!wsi->parent->http.cgi) {
lwsl_notice("%s: stdwsi content with deleted cgi object\n",
__func__);
return LWS_HPI_RET_HANDLED;
}
if (!wsi->parent->http.cgi->lsp) {
lwsl_notice("%s: stdwsi content with reaped lsp\n",
__func__);
return LWS_HPI_RET_HANDLED;
}
args.ch = wsi->lsp_channel;
args.stdwsi = &wsi->parent->http.cgi->lsp->stdwsi[0];
args.hdr_state = (enum lws_cgi_hdr_state)wsi->hdr_state;
lwsl_debug("CGI LWS_STDOUT %p wsistate 0x%x\n",
wsi->parent, wsi->wsistate);
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->parent->a.protocol->callback,
wsi->parent, LWS_CALLBACK_CGI,
wsi->parent->user_space,
(void *)&args, 0))
return 1;
return LWS_HPI_RET_HANDLED;
}
static int
rops_handle_POLLOUT_cgi(struct lws *wsi)
{
return LWS_HP_RET_USER_SERVICE;
}
2018-06-16 09:35:07 +08:00
static int
rops_destroy_role_cgi(struct lws *wsi)
{
#if defined(LWS_WITH_ZLIB)
if (!wsi->http.cgi)
return 0;
if (!wsi->http.cgi->gzip_init)
return 0;
inflateEnd(&wsi->http.cgi->inflate);
wsi->http.cgi->gzip_init = 0;
#endif
return 0;
}
void
lws_cgi_sul_cb(lws_sorted_usec_list_t *sul)
{
struct lws_context_per_thread *pt = lws_container_of(sul,
struct lws_context_per_thread, sul_cgi);
lws_cgi_kill_terminated(pt);
if (pt->http.cgi_list)
lws_sul_schedule(pt->context, (int)(pt - pt->context->pt),
&pt->sul_cgi, lws_cgi_sul_cb, 3 * LWS_US_PER_SEC);
}
static int
rops_pt_init_destroy_cgi(struct lws_context *context,
const struct lws_context_creation_info *info,
struct lws_context_per_thread *pt, int destroy)
{
lws_sul_cancel(&pt->sul_cgi);
return 0;
}
static int
rops_close_role_cgi(struct lws_context_per_thread *pt, struct lws *wsi)
{
if (wsi->parent && wsi->parent->http.cgi && wsi->parent->http.cgi->lsp)
lws_spawn_stdwsi_closed(wsi->parent->http.cgi->lsp, wsi);
return 0;
}
static const lws_rops_t rops_table_cgi[] = {
/* 1 */ { .pt_init_destroy = rops_pt_init_destroy_cgi },
/* 2 */ { .handle_POLLIN = rops_handle_POLLIN_cgi },
/* 3 */ { .handle_POLLOUT = rops_handle_POLLOUT_cgi },
/* 4 */ { .close_role = rops_close_role_cgi },
/* 5 */ { .destroy_role = rops_destroy_role_cgi },
};
const struct lws_role_ops role_ops_cgi = {
/* role name */ "cgi",
/* alpn id */ NULL,
/* rops_table */ rops_table_cgi,
/* rops_idx */ {
/* LWS_ROPS_check_upgrades */
/* LWS_ROPS_pt_init_destroy */ 0x01,
/* LWS_ROPS_init_vhost */
/* LWS_ROPS_destroy_vhost */ 0x00,
/* LWS_ROPS_service_flag_pending */
/* LWS_ROPS_handle_POLLIN */ 0x02,
/* LWS_ROPS_handle_POLLOUT */
/* LWS_ROPS_perform_user_POLLOUT */ 0x30,
/* LWS_ROPS_callback_on_writable */
/* LWS_ROPS_tx_credit */ 0x00,
/* LWS_ROPS_write_role_protocol */
/* LWS_ROPS_encapsulation_parent */ 0x00,
/* LWS_ROPS_alpn_negotiated */
/* LWS_ROPS_close_via_role_protocol */ 0x00,
/* LWS_ROPS_close_role */
/* LWS_ROPS_close_kill_connection */ 0x40,
/* LWS_ROPS_destroy_role */
/* LWS_ROPS_adoption_bind */ 0x50,
/* LWS_ROPS_client_bind */
/* LWS_ROPS_issue_keepalive */ 0x00,
},
2018-11-29 08:29:48 +08:00
/* adoption_cb clnt, srv */ { 0, 0 },
/* rx_cb clnt, srv */ { 0, 0 },
/* writeable cb clnt, srv */ { 0, 0 },
/* close cb clnt, srv */ { 0, 0 },
/* protocol_bind_cb c,s */ { 0, 0 },
/* protocol_unbind_cb c,s */ { 0, 0 },
/* file_handle */ 0,
};