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 fcgi_create_end_record(c);
144 fcgi_cleanup_request(c);
147 log_warn("unimplemented type %d", h->type);
151 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
156 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
157 struct request *c, uint16_t id)
159 /* XXX -- FCGI_CANT_MPX_CONN */
160 if (c->request_started) {
161 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
165 if (n != sizeof(struct fcgi_begin_request_body)) {
166 log_warn("wrong size %d != %lu", n,
167 sizeof(struct fcgi_begin_request_body));
171 c->request_started = 1;
176 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
178 uint32_t name_len, val_len;
181 if (!c->request_started) {
182 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
187 log_warn("unexpected id, ignoring");
192 gotweb_process_request(c);
193 template_flush(c->tp);
198 if (buf[0] >> 7 == 0) {
204 name_len = ((buf[0] & 0x7f) << 24) +
205 (buf[1] << 16) + (buf[2] << 8) + buf[3];
215 if (buf[0] >> 7 == 0) {
221 val_len = ((buf[0] & 0x7f) << 24) +
222 (buf[1] << 16) + (buf[2] << 8) +
230 if (n < name_len + val_len)
233 val = buf + name_len;
235 if (c->querystring[0] == '\0' &&
236 val_len < MAX_QUERYSTRING &&
238 strncmp(buf, "QUERY_STRING", 12) == 0) {
239 memcpy(c->querystring, val, val_len);
240 c->querystring[val_len] = '\0';
243 if (c->document_uri[0] == '\0' &&
244 val_len < MAX_DOCUMENT_URI &&
246 strncmp(buf, "DOCUMENT_URI", 12) == 0) {
247 memcpy(c->document_uri, val, val_len);
248 c->document_uri[val_len] = '\0';
251 if (c->server_name[0] == '\0' &&
252 val_len < MAX_SERVER_NAME &&
254 strncmp(buf, "SERVER_NAME", 11) == 0) {
255 memcpy(c->server_name, val, val_len);
256 c->server_name[val_len] = '\0';
260 strncmp(buf, "HTTPS", 5) == 0)
263 buf += name_len + val_len;
264 n -= name_len - val_len;
269 fcgi_timeout(int fd, short events, void *arg)
271 fcgi_cleanup_request((struct request*) arg);
275 send_response(struct request *c, int type, const uint8_t *data,
278 static const uint8_t padding[FCGI_PADDING_SIZE];
279 struct fcgi_record_header header;
283 size_t padded_len, tot;
284 int i, err = 0, th = 2000;
289 memset(&header, 0, sizeof(header));
292 header.id = htons(c->id);
293 header.content_len = htons(len);
295 /* The FastCGI spec suggests to align the output buffer */
296 tot = sizeof(header) + len;
297 padded_len = FCGI_ALIGN(tot);
298 if (padded_len > tot) {
299 header.padding_len = padded_len - tot;
300 tot += header.padding_len;
303 iov[0].iov_base = &header;
304 iov[0].iov_len = sizeof(header);
306 iov[1].iov_base = (void *)data;
307 iov[1].iov_len = len;
309 iov[2].iov_base = (void *)padding;
310 iov[2].iov_len = header.padding_len;
312 dump_fcgi_record("resp ", &header);
315 * XXX: add some simple write heuristics here
316 * On slower VMs, spotty connections, etc., we don't want to go right to
317 * disconnect. Let's at least try to write the data a few times before
321 nw = writev(c->fd, iov, nitems(iov));
323 c->sock->client_status = CLIENT_DISCONNECT;
328 if (errno == EAGAIN && err < th) {
329 nanosleep(&ts, NULL);
332 log_debug("%s: write failure: %s", __func__,
334 c->sock->client_status = CLIENT_DISCONNECT;
339 log_debug("%s: partial write: %zu vs %zu", __func__,
343 for (i = 0; i < nitems(iov); ++i) {
344 if (nw < iov[i].iov_len) {
345 iov[i].iov_base += nw;
346 iov[i].iov_len -= nw;
349 nw -= iov[i].iov_len;
358 fcgi_send_response(struct request *c, int type, const void *data,
361 if (c->sock->client_status == CLIENT_DISCONNECT)
364 while (len > FCGI_CONTENT_SIZE) {
365 if (send_response(c, type, data, len) == -1)
368 data += FCGI_CONTENT_SIZE;
369 len -= FCGI_CONTENT_SIZE;
375 return send_response(c, type, data, len);
379 fcgi_write(void *arg, const void *buf, size_t len)
381 struct request *c = arg;
383 return fcgi_send_response(c, FCGI_STDOUT, buf, len);
387 fcgi_create_end_record(struct request *c)
389 struct fcgi_end_request_body end_request;
391 memset(&end_request, 0, sizeof(end_request));
392 end_request.app_status = htonl(0); /* script status */
393 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
395 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
396 sizeof(end_request));
400 fcgi_cleanup_request(struct request *c)
405 evtimer_del(&c->tmo);
406 if (event_initialized(&c->ev))
410 template_free(c->tp);
412 gotweb_free_transport(c->t);
417 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
419 dump_fcgi_record_header(p, h);
421 if (h->type == FCGI_BEGIN_REQUEST)
422 dump_fcgi_begin_request_body(p,
423 (struct fcgi_begin_request_body *)(h + 1));
424 else if (h->type == FCGI_END_REQUEST)
425 dump_fcgi_end_request_body(p,
426 (struct fcgi_end_request_body *)(h + 1));
430 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
432 log_debug("%sversion: %d", p, h->version);
433 log_debug("%stype: %d", p, h->type);
434 log_debug("%srequestId: %d", p, ntohs(h->id));
435 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
436 log_debug("%spaddingLength: %d", p, h->padding_len);
437 log_debug("%sreserved: %d", p, h->reserved);
441 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
443 log_debug("%srole %d", p, ntohs(b->role));
444 log_debug("%sflags %d", p, b->flags);
448 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
450 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
451 log_debug("%sprotocolStatus: %d", p, b->protocol_status);