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