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 "proc.h"
39 a596b957 2022-07-14 tracey #include "gotwebd.h"
40 ed619ca0 2022-12-14 op #include "tmpl.h"
41 a596b957 2022-07-14 tracey
42 a596b957 2022-07-14 tracey size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
43 a596b957 2022-07-14 tracey void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
44 a596b957 2022-07-14 tracey uint16_t);
45 a596b957 2022-07-14 tracey void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
46 3ff00ead 2022-08-09 op int fcgi_send_response(struct request *, int, const void *, size_t);
47 a596b957 2022-07-14 tracey
48 a596b957 2022-07-14 tracey void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
49 a596b957 2022-07-14 tracey void dump_fcgi_begin_request_body(const char *,
50 a596b957 2022-07-14 tracey struct fcgi_begin_request_body *);
51 a596b957 2022-07-14 tracey void dump_fcgi_end_request_body(const char *,
52 a596b957 2022-07-14 tracey struct fcgi_end_request_body *);
53 a596b957 2022-07-14 tracey
54 a596b957 2022-07-14 tracey extern int cgi_inflight;
55 a596b957 2022-07-14 tracey extern volatile int client_cnt;
56 a596b957 2022-07-14 tracey
57 a596b957 2022-07-14 tracey void
58 a596b957 2022-07-14 tracey fcgi_request(int fd, short events, void *arg)
59 a596b957 2022-07-14 tracey {
60 a596b957 2022-07-14 tracey struct request *c = arg;
61 a596b957 2022-07-14 tracey ssize_t n;
62 a596b957 2022-07-14 tracey size_t parsed = 0;
63 a596b957 2022-07-14 tracey
64 a596b957 2022-07-14 tracey n = read(fd, c->buf + c->buf_pos + c->buf_len,
65 a596b957 2022-07-14 tracey FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
66 a596b957 2022-07-14 tracey
67 a596b957 2022-07-14 tracey switch (n) {
68 a596b957 2022-07-14 tracey case -1:
69 a596b957 2022-07-14 tracey switch (errno) {
70 a596b957 2022-07-14 tracey case EINTR:
71 a596b957 2022-07-14 tracey case EAGAIN:
72 a596b957 2022-07-14 tracey return;
73 a596b957 2022-07-14 tracey default:
74 a596b957 2022-07-14 tracey goto fail;
75 a596b957 2022-07-14 tracey }
76 a596b957 2022-07-14 tracey break;
77 a596b957 2022-07-14 tracey
78 a596b957 2022-07-14 tracey case 0:
79 a596b957 2022-07-14 tracey log_debug("closed connection");
80 a596b957 2022-07-14 tracey goto fail;
81 a596b957 2022-07-14 tracey default:
82 a596b957 2022-07-14 tracey break;
83 a596b957 2022-07-14 tracey }
84 a596b957 2022-07-14 tracey
85 a596b957 2022-07-14 tracey c->buf_len += n;
86 a596b957 2022-07-14 tracey
87 a596b957 2022-07-14 tracey /*
88 a596b957 2022-07-14 tracey * Parse the records as they are received. Per the FastCGI
89 a596b957 2022-07-14 tracey * specification, the server need only receive the FastCGI
90 a596b957 2022-07-14 tracey * parameter records in full; it is free to begin execution
91 a596b957 2022-07-14 tracey * at that point, which is what happens here.
92 a596b957 2022-07-14 tracey */
93 a596b957 2022-07-14 tracey do {
94 a596b957 2022-07-14 tracey parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
95 a596b957 2022-07-14 tracey if (parsed != 0) {
96 a596b957 2022-07-14 tracey c->buf_pos += parsed;
97 a596b957 2022-07-14 tracey c->buf_len -= parsed;
98 a596b957 2022-07-14 tracey }
99 a596b957 2022-07-14 tracey
100 5add7f42 2023-03-10 op /* drop the parsed record */
101 5add7f42 2023-03-10 op if (parsed != 0 && c->buf_len > 0) {
102 a596b957 2022-07-14 tracey bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
103 a596b957 2022-07-14 tracey c->buf_pos = 0;
104 a596b957 2022-07-14 tracey }
105 5add7f42 2023-03-10 op } while (parsed > 0 && c->buf_len > 0);
106 5add7f42 2023-03-10 op
107 a596b957 2022-07-14 tracey return;
108 a596b957 2022-07-14 tracey fail:
109 a596b957 2022-07-14 tracey fcgi_cleanup_request(c);
110 a596b957 2022-07-14 tracey }
111 a596b957 2022-07-14 tracey
112 a596b957 2022-07-14 tracey size_t
113 a596b957 2022-07-14 tracey fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
114 a596b957 2022-07-14 tracey {
115 a596b957 2022-07-14 tracey struct fcgi_record_header *h;
116 a596b957 2022-07-14 tracey
117 a596b957 2022-07-14 tracey if (n < sizeof(struct fcgi_record_header))
118 a596b957 2022-07-14 tracey return 0;
119 a596b957 2022-07-14 tracey
120 a596b957 2022-07-14 tracey h = (struct fcgi_record_header*) buf;
121 a596b957 2022-07-14 tracey
122 a596b957 2022-07-14 tracey dump_fcgi_record("", h);
123 a596b957 2022-07-14 tracey
124 a596b957 2022-07-14 tracey if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
125 a596b957 2022-07-14 tracey + h->padding_len)
126 a596b957 2022-07-14 tracey return 0;
127 a596b957 2022-07-14 tracey
128 a596b957 2022-07-14 tracey if (h->version != 1)
129 a596b957 2022-07-14 tracey log_warn("wrong version");
130 a596b957 2022-07-14 tracey
131 a596b957 2022-07-14 tracey switch (h->type) {
132 a596b957 2022-07-14 tracey case FCGI_BEGIN_REQUEST:
133 a596b957 2022-07-14 tracey fcgi_parse_begin_request(buf +
134 a596b957 2022-07-14 tracey sizeof(struct fcgi_record_header),
135 a596b957 2022-07-14 tracey ntohs(h->content_len), c, ntohs(h->id));
136 a596b957 2022-07-14 tracey break;
137 a596b957 2022-07-14 tracey case FCGI_PARAMS:
138 a596b957 2022-07-14 tracey fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
139 a596b957 2022-07-14 tracey ntohs(h->content_len), c, ntohs(h->id));
140 a596b957 2022-07-14 tracey break;
141 a596b957 2022-07-14 tracey case FCGI_STDIN:
142 a596b957 2022-07-14 tracey case FCGI_ABORT_REQUEST:
143 3ff00ead 2022-08-09 op if (c->sock->client_status != CLIENT_DISCONNECT &&
144 3ff00ead 2022-08-09 op c->outbuf_len != 0) {
145 3ff00ead 2022-08-09 op fcgi_send_response(c, FCGI_STDOUT, c->outbuf,
146 3ff00ead 2022-08-09 op c->outbuf_len);
147 3ff00ead 2022-08-09 op }
148 3ff00ead 2022-08-09 op
149 a596b957 2022-07-14 tracey fcgi_create_end_record(c);
150 a596b957 2022-07-14 tracey fcgi_cleanup_request(c);
151 a596b957 2022-07-14 tracey return 0;
152 a596b957 2022-07-14 tracey default:
153 a596b957 2022-07-14 tracey log_warn("unimplemented type %d", h->type);
154 a596b957 2022-07-14 tracey break;
155 a596b957 2022-07-14 tracey }
156 a596b957 2022-07-14 tracey
157 a596b957 2022-07-14 tracey return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
158 a596b957 2022-07-14 tracey + h->padding_len);
159 a596b957 2022-07-14 tracey }
160 a596b957 2022-07-14 tracey
161 a596b957 2022-07-14 tracey void
162 a596b957 2022-07-14 tracey fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
163 a596b957 2022-07-14 tracey struct request *c, uint16_t id)
164 a596b957 2022-07-14 tracey {
165 a596b957 2022-07-14 tracey /* XXX -- FCGI_CANT_MPX_CONN */
166 a596b957 2022-07-14 tracey if (c->request_started) {
167 a596b957 2022-07-14 tracey log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
168 a596b957 2022-07-14 tracey return;
169 a596b957 2022-07-14 tracey }
170 a596b957 2022-07-14 tracey
171 a596b957 2022-07-14 tracey if (n != sizeof(struct fcgi_begin_request_body)) {
172 a596b957 2022-07-14 tracey log_warn("wrong size %d != %lu", n,
173 a596b957 2022-07-14 tracey sizeof(struct fcgi_begin_request_body));
174 a596b957 2022-07-14 tracey return;
175 a596b957 2022-07-14 tracey }
176 a596b957 2022-07-14 tracey
177 a596b957 2022-07-14 tracey c->request_started = 1;
178 a596b957 2022-07-14 tracey c->id = id;
179 a596b957 2022-07-14 tracey }
180 a596b957 2022-07-14 tracey
181 a596b957 2022-07-14 tracey void
182 a596b957 2022-07-14 tracey fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
183 a596b957 2022-07-14 tracey {
184 a596b957 2022-07-14 tracey uint32_t name_len, val_len;
185 c8af7691 2023-06-22 op uint8_t *val;
186 a596b957 2022-07-14 tracey
187 a596b957 2022-07-14 tracey if (!c->request_started) {
188 a596b957 2022-07-14 tracey log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
189 a596b957 2022-07-14 tracey return;
190 a596b957 2022-07-14 tracey }
191 a596b957 2022-07-14 tracey
192 a596b957 2022-07-14 tracey if (c->id != id) {
193 a596b957 2022-07-14 tracey log_warn("unexpected id, ignoring");
194 a596b957 2022-07-14 tracey return;
195 a596b957 2022-07-14 tracey }
196 a596b957 2022-07-14 tracey
197 a596b957 2022-07-14 tracey if (n == 0) {
198 a596b957 2022-07-14 tracey gotweb_process_request(c);
199 a596b957 2022-07-14 tracey return;
200 a596b957 2022-07-14 tracey }
201 a596b957 2022-07-14 tracey
202 a596b957 2022-07-14 tracey while (n > 0) {
203 a596b957 2022-07-14 tracey if (buf[0] >> 7 == 0) {
204 a596b957 2022-07-14 tracey name_len = buf[0];
205 a596b957 2022-07-14 tracey n--;
206 a596b957 2022-07-14 tracey buf++;
207 a596b957 2022-07-14 tracey } else {
208 a596b957 2022-07-14 tracey if (n > 3) {
209 a596b957 2022-07-14 tracey name_len = ((buf[0] & 0x7f) << 24) +
210 a596b957 2022-07-14 tracey (buf[1] << 16) + (buf[2] << 8) + buf[3];
211 a596b957 2022-07-14 tracey n -= 4;
212 a596b957 2022-07-14 tracey buf += 4;
213 a596b957 2022-07-14 tracey } else
214 a596b957 2022-07-14 tracey return;
215 a596b957 2022-07-14 tracey }
216 a596b957 2022-07-14 tracey
217 40a95f4f 2022-09-01 op if (n == 0)
218 a596b957 2022-07-14 tracey return;
219 a596b957 2022-07-14 tracey
220 40a95f4f 2022-09-01 op if (buf[0] >> 7 == 0) {
221 40a95f4f 2022-09-01 op val_len = buf[0];
222 40a95f4f 2022-09-01 op n--;
223 40a95f4f 2022-09-01 op buf++;
224 40a95f4f 2022-09-01 op } else {
225 40a95f4f 2022-09-01 op if (n > 3) {
226 40a95f4f 2022-09-01 op val_len = ((buf[0] & 0x7f) << 24) +
227 40a95f4f 2022-09-01 op (buf[1] << 16) + (buf[2] << 8) +
228 40a95f4f 2022-09-01 op buf[3];
229 40a95f4f 2022-09-01 op n -= 4;
230 40a95f4f 2022-09-01 op buf += 4;
231 40a95f4f 2022-09-01 op } else
232 40a95f4f 2022-09-01 op return;
233 a596b957 2022-07-14 tracey }
234 a596b957 2022-07-14 tracey
235 40a95f4f 2022-09-01 op if (n < name_len + val_len)
236 a596b957 2022-07-14 tracey return;
237 a596b957 2022-07-14 tracey
238 40a95f4f 2022-09-01 op val = buf + name_len;
239 a596b957 2022-07-14 tracey
240 40a95f4f 2022-09-01 op if (c->querystring[0] == '\0' &&
241 40a95f4f 2022-09-01 op val_len < MAX_QUERYSTRING &&
242 40a95f4f 2022-09-01 op name_len == 12 &&
243 40a95f4f 2022-09-01 op strncmp(buf, "QUERY_STRING", 12) == 0) {
244 40a95f4f 2022-09-01 op memcpy(c->querystring, val, val_len);
245 a596b957 2022-07-14 tracey c->querystring[val_len] = '\0';
246 a596b957 2022-07-14 tracey }
247 a596b957 2022-07-14 tracey
248 d19d9fce 2022-12-20 op if (c->document_uri[0] == '\0' &&
249 d19d9fce 2022-12-20 op val_len < MAX_DOCUMENT_URI &&
250 d19d9fce 2022-12-20 op name_len == 12 &&
251 d19d9fce 2022-12-20 op strncmp(buf, "DOCUMENT_URI", 12) == 0) {
252 d19d9fce 2022-12-20 op memcpy(c->document_uri, val, val_len);
253 d19d9fce 2022-12-20 op c->document_uri[val_len] = '\0';
254 a596b957 2022-07-14 tracey }
255 40a95f4f 2022-09-01 op
256 40a95f4f 2022-09-01 op if (c->server_name[0] == '\0' &&
257 40a95f4f 2022-09-01 op val_len < MAX_SERVER_NAME &&
258 40a95f4f 2022-09-01 op name_len == 11 &&
259 40a95f4f 2022-09-01 op strncmp(buf, "SERVER_NAME", 11) == 0) {
260 40a95f4f 2022-09-01 op memcpy(c->server_name, val, val_len);
261 a596b957 2022-07-14 tracey c->server_name[val_len] = '\0';
262 a596b957 2022-07-14 tracey }
263 1abb18e1 2022-12-20 op
264 1abb18e1 2022-12-20 op if (name_len == 5 &&
265 1abb18e1 2022-12-20 op strncmp(buf, "HTTPS", 5) == 0)
266 1abb18e1 2022-12-20 op c->https = 1;
267 a596b957 2022-07-14 tracey
268 40a95f4f 2022-09-01 op buf += name_len + val_len;
269 40a95f4f 2022-09-01 op n -= name_len - val_len;
270 a596b957 2022-07-14 tracey }
271 a596b957 2022-07-14 tracey }
272 a596b957 2022-07-14 tracey
273 a596b957 2022-07-14 tracey void
274 a596b957 2022-07-14 tracey fcgi_timeout(int fd, short events, void *arg)
275 a596b957 2022-07-14 tracey {
276 a596b957 2022-07-14 tracey fcgi_cleanup_request((struct request*) arg);
277 01498c42 2022-08-19 op }
278 01498c42 2022-08-19 op
279 01498c42 2022-08-19 op int
280 ed619ca0 2022-12-14 op fcgi_puts(struct template *tp, const char *str)
281 ed619ca0 2022-12-14 op {
282 ed619ca0 2022-12-14 op if (str == NULL)
283 ed619ca0 2022-12-14 op return 0;
284 ed619ca0 2022-12-14 op return fcgi_gen_binary_response(tp->tp_arg, str, strlen(str));
285 ed619ca0 2022-12-14 op }
286 ed619ca0 2022-12-14 op
287 ed619ca0 2022-12-14 op int
288 ed619ca0 2022-12-14 op fcgi_putc(struct template *tp, int ch)
289 ed619ca0 2022-12-14 op {
290 ed619ca0 2022-12-14 op uint8_t c = ch;
291 ed619ca0 2022-12-14 op return fcgi_gen_binary_response(tp->tp_arg, &c, 1);
292 ed619ca0 2022-12-14 op }
293 ed619ca0 2022-12-14 op
294 ed619ca0 2022-12-14 op int
295 8d02314f 2022-09-07 op fcgi_vprintf(struct request *c, const char *fmt, va_list ap)
296 01498c42 2022-08-19 op {
297 01498c42 2022-08-19 op char *str;
298 01498c42 2022-08-19 op int r;
299 01498c42 2022-08-19 op
300 01498c42 2022-08-19 op r = vasprintf(&str, fmt, ap);
301 01498c42 2022-08-19 op if (r == -1) {
302 01498c42 2022-08-19 op log_warn("%s: asprintf", __func__);
303 01498c42 2022-08-19 op return -1;
304 01498c42 2022-08-19 op }
305 01498c42 2022-08-19 op
306 01498c42 2022-08-19 op r = fcgi_gen_binary_response(c, str, r);
307 01498c42 2022-08-19 op free(str);
308 01498c42 2022-08-19 op return r;
309 a596b957 2022-07-14 tracey }
310 a596b957 2022-07-14 tracey
311 a596b957 2022-07-14 tracey int
312 8d02314f 2022-09-07 op fcgi_printf(struct request *c, const char *fmt, ...)
313 8d02314f 2022-09-07 op {
314 8d02314f 2022-09-07 op va_list ap;
315 8d02314f 2022-09-07 op int r;
316 8d02314f 2022-09-07 op
317 8d02314f 2022-09-07 op va_start(ap, fmt);
318 8d02314f 2022-09-07 op r = fcgi_vprintf(c, fmt, ap);
319 8d02314f 2022-09-07 op va_end(ap);
320 8d02314f 2022-09-07 op
321 8d02314f 2022-09-07 op return r;
322 8d02314f 2022-09-07 op }
323 8d02314f 2022-09-07 op
324 8d02314f 2022-09-07 op int
325 a596b957 2022-07-14 tracey fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
326 a596b957 2022-07-14 tracey {
327 3ff00ead 2022-08-09 op int r;
328 3ff00ead 2022-08-09 op
329 a596b957 2022-07-14 tracey if (c->sock->client_status == CLIENT_DISCONNECT)
330 a596b957 2022-07-14 tracey return -1;
331 a596b957 2022-07-14 tracey
332 9dd0e5e9 2022-07-28 op if (data == NULL || len == 0)
333 a596b957 2022-07-14 tracey return 0;
334 a596b957 2022-07-14 tracey
335 3ff00ead 2022-08-09 op /*
336 3ff00ead 2022-08-09 op * special case: send big replies -like blobs- directly
337 3ff00ead 2022-08-09 op * without copying.
338 3ff00ead 2022-08-09 op */
339 3ff00ead 2022-08-09 op if (len > sizeof(c->outbuf)) {
340 3ff00ead 2022-08-09 op if (c->outbuf_len > 0) {
341 3ff00ead 2022-08-09 op fcgi_send_response(c, FCGI_STDOUT,
342 3ff00ead 2022-08-09 op c->outbuf, c->outbuf_len);
343 3ff00ead 2022-08-09 op c->outbuf_len = 0;
344 3ff00ead 2022-08-09 op }
345 3ff00ead 2022-08-09 op return fcgi_send_response(c, FCGI_STDOUT, data, len);
346 3ff00ead 2022-08-09 op }
347 3ff00ead 2022-08-09 op
348 3ff00ead 2022-08-09 op if (len < sizeof(c->outbuf) - c->outbuf_len) {
349 3ff00ead 2022-08-09 op memcpy(c->outbuf + c->outbuf_len, data, len);
350 3ff00ead 2022-08-09 op c->outbuf_len += len;
351 3ff00ead 2022-08-09 op return 0;
352 3ff00ead 2022-08-09 op }
353 3ff00ead 2022-08-09 op
354 3ff00ead 2022-08-09 op r = fcgi_send_response(c, FCGI_STDOUT, c->outbuf, c->outbuf_len);
355 3ff00ead 2022-08-09 op if (r == -1)
356 3ff00ead 2022-08-09 op return -1;
357 3ff00ead 2022-08-09 op
358 3ff00ead 2022-08-09 op memcpy(c->outbuf, data, len);
359 3ff00ead 2022-08-09 op c->outbuf_len = len;
360 a596b957 2022-07-14 tracey return 0;
361 a596b957 2022-07-14 tracey }
362 a596b957 2022-07-14 tracey
363 3ff00ead 2022-08-09 op static int
364 311b7e33 2022-08-01 op send_response(struct request *c, int type, const uint8_t *data,
365 311b7e33 2022-08-01 op size_t len)
366 a596b957 2022-07-14 tracey {
367 311b7e33 2022-08-01 op static const uint8_t padding[FCGI_PADDING_SIZE];
368 311b7e33 2022-08-01 op struct fcgi_record_header header;
369 311b7e33 2022-08-01 op struct iovec iov[3];
370 a596b957 2022-07-14 tracey struct timespec ts;
371 cb8b8986 2022-07-28 op ssize_t nw;
372 311b7e33 2022-08-01 op size_t padded_len, tot;
373 311b7e33 2022-08-01 op int i, err = 0, th = 2000;
374 a596b957 2022-07-14 tracey
375 a596b957 2022-07-14 tracey ts.tv_sec = 0;
376 a596b957 2022-07-14 tracey ts.tv_nsec = 50;
377 a596b957 2022-07-14 tracey
378 311b7e33 2022-08-01 op memset(&header, 0, sizeof(header));
379 311b7e33 2022-08-01 op header.version = 1;
380 311b7e33 2022-08-01 op header.type = type;
381 311b7e33 2022-08-01 op header.id = htons(c->id);
382 311b7e33 2022-08-01 op header.content_len = htons(len);
383 a596b957 2022-07-14 tracey
384 a596b957 2022-07-14 tracey /* The FastCGI spec suggests to align the output buffer */
385 311b7e33 2022-08-01 op tot = sizeof(header) + len;
386 311b7e33 2022-08-01 op padded_len = FCGI_ALIGN(tot);
387 311b7e33 2022-08-01 op if (padded_len > tot) {
388 311b7e33 2022-08-01 op header.padding_len = padded_len - tot;
389 311b7e33 2022-08-01 op tot += header.padding_len;
390 a596b957 2022-07-14 tracey }
391 a596b957 2022-07-14 tracey
392 311b7e33 2022-08-01 op iov[0].iov_base = &header;
393 311b7e33 2022-08-01 op iov[0].iov_len = sizeof(header);
394 a596b957 2022-07-14 tracey
395 311b7e33 2022-08-01 op iov[1].iov_base = (void *)data;
396 311b7e33 2022-08-01 op iov[1].iov_len = len;
397 311b7e33 2022-08-01 op
398 311b7e33 2022-08-01 op iov[2].iov_base = (void *)padding;
399 311b7e33 2022-08-01 op iov[2].iov_len = header.padding_len;
400 311b7e33 2022-08-01 op
401 311b7e33 2022-08-01 op dump_fcgi_record("resp ", &header);
402 311b7e33 2022-08-01 op
403 a596b957 2022-07-14 tracey /*
404 a596b957 2022-07-14 tracey * XXX: add some simple write heuristics here
405 a596b957 2022-07-14 tracey * On slower VMs, spotty connections, etc., we don't want to go right to
406 a596b957 2022-07-14 tracey * disconnect. Let's at least try to write the data a few times before
407 a596b957 2022-07-14 tracey * giving up.
408 a596b957 2022-07-14 tracey */
409 311b7e33 2022-08-01 op while (tot > 0) {
410 311b7e33 2022-08-01 op nw = writev(c->fd, iov, nitems(iov));
411 cb8b8986 2022-07-28 op if (nw == 0) {
412 cb8b8986 2022-07-28 op c->sock->client_status = CLIENT_DISCONNECT;
413 cb8b8986 2022-07-28 op break;
414 cb8b8986 2022-07-28 op }
415 cb8b8986 2022-07-28 op if (nw == -1) {
416 cb8b8986 2022-07-28 op err++;
417 cb8b8986 2022-07-28 op if (errno == EAGAIN && err < th) {
418 cb8b8986 2022-07-28 op nanosleep(&ts, NULL);
419 cb8b8986 2022-07-28 op continue;
420 cb8b8986 2022-07-28 op }
421 8a078d7f 2023-05-17 op log_debug("%s: write failure: %s", __func__,
422 8a078d7f 2023-05-17 op strerror(errno));
423 a596b957 2022-07-14 tracey c->sock->client_status = CLIENT_DISCONNECT;
424 3ff00ead 2022-08-09 op return -1;
425 a596b957 2022-07-14 tracey }
426 cb8b8986 2022-07-28 op
427 311b7e33 2022-08-01 op if (nw != tot)
428 311b7e33 2022-08-01 op log_debug("%s: partial write: %zu vs %zu", __func__,
429 311b7e33 2022-08-01 op nw, tot);
430 311b7e33 2022-08-01 op
431 311b7e33 2022-08-01 op tot -= nw;
432 311b7e33 2022-08-01 op for (i = 0; i < nitems(iov); ++i) {
433 311b7e33 2022-08-01 op if (nw < iov[i].iov_len) {
434 311b7e33 2022-08-01 op iov[i].iov_base += nw;
435 311b7e33 2022-08-01 op iov[i].iov_len -= nw;
436 311b7e33 2022-08-01 op break;
437 311b7e33 2022-08-01 op }
438 311b7e33 2022-08-01 op nw -= iov[i].iov_len;
439 311b7e33 2022-08-01 op iov[i].iov_len = 0;
440 311b7e33 2022-08-01 op }
441 a596b957 2022-07-14 tracey }
442 3ff00ead 2022-08-09 op
443 3ff00ead 2022-08-09 op return 0;
444 311b7e33 2022-08-01 op }
445 a596b957 2022-07-14 tracey
446 3ff00ead 2022-08-09 op int
447 311b7e33 2022-08-01 op fcgi_send_response(struct request *c, int type, const void *data,
448 311b7e33 2022-08-01 op size_t len)
449 311b7e33 2022-08-01 op {
450 3ff00ead 2022-08-09 op if (c->sock->client_status == CLIENT_DISCONNECT)
451 3ff00ead 2022-08-09 op return -1;
452 3ff00ead 2022-08-09 op
453 311b7e33 2022-08-01 op while (len > FCGI_CONTENT_SIZE) {
454 3ff00ead 2022-08-09 op if (send_response(c, type, data, len) == -1)
455 3ff00ead 2022-08-09 op return -1;
456 311b7e33 2022-08-01 op
457 311b7e33 2022-08-01 op data += FCGI_CONTENT_SIZE;
458 311b7e33 2022-08-01 op len -= FCGI_CONTENT_SIZE;
459 311b7e33 2022-08-01 op }
460 311b7e33 2022-08-01 op
461 311b7e33 2022-08-01 op if (len == 0)
462 3ff00ead 2022-08-09 op return 0;
463 311b7e33 2022-08-01 op
464 3ff00ead 2022-08-09 op return send_response(c, type, data, len);
465 a596b957 2022-07-14 tracey }
466 a596b957 2022-07-14 tracey
467 a596b957 2022-07-14 tracey void
468 a596b957 2022-07-14 tracey fcgi_create_end_record(struct request *c)
469 a596b957 2022-07-14 tracey {
470 311b7e33 2022-08-01 op struct fcgi_end_request_body end_request;
471 a596b957 2022-07-14 tracey
472 311b7e33 2022-08-01 op memset(&end_request, 0, sizeof(end_request));
473 311b7e33 2022-08-01 op end_request.app_status = htonl(0); /* script status */
474 311b7e33 2022-08-01 op end_request.protocol_status = FCGI_REQUEST_COMPLETE;
475 311b7e33 2022-08-01 op
476 311b7e33 2022-08-01 op fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
477 311b7e33 2022-08-01 op sizeof(end_request));
478 a596b957 2022-07-14 tracey }
479 a596b957 2022-07-14 tracey
480 a596b957 2022-07-14 tracey void
481 a596b957 2022-07-14 tracey fcgi_cleanup_request(struct request *c)
482 a596b957 2022-07-14 tracey {
483 a596b957 2022-07-14 tracey cgi_inflight--;
484 a596b957 2022-07-14 tracey client_cnt--;
485 a596b957 2022-07-14 tracey
486 a596b957 2022-07-14 tracey evtimer_del(&c->tmo);
487 a596b957 2022-07-14 tracey if (event_initialized(&c->ev))
488 a596b957 2022-07-14 tracey event_del(&c->ev);
489 a596b957 2022-07-14 tracey
490 a596b957 2022-07-14 tracey close(c->fd);
491 ed619ca0 2022-12-14 op template_free(c->tp);
492 5add7f42 2023-03-10 op if (c->t != NULL)
493 5add7f42 2023-03-10 op gotweb_free_transport(c->t);
494 a596b957 2022-07-14 tracey free(c);
495 a596b957 2022-07-14 tracey }
496 a596b957 2022-07-14 tracey
497 a596b957 2022-07-14 tracey void
498 a596b957 2022-07-14 tracey dump_fcgi_record(const char *p, struct fcgi_record_header *h)
499 a596b957 2022-07-14 tracey {
500 a596b957 2022-07-14 tracey dump_fcgi_record_header(p, h);
501 a596b957 2022-07-14 tracey
502 a596b957 2022-07-14 tracey if (h->type == FCGI_BEGIN_REQUEST)
503 a596b957 2022-07-14 tracey dump_fcgi_begin_request_body(p,
504 a596b957 2022-07-14 tracey (struct fcgi_begin_request_body *)(h + 1));
505 a596b957 2022-07-14 tracey else if (h->type == FCGI_END_REQUEST)
506 a596b957 2022-07-14 tracey dump_fcgi_end_request_body(p,
507 a596b957 2022-07-14 tracey (struct fcgi_end_request_body *)(h + 1));
508 a596b957 2022-07-14 tracey }
509 a596b957 2022-07-14 tracey
510 a596b957 2022-07-14 tracey void
511 a596b957 2022-07-14 tracey dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
512 a596b957 2022-07-14 tracey {
513 a596b957 2022-07-14 tracey log_debug("%sversion: %d", p, h->version);
514 a596b957 2022-07-14 tracey log_debug("%stype: %d", p, h->type);
515 a596b957 2022-07-14 tracey log_debug("%srequestId: %d", p, ntohs(h->id));
516 a596b957 2022-07-14 tracey log_debug("%scontentLength: %d", p, ntohs(h->content_len));
517 a596b957 2022-07-14 tracey log_debug("%spaddingLength: %d", p, h->padding_len);
518 a596b957 2022-07-14 tracey log_debug("%sreserved: %d", p, h->reserved);
519 a596b957 2022-07-14 tracey }
520 a596b957 2022-07-14 tracey
521 a596b957 2022-07-14 tracey void
522 a596b957 2022-07-14 tracey dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
523 a596b957 2022-07-14 tracey {
524 a596b957 2022-07-14 tracey log_debug("%srole %d", p, ntohs(b->role));
525 a596b957 2022-07-14 tracey log_debug("%sflags %d", p, b->flags);
526 a596b957 2022-07-14 tracey }
527 a596b957 2022-07-14 tracey
528 a596b957 2022-07-14 tracey void
529 a596b957 2022-07-14 tracey dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
530 a596b957 2022-07-14 tracey {
531 a596b957 2022-07-14 tracey log_debug("%sappStatus: %d", p, ntohl(b->app_status));
532 a596b957 2022-07-14 tracey log_debug("%sprotocolStatus: %d", p, b->protocol_status);
533 a596b957 2022-07-14 tracey }