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>
33 #include "got_error.h"
35 #include "got_compat.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 void 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 fcgi_create_end_record(c);
142 fcgi_cleanup_request(c);
145 log_warn("unimplemented type %d", h->type);
149 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
154 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
155 struct request *c, uint16_t id)
157 /* XXX -- FCGI_CANT_MPX_CONN */
158 if (c->request_started) {
159 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
163 if (n != sizeof(struct fcgi_begin_request_body)) {
164 log_warn("wrong size %d != %lu", n,
165 sizeof(struct fcgi_begin_request_body));
169 c->request_started = 1;
177 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
179 struct env_val *env_entry;
180 uint32_t name_len, val_len;
181 uint8_t *sd, *dr_buf;
183 if (!c->request_started) {
184 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
189 log_warn("unexpected id, ignoring");
194 gotweb_process_request(c);
199 if (buf[0] >> 7 == 0) {
205 name_len = ((buf[0] & 0x7f) << 24) +
206 (buf[1] << 16) + (buf[2] << 8) + buf[3];
214 if (buf[0] >> 7 == 0) {
220 val_len = ((buf[0] & 0x7f) << 24) +
221 (buf[1] << 16) + (buf[2] << 8) +
231 if (n < name_len + val_len)
234 if ((env_entry = malloc(sizeof(struct env_val))) == NULL) {
235 log_warn("cannot malloc env_entry");
239 if ((env_entry->val = calloc(sizeof(char), name_len + val_len +
241 log_warn("cannot allocate env_entry->val");
246 bcopy(buf, env_entry->val, name_len);
250 env_entry->val[name_len] = '\0';
251 if (val_len < MAX_QUERYSTRING && strcmp(env_entry->val,
252 "QUERY_STRING") == 0 && c->querystring[0] == '\0') {
253 bcopy(buf, c->querystring, val_len);
254 c->querystring[val_len] = '\0';
256 if (val_len < GOTWEBD_MAXTEXT && strcmp(env_entry->val,
257 "HTTP_HOST") == 0 && c->http_host[0] == '\0') {
260 * lazily get subdomain
261 * will only get domain if no subdomain exists
262 * this can still work if gotweb server name is the same
264 sd = strchr(buf, '.');
268 bcopy(buf, c->http_host, val_len);
269 c->http_host[val_len] = '\0';
271 if (val_len < MAX_DOCUMENT_ROOT && strcmp(env_entry->val,
272 "DOCUMENT_ROOT") == 0 && c->document_root[0] == '\0') {
274 /* drop first char, as it's always / */
277 bcopy(dr_buf, c->document_root, val_len - 1);
278 c->document_root[val_len] = '\0';
280 if (val_len < MAX_SERVER_NAME && strcmp(env_entry->val,
281 "SERVER_NAME") == 0 && c->server_name[0] == '\0') {
282 /* drop first char, as it's always / */
284 bcopy(buf, c->server_name, val_len);
285 c->server_name[val_len] = '\0';
287 env_entry->val[name_len] = '=';
289 bcopy(buf, (env_entry->val) + name_len + 1, val_len);
293 SLIST_INSERT_HEAD(&c->env, env_entry, entry);
294 log_debug("env[%d], %s", c->env_count, env_entry->val);
300 fcgi_timeout(int fd, short events, void *arg)
302 fcgi_cleanup_request((struct request*) arg);
306 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
308 if (c->sock->client_status == CLIENT_DISCONNECT)
311 if (data == NULL || len == 0)
314 fcgi_send_response(c, FCGI_STDOUT, data, len);
319 fcgi_gen_response(struct request *c, const char *data)
321 if (data == NULL || *data == '\0')
323 return fcgi_gen_binary_response(c, data, strlen(data));
327 send_response(struct request *c, int type, const uint8_t *data,
330 static const uint8_t padding[FCGI_PADDING_SIZE];
331 struct fcgi_record_header header;
335 size_t padded_len, tot;
336 int i, err = 0, th = 2000;
341 memset(&header, 0, sizeof(header));
344 header.id = htons(c->id);
345 header.content_len = htons(len);
347 /* The FastCGI spec suggests to align the output buffer */
348 tot = sizeof(header) + len;
349 padded_len = FCGI_ALIGN(tot);
350 if (padded_len > tot) {
351 header.padding_len = padded_len - tot;
352 tot += header.padding_len;
355 iov[0].iov_base = &header;
356 iov[0].iov_len = sizeof(header);
358 iov[1].iov_base = (void *)data;
359 iov[1].iov_len = len;
361 iov[2].iov_base = (void *)padding;
362 iov[2].iov_len = header.padding_len;
364 dump_fcgi_record("resp ", &header);
367 * XXX: add some simple write heuristics here
368 * On slower VMs, spotty connections, etc., we don't want to go right to
369 * disconnect. Let's at least try to write the data a few times before
373 nw = writev(c->fd, iov, nitems(iov));
375 c->sock->client_status = CLIENT_DISCONNECT;
380 if (errno == EAGAIN && err < th) {
381 nanosleep(&ts, NULL);
384 log_warn("%s: write failure", __func__);
385 c->sock->client_status = CLIENT_DISCONNECT;
390 log_debug("%s: partial write: %zu vs %zu", __func__,
394 for (i = 0; i < nitems(iov); ++i) {
395 if (nw < iov[i].iov_len) {
396 iov[i].iov_base += nw;
397 iov[i].iov_len -= nw;
400 nw -= iov[i].iov_len;
407 fcgi_send_response(struct request *c, int type, const void *data,
410 while (len > FCGI_CONTENT_SIZE) {
411 send_response(c, type, data, len);
412 if (c->sock->client_status == CLIENT_DISCONNECT)
415 data += FCGI_CONTENT_SIZE;
416 len -= FCGI_CONTENT_SIZE;
422 send_response(c, type, data, len);
426 fcgi_create_end_record(struct request *c)
428 struct fcgi_end_request_body end_request;
430 memset(&end_request, 0, sizeof(end_request));
431 end_request.app_status = htonl(0); /* script status */
432 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
434 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
435 sizeof(end_request));
439 fcgi_cleanup_request(struct request *c)
444 evtimer_del(&c->tmo);
445 if (event_initialized(&c->ev))
449 gotweb_free_transport(c->t);
454 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
456 dump_fcgi_record_header(p, h);
458 if (h->type == FCGI_BEGIN_REQUEST)
459 dump_fcgi_begin_request_body(p,
460 (struct fcgi_begin_request_body *)(h + 1));
461 else if (h->type == FCGI_END_REQUEST)
462 dump_fcgi_end_request_body(p,
463 (struct fcgi_end_request_body *)(h + 1));
467 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
469 log_debug("%sversion: %d", p, h->version);
470 log_debug("%stype: %d", p, h->type);
471 log_debug("%srequestId: %d", p, ntohs(h->id));
472 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
473 log_debug("%spaddingLength: %d", p, h->padding_len);
474 log_debug("%sreserved: %d", p, h->reserved);
478 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
480 log_debug("%srole %d", p, ntohs(b->role));
481 log_debug("%sflags %d", p, b->flags);
485 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
487 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
488 log_debug("%sprotocolStatus: %d", p, b->protocol_status);