Blame


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