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"
38 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
39 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
41 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
42 void fcgi_send_response(struct request *, struct fcgi_response *);
44 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
45 void dump_fcgi_begin_request_body(const char *,
46 struct fcgi_begin_request_body *);
47 void dump_fcgi_end_request_body(const char *,
48 struct fcgi_end_request_body *);
50 extern int cgi_inflight;
51 extern volatile int client_cnt;
54 fcgi_request(int fd, short events, void *arg)
56 struct request *c = arg;
60 n = read(fd, c->buf + c->buf_pos + c->buf_len,
61 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
75 log_debug("closed connection");
84 * Parse the records as they are received. Per the FastCGI
85 * specification, the server need only receive the FastCGI
86 * parameter records in full; it is free to begin execution
87 * at that point, which is what happens here.
90 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
95 } while (parsed > 0 && c->buf_len > 0);
97 /* Make space for further reads */
100 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
105 fcgi_cleanup_request(c);
109 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
111 struct fcgi_record_header *h;
113 if (n < sizeof(struct fcgi_record_header))
116 h = (struct fcgi_record_header*) buf;
118 dump_fcgi_record("", h);
120 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
125 log_warn("wrong version");
128 case FCGI_BEGIN_REQUEST:
129 fcgi_parse_begin_request(buf +
130 sizeof(struct fcgi_record_header),
131 ntohs(h->content_len), c, ntohs(h->id));
134 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
135 ntohs(h->content_len), c, ntohs(h->id));
138 case FCGI_ABORT_REQUEST:
139 fcgi_create_end_record(c);
140 fcgi_cleanup_request(c);
143 log_warn("unimplemented type %d", h->type);
147 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
152 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
153 struct request *c, uint16_t id)
155 /* XXX -- FCGI_CANT_MPX_CONN */
156 if (c->request_started) {
157 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
161 if (n != sizeof(struct fcgi_begin_request_body)) {
162 log_warn("wrong size %d != %lu", n,
163 sizeof(struct fcgi_begin_request_body));
167 c->request_started = 1;
175 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
177 struct env_val *env_entry;
178 uint32_t name_len, val_len;
179 uint8_t *sd, *dr_buf;
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);
197 if (buf[0] >> 7 == 0) {
203 name_len = ((buf[0] & 0x7f) << 24) +
204 (buf[1] << 16) + (buf[2] << 8) + buf[3];
212 if (buf[0] >> 7 == 0) {
218 val_len = ((buf[0] & 0x7f) << 24) +
219 (buf[1] << 16) + (buf[2] << 8) +
229 if (n < name_len + val_len)
232 if ((env_entry = malloc(sizeof(struct env_val))) == NULL) {
233 log_warn("cannot malloc env_entry");
237 if ((env_entry->val = calloc(sizeof(char), name_len + val_len +
239 log_warn("cannot allocate env_entry->val");
244 bcopy(buf, env_entry->val, name_len);
248 env_entry->val[name_len] = '\0';
249 if (val_len < MAX_QUERYSTRING && strcmp(env_entry->val,
250 "QUERY_STRING") == 0 && c->querystring[0] == '\0') {
251 bcopy(buf, c->querystring, val_len);
252 c->querystring[val_len] = '\0';
254 if (val_len < GOTWEBD_MAXTEXT && strcmp(env_entry->val,
255 "HTTP_HOST") == 0 && c->http_host[0] == '\0') {
258 * lazily get subdomain
259 * will only get domain if no subdomain exists
260 * this can still work if gotweb server name is the same
262 sd = strchr(buf, '.');
266 bcopy(buf, c->http_host, val_len);
267 c->http_host[val_len] = '\0';
269 if (val_len < MAX_DOCUMENT_ROOT && strcmp(env_entry->val,
270 "DOCUMENT_ROOT") == 0 && c->document_root[0] == '\0') {
272 /* drop first char, as it's always / */
275 bcopy(dr_buf, c->document_root, val_len - 1);
276 c->document_root[val_len] = '\0';
278 if (val_len < MAX_SERVER_NAME && strcmp(env_entry->val,
279 "SERVER_NAME") == 0 && c->server_name[0] == '\0') {
280 /* drop first char, as it's always / */
282 bcopy(buf, c->server_name, val_len);
283 c->server_name[val_len] = '\0';
285 env_entry->val[name_len] = '=';
287 bcopy(buf, (env_entry->val) + name_len + 1, val_len);
291 SLIST_INSERT_HEAD(&c->env, env_entry, entry);
292 log_debug("env[%d], %s", c->env_count, env_entry->val);
298 fcgi_timeout(int fd, short events, void *arg)
300 fcgi_cleanup_request((struct request*) arg);
304 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
306 struct fcgi_response *resp;
307 struct fcgi_record_header *header;
311 if (c->sock->client_status == CLIENT_DISCONNECT)
317 if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
318 log_warn("%s: cannot calloc fcgi_response", __func__);
322 header = (struct fcgi_record_header*) resp->data;
324 header->type = FCGI_STDOUT;
325 header->id = htons(c->id);
326 header->padding_len = 0;
327 header->reserved = 0;
329 for (i = 0; i < len; i++) {
330 resp->data[i+8] = data[i];
334 header->content_len = htons(n);
336 resp->data_len = n + sizeof(struct fcgi_record_header);
337 fcgi_send_response(c, resp);
343 fcgi_gen_response(struct request *c, const char *data)
345 struct fcgi_response *resp;
346 struct fcgi_record_header *header;
350 if (c->sock->client_status == CLIENT_DISCONNECT)
356 if (strlen(data) == 0)
359 if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
360 log_warn("%s: cannot calloc fcgi_response", __func__);
364 header = (struct fcgi_record_header*) resp->data;
366 header->type = FCGI_STDOUT;
367 header->id = htons(c->id);
368 header->padding_len = 0;
369 header->reserved = 0;
371 for (i = 0; i < strlen(data); i++) {
372 resp->data[i+8] = data[i];
376 header->content_len = htons(n);
378 resp->data_len = n + sizeof(struct fcgi_record_header);
379 fcgi_send_response(c, resp);
385 fcgi_send_response(struct request *c, struct fcgi_response *resp)
387 struct fcgi_record_header *header;
390 int err = 0, th = 2000;
395 header = (struct fcgi_record_header*)resp->data;
397 /* The FastCGI spec suggests to align the output buffer */
398 padded_len = FCGI_ALIGN(resp->data_len);
399 if (padded_len > resp->data_len) {
400 /* There should always be FCGI_PADDING_SIZE bytes left */
401 if (padded_len > FCGI_RECORD_SIZE)
402 log_warn("response too long");
403 header->padding_len = padded_len - resp->data_len;
404 resp->data_len = padded_len;
407 dump_fcgi_record("resp ", header);
410 * XXX: add some simple write heuristics here
411 * On slower VMs, spotty connections, etc., we don't want to go right to
412 * disconnect. Let's at least try to write the data a few times before
415 while ((write(c->fd, resp->data + resp->data_pos,
416 resp->data_len)) == -1) {
417 nanosleep(&ts, NULL);
420 c->sock->client_status = CLIENT_DISCONNECT;
429 fcgi_create_end_record(struct request *c)
431 struct fcgi_response *resp;
432 struct fcgi_record_header *header;
433 struct fcgi_end_request_body *end_request;
435 if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
436 log_warn("cannot calloc fcgi_response");
439 header = (struct fcgi_record_header*) resp->data;
441 header->type = FCGI_END_REQUEST;
442 header->id = htons(c->id);
443 header->content_len = htons(sizeof(struct
444 fcgi_end_request_body));
445 header->padding_len = 0;
446 header->reserved = 0;
447 end_request = (struct fcgi_end_request_body *) (resp->data +
448 sizeof(struct fcgi_record_header));
449 end_request->app_status = htonl(0); /* script_status */
450 end_request->protocol_status = FCGI_REQUEST_COMPLETE;
451 end_request->reserved[0] = 0;
452 end_request->reserved[1] = 0;
453 end_request->reserved[2] = 0;
455 resp->data_len = sizeof(struct fcgi_end_request_body) +
456 sizeof(struct fcgi_record_header);
457 fcgi_send_response(c, resp);
461 fcgi_cleanup_request(struct request *c)
466 evtimer_del(&c->tmo);
467 if (event_initialized(&c->ev))
471 gotweb_free_transport(c->t);
476 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
478 dump_fcgi_record_header(p, h);
480 if (h->type == FCGI_BEGIN_REQUEST)
481 dump_fcgi_begin_request_body(p,
482 (struct fcgi_begin_request_body *)(h + 1));
483 else if (h->type == FCGI_END_REQUEST)
484 dump_fcgi_end_request_body(p,
485 (struct fcgi_end_request_body *)(h + 1));
489 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
491 log_debug("%sversion: %d", p, h->version);
492 log_debug("%stype: %d", p, h->type);
493 log_debug("%srequestId: %d", p, ntohs(h->id));
494 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
495 log_debug("%spaddingLength: %d", p, h->padding_len);
496 log_debug("%sreserved: %d", p, h->reserved);
500 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
502 log_debug("%srole %d", p, ntohs(b->role));
503 log_debug("%sflags %d", p, b->flags);
507 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
509 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
510 log_debug("%sprotocolStatus: %d", p, b->protocol_status);