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>
32 #include "got_error.h"
34 #include "got_compat.h"
39 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
40 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
42 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
43 void fcgi_send_response(struct request *, struct fcgi_response *);
45 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
46 void dump_fcgi_begin_request_body(const char *,
47 struct fcgi_begin_request_body *);
48 void dump_fcgi_end_request_body(const char *,
49 struct fcgi_end_request_body *);
51 extern int cgi_inflight;
52 extern volatile int client_cnt;
55 fcgi_request(int fd, short events, void *arg)
57 struct request *c = arg;
61 n = read(fd, c->buf + c->buf_pos + c->buf_len,
62 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
76 log_debug("closed connection");
85 * Parse the records as they are received. Per the FastCGI
86 * specification, the server need only receive the FastCGI
87 * parameter records in full; it is free to begin execution
88 * at that point, which is what happens here.
91 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
96 } while (parsed > 0 && c->buf_len > 0);
98 /* Make space for further reads */
100 if (c->buf_len > 0) {
101 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
106 fcgi_cleanup_request(c);
110 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
112 struct fcgi_record_header *h;
114 if (n < sizeof(struct fcgi_record_header))
117 h = (struct fcgi_record_header*) buf;
119 dump_fcgi_record("", h);
121 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
126 log_warn("wrong version");
129 case FCGI_BEGIN_REQUEST:
130 fcgi_parse_begin_request(buf +
131 sizeof(struct fcgi_record_header),
132 ntohs(h->content_len), c, ntohs(h->id));
135 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
136 ntohs(h->content_len), c, ntohs(h->id));
139 case FCGI_ABORT_REQUEST:
140 fcgi_create_end_record(c);
141 fcgi_cleanup_request(c);
144 log_warn("unimplemented type %d", h->type);
148 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
153 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
154 struct request *c, uint16_t id)
156 /* XXX -- FCGI_CANT_MPX_CONN */
157 if (c->request_started) {
158 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
162 if (n != sizeof(struct fcgi_begin_request_body)) {
163 log_warn("wrong size %d != %lu", n,
164 sizeof(struct fcgi_begin_request_body));
168 c->request_started = 1;
176 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
178 struct env_val *env_entry;
179 uint32_t name_len, val_len;
180 uint8_t *sd, *dr_buf;
182 if (!c->request_started) {
183 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
188 log_warn("unexpected id, ignoring");
193 gotweb_process_request(c);
198 if (buf[0] >> 7 == 0) {
204 name_len = ((buf[0] & 0x7f) << 24) +
205 (buf[1] << 16) + (buf[2] << 8) + buf[3];
213 if (buf[0] >> 7 == 0) {
219 val_len = ((buf[0] & 0x7f) << 24) +
220 (buf[1] << 16) + (buf[2] << 8) +
230 if (n < name_len + val_len)
233 if ((env_entry = malloc(sizeof(struct env_val))) == NULL) {
234 log_warn("cannot malloc env_entry");
238 if ((env_entry->val = calloc(sizeof(char), name_len + val_len +
240 log_warn("cannot allocate env_entry->val");
245 bcopy(buf, env_entry->val, name_len);
249 env_entry->val[name_len] = '\0';
250 if (val_len < MAX_QUERYSTRING && strcmp(env_entry->val,
251 "QUERY_STRING") == 0 && c->querystring[0] == '\0') {
252 bcopy(buf, c->querystring, val_len);
253 c->querystring[val_len] = '\0';
255 if (val_len < GOTWEBD_MAXTEXT && strcmp(env_entry->val,
256 "HTTP_HOST") == 0 && c->http_host[0] == '\0') {
259 * lazily get subdomain
260 * will only get domain if no subdomain exists
261 * this can still work if gotweb server name is the same
263 sd = strchr(buf, '.');
267 bcopy(buf, c->http_host, val_len);
268 c->http_host[val_len] = '\0';
270 if (val_len < MAX_DOCUMENT_ROOT && strcmp(env_entry->val,
271 "DOCUMENT_ROOT") == 0 && c->document_root[0] == '\0') {
273 /* drop first char, as it's always / */
276 bcopy(dr_buf, c->document_root, val_len - 1);
277 c->document_root[val_len] = '\0';
279 if (val_len < MAX_SERVER_NAME && strcmp(env_entry->val,
280 "SERVER_NAME") == 0 && c->server_name[0] == '\0') {
281 /* drop first char, as it's always / */
283 bcopy(buf, c->server_name, val_len);
284 c->server_name[val_len] = '\0';
286 env_entry->val[name_len] = '=';
288 bcopy(buf, (env_entry->val) + name_len + 1, val_len);
292 SLIST_INSERT_HEAD(&c->env, env_entry, entry);
293 log_debug("env[%d], %s", c->env_count, env_entry->val);
299 fcgi_timeout(int fd, short events, void *arg)
301 fcgi_cleanup_request((struct request*) arg);
305 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
307 struct fcgi_response *resp;
308 struct fcgi_record_header *header;
312 if (c->sock->client_status == CLIENT_DISCONNECT)
318 if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
319 log_warn("%s: cannot calloc fcgi_response", __func__);
323 header = (struct fcgi_record_header*) resp->data;
325 header->type = FCGI_STDOUT;
326 header->id = htons(c->id);
327 header->padding_len = 0;
328 header->reserved = 0;
330 for (i = 0; i < len; i++) {
331 resp->data[i+8] = data[i];
335 header->content_len = htons(n);
337 resp->data_len = n + sizeof(struct fcgi_record_header);
338 fcgi_send_response(c, resp);
344 fcgi_gen_response(struct request *c, const char *data)
346 struct fcgi_response *resp;
347 struct fcgi_record_header *header;
351 if (c->sock->client_status == CLIENT_DISCONNECT)
357 if (strlen(data) == 0)
360 if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
361 log_warn("%s: cannot calloc fcgi_response", __func__);
365 header = (struct fcgi_record_header*) resp->data;
367 header->type = FCGI_STDOUT;
368 header->id = htons(c->id);
369 header->padding_len = 0;
370 header->reserved = 0;
372 for (i = 0; i < strlen(data); i++) {
373 resp->data[i+8] = data[i];
377 header->content_len = htons(n);
379 resp->data_len = n + sizeof(struct fcgi_record_header);
380 fcgi_send_response(c, resp);
386 fcgi_send_response(struct request *c, struct fcgi_response *resp)
388 struct fcgi_record_header *header;
391 int err = 0, th = 2000;
396 header = (struct fcgi_record_header*)resp->data;
398 /* The FastCGI spec suggests to align the output buffer */
399 padded_len = FCGI_ALIGN(resp->data_len);
400 if (padded_len > resp->data_len) {
401 /* There should always be FCGI_PADDING_SIZE bytes left */
402 if (padded_len > FCGI_RECORD_SIZE)
403 log_warn("response too long");
404 header->padding_len = padded_len - resp->data_len;
405 resp->data_len = padded_len;
408 dump_fcgi_record("resp ", header);
411 * XXX: add some simple write heuristics here
412 * On slower VMs, spotty connections, etc., we don't want to go right to
413 * disconnect. Let's at least try to write the data a few times before
416 while ((write(c->fd, resp->data + resp->data_pos,
417 resp->data_len)) == -1) {
418 nanosleep(&ts, NULL);
421 c->sock->client_status = CLIENT_DISCONNECT;
430 fcgi_create_end_record(struct request *c)
432 struct fcgi_response *resp;
433 struct fcgi_record_header *header;
434 struct fcgi_end_request_body *end_request;
436 if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
437 log_warn("cannot calloc fcgi_response");
440 header = (struct fcgi_record_header*) resp->data;
442 header->type = FCGI_END_REQUEST;
443 header->id = htons(c->id);
444 header->content_len = htons(sizeof(struct
445 fcgi_end_request_body));
446 header->padding_len = 0;
447 header->reserved = 0;
448 end_request = (struct fcgi_end_request_body *) (resp->data +
449 sizeof(struct fcgi_record_header));
450 end_request->app_status = htonl(0); /* script_status */
451 end_request->protocol_status = FCGI_REQUEST_COMPLETE;
452 end_request->reserved[0] = 0;
453 end_request->reserved[1] = 0;
454 end_request->reserved[2] = 0;
456 resp->data_len = sizeof(struct fcgi_end_request_body) +
457 sizeof(struct fcgi_record_header);
458 fcgi_send_response(c, resp);
462 fcgi_cleanup_request(struct request *c)
467 evtimer_del(&c->tmo);
468 if (event_initialized(&c->ev))
472 gotweb_free_transport(c->t);
477 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
479 dump_fcgi_record_header(p, h);
481 if (h->type == FCGI_BEGIN_REQUEST)
482 dump_fcgi_begin_request_body(p,
483 (struct fcgi_begin_request_body *)(h + 1));
484 else if (h->type == FCGI_END_REQUEST)
485 dump_fcgi_end_request_body(p,
486 (struct fcgi_end_request_body *)(h + 1));
490 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
492 log_debug("%sversion: %d", p, h->version);
493 log_debug("%stype: %d", p, h->type);
494 log_debug("%srequestId: %d", p, ntohs(h->id));
495 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
496 log_debug("%spaddingLength: %d", p, h->padding_len);
497 log_debug("%sreserved: %d", p, h->reserved);
501 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
503 log_debug("%srole %d", p, ntohs(b->role));
504 log_debug("%sflags %d", p, b->flags);
508 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
510 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
511 log_debug("%sprotocolStatus: %d", p, b->protocol_status);