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 8a35f56c 2022-07-16 thomas
19 8a35f56c 2022-07-16 thomas #include <arpa/inet.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
22 8a35f56c 2022-07-16 thomas #include <sys/types.h>
23 0335d24d 2022-08-06 thomas #include <sys/uio.h>
24 8a35f56c 2022-07-16 thomas
25 8a35f56c 2022-07-16 thomas #include <errno.h>
26 8a35f56c 2022-07-16 thomas #include <event.h>
27 79393471 2022-08-27 thomas #include <imsg.h>
28 79393471 2022-08-27 thomas #include <stdarg.h>
29 8a35f56c 2022-07-16 thomas #include <stdlib.h>
30 8a35f56c 2022-07-16 thomas #include <stdio.h>
31 8a35f56c 2022-07-16 thomas #include <string.h>
32 8a35f56c 2022-07-16 thomas #include <time.h>
33 8a35f56c 2022-07-16 thomas #include <unistd.h>
34 8a35f56c 2022-07-16 thomas
35 8a35f56c 2022-07-16 thomas #include "got_error.h"
36 ff36aeea 2022-07-16 thomas
37 ff36aeea 2022-07-16 thomas #include "got_compat.h"
38 8a35f56c 2022-07-16 thomas
39 8a35f56c 2022-07-16 thomas #include "proc.h"
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 } while (parsed > 0 && c->buf_len > 0);
101 8a35f56c 2022-07-16 thomas
102 8a35f56c 2022-07-16 thomas /* Make space for further reads */
103 8a35f56c 2022-07-16 thomas if (parsed != 0)
104 8a35f56c 2022-07-16 thomas if (c->buf_len > 0) {
105 8a35f56c 2022-07-16 thomas bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
106 8a35f56c 2022-07-16 thomas c->buf_pos = 0;
107 8a35f56c 2022-07-16 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 e5539f76 2022-08-10 thomas if (c->sock->client_status != CLIENT_DISCONNECT &&
145 e5539f76 2022-08-10 thomas c->outbuf_len != 0) {
146 e5539f76 2022-08-10 thomas fcgi_send_response(c, FCGI_STDOUT, c->outbuf,
147 e5539f76 2022-08-10 thomas c->outbuf_len);
148 e5539f76 2022-08-10 thomas }
149 e5539f76 2022-08-10 thomas
150 8a35f56c 2022-07-16 thomas fcgi_create_end_record(c);
151 8a35f56c 2022-07-16 thomas fcgi_cleanup_request(c);
152 8a35f56c 2022-07-16 thomas return 0;
153 8a35f56c 2022-07-16 thomas default:
154 8a35f56c 2022-07-16 thomas log_warn("unimplemented type %d", h->type);
155 8a35f56c 2022-07-16 thomas break;
156 8a35f56c 2022-07-16 thomas }
157 8a35f56c 2022-07-16 thomas
158 8a35f56c 2022-07-16 thomas return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
159 8a35f56c 2022-07-16 thomas + h->padding_len);
160 8a35f56c 2022-07-16 thomas }
161 8a35f56c 2022-07-16 thomas
162 8a35f56c 2022-07-16 thomas void
163 8a35f56c 2022-07-16 thomas fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
164 8a35f56c 2022-07-16 thomas struct request *c, uint16_t id)
165 8a35f56c 2022-07-16 thomas {
166 8a35f56c 2022-07-16 thomas /* XXX -- FCGI_CANT_MPX_CONN */
167 8a35f56c 2022-07-16 thomas if (c->request_started) {
168 8a35f56c 2022-07-16 thomas log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
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 if (n != sizeof(struct fcgi_begin_request_body)) {
173 8a35f56c 2022-07-16 thomas log_warn("wrong size %d != %lu", n,
174 8a35f56c 2022-07-16 thomas sizeof(struct fcgi_begin_request_body));
175 8a35f56c 2022-07-16 thomas return;
176 8a35f56c 2022-07-16 thomas }
177 8a35f56c 2022-07-16 thomas
178 8a35f56c 2022-07-16 thomas c->request_started = 1;
179 8a35f56c 2022-07-16 thomas c->id = id;
180 8a35f56c 2022-07-16 thomas }
181 8a35f56c 2022-07-16 thomas
182 8a35f56c 2022-07-16 thomas void
183 8a35f56c 2022-07-16 thomas fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
184 8a35f56c 2022-07-16 thomas {
185 8a35f56c 2022-07-16 thomas uint32_t name_len, val_len;
186 4630b4b5 2022-09-01 thomas uint8_t *sd, *val;
187 8a35f56c 2022-07-16 thomas
188 8a35f56c 2022-07-16 thomas if (!c->request_started) {
189 8a35f56c 2022-07-16 thomas log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
190 8a35f56c 2022-07-16 thomas return;
191 8a35f56c 2022-07-16 thomas }
192 8a35f56c 2022-07-16 thomas
193 8a35f56c 2022-07-16 thomas if (c->id != id) {
194 8a35f56c 2022-07-16 thomas log_warn("unexpected id, ignoring");
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 if (n == 0) {
199 8a35f56c 2022-07-16 thomas gotweb_process_request(c);
200 8a35f56c 2022-07-16 thomas return;
201 8a35f56c 2022-07-16 thomas }
202 8a35f56c 2022-07-16 thomas
203 8a35f56c 2022-07-16 thomas while (n > 0) {
204 8a35f56c 2022-07-16 thomas if (buf[0] >> 7 == 0) {
205 8a35f56c 2022-07-16 thomas name_len = buf[0];
206 8a35f56c 2022-07-16 thomas n--;
207 8a35f56c 2022-07-16 thomas buf++;
208 8a35f56c 2022-07-16 thomas } else {
209 8a35f56c 2022-07-16 thomas if (n > 3) {
210 8a35f56c 2022-07-16 thomas name_len = ((buf[0] & 0x7f) << 24) +
211 8a35f56c 2022-07-16 thomas (buf[1] << 16) + (buf[2] << 8) + buf[3];
212 8a35f56c 2022-07-16 thomas n -= 4;
213 8a35f56c 2022-07-16 thomas buf += 4;
214 8a35f56c 2022-07-16 thomas } else
215 8a35f56c 2022-07-16 thomas return;
216 8a35f56c 2022-07-16 thomas }
217 8a35f56c 2022-07-16 thomas
218 4630b4b5 2022-09-01 thomas if (n == 0)
219 8a35f56c 2022-07-16 thomas return;
220 8a35f56c 2022-07-16 thomas
221 4630b4b5 2022-09-01 thomas if (buf[0] >> 7 == 0) {
222 4630b4b5 2022-09-01 thomas val_len = buf[0];
223 4630b4b5 2022-09-01 thomas n--;
224 4630b4b5 2022-09-01 thomas buf++;
225 4630b4b5 2022-09-01 thomas } else {
226 4630b4b5 2022-09-01 thomas if (n > 3) {
227 4630b4b5 2022-09-01 thomas val_len = ((buf[0] & 0x7f) << 24) +
228 4630b4b5 2022-09-01 thomas (buf[1] << 16) + (buf[2] << 8) +
229 4630b4b5 2022-09-01 thomas buf[3];
230 4630b4b5 2022-09-01 thomas n -= 4;
231 4630b4b5 2022-09-01 thomas buf += 4;
232 4630b4b5 2022-09-01 thomas } else
233 4630b4b5 2022-09-01 thomas return;
234 8a35f56c 2022-07-16 thomas }
235 8a35f56c 2022-07-16 thomas
236 4630b4b5 2022-09-01 thomas if (n < name_len + val_len)
237 8a35f56c 2022-07-16 thomas return;
238 8a35f56c 2022-07-16 thomas
239 4630b4b5 2022-09-01 thomas val = buf + name_len;
240 8a35f56c 2022-07-16 thomas
241 4630b4b5 2022-09-01 thomas if (c->querystring[0] == '\0' &&
242 4630b4b5 2022-09-01 thomas val_len < MAX_QUERYSTRING &&
243 4630b4b5 2022-09-01 thomas name_len == 12 &&
244 4630b4b5 2022-09-01 thomas strncmp(buf, "QUERY_STRING", 12) == 0) {
245 4630b4b5 2022-09-01 thomas memcpy(c->querystring, val, val_len);
246 8a35f56c 2022-07-16 thomas c->querystring[val_len] = '\0';
247 8a35f56c 2022-07-16 thomas }
248 8a35f56c 2022-07-16 thomas
249 4630b4b5 2022-09-01 thomas if (c->http_host[0] == '\0' &&
250 4630b4b5 2022-09-01 thomas val_len < GOTWEBD_MAXTEXT &&
251 4630b4b5 2022-09-01 thomas name_len == 9 &&
252 4630b4b5 2022-09-01 thomas strncmp(buf, "HTTP_HOST", 9) == 0) {
253 4630b4b5 2022-09-01 thomas memcpy(c->http_host, val, val_len);
254 4630b4b5 2022-09-01 thomas c->http_host[val_len] = '\0';
255 4630b4b5 2022-09-01 thomas
256 8a35f56c 2022-07-16 thomas /*
257 8a35f56c 2022-07-16 thomas * lazily get subdomain
258 8a35f56c 2022-07-16 thomas * will only get domain if no subdomain exists
259 8a35f56c 2022-07-16 thomas * this can still work if gotweb server name is the same
260 8a35f56c 2022-07-16 thomas */
261 4630b4b5 2022-09-01 thomas sd = strchr(c->http_host, '.');
262 8a35f56c 2022-07-16 thomas if (sd)
263 8a35f56c 2022-07-16 thomas *sd = '\0';
264 8a35f56c 2022-07-16 thomas }
265 4630b4b5 2022-09-01 thomas
266 3191e256 2022-12-30 thomas if (c->document_uri[0] == '\0' &&
267 3191e256 2022-12-30 thomas val_len < MAX_DOCUMENT_URI &&
268 3191e256 2022-12-30 thomas name_len == 12 &&
269 3191e256 2022-12-30 thomas strncmp(buf, "DOCUMENT_URI", 12) == 0) {
270 3191e256 2022-12-30 thomas memcpy(c->document_uri, val, val_len);
271 3191e256 2022-12-30 thomas c->document_uri[val_len] = '\0';
272 8a35f56c 2022-07-16 thomas }
273 4630b4b5 2022-09-01 thomas
274 4630b4b5 2022-09-01 thomas if (c->server_name[0] == '\0' &&
275 4630b4b5 2022-09-01 thomas val_len < MAX_SERVER_NAME &&
276 4630b4b5 2022-09-01 thomas name_len == 11 &&
277 4630b4b5 2022-09-01 thomas strncmp(buf, "SERVER_NAME", 11) == 0) {
278 4630b4b5 2022-09-01 thomas memcpy(c->server_name, val, val_len);
279 8a35f56c 2022-07-16 thomas c->server_name[val_len] = '\0';
280 8a35f56c 2022-07-16 thomas }
281 d6795e9f 2022-12-30 thomas
282 d6795e9f 2022-12-30 thomas if (name_len == 5 &&
283 d6795e9f 2022-12-30 thomas strncmp(buf, "HTTPS", 5) == 0)
284 d6795e9f 2022-12-30 thomas c->https = 1;
285 8a35f56c 2022-07-16 thomas
286 4630b4b5 2022-09-01 thomas buf += name_len + val_len;
287 4630b4b5 2022-09-01 thomas n -= name_len - val_len;
288 8a35f56c 2022-07-16 thomas }
289 8a35f56c 2022-07-16 thomas }
290 8a35f56c 2022-07-16 thomas
291 8a35f56c 2022-07-16 thomas void
292 8a35f56c 2022-07-16 thomas fcgi_timeout(int fd, short events, void *arg)
293 8a35f56c 2022-07-16 thomas {
294 8a35f56c 2022-07-16 thomas fcgi_cleanup_request((struct request*) arg);
295 8a35f56c 2022-07-16 thomas }
296 8a35f56c 2022-07-16 thomas
297 8a35f56c 2022-07-16 thomas int
298 e7e5fa49 2022-12-30 thomas fcgi_puts(struct template *tp, const char *str)
299 e7e5fa49 2022-12-30 thomas {
300 e7e5fa49 2022-12-30 thomas if (str == NULL)
301 e7e5fa49 2022-12-30 thomas return 0;
302 e7e5fa49 2022-12-30 thomas return fcgi_gen_binary_response(tp->tp_arg, str, strlen(str));
303 e7e5fa49 2022-12-30 thomas }
304 e7e5fa49 2022-12-30 thomas
305 e7e5fa49 2022-12-30 thomas int
306 e7e5fa49 2022-12-30 thomas fcgi_putc(struct template *tp, int ch)
307 e7e5fa49 2022-12-30 thomas {
308 e7e5fa49 2022-12-30 thomas uint8_t c = ch;
309 e7e5fa49 2022-12-30 thomas return fcgi_gen_binary_response(tp->tp_arg, &c, 1);
310 e7e5fa49 2022-12-30 thomas }
311 e7e5fa49 2022-12-30 thomas
312 e7e5fa49 2022-12-30 thomas int
313 55144267 2022-09-07 thomas fcgi_vprintf(struct request *c, const char *fmt, va_list ap)
314 79393471 2022-08-27 thomas {
315 79393471 2022-08-27 thomas char *str;
316 79393471 2022-08-27 thomas int r;
317 79393471 2022-08-27 thomas
318 79393471 2022-08-27 thomas r = vasprintf(&str, fmt, ap);
319 79393471 2022-08-27 thomas if (r == -1) {
320 79393471 2022-08-27 thomas log_warn("%s: asprintf", __func__);
321 79393471 2022-08-27 thomas return -1;
322 79393471 2022-08-27 thomas }
323 79393471 2022-08-27 thomas
324 79393471 2022-08-27 thomas r = fcgi_gen_binary_response(c, str, r);
325 79393471 2022-08-27 thomas free(str);
326 79393471 2022-08-27 thomas return r;
327 79393471 2022-08-27 thomas }
328 79393471 2022-08-27 thomas
329 79393471 2022-08-27 thomas int
330 55144267 2022-09-07 thomas fcgi_printf(struct request *c, const char *fmt, ...)
331 55144267 2022-09-07 thomas {
332 55144267 2022-09-07 thomas va_list ap;
333 55144267 2022-09-07 thomas int r;
334 55144267 2022-09-07 thomas
335 55144267 2022-09-07 thomas va_start(ap, fmt);
336 55144267 2022-09-07 thomas r = fcgi_vprintf(c, fmt, ap);
337 55144267 2022-09-07 thomas va_end(ap);
338 55144267 2022-09-07 thomas
339 55144267 2022-09-07 thomas return r;
340 55144267 2022-09-07 thomas }
341 55144267 2022-09-07 thomas
342 55144267 2022-09-07 thomas int
343 8a35f56c 2022-07-16 thomas fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
344 8a35f56c 2022-07-16 thomas {
345 e5539f76 2022-08-10 thomas int r;
346 e5539f76 2022-08-10 thomas
347 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
348 8a35f56c 2022-07-16 thomas return -1;
349 8a35f56c 2022-07-16 thomas
350 0c2c6365 2022-07-28 thomas if (data == NULL || len == 0)
351 8a35f56c 2022-07-16 thomas return 0;
352 8a35f56c 2022-07-16 thomas
353 e5539f76 2022-08-10 thomas /*
354 e5539f76 2022-08-10 thomas * special case: send big replies -like blobs- directly
355 e5539f76 2022-08-10 thomas * without copying.
356 e5539f76 2022-08-10 thomas */
357 e5539f76 2022-08-10 thomas if (len > sizeof(c->outbuf)) {
358 e5539f76 2022-08-10 thomas if (c->outbuf_len > 0) {
359 e5539f76 2022-08-10 thomas fcgi_send_response(c, FCGI_STDOUT,
360 e5539f76 2022-08-10 thomas c->outbuf, c->outbuf_len);
361 e5539f76 2022-08-10 thomas c->outbuf_len = 0;
362 e5539f76 2022-08-10 thomas }
363 e5539f76 2022-08-10 thomas return fcgi_send_response(c, FCGI_STDOUT, data, len);
364 e5539f76 2022-08-10 thomas }
365 e5539f76 2022-08-10 thomas
366 e5539f76 2022-08-10 thomas if (len < sizeof(c->outbuf) - c->outbuf_len) {
367 e5539f76 2022-08-10 thomas memcpy(c->outbuf + c->outbuf_len, data, len);
368 e5539f76 2022-08-10 thomas c->outbuf_len += len;
369 e5539f76 2022-08-10 thomas return 0;
370 e5539f76 2022-08-10 thomas }
371 e5539f76 2022-08-10 thomas
372 e5539f76 2022-08-10 thomas r = fcgi_send_response(c, FCGI_STDOUT, c->outbuf, c->outbuf_len);
373 e5539f76 2022-08-10 thomas if (r == -1)
374 e5539f76 2022-08-10 thomas return -1;
375 e5539f76 2022-08-10 thomas
376 e5539f76 2022-08-10 thomas memcpy(c->outbuf, data, len);
377 e5539f76 2022-08-10 thomas c->outbuf_len = len;
378 8a35f56c 2022-07-16 thomas return 0;
379 8a35f56c 2022-07-16 thomas }
380 8a35f56c 2022-07-16 thomas
381 e5539f76 2022-08-10 thomas static int
382 0335d24d 2022-08-06 thomas send_response(struct request *c, int type, const uint8_t *data,
383 0335d24d 2022-08-06 thomas size_t len)
384 8a35f56c 2022-07-16 thomas {
385 0335d24d 2022-08-06 thomas static const uint8_t padding[FCGI_PADDING_SIZE];
386 0335d24d 2022-08-06 thomas struct fcgi_record_header header;
387 0335d24d 2022-08-06 thomas struct iovec iov[3];
388 8a35f56c 2022-07-16 thomas struct timespec ts;
389 a5a99557 2022-07-29 thomas ssize_t nw;
390 0335d24d 2022-08-06 thomas size_t padded_len, tot;
391 0335d24d 2022-08-06 thomas int i, err = 0, th = 2000;
392 8a35f56c 2022-07-16 thomas
393 8a35f56c 2022-07-16 thomas ts.tv_sec = 0;
394 8a35f56c 2022-07-16 thomas ts.tv_nsec = 50;
395 8a35f56c 2022-07-16 thomas
396 0335d24d 2022-08-06 thomas memset(&header, 0, sizeof(header));
397 0335d24d 2022-08-06 thomas header.version = 1;
398 0335d24d 2022-08-06 thomas header.type = type;
399 0335d24d 2022-08-06 thomas header.id = htons(c->id);
400 0335d24d 2022-08-06 thomas header.content_len = htons(len);
401 8a35f56c 2022-07-16 thomas
402 8a35f56c 2022-07-16 thomas /* The FastCGI spec suggests to align the output buffer */
403 0335d24d 2022-08-06 thomas tot = sizeof(header) + len;
404 0335d24d 2022-08-06 thomas padded_len = FCGI_ALIGN(tot);
405 0335d24d 2022-08-06 thomas if (padded_len > tot) {
406 0335d24d 2022-08-06 thomas header.padding_len = padded_len - tot;
407 0335d24d 2022-08-06 thomas tot += header.padding_len;
408 8a35f56c 2022-07-16 thomas }
409 8a35f56c 2022-07-16 thomas
410 0335d24d 2022-08-06 thomas iov[0].iov_base = &header;
411 0335d24d 2022-08-06 thomas iov[0].iov_len = sizeof(header);
412 8a35f56c 2022-07-16 thomas
413 0335d24d 2022-08-06 thomas iov[1].iov_base = (void *)data;
414 0335d24d 2022-08-06 thomas iov[1].iov_len = len;
415 0335d24d 2022-08-06 thomas
416 0335d24d 2022-08-06 thomas iov[2].iov_base = (void *)padding;
417 0335d24d 2022-08-06 thomas iov[2].iov_len = header.padding_len;
418 0335d24d 2022-08-06 thomas
419 0335d24d 2022-08-06 thomas dump_fcgi_record("resp ", &header);
420 0335d24d 2022-08-06 thomas
421 8a35f56c 2022-07-16 thomas /*
422 8a35f56c 2022-07-16 thomas * XXX: add some simple write heuristics here
423 8a35f56c 2022-07-16 thomas * On slower VMs, spotty connections, etc., we don't want to go right to
424 8a35f56c 2022-07-16 thomas * disconnect. Let's at least try to write the data a few times before
425 8a35f56c 2022-07-16 thomas * giving up.
426 8a35f56c 2022-07-16 thomas */
427 0335d24d 2022-08-06 thomas while (tot > 0) {
428 0335d24d 2022-08-06 thomas nw = writev(c->fd, iov, nitems(iov));
429 a5a99557 2022-07-29 thomas if (nw == 0) {
430 a5a99557 2022-07-29 thomas c->sock->client_status = CLIENT_DISCONNECT;
431 a5a99557 2022-07-29 thomas break;
432 a5a99557 2022-07-29 thomas }
433 a5a99557 2022-07-29 thomas if (nw == -1) {
434 a5a99557 2022-07-29 thomas err++;
435 a5a99557 2022-07-29 thomas if (errno == EAGAIN && err < th) {
436 a5a99557 2022-07-29 thomas nanosleep(&ts, NULL);
437 a5a99557 2022-07-29 thomas continue;
438 a5a99557 2022-07-29 thomas }
439 460244b7 2022-07-29 thomas log_warn("%s: write failure", __func__);
440 8a35f56c 2022-07-16 thomas c->sock->client_status = CLIENT_DISCONNECT;
441 e5539f76 2022-08-10 thomas return -1;
442 8a35f56c 2022-07-16 thomas }
443 a5a99557 2022-07-29 thomas
444 0335d24d 2022-08-06 thomas if (nw != tot)
445 0335d24d 2022-08-06 thomas log_debug("%s: partial write: %zu vs %zu", __func__,
446 0335d24d 2022-08-06 thomas nw, tot);
447 0335d24d 2022-08-06 thomas
448 0335d24d 2022-08-06 thomas tot -= nw;
449 0335d24d 2022-08-06 thomas for (i = 0; i < nitems(iov); ++i) {
450 0335d24d 2022-08-06 thomas if (nw < iov[i].iov_len) {
451 0335d24d 2022-08-06 thomas iov[i].iov_base += nw;
452 0335d24d 2022-08-06 thomas iov[i].iov_len -= nw;
453 0335d24d 2022-08-06 thomas break;
454 0335d24d 2022-08-06 thomas }
455 0335d24d 2022-08-06 thomas nw -= iov[i].iov_len;
456 0335d24d 2022-08-06 thomas iov[i].iov_len = 0;
457 0335d24d 2022-08-06 thomas }
458 8a35f56c 2022-07-16 thomas }
459 e5539f76 2022-08-10 thomas
460 e5539f76 2022-08-10 thomas return 0;
461 0335d24d 2022-08-06 thomas }
462 8a35f56c 2022-07-16 thomas
463 e5539f76 2022-08-10 thomas int
464 0335d24d 2022-08-06 thomas fcgi_send_response(struct request *c, int type, const void *data,
465 0335d24d 2022-08-06 thomas size_t len)
466 0335d24d 2022-08-06 thomas {
467 e5539f76 2022-08-10 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
468 e5539f76 2022-08-10 thomas return -1;
469 e5539f76 2022-08-10 thomas
470 0335d24d 2022-08-06 thomas while (len > FCGI_CONTENT_SIZE) {
471 e5539f76 2022-08-10 thomas if (send_response(c, type, data, len) == -1)
472 e5539f76 2022-08-10 thomas return -1;
473 0335d24d 2022-08-06 thomas
474 0335d24d 2022-08-06 thomas data += FCGI_CONTENT_SIZE;
475 0335d24d 2022-08-06 thomas len -= FCGI_CONTENT_SIZE;
476 0335d24d 2022-08-06 thomas }
477 0335d24d 2022-08-06 thomas
478 0335d24d 2022-08-06 thomas if (len == 0)
479 e5539f76 2022-08-10 thomas return 0;
480 0335d24d 2022-08-06 thomas
481 e5539f76 2022-08-10 thomas return send_response(c, type, data, len);
482 8a35f56c 2022-07-16 thomas }
483 8a35f56c 2022-07-16 thomas
484 8a35f56c 2022-07-16 thomas void
485 8a35f56c 2022-07-16 thomas fcgi_create_end_record(struct request *c)
486 8a35f56c 2022-07-16 thomas {
487 0335d24d 2022-08-06 thomas struct fcgi_end_request_body end_request;
488 8a35f56c 2022-07-16 thomas
489 0335d24d 2022-08-06 thomas memset(&end_request, 0, sizeof(end_request));
490 0335d24d 2022-08-06 thomas end_request.app_status = htonl(0); /* script status */
491 0335d24d 2022-08-06 thomas end_request.protocol_status = FCGI_REQUEST_COMPLETE;
492 0335d24d 2022-08-06 thomas
493 0335d24d 2022-08-06 thomas fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
494 0335d24d 2022-08-06 thomas sizeof(end_request));
495 8a35f56c 2022-07-16 thomas }
496 8a35f56c 2022-07-16 thomas
497 8a35f56c 2022-07-16 thomas void
498 8a35f56c 2022-07-16 thomas fcgi_cleanup_request(struct request *c)
499 8a35f56c 2022-07-16 thomas {
500 8a35f56c 2022-07-16 thomas cgi_inflight--;
501 8a35f56c 2022-07-16 thomas client_cnt--;
502 8a35f56c 2022-07-16 thomas
503 8a35f56c 2022-07-16 thomas evtimer_del(&c->tmo);
504 8a35f56c 2022-07-16 thomas if (event_initialized(&c->ev))
505 8a35f56c 2022-07-16 thomas event_del(&c->ev);
506 8a35f56c 2022-07-16 thomas
507 8a35f56c 2022-07-16 thomas close(c->fd);
508 e7e5fa49 2022-12-30 thomas template_free(c->tp);
509 8a35f56c 2022-07-16 thomas gotweb_free_transport(c->t);
510 8a35f56c 2022-07-16 thomas free(c);
511 8a35f56c 2022-07-16 thomas }
512 8a35f56c 2022-07-16 thomas
513 8a35f56c 2022-07-16 thomas void
514 8a35f56c 2022-07-16 thomas dump_fcgi_record(const char *p, struct fcgi_record_header *h)
515 8a35f56c 2022-07-16 thomas {
516 8a35f56c 2022-07-16 thomas dump_fcgi_record_header(p, h);
517 8a35f56c 2022-07-16 thomas
518 8a35f56c 2022-07-16 thomas if (h->type == FCGI_BEGIN_REQUEST)
519 8a35f56c 2022-07-16 thomas dump_fcgi_begin_request_body(p,
520 8a35f56c 2022-07-16 thomas (struct fcgi_begin_request_body *)(h + 1));
521 8a35f56c 2022-07-16 thomas else if (h->type == FCGI_END_REQUEST)
522 8a35f56c 2022-07-16 thomas dump_fcgi_end_request_body(p,
523 8a35f56c 2022-07-16 thomas (struct fcgi_end_request_body *)(h + 1));
524 8a35f56c 2022-07-16 thomas }
525 8a35f56c 2022-07-16 thomas
526 8a35f56c 2022-07-16 thomas void
527 8a35f56c 2022-07-16 thomas dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
528 8a35f56c 2022-07-16 thomas {
529 8a35f56c 2022-07-16 thomas log_debug("%sversion: %d", p, h->version);
530 8a35f56c 2022-07-16 thomas log_debug("%stype: %d", p, h->type);
531 8a35f56c 2022-07-16 thomas log_debug("%srequestId: %d", p, ntohs(h->id));
532 8a35f56c 2022-07-16 thomas log_debug("%scontentLength: %d", p, ntohs(h->content_len));
533 8a35f56c 2022-07-16 thomas log_debug("%spaddingLength: %d", p, h->padding_len);
534 8a35f56c 2022-07-16 thomas log_debug("%sreserved: %d", p, h->reserved);
535 8a35f56c 2022-07-16 thomas }
536 8a35f56c 2022-07-16 thomas
537 8a35f56c 2022-07-16 thomas void
538 8a35f56c 2022-07-16 thomas dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
539 8a35f56c 2022-07-16 thomas {
540 8a35f56c 2022-07-16 thomas log_debug("%srole %d", p, ntohs(b->role));
541 8a35f56c 2022-07-16 thomas log_debug("%sflags %d", p, b->flags);
542 8a35f56c 2022-07-16 thomas }
543 8a35f56c 2022-07-16 thomas
544 8a35f56c 2022-07-16 thomas void
545 8a35f56c 2022-07-16 thomas dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
546 8a35f56c 2022-07-16 thomas {
547 8a35f56c 2022-07-16 thomas log_debug("%sappStatus: %d", p, ntohl(b->app_status));
548 8a35f56c 2022-07-16 thomas log_debug("%sprotocolStatus: %d", p, b->protocol_status);
549 8a35f56c 2022-07-16 thomas }