2 * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
4 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <arpa/inet.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
35 #include "got_error.h"
37 #include "got_compat.h"
42 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
43 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
45 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
46 int fcgi_send_response(struct request *, int, const void *, size_t);
48 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
49 void dump_fcgi_begin_request_body(const char *,
50 struct fcgi_begin_request_body *);
51 void dump_fcgi_end_request_body(const char *,
52 struct fcgi_end_request_body *);
54 extern int cgi_inflight;
55 extern volatile int client_cnt;
58 fcgi_request(int fd, short events, void *arg)
60 struct request *c = arg;
64 n = read(fd, c->buf + c->buf_pos + c->buf_len,
65 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
79 log_debug("closed connection");
88 * Parse the records as they are received. Per the FastCGI
89 * specification, the server need only receive the FastCGI
90 * parameter records in full; it is free to begin execution
91 * at that point, which is what happens here.
94 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
99 } while (parsed > 0 && c->buf_len > 0);
101 /* Make space for further reads */
103 if (c->buf_len > 0) {
104 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
109 fcgi_cleanup_request(c);
113 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
115 struct fcgi_record_header *h;
117 if (n < sizeof(struct fcgi_record_header))
120 h = (struct fcgi_record_header*) buf;
122 dump_fcgi_record("", h);
124 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
129 log_warn("wrong version");
132 case FCGI_BEGIN_REQUEST:
133 fcgi_parse_begin_request(buf +
134 sizeof(struct fcgi_record_header),
135 ntohs(h->content_len), c, ntohs(h->id));
138 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
139 ntohs(h->content_len), c, ntohs(h->id));
142 case FCGI_ABORT_REQUEST:
143 if (c->sock->client_status != CLIENT_DISCONNECT &&
144 c->outbuf_len != 0) {
145 fcgi_send_response(c, FCGI_STDOUT, c->outbuf,
149 fcgi_create_end_record(c);
150 fcgi_cleanup_request(c);
153 log_warn("unimplemented type %d", h->type);
157 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
162 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
163 struct request *c, uint16_t id)
165 /* XXX -- FCGI_CANT_MPX_CONN */
166 if (c->request_started) {
167 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
171 if (n != sizeof(struct fcgi_begin_request_body)) {
172 log_warn("wrong size %d != %lu", n,
173 sizeof(struct fcgi_begin_request_body));
177 c->request_started = 1;
182 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
184 uint32_t name_len, val_len;
187 if (!c->request_started) {
188 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
193 log_warn("unexpected id, ignoring");
198 gotweb_process_request(c);
203 if (buf[0] >> 7 == 0) {
209 name_len = ((buf[0] & 0x7f) << 24) +
210 (buf[1] << 16) + (buf[2] << 8) + buf[3];
220 if (buf[0] >> 7 == 0) {
226 val_len = ((buf[0] & 0x7f) << 24) +
227 (buf[1] << 16) + (buf[2] << 8) +
235 if (n < name_len + val_len)
238 val = buf + name_len;
240 if (c->querystring[0] == '\0' &&
241 val_len < MAX_QUERYSTRING &&
243 strncmp(buf, "QUERY_STRING", 12) == 0) {
244 memcpy(c->querystring, val, val_len);
245 c->querystring[val_len] = '\0';
248 if (c->http_host[0] == '\0' &&
249 val_len < GOTWEBD_MAXTEXT &&
251 strncmp(buf, "HTTP_HOST", 9) == 0) {
252 memcpy(c->http_host, val, val_len);
253 c->http_host[val_len] = '\0';
256 * lazily get subdomain
257 * will only get domain if no subdomain exists
258 * this can still work if gotweb server name is the same
260 sd = strchr(c->http_host, '.');
265 if (c->script_name[0] == '\0' &&
266 val_len < MAX_SCRIPT_NAME &&
268 strncmp(buf, "SCRIPT_NAME", 11) == 0) {
269 memcpy(c->script_name, val, val_len);
270 c->script_name[val_len] = '\0';
273 if (c->server_name[0] == '\0' &&
274 val_len < MAX_SERVER_NAME &&
276 strncmp(buf, "SERVER_NAME", 11) == 0) {
277 memcpy(c->server_name, val, val_len);
278 c->server_name[val_len] = '\0';
281 buf += name_len + val_len;
282 n -= name_len - val_len;
287 fcgi_timeout(int fd, short events, void *arg)
289 fcgi_cleanup_request((struct request*) arg);
293 fcgi_printf(struct request *c, const char *fmt, ...)
300 r = vasprintf(&str, fmt, ap);
304 log_warn("%s: asprintf", __func__);
308 r = fcgi_gen_binary_response(c, str, r);
314 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
318 if (c->sock->client_status == CLIENT_DISCONNECT)
321 if (data == NULL || len == 0)
325 * special case: send big replies -like blobs- directly
328 if (len > sizeof(c->outbuf)) {
329 if (c->outbuf_len > 0) {
330 fcgi_send_response(c, FCGI_STDOUT,
331 c->outbuf, c->outbuf_len);
334 return fcgi_send_response(c, FCGI_STDOUT, data, len);
337 if (len < sizeof(c->outbuf) - c->outbuf_len) {
338 memcpy(c->outbuf + c->outbuf_len, data, len);
339 c->outbuf_len += len;
343 r = fcgi_send_response(c, FCGI_STDOUT, c->outbuf, c->outbuf_len);
347 memcpy(c->outbuf, data, len);
353 send_response(struct request *c, int type, const uint8_t *data,
356 static const uint8_t padding[FCGI_PADDING_SIZE];
357 struct fcgi_record_header header;
361 size_t padded_len, tot;
362 int i, err = 0, th = 2000;
367 memset(&header, 0, sizeof(header));
370 header.id = htons(c->id);
371 header.content_len = htons(len);
373 /* The FastCGI spec suggests to align the output buffer */
374 tot = sizeof(header) + len;
375 padded_len = FCGI_ALIGN(tot);
376 if (padded_len > tot) {
377 header.padding_len = padded_len - tot;
378 tot += header.padding_len;
381 iov[0].iov_base = &header;
382 iov[0].iov_len = sizeof(header);
384 iov[1].iov_base = (void *)data;
385 iov[1].iov_len = len;
387 iov[2].iov_base = (void *)padding;
388 iov[2].iov_len = header.padding_len;
390 dump_fcgi_record("resp ", &header);
393 * XXX: add some simple write heuristics here
394 * On slower VMs, spotty connections, etc., we don't want to go right to
395 * disconnect. Let's at least try to write the data a few times before
399 nw = writev(c->fd, iov, nitems(iov));
401 c->sock->client_status = CLIENT_DISCONNECT;
406 if (errno == EAGAIN && err < th) {
407 nanosleep(&ts, NULL);
410 log_warn("%s: write failure", __func__);
411 c->sock->client_status = CLIENT_DISCONNECT;
416 log_debug("%s: partial write: %zu vs %zu", __func__,
420 for (i = 0; i < nitems(iov); ++i) {
421 if (nw < iov[i].iov_len) {
422 iov[i].iov_base += nw;
423 iov[i].iov_len -= nw;
426 nw -= iov[i].iov_len;
435 fcgi_send_response(struct request *c, int type, const void *data,
438 if (c->sock->client_status == CLIENT_DISCONNECT)
441 while (len > FCGI_CONTENT_SIZE) {
442 if (send_response(c, type, data, len) == -1)
445 data += FCGI_CONTENT_SIZE;
446 len -= FCGI_CONTENT_SIZE;
452 return send_response(c, type, data, len);
456 fcgi_create_end_record(struct request *c)
458 struct fcgi_end_request_body end_request;
460 memset(&end_request, 0, sizeof(end_request));
461 end_request.app_status = htonl(0); /* script status */
462 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
464 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
465 sizeof(end_request));
469 fcgi_cleanup_request(struct request *c)
474 evtimer_del(&c->tmo);
475 if (event_initialized(&c->ev))
479 gotweb_free_transport(c->t);
484 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
486 dump_fcgi_record_header(p, h);
488 if (h->type == FCGI_BEGIN_REQUEST)
489 dump_fcgi_begin_request_body(p,
490 (struct fcgi_begin_request_body *)(h + 1));
491 else if (h->type == FCGI_END_REQUEST)
492 dump_fcgi_end_request_body(p,
493 (struct fcgi_end_request_body *)(h + 1));
497 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
499 log_debug("%sversion: %d", p, h->version);
500 log_debug("%stype: %d", p, h->type);
501 log_debug("%srequestId: %d", p, ntohs(h->id));
502 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
503 log_debug("%spaddingLength: %d", p, h->padding_len);
504 log_debug("%sreserved: %d", p, h->reserved);
508 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
510 log_debug("%srole %d", p, ntohs(b->role));
511 log_debug("%sflags %d", p, b->flags);
515 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
517 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
518 log_debug("%sprotocolStatus: %d", p, b->protocol_status);