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