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_info("failed at set pollfd\n");
46 return LWS_HPI_RET_WSI_ALREADY_DIED;
47 }
48
49 args.ch = wsi->lsp_channel;
50 args.stdwsi = &wsi->parent->http.cgi->lsp->stdwsi[0];
51 args.hdr_state = wsi->hdr_state;
52
53 lwsl_debug("CGI LWS_STDOUT %p wsistate 0x%x\n",
54 wsi->parent, wsi->wsistate);
55
56 if (user_callback_handle_rxflow(wsi->parent->protocol->callback,
57 wsi->parent, LWS_CALLBACK_CGI,
58 wsi->parent->user_space,
59 (void *)&args, 0))
60 return 1;
61
62 return LWS_HPI_RET_HANDLED;
63 }
64
65 static int
rops_handle_POLLOUT_cgi(struct lws * wsi)66 rops_handle_POLLOUT_cgi(struct lws *wsi)
67 {
68 return LWS_HP_RET_USER_SERVICE;
69 }
70
71 static int
rops_destroy_role_cgi(struct lws * wsi)72 rops_destroy_role_cgi(struct lws *wsi)
73 {
74 #if defined(LWS_WITH_ZLIB)
75 if (!wsi->http.cgi)
76 return 0;
77 if (!wsi->http.cgi->gzip_init)
78 return 0;
79
80 inflateEnd(&wsi->http.cgi->inflate);
81 wsi->http.cgi->gzip_init = 0;
82 #endif
83
84 return 0;
85 }
86
87 static void
lws_cgi_sul_cb(lws_sorted_usec_list_t * sul)88 lws_cgi_sul_cb(lws_sorted_usec_list_t *sul)
89 {
90 struct lws_context_per_thread *pt = lws_container_of(sul,
91 struct lws_context_per_thread, sul_cgi);
92
93 lws_cgi_kill_terminated(pt);
94
95 __lws_sul_insert(&pt->pt_sul_owner, &pt->sul_cgi,
96 3 * LWS_US_PER_SEC);
97 }
98
99 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)100 rops_pt_init_destroy_cgi(struct lws_context *context,
101 const struct lws_context_creation_info *info,
102 struct lws_context_per_thread *pt, int destroy)
103 {
104 if (!destroy) {
105
106 pt->sul_cgi.cb = lws_cgi_sul_cb;
107
108 __lws_sul_insert(&pt->pt_sul_owner, &pt->sul_cgi,
109 3 * LWS_US_PER_SEC);
110 } else
111 lws_dll2_remove(&pt->sul_cgi.list);
112
113 return 0;
114 }
115
116
117 const struct lws_role_ops role_ops_cgi = {
118 /* role name */ "cgi",
119 /* alpn id */ NULL,
120 /* check_upgrades */ NULL,
121 /* pt_init_destroy */ rops_pt_init_destroy_cgi,
122 /* init_vhost */ NULL,
123 /* destroy_vhost */ NULL,
124 /* service_flag_pending */ NULL,
125 /* handle_POLLIN */ rops_handle_POLLIN_cgi,
126 /* handle_POLLOUT */ rops_handle_POLLOUT_cgi,
127 /* perform_user_POLLOUT */ NULL,
128 /* callback_on_writable */ NULL,
129 /* tx_credit */ NULL,
130 /* write_role_protocol */ NULL,
131 /* encapsulation_parent */ NULL,
132 /* alpn_negotiated */ NULL,
133 /* close_via_role_protocol */ NULL,
134 /* close_role */ NULL,
135 /* close_kill_connection */ NULL,
136 /* destroy_role */ rops_destroy_role_cgi,
137 /* adoption_bind */ NULL,
138 /* client_bind */ NULL,
139 /* issue_keepalive */ NULL,
140 /* adoption_cb clnt, srv */ { 0, 0 },
141 /* rx_cb clnt, srv */ { 0, 0 },
142 /* writeable cb clnt, srv */ { 0, 0 },
143 /* close cb clnt, srv */ { 0, 0 },
144 /* protocol_bind_cb c,s */ { 0, 0 },
145 /* protocol_unbind_cb c,s */ { 0, 0 },
146 /* file_handle */ 0,
147 };
148