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