1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include <private-lib-core.h>
26
27 static int
rops_handle_POLLIN_cgi(struct lws_context_per_thread * pt,struct lws * wsi,struct lws_pollfd * pollfd)28 rops_handle_POLLIN_cgi(struct lws_context_per_thread *pt, struct lws *wsi,
29 struct lws_pollfd *pollfd)
30 {
31 struct lws_cgi_args args;
32
33 assert(wsi->role_ops == &role_ops_cgi);
34
35 if (wsi->lsp_channel >= LWS_STDOUT &&
36 !(pollfd->revents & pollfd->events & LWS_POLLIN))
37 return LWS_HPI_RET_HANDLED;
38
39 if (wsi->lsp_channel == LWS_STDIN &&
40 !(pollfd->revents & pollfd->events & LWS_POLLOUT))
41 return LWS_HPI_RET_HANDLED;
42
43 if (wsi->lsp_channel == LWS_STDIN &&
44 lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
45 lwsl_wsi_info(wsi, "failed at set pollfd");
46 return LWS_HPI_RET_WSI_ALREADY_DIED;
47 }
48
49 if (!wsi->parent) {
50 lwsl_wsi_debug(wsi, "stdwsi content with parent");
51
52 return LWS_HPI_RET_HANDLED;
53 }
54
55 if (!wsi->parent->http.cgi) {
56 lwsl_wsi_notice(wsi, "stdwsi content with deleted cgi object");
57
58 return LWS_HPI_RET_HANDLED;
59 }
60
61 if (!wsi->parent->http.cgi->lsp) {
62 lwsl_wsi_notice(wsi, "stdwsi content with reaped lsp");
63
64 return LWS_HPI_RET_HANDLED;
65 }
66
67 args.ch = wsi->lsp_channel;
68 args.stdwsi = &wsi->parent->http.cgi->lsp->stdwsi[0];
69 args.hdr_state = (enum lws_cgi_hdr_state)wsi->hdr_state;
70
71 lwsl_wsi_debug(wsi, "CGI LWS_STDOUT %p wsistate 0x%x",
72 wsi->parent, wsi->wsistate);
73
74 if (user_callback_handle_rxflow(wsi->parent->a.protocol->callback,
75 wsi->parent, LWS_CALLBACK_CGI,
76 wsi->parent->user_space,
77 (void *)&args, 0))
78 return 1;
79
80 return LWS_HPI_RET_HANDLED;
81 }
82
83 static int
rops_handle_POLLOUT_cgi(struct lws * wsi)84 rops_handle_POLLOUT_cgi(struct lws *wsi)
85 {
86 return LWS_HP_RET_USER_SERVICE;
87 }
88
89 static int
rops_destroy_role_cgi(struct lws * wsi)90 rops_destroy_role_cgi(struct lws *wsi)
91 {
92 #if defined(LWS_WITH_ZLIB)
93 if (!wsi->http.cgi)
94 return 0;
95 if (!wsi->http.cgi->gzip_init)
96 return 0;
97
98 inflateEnd(&wsi->http.cgi->inflate);
99 wsi->http.cgi->gzip_init = 0;
100 #endif
101
102 return 0;
103 }
104
105 void
lws_cgi_sul_cb(lws_sorted_usec_list_t * sul)106 lws_cgi_sul_cb(lws_sorted_usec_list_t *sul)
107 {
108 struct lws_context_per_thread *pt = lws_container_of(sul,
109 struct lws_context_per_thread, sul_cgi);
110
111 lws_cgi_kill_terminated(pt);
112
113 if (pt->http.cgi_list)
114 lws_sul_schedule(pt->context, (int)(pt - pt->context->pt),
115 &pt->sul_cgi, lws_cgi_sul_cb, 3 * LWS_US_PER_SEC);
116 }
117
118 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)119 rops_pt_init_destroy_cgi(struct lws_context *context,
120 const struct lws_context_creation_info *info,
121 struct lws_context_per_thread *pt, int destroy)
122 {
123
124 lws_sul_cancel(&pt->sul_cgi);
125
126 return 0;
127 }
128
129 static int
rops_close_role_cgi(struct lws_context_per_thread * pt,struct lws * wsi)130 rops_close_role_cgi(struct lws_context_per_thread *pt, struct lws *wsi)
131 {
132 if (wsi->parent && wsi->parent->http.cgi && wsi->parent->http.cgi->lsp)
133 lws_spawn_stdwsi_closed(wsi->parent->http.cgi->lsp, wsi);
134
135 return 0;
136 }
137
138 static const lws_rops_t rops_table_cgi[] = {
139 /* 1 */ { .pt_init_destroy = rops_pt_init_destroy_cgi },
140 /* 2 */ { .handle_POLLIN = rops_handle_POLLIN_cgi },
141 /* 3 */ { .handle_POLLOUT = rops_handle_POLLOUT_cgi },
142 /* 4 */ { .close_role = rops_close_role_cgi },
143 /* 5 */ { .destroy_role = rops_destroy_role_cgi },
144 };
145
146 const struct lws_role_ops role_ops_cgi = {
147 /* role name */ "cgi",
148 /* alpn id */ NULL,
149
150 /* rops_table */ rops_table_cgi,
151 /* rops_idx */ {
152 /* LWS_ROPS_check_upgrades */
153 /* LWS_ROPS_pt_init_destroy */ 0x01,
154 /* LWS_ROPS_init_vhost */
155 /* LWS_ROPS_destroy_vhost */ 0x00,
156 /* LWS_ROPS_service_flag_pending */
157 /* LWS_ROPS_handle_POLLIN */ 0x02,
158 /* LWS_ROPS_handle_POLLOUT */
159 /* LWS_ROPS_perform_user_POLLOUT */ 0x30,
160 /* LWS_ROPS_callback_on_writable */
161 /* LWS_ROPS_tx_credit */ 0x00,
162 /* LWS_ROPS_write_role_protocol */
163 /* LWS_ROPS_encapsulation_parent */ 0x00,
164 /* LWS_ROPS_alpn_negotiated */
165 /* LWS_ROPS_close_via_role_protocol */ 0x00,
166 /* LWS_ROPS_close_role */
167 /* LWS_ROPS_close_kill_connection */ 0x40,
168 /* LWS_ROPS_destroy_role */
169 /* LWS_ROPS_adoption_bind */ 0x50,
170 /* LWS_ROPS_client_bind */
171 /* LWS_ROPS_issue_keepalive */ 0x00,
172 },
173
174 /* adoption_cb clnt, srv */ { 0, 0 },
175 /* rx_cb clnt, srv */ { 0, 0 },
176 /* writeable cb clnt, srv */ { 0, 0 },
177 /* close cb clnt, srv */ { 0, 0 },
178 /* protocol_bind_cb c,s */ { 0, 0 },
179 /* protocol_unbind_cb c,s */ { 0, 0 },
180
181 /* file_handle */ 0,
182 };
183