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