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"
36 #include "got_reference.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);
100 /* drop the parsed record */
101 if (parsed != 0 && c->buf_len > 0) {
102 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
105 } while (parsed > 0 && c->buf_len > 0);
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->document_uri[0] == '\0' &&
249 val_len < MAX_DOCUMENT_URI &&
251 strncmp(buf, "DOCUMENT_URI", 12) == 0) {
252 memcpy(c->document_uri, val, val_len);
253 c->document_uri[val_len] = '\0';
256 if (c->server_name[0] == '\0' &&
257 val_len < MAX_SERVER_NAME &&
259 strncmp(buf, "SERVER_NAME", 11) == 0) {
260 memcpy(c->server_name, val, val_len);
261 c->server_name[val_len] = '\0';
265 strncmp(buf, "HTTPS", 5) == 0)
268 buf += name_len + val_len;
269 n -= name_len - val_len;
274 fcgi_timeout(int fd, short events, void *arg)
276 fcgi_cleanup_request((struct request*) arg);
280 fcgi_puts(struct template *tp, const char *str)
284 return fcgi_gen_binary_response(tp->tp_arg, str, strlen(str));
288 fcgi_putc(struct template *tp, int ch)
291 return fcgi_gen_binary_response(tp->tp_arg, &c, 1);
295 fcgi_vprintf(struct request *c, const char *fmt, va_list ap)
300 r = vasprintf(&str, fmt, ap);
302 log_warn("%s: asprintf", __func__);
306 r = fcgi_gen_binary_response(c, str, r);
312 fcgi_printf(struct request *c, const char *fmt, ...)
318 r = fcgi_vprintf(c, fmt, ap);
325 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
329 if (c->sock->client_status == CLIENT_DISCONNECT)
332 if (data == NULL || len == 0)
336 * special case: send big replies -like blobs- directly
339 if (len > sizeof(c->outbuf)) {
340 if (c->outbuf_len > 0) {
341 fcgi_send_response(c, FCGI_STDOUT,
342 c->outbuf, c->outbuf_len);
345 return fcgi_send_response(c, FCGI_STDOUT, data, len);
348 if (len < sizeof(c->outbuf) - c->outbuf_len) {
349 memcpy(c->outbuf + c->outbuf_len, data, len);
350 c->outbuf_len += len;
354 r = fcgi_send_response(c, FCGI_STDOUT, c->outbuf, c->outbuf_len);
358 memcpy(c->outbuf, data, len);
364 send_response(struct request *c, int type, const uint8_t *data,
367 static const uint8_t padding[FCGI_PADDING_SIZE];
368 struct fcgi_record_header header;
372 size_t padded_len, tot;
373 int i, err = 0, th = 2000;
378 memset(&header, 0, sizeof(header));
381 header.id = htons(c->id);
382 header.content_len = htons(len);
384 /* The FastCGI spec suggests to align the output buffer */
385 tot = sizeof(header) + len;
386 padded_len = FCGI_ALIGN(tot);
387 if (padded_len > tot) {
388 header.padding_len = padded_len - tot;
389 tot += header.padding_len;
392 iov[0].iov_base = &header;
393 iov[0].iov_len = sizeof(header);
395 iov[1].iov_base = (void *)data;
396 iov[1].iov_len = len;
398 iov[2].iov_base = (void *)padding;
399 iov[2].iov_len = header.padding_len;
401 dump_fcgi_record("resp ", &header);
404 * XXX: add some simple write heuristics here
405 * On slower VMs, spotty connections, etc., we don't want to go right to
406 * disconnect. Let's at least try to write the data a few times before
410 nw = writev(c->fd, iov, nitems(iov));
412 c->sock->client_status = CLIENT_DISCONNECT;
417 if (errno == EAGAIN && err < th) {
418 nanosleep(&ts, NULL);
421 log_debug("%s: write failure: %s", __func__,
423 c->sock->client_status = CLIENT_DISCONNECT;
428 log_debug("%s: partial write: %zu vs %zu", __func__,
432 for (i = 0; i < nitems(iov); ++i) {
433 if (nw < iov[i].iov_len) {
434 iov[i].iov_base += nw;
435 iov[i].iov_len -= nw;
438 nw -= iov[i].iov_len;
447 fcgi_send_response(struct request *c, int type, const void *data,
450 if (c->sock->client_status == CLIENT_DISCONNECT)
453 while (len > FCGI_CONTENT_SIZE) {
454 if (send_response(c, type, data, len) == -1)
457 data += FCGI_CONTENT_SIZE;
458 len -= FCGI_CONTENT_SIZE;
464 return send_response(c, type, data, len);
468 fcgi_create_end_record(struct request *c)
470 struct fcgi_end_request_body end_request;
472 memset(&end_request, 0, sizeof(end_request));
473 end_request.app_status = htonl(0); /* script status */
474 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
476 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
477 sizeof(end_request));
481 fcgi_cleanup_request(struct request *c)
486 evtimer_del(&c->tmo);
487 if (event_initialized(&c->ev))
491 template_free(c->tp);
493 gotweb_free_transport(c->t);
498 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
500 dump_fcgi_record_header(p, h);
502 if (h->type == FCGI_BEGIN_REQUEST)
503 dump_fcgi_begin_request_body(p,
504 (struct fcgi_begin_request_body *)(h + 1));
505 else if (h->type == FCGI_END_REQUEST)
506 dump_fcgi_end_request_body(p,
507 (struct fcgi_end_request_body *)(h + 1));
511 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
513 log_debug("%sversion: %d", p, h->version);
514 log_debug("%stype: %d", p, h->type);
515 log_debug("%srequestId: %d", p, ntohs(h->id));
516 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
517 log_debug("%spaddingLength: %d", p, h->padding_len);
518 log_debug("%sreserved: %d", p, h->reserved);
522 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
524 log_debug("%srole %d", p, ntohs(b->role));
525 log_debug("%sflags %d", p, b->flags);
529 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
531 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
532 log_debug("%sprotocolStatus: %d", p, b->protocol_status);