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"
41 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
42 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
44 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
45 int fcgi_send_response(struct request *, int, const void *, size_t);
47 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
48 void dump_fcgi_begin_request_body(const char *,
49 struct fcgi_begin_request_body *);
50 void dump_fcgi_end_request_body(const char *,
51 struct fcgi_end_request_body *);
53 extern int cgi_inflight;
54 extern volatile int client_cnt;
57 fcgi_request(int fd, short events, void *arg)
59 struct request *c = arg;
63 n = read(fd, c->buf + c->buf_pos + c->buf_len,
64 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
78 log_debug("closed connection");
87 * Parse the records as they are received. Per the FastCGI
88 * specification, the server need only receive the FastCGI
89 * parameter records in full; it is free to begin execution
90 * at that point, which is what happens here.
93 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
98 } while (parsed > 0 && c->buf_len > 0);
100 /* Make space for further reads */
102 if (c->buf_len > 0) {
103 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
108 fcgi_cleanup_request(c);
112 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
114 struct fcgi_record_header *h;
116 if (n < sizeof(struct fcgi_record_header))
119 h = (struct fcgi_record_header*) buf;
121 dump_fcgi_record("", h);
123 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
128 log_warn("wrong version");
131 case FCGI_BEGIN_REQUEST:
132 fcgi_parse_begin_request(buf +
133 sizeof(struct fcgi_record_header),
134 ntohs(h->content_len), c, ntohs(h->id));
137 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
138 ntohs(h->content_len), c, ntohs(h->id));
141 case FCGI_ABORT_REQUEST:
142 if (c->sock->client_status != CLIENT_DISCONNECT &&
143 c->outbuf_len != 0) {
144 fcgi_send_response(c, FCGI_STDOUT, c->outbuf,
148 fcgi_create_end_record(c);
149 fcgi_cleanup_request(c);
152 log_warn("unimplemented type %d", h->type);
156 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
161 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
162 struct request *c, uint16_t id)
164 /* XXX -- FCGI_CANT_MPX_CONN */
165 if (c->request_started) {
166 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
170 if (n != sizeof(struct fcgi_begin_request_body)) {
171 log_warn("wrong size %d != %lu", n,
172 sizeof(struct fcgi_begin_request_body));
176 c->request_started = 1;
181 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
183 uint32_t name_len, val_len;
186 if (!c->request_started) {
187 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
192 log_warn("unexpected id, ignoring");
197 gotweb_process_request(c);
202 if (buf[0] >> 7 == 0) {
208 name_len = ((buf[0] & 0x7f) << 24) +
209 (buf[1] << 16) + (buf[2] << 8) + buf[3];
219 if (buf[0] >> 7 == 0) {
225 val_len = ((buf[0] & 0x7f) << 24) +
226 (buf[1] << 16) + (buf[2] << 8) +
234 if (n < name_len + val_len)
237 val = buf + name_len;
239 if (c->querystring[0] == '\0' &&
240 val_len < MAX_QUERYSTRING &&
242 strncmp(buf, "QUERY_STRING", 12) == 0) {
243 memcpy(c->querystring, val, val_len);
244 c->querystring[val_len] = '\0';
247 if (c->http_host[0] == '\0' &&
248 val_len < GOTWEBD_MAXTEXT &&
250 strncmp(buf, "HTTP_HOST", 9) == 0) {
251 memcpy(c->http_host, val, val_len);
252 c->http_host[val_len] = '\0';
255 * lazily get subdomain
256 * will only get domain if no subdomain exists
257 * this can still work if gotweb server name is the same
259 sd = strchr(c->http_host, '.');
264 if (c->document_uri[0] == '\0' &&
265 val_len < MAX_DOCUMENT_URI &&
267 strncmp(buf, "DOCUMENT_URI", 12) == 0) {
268 memcpy(c->document_uri, val, val_len);
269 c->document_uri[val_len] = '\0';
272 if (c->server_name[0] == '\0' &&
273 val_len < MAX_SERVER_NAME &&
275 strncmp(buf, "SERVER_NAME", 11) == 0) {
276 memcpy(c->server_name, val, val_len);
277 c->server_name[val_len] = '\0';
280 buf += name_len + val_len;
281 n -= name_len - val_len;
286 fcgi_timeout(int fd, short events, void *arg)
288 fcgi_cleanup_request((struct request*) arg);
292 fcgi_puts(struct template *tp, const char *str)
296 return fcgi_gen_binary_response(tp->tp_arg, str, strlen(str));
300 fcgi_putc(struct template *tp, int ch)
303 return fcgi_gen_binary_response(tp->tp_arg, &c, 1);
307 fcgi_vprintf(struct request *c, const char *fmt, va_list ap)
312 r = vasprintf(&str, fmt, ap);
314 log_warn("%s: asprintf", __func__);
318 r = fcgi_gen_binary_response(c, str, r);
324 fcgi_printf(struct request *c, const char *fmt, ...)
330 r = fcgi_vprintf(c, fmt, ap);
337 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
341 if (c->sock->client_status == CLIENT_DISCONNECT)
344 if (data == NULL || len == 0)
348 * special case: send big replies -like blobs- directly
351 if (len > sizeof(c->outbuf)) {
352 if (c->outbuf_len > 0) {
353 fcgi_send_response(c, FCGI_STDOUT,
354 c->outbuf, c->outbuf_len);
357 return fcgi_send_response(c, FCGI_STDOUT, data, len);
360 if (len < sizeof(c->outbuf) - c->outbuf_len) {
361 memcpy(c->outbuf + c->outbuf_len, data, len);
362 c->outbuf_len += len;
366 r = fcgi_send_response(c, FCGI_STDOUT, c->outbuf, c->outbuf_len);
370 memcpy(c->outbuf, data, len);
376 send_response(struct request *c, int type, const uint8_t *data,
379 static const uint8_t padding[FCGI_PADDING_SIZE];
380 struct fcgi_record_header header;
384 size_t padded_len, tot;
385 int i, err = 0, th = 2000;
390 memset(&header, 0, sizeof(header));
393 header.id = htons(c->id);
394 header.content_len = htons(len);
396 /* The FastCGI spec suggests to align the output buffer */
397 tot = sizeof(header) + len;
398 padded_len = FCGI_ALIGN(tot);
399 if (padded_len > tot) {
400 header.padding_len = padded_len - tot;
401 tot += header.padding_len;
404 iov[0].iov_base = &header;
405 iov[0].iov_len = sizeof(header);
407 iov[1].iov_base = (void *)data;
408 iov[1].iov_len = len;
410 iov[2].iov_base = (void *)padding;
411 iov[2].iov_len = header.padding_len;
413 dump_fcgi_record("resp ", &header);
416 * XXX: add some simple write heuristics here
417 * On slower VMs, spotty connections, etc., we don't want to go right to
418 * disconnect. Let's at least try to write the data a few times before
422 nw = writev(c->fd, iov, nitems(iov));
424 c->sock->client_status = CLIENT_DISCONNECT;
429 if (errno == EAGAIN && err < th) {
430 nanosleep(&ts, NULL);
433 log_warn("%s: write failure", __func__);
434 c->sock->client_status = CLIENT_DISCONNECT;
439 log_debug("%s: partial write: %zu vs %zu", __func__,
443 for (i = 0; i < nitems(iov); ++i) {
444 if (nw < iov[i].iov_len) {
445 iov[i].iov_base += nw;
446 iov[i].iov_len -= nw;
449 nw -= iov[i].iov_len;
458 fcgi_send_response(struct request *c, int type, const void *data,
461 if (c->sock->client_status == CLIENT_DISCONNECT)
464 while (len > FCGI_CONTENT_SIZE) {
465 if (send_response(c, type, data, len) == -1)
468 data += FCGI_CONTENT_SIZE;
469 len -= FCGI_CONTENT_SIZE;
475 return send_response(c, type, data, len);
479 fcgi_create_end_record(struct request *c)
481 struct fcgi_end_request_body end_request;
483 memset(&end_request, 0, sizeof(end_request));
484 end_request.app_status = htonl(0); /* script status */
485 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
487 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
488 sizeof(end_request));
492 fcgi_cleanup_request(struct request *c)
497 evtimer_del(&c->tmo);
498 if (event_initialized(&c->ev))
502 template_free(c->tp);
503 gotweb_free_transport(c->t);
508 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
510 dump_fcgi_record_header(p, h);
512 if (h->type == FCGI_BEGIN_REQUEST)
513 dump_fcgi_begin_request_body(p,
514 (struct fcgi_begin_request_body *)(h + 1));
515 else if (h->type == FCGI_END_REQUEST)
516 dump_fcgi_end_request_body(p,
517 (struct fcgi_end_request_body *)(h + 1));
521 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
523 log_debug("%sversion: %d", p, h->version);
524 log_debug("%stype: %d", p, h->type);
525 log_debug("%srequestId: %d", p, ntohs(h->id));
526 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
527 log_debug("%spaddingLength: %d", p, h->padding_len);
528 log_debug("%sreserved: %d", p, h->reserved);
532 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
534 log_debug("%srole %d", p, ntohs(b->role));
535 log_debug("%sflags %d", p, b->flags);
539 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
541 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
542 log_debug("%sprotocolStatus: %d", p, b->protocol_status);