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"
43 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
44 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
46 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
47 int fcgi_send_response(struct request *, int, const void *, size_t);
49 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
50 void dump_fcgi_begin_request_body(const char *,
51 struct fcgi_begin_request_body *);
52 void dump_fcgi_end_request_body(const char *,
53 struct fcgi_end_request_body *);
55 extern int cgi_inflight;
56 extern volatile int client_cnt;
59 fcgi_request(int fd, short events, void *arg)
61 struct request *c = arg;
65 n = read(fd, c->buf + c->buf_pos + c->buf_len,
66 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
80 log_debug("closed connection");
89 * Parse the records as they are received. Per the FastCGI
90 * specification, the server need only receive the FastCGI
91 * parameter records in full; it is free to begin execution
92 * at that point, which is what happens here.
95 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
100 } while (parsed > 0 && c->buf_len > 0);
102 /* Make space for further reads */
104 if (c->buf_len > 0) {
105 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
110 fcgi_cleanup_request(c);
114 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
116 struct fcgi_record_header *h;
118 if (n < sizeof(struct fcgi_record_header))
121 h = (struct fcgi_record_header*) buf;
123 dump_fcgi_record("", h);
125 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
130 log_warn("wrong version");
133 case FCGI_BEGIN_REQUEST:
134 fcgi_parse_begin_request(buf +
135 sizeof(struct fcgi_record_header),
136 ntohs(h->content_len), c, ntohs(h->id));
139 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
140 ntohs(h->content_len), c, ntohs(h->id));
143 case FCGI_ABORT_REQUEST:
144 if (c->sock->client_status != CLIENT_DISCONNECT &&
145 c->outbuf_len != 0) {
146 fcgi_send_response(c, FCGI_STDOUT, c->outbuf,
150 fcgi_create_end_record(c);
151 fcgi_cleanup_request(c);
154 log_warn("unimplemented type %d", h->type);
158 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
163 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
164 struct request *c, uint16_t id)
166 /* XXX -- FCGI_CANT_MPX_CONN */
167 if (c->request_started) {
168 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
172 if (n != sizeof(struct fcgi_begin_request_body)) {
173 log_warn("wrong size %d != %lu", n,
174 sizeof(struct fcgi_begin_request_body));
178 c->request_started = 1;
183 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
185 uint32_t name_len, val_len;
188 if (!c->request_started) {
189 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
194 log_warn("unexpected id, ignoring");
199 gotweb_process_request(c);
204 if (buf[0] >> 7 == 0) {
210 name_len = ((buf[0] & 0x7f) << 24) +
211 (buf[1] << 16) + (buf[2] << 8) + buf[3];
221 if (buf[0] >> 7 == 0) {
227 val_len = ((buf[0] & 0x7f) << 24) +
228 (buf[1] << 16) + (buf[2] << 8) +
236 if (n < name_len + val_len)
239 val = buf + name_len;
241 if (c->querystring[0] == '\0' &&
242 val_len < MAX_QUERYSTRING &&
244 strncmp(buf, "QUERY_STRING", 12) == 0) {
245 memcpy(c->querystring, val, val_len);
246 c->querystring[val_len] = '\0';
249 if (c->http_host[0] == '\0' &&
250 val_len < GOTWEBD_MAXTEXT &&
252 strncmp(buf, "HTTP_HOST", 9) == 0) {
253 memcpy(c->http_host, val, val_len);
254 c->http_host[val_len] = '\0';
257 * lazily get subdomain
258 * will only get domain if no subdomain exists
259 * this can still work if gotweb server name is the same
261 sd = strchr(c->http_host, '.');
266 if (c->document_uri[0] == '\0' &&
267 val_len < MAX_DOCUMENT_URI &&
269 strncmp(buf, "DOCUMENT_URI", 12) == 0) {
270 memcpy(c->document_uri, val, val_len);
271 c->document_uri[val_len] = '\0';
274 if (c->server_name[0] == '\0' &&
275 val_len < MAX_SERVER_NAME &&
277 strncmp(buf, "SERVER_NAME", 11) == 0) {
278 memcpy(c->server_name, val, val_len);
279 c->server_name[val_len] = '\0';
283 strncmp(buf, "HTTPS", 5) == 0)
286 buf += name_len + val_len;
287 n -= name_len - val_len;
292 fcgi_timeout(int fd, short events, void *arg)
294 fcgi_cleanup_request((struct request*) arg);
298 fcgi_puts(struct template *tp, const char *str)
302 return fcgi_gen_binary_response(tp->tp_arg, str, strlen(str));
306 fcgi_putc(struct template *tp, int ch)
309 return fcgi_gen_binary_response(tp->tp_arg, &c, 1);
313 fcgi_vprintf(struct request *c, const char *fmt, va_list ap)
318 r = vasprintf(&str, fmt, ap);
320 log_warn("%s: asprintf", __func__);
324 r = fcgi_gen_binary_response(c, str, r);
330 fcgi_printf(struct request *c, const char *fmt, ...)
336 r = fcgi_vprintf(c, fmt, ap);
343 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
347 if (c->sock->client_status == CLIENT_DISCONNECT)
350 if (data == NULL || len == 0)
354 * special case: send big replies -like blobs- directly
357 if (len > sizeof(c->outbuf)) {
358 if (c->outbuf_len > 0) {
359 fcgi_send_response(c, FCGI_STDOUT,
360 c->outbuf, c->outbuf_len);
363 return fcgi_send_response(c, FCGI_STDOUT, data, len);
366 if (len < sizeof(c->outbuf) - c->outbuf_len) {
367 memcpy(c->outbuf + c->outbuf_len, data, len);
368 c->outbuf_len += len;
372 r = fcgi_send_response(c, FCGI_STDOUT, c->outbuf, c->outbuf_len);
376 memcpy(c->outbuf, data, len);
382 send_response(struct request *c, int type, const uint8_t *data,
385 static const uint8_t padding[FCGI_PADDING_SIZE];
386 struct fcgi_record_header header;
390 size_t padded_len, tot;
391 int i, err = 0, th = 2000;
396 memset(&header, 0, sizeof(header));
399 header.id = htons(c->id);
400 header.content_len = htons(len);
402 /* The FastCGI spec suggests to align the output buffer */
403 tot = sizeof(header) + len;
404 padded_len = FCGI_ALIGN(tot);
405 if (padded_len > tot) {
406 header.padding_len = padded_len - tot;
407 tot += header.padding_len;
410 iov[0].iov_base = &header;
411 iov[0].iov_len = sizeof(header);
413 iov[1].iov_base = (void *)data;
414 iov[1].iov_len = len;
416 iov[2].iov_base = (void *)padding;
417 iov[2].iov_len = header.padding_len;
419 dump_fcgi_record("resp ", &header);
422 * XXX: add some simple write heuristics here
423 * On slower VMs, spotty connections, etc., we don't want to go right to
424 * disconnect. Let's at least try to write the data a few times before
428 nw = writev(c->fd, iov, nitems(iov));
430 c->sock->client_status = CLIENT_DISCONNECT;
435 if (errno == EAGAIN && err < th) {
436 nanosleep(&ts, NULL);
439 log_warn("%s: write failure", __func__);
440 c->sock->client_status = CLIENT_DISCONNECT;
445 log_debug("%s: partial write: %zu vs %zu", __func__,
449 for (i = 0; i < nitems(iov); ++i) {
450 if (nw < iov[i].iov_len) {
451 iov[i].iov_base += nw;
452 iov[i].iov_len -= nw;
455 nw -= iov[i].iov_len;
464 fcgi_send_response(struct request *c, int type, const void *data,
467 if (c->sock->client_status == CLIENT_DISCONNECT)
470 while (len > FCGI_CONTENT_SIZE) {
471 if (send_response(c, type, data, len) == -1)
474 data += FCGI_CONTENT_SIZE;
475 len -= FCGI_CONTENT_SIZE;
481 return send_response(c, type, data, len);
485 fcgi_create_end_record(struct request *c)
487 struct fcgi_end_request_body end_request;
489 memset(&end_request, 0, sizeof(end_request));
490 end_request.app_status = htonl(0); /* script status */
491 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
493 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
494 sizeof(end_request));
498 fcgi_cleanup_request(struct request *c)
503 evtimer_del(&c->tmo);
504 if (event_initialized(&c->ev))
508 template_free(c->tp);
509 gotweb_free_transport(c->t);
514 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
516 dump_fcgi_record_header(p, h);
518 if (h->type == FCGI_BEGIN_REQUEST)
519 dump_fcgi_begin_request_body(p,
520 (struct fcgi_begin_request_body *)(h + 1));
521 else if (h->type == FCGI_END_REQUEST)
522 dump_fcgi_end_request_body(p,
523 (struct fcgi_end_request_body *)(h + 1));
527 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
529 log_debug("%sversion: %d", p, h->version);
530 log_debug("%stype: %d", p, h->type);
531 log_debug("%srequestId: %d", p, ntohs(h->id));
532 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
533 log_debug("%spaddingLength: %d", p, h->padding_len);
534 log_debug("%sreserved: %d", p, h->reserved);
538 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
540 log_debug("%srole %d", p, ntohs(b->role));
541 log_debug("%sflags %d", p, b->flags);
545 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
547 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
548 log_debug("%sprotocolStatus: %d", p, b->protocol_status);