Blame


1 a596b957 2022-07-14 tracey /*
2 a596b957 2022-07-14 tracey * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
4 a596b957 2022-07-14 tracey * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
5 a596b957 2022-07-14 tracey *
6 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
7 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
8 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
9 a596b957 2022-07-14 tracey *
10 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a596b957 2022-07-14 tracey */
18 a596b957 2022-07-14 tracey
19 a596b957 2022-07-14 tracey #include <arpa/inet.h>
20 a596b957 2022-07-14 tracey #include <sys/queue.h>
21 a596b957 2022-07-14 tracey #include <sys/socket.h>
22 a596b957 2022-07-14 tracey #include <sys/types.h>
23 311b7e33 2022-08-01 op #include <sys/uio.h>
24 a596b957 2022-07-14 tracey
25 a596b957 2022-07-14 tracey #include <errno.h>
26 a596b957 2022-07-14 tracey #include <event.h>
27 a596b957 2022-07-14 tracey #include <imsg.h>
28 01498c42 2022-08-19 op #include <stdarg.h>
29 a596b957 2022-07-14 tracey #include <stdlib.h>
30 a596b957 2022-07-14 tracey #include <stdio.h>
31 a596b957 2022-07-14 tracey #include <string.h>
32 a596b957 2022-07-14 tracey #include <time.h>
33 a596b957 2022-07-14 tracey #include <unistd.h>
34 a596b957 2022-07-14 tracey
35 a596b957 2022-07-14 tracey #include "got_error.h"
36 df2d3cd2 2023-03-11 op #include "got_reference.h"
37 a596b957 2022-07-14 tracey
38 a596b957 2022-07-14 tracey #include "gotwebd.h"
39 ed619ca0 2022-12-14 op #include "tmpl.h"
40 a596b957 2022-07-14 tracey
41 a596b957 2022-07-14 tracey size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
42 a596b957 2022-07-14 tracey void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
43 a596b957 2022-07-14 tracey uint16_t);
44 a596b957 2022-07-14 tracey void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
45 3ff00ead 2022-08-09 op int fcgi_send_response(struct request *, int, const void *, size_t);
46 a596b957 2022-07-14 tracey
47 a596b957 2022-07-14 tracey void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
48 a596b957 2022-07-14 tracey void dump_fcgi_begin_request_body(const char *,
49 a596b957 2022-07-14 tracey struct fcgi_begin_request_body *);
50 a596b957 2022-07-14 tracey void dump_fcgi_end_request_body(const char *,
51 a596b957 2022-07-14 tracey struct fcgi_end_request_body *);
52 a596b957 2022-07-14 tracey
53 a596b957 2022-07-14 tracey extern int cgi_inflight;
54 a596b957 2022-07-14 tracey extern volatile int client_cnt;
55 a596b957 2022-07-14 tracey
56 a596b957 2022-07-14 tracey void
57 a596b957 2022-07-14 tracey fcgi_request(int fd, short events, void *arg)
58 a596b957 2022-07-14 tracey {
59 a596b957 2022-07-14 tracey struct request *c = arg;
60 a596b957 2022-07-14 tracey ssize_t n;
61 a596b957 2022-07-14 tracey size_t parsed = 0;
62 a596b957 2022-07-14 tracey
63 a596b957 2022-07-14 tracey n = read(fd, c->buf + c->buf_pos + c->buf_len,
64 a596b957 2022-07-14 tracey FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
65 a596b957 2022-07-14 tracey
66 a596b957 2022-07-14 tracey switch (n) {
67 a596b957 2022-07-14 tracey case -1:
68 a596b957 2022-07-14 tracey switch (errno) {
69 a596b957 2022-07-14 tracey case EINTR:
70 a596b957 2022-07-14 tracey case EAGAIN:
71 a596b957 2022-07-14 tracey return;
72 a596b957 2022-07-14 tracey default:
73 a596b957 2022-07-14 tracey goto fail;
74 a596b957 2022-07-14 tracey }
75 a596b957 2022-07-14 tracey break;
76 a596b957 2022-07-14 tracey
77 a596b957 2022-07-14 tracey case 0:
78 a596b957 2022-07-14 tracey log_debug("closed connection");
79 a596b957 2022-07-14 tracey goto fail;
80 a596b957 2022-07-14 tracey default:
81 a596b957 2022-07-14 tracey break;
82 a596b957 2022-07-14 tracey }
83 a596b957 2022-07-14 tracey
84 a596b957 2022-07-14 tracey c->buf_len += n;
85 a596b957 2022-07-14 tracey
86 a596b957 2022-07-14 tracey /*
87 a596b957 2022-07-14 tracey * Parse the records as they are received. Per the FastCGI
88 a596b957 2022-07-14 tracey * specification, the server need only receive the FastCGI
89 a596b957 2022-07-14 tracey * parameter records in full; it is free to begin execution
90 a596b957 2022-07-14 tracey * at that point, which is what happens here.
91 a596b957 2022-07-14 tracey */
92 a596b957 2022-07-14 tracey do {
93 a596b957 2022-07-14 tracey parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
94 a596b957 2022-07-14 tracey if (parsed != 0) {
95 a596b957 2022-07-14 tracey c->buf_pos += parsed;
96 a596b957 2022-07-14 tracey c->buf_len -= parsed;
97 a596b957 2022-07-14 tracey }
98 a596b957 2022-07-14 tracey
99 5add7f42 2023-03-10 op /* drop the parsed record */
100 5add7f42 2023-03-10 op if (parsed != 0 && c->buf_len > 0) {
101 a596b957 2022-07-14 tracey bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
102 a596b957 2022-07-14 tracey c->buf_pos = 0;
103 a596b957 2022-07-14 tracey }
104 5add7f42 2023-03-10 op } while (parsed > 0 && c->buf_len > 0);
105 5add7f42 2023-03-10 op
106 a596b957 2022-07-14 tracey return;
107 a596b957 2022-07-14 tracey fail:
108 a596b957 2022-07-14 tracey fcgi_cleanup_request(c);
109 a596b957 2022-07-14 tracey }
110 a596b957 2022-07-14 tracey
111 a596b957 2022-07-14 tracey size_t
112 a596b957 2022-07-14 tracey fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
113 a596b957 2022-07-14 tracey {
114 a596b957 2022-07-14 tracey struct fcgi_record_header *h;
115 a596b957 2022-07-14 tracey
116 a596b957 2022-07-14 tracey if (n < sizeof(struct fcgi_record_header))
117 a596b957 2022-07-14 tracey return 0;
118 a596b957 2022-07-14 tracey
119 a596b957 2022-07-14 tracey h = (struct fcgi_record_header*) buf;
120 a596b957 2022-07-14 tracey
121 a596b957 2022-07-14 tracey dump_fcgi_record("", h);
122 a596b957 2022-07-14 tracey
123 a596b957 2022-07-14 tracey if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
124 a596b957 2022-07-14 tracey + h->padding_len)
125 a596b957 2022-07-14 tracey return 0;
126 a596b957 2022-07-14 tracey
127 a596b957 2022-07-14 tracey if (h->version != 1)
128 a596b957 2022-07-14 tracey log_warn("wrong version");
129 a596b957 2022-07-14 tracey
130 a596b957 2022-07-14 tracey switch (h->type) {
131 a596b957 2022-07-14 tracey case FCGI_BEGIN_REQUEST:
132 a596b957 2022-07-14 tracey fcgi_parse_begin_request(buf +
133 a596b957 2022-07-14 tracey sizeof(struct fcgi_record_header),
134 a596b957 2022-07-14 tracey ntohs(h->content_len), c, ntohs(h->id));
135 a596b957 2022-07-14 tracey break;
136 a596b957 2022-07-14 tracey case FCGI_PARAMS:
137 a596b957 2022-07-14 tracey fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
138 a596b957 2022-07-14 tracey ntohs(h->content_len), c, ntohs(h->id));
139 a596b957 2022-07-14 tracey break;
140 a596b957 2022-07-14 tracey case FCGI_STDIN:
141 a596b957 2022-07-14 tracey case FCGI_ABORT_REQUEST:
142 a596b957 2022-07-14 tracey fcgi_create_end_record(c);
143 a596b957 2022-07-14 tracey fcgi_cleanup_request(c);
144 a596b957 2022-07-14 tracey return 0;
145 a596b957 2022-07-14 tracey default:
146 a596b957 2022-07-14 tracey log_warn("unimplemented type %d", h->type);
147 a596b957 2022-07-14 tracey break;
148 a596b957 2022-07-14 tracey }
149 a596b957 2022-07-14 tracey
150 a596b957 2022-07-14 tracey return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
151 a596b957 2022-07-14 tracey + h->padding_len);
152 a596b957 2022-07-14 tracey }
153 a596b957 2022-07-14 tracey
154 a596b957 2022-07-14 tracey void
155 a596b957 2022-07-14 tracey fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
156 a596b957 2022-07-14 tracey struct request *c, uint16_t id)
157 a596b957 2022-07-14 tracey {
158 a596b957 2022-07-14 tracey /* XXX -- FCGI_CANT_MPX_CONN */
159 a596b957 2022-07-14 tracey if (c->request_started) {
160 a596b957 2022-07-14 tracey log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
161 a596b957 2022-07-14 tracey return;
162 a596b957 2022-07-14 tracey }
163 a596b957 2022-07-14 tracey
164 a596b957 2022-07-14 tracey if (n != sizeof(struct fcgi_begin_request_body)) {
165 a596b957 2022-07-14 tracey log_warn("wrong size %d != %lu", n,
166 a596b957 2022-07-14 tracey sizeof(struct fcgi_begin_request_body));
167 a596b957 2022-07-14 tracey return;
168 a596b957 2022-07-14 tracey }
169 a596b957 2022-07-14 tracey
170 a596b957 2022-07-14 tracey c->request_started = 1;
171 a596b957 2022-07-14 tracey c->id = id;
172 a596b957 2022-07-14 tracey }
173 a596b957 2022-07-14 tracey
174 a596b957 2022-07-14 tracey void
175 a596b957 2022-07-14 tracey fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
176 a596b957 2022-07-14 tracey {
177 a596b957 2022-07-14 tracey uint32_t name_len, val_len;
178 c8af7691 2023-06-22 op uint8_t *val;
179 a596b957 2022-07-14 tracey
180 a596b957 2022-07-14 tracey if (!c->request_started) {
181 a596b957 2022-07-14 tracey log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
182 a596b957 2022-07-14 tracey return;
183 a596b957 2022-07-14 tracey }
184 a596b957 2022-07-14 tracey
185 a596b957 2022-07-14 tracey if (c->id != id) {
186 a596b957 2022-07-14 tracey log_warn("unexpected id, ignoring");
187 a596b957 2022-07-14 tracey return;
188 a596b957 2022-07-14 tracey }
189 a596b957 2022-07-14 tracey
190 a596b957 2022-07-14 tracey if (n == 0) {
191 a596b957 2022-07-14 tracey gotweb_process_request(c);
192 62eab86e 2023-09-13 op template_flush(c->tp);
193 a596b957 2022-07-14 tracey return;
194 a596b957 2022-07-14 tracey }
195 a596b957 2022-07-14 tracey
196 a596b957 2022-07-14 tracey while (n > 0) {
197 a596b957 2022-07-14 tracey if (buf[0] >> 7 == 0) {
198 a596b957 2022-07-14 tracey name_len = buf[0];
199 a596b957 2022-07-14 tracey n--;
200 a596b957 2022-07-14 tracey buf++;
201 a596b957 2022-07-14 tracey } else {
202 a596b957 2022-07-14 tracey if (n > 3) {
203 a596b957 2022-07-14 tracey name_len = ((buf[0] & 0x7f) << 24) +
204 a596b957 2022-07-14 tracey (buf[1] << 16) + (buf[2] << 8) + buf[3];
205 a596b957 2022-07-14 tracey n -= 4;
206 a596b957 2022-07-14 tracey buf += 4;
207 a596b957 2022-07-14 tracey } else
208 a596b957 2022-07-14 tracey return;
209 a596b957 2022-07-14 tracey }
210 a596b957 2022-07-14 tracey
211 40a95f4f 2022-09-01 op if (n == 0)
212 a596b957 2022-07-14 tracey return;
213 a596b957 2022-07-14 tracey
214 40a95f4f 2022-09-01 op if (buf[0] >> 7 == 0) {
215 40a95f4f 2022-09-01 op val_len = buf[0];
216 40a95f4f 2022-09-01 op n--;
217 40a95f4f 2022-09-01 op buf++;
218 40a95f4f 2022-09-01 op } else {
219 40a95f4f 2022-09-01 op if (n > 3) {
220 40a95f4f 2022-09-01 op val_len = ((buf[0] & 0x7f) << 24) +
221 40a95f4f 2022-09-01 op (buf[1] << 16) + (buf[2] << 8) +
222 40a95f4f 2022-09-01 op buf[3];
223 40a95f4f 2022-09-01 op n -= 4;
224 40a95f4f 2022-09-01 op buf += 4;
225 40a95f4f 2022-09-01 op } else
226 40a95f4f 2022-09-01 op return;
227 a596b957 2022-07-14 tracey }
228 a596b957 2022-07-14 tracey
229 40a95f4f 2022-09-01 op if (n < name_len + val_len)
230 a596b957 2022-07-14 tracey return;
231 a596b957 2022-07-14 tracey
232 40a95f4f 2022-09-01 op val = buf + name_len;
233 a596b957 2022-07-14 tracey
234 40a95f4f 2022-09-01 op if (c->querystring[0] == '\0' &&
235 40a95f4f 2022-09-01 op val_len < MAX_QUERYSTRING &&
236 40a95f4f 2022-09-01 op name_len == 12 &&
237 40a95f4f 2022-09-01 op strncmp(buf, "QUERY_STRING", 12) == 0) {
238 40a95f4f 2022-09-01 op memcpy(c->querystring, val, val_len);
239 a596b957 2022-07-14 tracey c->querystring[val_len] = '\0';
240 a596b957 2022-07-14 tracey }
241 a596b957 2022-07-14 tracey
242 d19d9fce 2022-12-20 op if (c->document_uri[0] == '\0' &&
243 d19d9fce 2022-12-20 op val_len < MAX_DOCUMENT_URI &&
244 d19d9fce 2022-12-20 op name_len == 12 &&
245 d19d9fce 2022-12-20 op strncmp(buf, "DOCUMENT_URI", 12) == 0) {
246 d19d9fce 2022-12-20 op memcpy(c->document_uri, val, val_len);
247 d19d9fce 2022-12-20 op c->document_uri[val_len] = '\0';
248 a596b957 2022-07-14 tracey }
249 40a95f4f 2022-09-01 op
250 40a95f4f 2022-09-01 op if (c->server_name[0] == '\0' &&
251 40a95f4f 2022-09-01 op val_len < MAX_SERVER_NAME &&
252 40a95f4f 2022-09-01 op name_len == 11 &&
253 40a95f4f 2022-09-01 op strncmp(buf, "SERVER_NAME", 11) == 0) {
254 40a95f4f 2022-09-01 op memcpy(c->server_name, val, val_len);
255 a596b957 2022-07-14 tracey c->server_name[val_len] = '\0';
256 a596b957 2022-07-14 tracey }
257 1abb18e1 2022-12-20 op
258 1abb18e1 2022-12-20 op if (name_len == 5 &&
259 1abb18e1 2022-12-20 op strncmp(buf, "HTTPS", 5) == 0)
260 1abb18e1 2022-12-20 op c->https = 1;
261 a596b957 2022-07-14 tracey
262 40a95f4f 2022-09-01 op buf += name_len + val_len;
263 40a95f4f 2022-09-01 op n -= name_len - val_len;
264 a596b957 2022-07-14 tracey }
265 a596b957 2022-07-14 tracey }
266 a596b957 2022-07-14 tracey
267 a596b957 2022-07-14 tracey void
268 a596b957 2022-07-14 tracey fcgi_timeout(int fd, short events, void *arg)
269 a596b957 2022-07-14 tracey {
270 a596b957 2022-07-14 tracey fcgi_cleanup_request((struct request*) arg);
271 ed619ca0 2022-12-14 op }
272 ed619ca0 2022-12-14 op
273 3ff00ead 2022-08-09 op static int
274 311b7e33 2022-08-01 op send_response(struct request *c, int type, const uint8_t *data,
275 311b7e33 2022-08-01 op size_t len)
276 a596b957 2022-07-14 tracey {
277 311b7e33 2022-08-01 op static const uint8_t padding[FCGI_PADDING_SIZE];
278 311b7e33 2022-08-01 op struct fcgi_record_header header;
279 311b7e33 2022-08-01 op struct iovec iov[3];
280 a596b957 2022-07-14 tracey struct timespec ts;
281 cb8b8986 2022-07-28 op ssize_t nw;
282 311b7e33 2022-08-01 op size_t padded_len, tot;
283 311b7e33 2022-08-01 op int i, err = 0, th = 2000;
284 a596b957 2022-07-14 tracey
285 a596b957 2022-07-14 tracey ts.tv_sec = 0;
286 a596b957 2022-07-14 tracey ts.tv_nsec = 50;
287 a596b957 2022-07-14 tracey
288 311b7e33 2022-08-01 op memset(&header, 0, sizeof(header));
289 311b7e33 2022-08-01 op header.version = 1;
290 311b7e33 2022-08-01 op header.type = type;
291 311b7e33 2022-08-01 op header.id = htons(c->id);
292 311b7e33 2022-08-01 op header.content_len = htons(len);
293 a596b957 2022-07-14 tracey
294 a596b957 2022-07-14 tracey /* The FastCGI spec suggests to align the output buffer */
295 311b7e33 2022-08-01 op tot = sizeof(header) + len;
296 311b7e33 2022-08-01 op padded_len = FCGI_ALIGN(tot);
297 311b7e33 2022-08-01 op if (padded_len > tot) {
298 311b7e33 2022-08-01 op header.padding_len = padded_len - tot;
299 311b7e33 2022-08-01 op tot += header.padding_len;
300 a596b957 2022-07-14 tracey }
301 a596b957 2022-07-14 tracey
302 311b7e33 2022-08-01 op iov[0].iov_base = &header;
303 311b7e33 2022-08-01 op iov[0].iov_len = sizeof(header);
304 a596b957 2022-07-14 tracey
305 311b7e33 2022-08-01 op iov[1].iov_base = (void *)data;
306 311b7e33 2022-08-01 op iov[1].iov_len = len;
307 311b7e33 2022-08-01 op
308 311b7e33 2022-08-01 op iov[2].iov_base = (void *)padding;
309 311b7e33 2022-08-01 op iov[2].iov_len = header.padding_len;
310 311b7e33 2022-08-01 op
311 311b7e33 2022-08-01 op dump_fcgi_record("resp ", &header);
312 311b7e33 2022-08-01 op
313 a596b957 2022-07-14 tracey /*
314 a596b957 2022-07-14 tracey * XXX: add some simple write heuristics here
315 a596b957 2022-07-14 tracey * On slower VMs, spotty connections, etc., we don't want to go right to
316 a596b957 2022-07-14 tracey * disconnect. Let's at least try to write the data a few times before
317 a596b957 2022-07-14 tracey * giving up.
318 a596b957 2022-07-14 tracey */
319 311b7e33 2022-08-01 op while (tot > 0) {
320 311b7e33 2022-08-01 op nw = writev(c->fd, iov, nitems(iov));
321 cb8b8986 2022-07-28 op if (nw == 0) {
322 cb8b8986 2022-07-28 op c->sock->client_status = CLIENT_DISCONNECT;
323 cb8b8986 2022-07-28 op break;
324 cb8b8986 2022-07-28 op }
325 cb8b8986 2022-07-28 op if (nw == -1) {
326 cb8b8986 2022-07-28 op err++;
327 cb8b8986 2022-07-28 op if (errno == EAGAIN && err < th) {
328 cb8b8986 2022-07-28 op nanosleep(&ts, NULL);
329 cb8b8986 2022-07-28 op continue;
330 cb8b8986 2022-07-28 op }
331 8a078d7f 2023-05-17 op log_debug("%s: write failure: %s", __func__,
332 8a078d7f 2023-05-17 op strerror(errno));
333 a596b957 2022-07-14 tracey c->sock->client_status = CLIENT_DISCONNECT;
334 3ff00ead 2022-08-09 op return -1;
335 a596b957 2022-07-14 tracey }
336 cb8b8986 2022-07-28 op
337 311b7e33 2022-08-01 op if (nw != tot)
338 311b7e33 2022-08-01 op log_debug("%s: partial write: %zu vs %zu", __func__,
339 311b7e33 2022-08-01 op nw, tot);
340 311b7e33 2022-08-01 op
341 311b7e33 2022-08-01 op tot -= nw;
342 311b7e33 2022-08-01 op for (i = 0; i < nitems(iov); ++i) {
343 311b7e33 2022-08-01 op if (nw < iov[i].iov_len) {
344 311b7e33 2022-08-01 op iov[i].iov_base += nw;
345 311b7e33 2022-08-01 op iov[i].iov_len -= nw;
346 311b7e33 2022-08-01 op break;
347 311b7e33 2022-08-01 op }
348 311b7e33 2022-08-01 op nw -= iov[i].iov_len;
349 311b7e33 2022-08-01 op iov[i].iov_len = 0;
350 311b7e33 2022-08-01 op }
351 a596b957 2022-07-14 tracey }
352 3ff00ead 2022-08-09 op
353 3ff00ead 2022-08-09 op return 0;
354 311b7e33 2022-08-01 op }
355 a596b957 2022-07-14 tracey
356 3ff00ead 2022-08-09 op int
357 311b7e33 2022-08-01 op fcgi_send_response(struct request *c, int type, const void *data,
358 311b7e33 2022-08-01 op size_t len)
359 311b7e33 2022-08-01 op {
360 3ff00ead 2022-08-09 op if (c->sock->client_status == CLIENT_DISCONNECT)
361 3ff00ead 2022-08-09 op return -1;
362 3ff00ead 2022-08-09 op
363 311b7e33 2022-08-01 op while (len > FCGI_CONTENT_SIZE) {
364 3ff00ead 2022-08-09 op if (send_response(c, type, data, len) == -1)
365 3ff00ead 2022-08-09 op return -1;
366 311b7e33 2022-08-01 op
367 311b7e33 2022-08-01 op data += FCGI_CONTENT_SIZE;
368 311b7e33 2022-08-01 op len -= FCGI_CONTENT_SIZE;
369 311b7e33 2022-08-01 op }
370 311b7e33 2022-08-01 op
371 311b7e33 2022-08-01 op if (len == 0)
372 3ff00ead 2022-08-09 op return 0;
373 311b7e33 2022-08-01 op
374 3ff00ead 2022-08-09 op return send_response(c, type, data, len);
375 a596b957 2022-07-14 tracey }
376 a596b957 2022-07-14 tracey
377 62eab86e 2023-09-13 op int
378 62eab86e 2023-09-13 op fcgi_write(void *arg, const void *buf, size_t len)
379 62eab86e 2023-09-13 op {
380 62eab86e 2023-09-13 op struct request *c = arg;
381 62eab86e 2023-09-13 op
382 62eab86e 2023-09-13 op return fcgi_send_response(c, FCGI_STDOUT, buf, len);
383 62eab86e 2023-09-13 op }
384 62eab86e 2023-09-13 op
385 a596b957 2022-07-14 tracey void
386 a596b957 2022-07-14 tracey fcgi_create_end_record(struct request *c)
387 a596b957 2022-07-14 tracey {
388 311b7e33 2022-08-01 op struct fcgi_end_request_body end_request;
389 a596b957 2022-07-14 tracey
390 311b7e33 2022-08-01 op memset(&end_request, 0, sizeof(end_request));
391 311b7e33 2022-08-01 op end_request.app_status = htonl(0); /* script status */
392 311b7e33 2022-08-01 op end_request.protocol_status = FCGI_REQUEST_COMPLETE;
393 311b7e33 2022-08-01 op
394 311b7e33 2022-08-01 op fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
395 311b7e33 2022-08-01 op sizeof(end_request));
396 a596b957 2022-07-14 tracey }
397 a596b957 2022-07-14 tracey
398 a596b957 2022-07-14 tracey void
399 a596b957 2022-07-14 tracey fcgi_cleanup_request(struct request *c)
400 a596b957 2022-07-14 tracey {
401 a596b957 2022-07-14 tracey cgi_inflight--;
402 a596b957 2022-07-14 tracey client_cnt--;
403 a596b957 2022-07-14 tracey
404 a596b957 2022-07-14 tracey evtimer_del(&c->tmo);
405 a596b957 2022-07-14 tracey if (event_initialized(&c->ev))
406 a596b957 2022-07-14 tracey event_del(&c->ev);
407 a596b957 2022-07-14 tracey
408 a596b957 2022-07-14 tracey close(c->fd);
409 ed619ca0 2022-12-14 op template_free(c->tp);
410 5add7f42 2023-03-10 op if (c->t != NULL)
411 5add7f42 2023-03-10 op gotweb_free_transport(c->t);
412 a596b957 2022-07-14 tracey free(c);
413 a596b957 2022-07-14 tracey }
414 a596b957 2022-07-14 tracey
415 a596b957 2022-07-14 tracey void
416 a596b957 2022-07-14 tracey dump_fcgi_record(const char *p, struct fcgi_record_header *h)
417 a596b957 2022-07-14 tracey {
418 a596b957 2022-07-14 tracey dump_fcgi_record_header(p, h);
419 a596b957 2022-07-14 tracey
420 a596b957 2022-07-14 tracey if (h->type == FCGI_BEGIN_REQUEST)
421 a596b957 2022-07-14 tracey dump_fcgi_begin_request_body(p,
422 a596b957 2022-07-14 tracey (struct fcgi_begin_request_body *)(h + 1));
423 a596b957 2022-07-14 tracey else if (h->type == FCGI_END_REQUEST)
424 a596b957 2022-07-14 tracey dump_fcgi_end_request_body(p,
425 a596b957 2022-07-14 tracey (struct fcgi_end_request_body *)(h + 1));
426 a596b957 2022-07-14 tracey }
427 a596b957 2022-07-14 tracey
428 a596b957 2022-07-14 tracey void
429 a596b957 2022-07-14 tracey dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
430 a596b957 2022-07-14 tracey {
431 a596b957 2022-07-14 tracey log_debug("%sversion: %d", p, h->version);
432 a596b957 2022-07-14 tracey log_debug("%stype: %d", p, h->type);
433 a596b957 2022-07-14 tracey log_debug("%srequestId: %d", p, ntohs(h->id));
434 a596b957 2022-07-14 tracey log_debug("%scontentLength: %d", p, ntohs(h->content_len));
435 a596b957 2022-07-14 tracey log_debug("%spaddingLength: %d", p, h->padding_len);
436 a596b957 2022-07-14 tracey log_debug("%sreserved: %d", p, h->reserved);
437 a596b957 2022-07-14 tracey }
438 a596b957 2022-07-14 tracey
439 a596b957 2022-07-14 tracey void
440 a596b957 2022-07-14 tracey dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
441 a596b957 2022-07-14 tracey {
442 a596b957 2022-07-14 tracey log_debug("%srole %d", p, ntohs(b->role));
443 a596b957 2022-07-14 tracey log_debug("%sflags %d", p, b->flags);
444 a596b957 2022-07-14 tracey }
445 a596b957 2022-07-14 tracey
446 a596b957 2022-07-14 tracey void
447 a596b957 2022-07-14 tracey dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
448 a596b957 2022-07-14 tracey {
449 a596b957 2022-07-14 tracey log_debug("%sappStatus: %d", p, ntohl(b->app_status));
450 a596b957 2022-07-14 tracey log_debug("%sprotocolStatus: %d", p, b->protocol_status);
451 a596b957 2022-07-14 tracey }