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"
40 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
41 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
43 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
44 int fcgi_send_response(struct request *, int, const void *, size_t);
46 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
47 void dump_fcgi_begin_request_body(const char *,
48 struct fcgi_begin_request_body *);
49 void dump_fcgi_end_request_body(const char *,
50 struct fcgi_end_request_body *);
52 extern int cgi_inflight;
53 extern volatile int client_cnt;
56 fcgi_request(int fd, short events, void *arg)
58 struct request *c = arg;
62 n = read(fd, c->buf + c->buf_pos + c->buf_len,
63 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
77 log_debug("closed connection");
86 * Parse the records as they are received. Per the FastCGI
87 * specification, the server need only receive the FastCGI
88 * parameter records in full; it is free to begin execution
89 * at that point, which is what happens here.
92 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
97 } while (parsed > 0 && c->buf_len > 0);
99 /* Make space for further reads */
101 if (c->buf_len > 0) {
102 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
107 fcgi_cleanup_request(c);
111 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
113 struct fcgi_record_header *h;
115 if (n < sizeof(struct fcgi_record_header))
118 h = (struct fcgi_record_header*) buf;
120 dump_fcgi_record("", h);
122 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
127 log_warn("wrong version");
130 case FCGI_BEGIN_REQUEST:
131 fcgi_parse_begin_request(buf +
132 sizeof(struct fcgi_record_header),
133 ntohs(h->content_len), c, ntohs(h->id));
136 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
137 ntohs(h->content_len), c, ntohs(h->id));
140 case FCGI_ABORT_REQUEST:
141 if (c->sock->client_status != CLIENT_DISCONNECT &&
142 c->outbuf_len != 0) {
143 fcgi_send_response(c, FCGI_STDOUT, c->outbuf,
147 fcgi_create_end_record(c);
148 fcgi_cleanup_request(c);
151 log_warn("unimplemented type %d", h->type);
155 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
160 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
161 struct request *c, uint16_t id)
163 /* XXX -- FCGI_CANT_MPX_CONN */
164 if (c->request_started) {
165 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
169 if (n != sizeof(struct fcgi_begin_request_body)) {
170 log_warn("wrong size %d != %lu", n,
171 sizeof(struct fcgi_begin_request_body));
175 c->request_started = 1;
183 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
185 struct env_val *env_entry;
186 uint32_t name_len, val_len;
187 uint8_t *sd, *dr_buf;
189 if (!c->request_started) {
190 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
195 log_warn("unexpected id, ignoring");
200 gotweb_process_request(c);
205 if (buf[0] >> 7 == 0) {
211 name_len = ((buf[0] & 0x7f) << 24) +
212 (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) +
237 if (n < name_len + val_len)
240 if ((env_entry = malloc(sizeof(struct env_val))) == NULL) {
241 log_warn("cannot malloc env_entry");
245 if ((env_entry->val = calloc(sizeof(char), name_len + val_len +
247 log_warn("cannot allocate env_entry->val");
252 bcopy(buf, env_entry->val, name_len);
256 env_entry->val[name_len] = '\0';
257 if (val_len < MAX_QUERYSTRING && strcmp(env_entry->val,
258 "QUERY_STRING") == 0 && c->querystring[0] == '\0') {
259 bcopy(buf, c->querystring, val_len);
260 c->querystring[val_len] = '\0';
262 if (val_len < GOTWEBD_MAXTEXT && strcmp(env_entry->val,
263 "HTTP_HOST") == 0 && c->http_host[0] == '\0') {
266 * lazily get subdomain
267 * will only get domain if no subdomain exists
268 * this can still work if gotweb server name is the same
270 sd = strchr(buf, '.');
274 bcopy(buf, c->http_host, val_len);
275 c->http_host[val_len] = '\0';
277 if (val_len < MAX_DOCUMENT_ROOT && strcmp(env_entry->val,
278 "DOCUMENT_ROOT") == 0 && c->document_root[0] == '\0') {
280 /* drop first char, as it's always / */
283 bcopy(dr_buf, c->document_root, val_len - 1);
284 c->document_root[val_len] = '\0';
286 if (val_len < MAX_SERVER_NAME && strcmp(env_entry->val,
287 "SERVER_NAME") == 0 && c->server_name[0] == '\0') {
288 /* drop first char, as it's always / */
290 bcopy(buf, c->server_name, val_len);
291 c->server_name[val_len] = '\0';
293 env_entry->val[name_len] = '=';
295 bcopy(buf, (env_entry->val) + name_len + 1, val_len);
299 SLIST_INSERT_HEAD(&c->env, env_entry, entry);
300 log_debug("env[%d], %s", c->env_count, env_entry->val);
306 fcgi_timeout(int fd, short events, void *arg)
308 fcgi_cleanup_request((struct request*) arg);
312 fcgi_printf(struct request *c, const char *fmt, ...)
319 r = vasprintf(&str, fmt, ap);
323 log_warn("%s: asprintf", __func__);
327 r = fcgi_gen_binary_response(c, str, r);
333 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
337 if (c->sock->client_status == CLIENT_DISCONNECT)
340 if (data == NULL || len == 0)
344 * special case: send big replies -like blobs- directly
347 if (len > sizeof(c->outbuf)) {
348 if (c->outbuf_len > 0) {
349 fcgi_send_response(c, FCGI_STDOUT,
350 c->outbuf, c->outbuf_len);
353 return fcgi_send_response(c, FCGI_STDOUT, data, len);
356 if (len < sizeof(c->outbuf) - c->outbuf_len) {
357 memcpy(c->outbuf + c->outbuf_len, data, len);
358 c->outbuf_len += len;
362 r = fcgi_send_response(c, FCGI_STDOUT, c->outbuf, c->outbuf_len);
366 memcpy(c->outbuf, data, len);
372 send_response(struct request *c, int type, const uint8_t *data,
375 static const uint8_t padding[FCGI_PADDING_SIZE];
376 struct fcgi_record_header header;
380 size_t padded_len, tot;
381 int i, err = 0, th = 2000;
386 memset(&header, 0, sizeof(header));
389 header.id = htons(c->id);
390 header.content_len = htons(len);
392 /* The FastCGI spec suggests to align the output buffer */
393 tot = sizeof(header) + len;
394 padded_len = FCGI_ALIGN(tot);
395 if (padded_len > tot) {
396 header.padding_len = padded_len - tot;
397 tot += header.padding_len;
400 iov[0].iov_base = &header;
401 iov[0].iov_len = sizeof(header);
403 iov[1].iov_base = (void *)data;
404 iov[1].iov_len = len;
406 iov[2].iov_base = (void *)padding;
407 iov[2].iov_len = header.padding_len;
409 dump_fcgi_record("resp ", &header);
412 * XXX: add some simple write heuristics here
413 * On slower VMs, spotty connections, etc., we don't want to go right to
414 * disconnect. Let's at least try to write the data a few times before
418 nw = writev(c->fd, iov, nitems(iov));
420 c->sock->client_status = CLIENT_DISCONNECT;
425 if (errno == EAGAIN && err < th) {
426 nanosleep(&ts, NULL);
429 log_warn("%s: write failure", __func__);
430 c->sock->client_status = CLIENT_DISCONNECT;
435 log_debug("%s: partial write: %zu vs %zu", __func__,
439 for (i = 0; i < nitems(iov); ++i) {
440 if (nw < iov[i].iov_len) {
441 iov[i].iov_base += nw;
442 iov[i].iov_len -= nw;
445 nw -= iov[i].iov_len;
454 fcgi_send_response(struct request *c, int type, const void *data,
457 if (c->sock->client_status == CLIENT_DISCONNECT)
460 while (len > FCGI_CONTENT_SIZE) {
461 if (send_response(c, type, data, len) == -1)
464 data += FCGI_CONTENT_SIZE;
465 len -= FCGI_CONTENT_SIZE;
471 return send_response(c, type, data, len);
475 fcgi_create_end_record(struct request *c)
477 struct fcgi_end_request_body end_request;
479 memset(&end_request, 0, sizeof(end_request));
480 end_request.app_status = htonl(0); /* script status */
481 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
483 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
484 sizeof(end_request));
488 fcgi_cleanup_request(struct request *c)
493 evtimer_del(&c->tmo);
494 if (event_initialized(&c->ev))
498 gotweb_free_transport(c->t);
503 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
505 dump_fcgi_record_header(p, h);
507 if (h->type == FCGI_BEGIN_REQUEST)
508 dump_fcgi_begin_request_body(p,
509 (struct fcgi_begin_request_body *)(h + 1));
510 else if (h->type == FCGI_END_REQUEST)
511 dump_fcgi_end_request_body(p,
512 (struct fcgi_end_request_body *)(h + 1));
516 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
518 log_debug("%sversion: %d", p, h->version);
519 log_debug("%stype: %d", p, h->type);
520 log_debug("%srequestId: %d", p, ntohs(h->id));
521 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
522 log_debug("%spaddingLength: %d", p, h->padding_len);
523 log_debug("%sreserved: %d", p, h->reserved);
527 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
529 log_debug("%srole %d", p, ntohs(b->role));
530 log_debug("%sflags %d", p, b->flags);
534 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
536 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
537 log_debug("%sprotocolStatus: %d", p, b->protocol_status);