Blame


1 56324ebe 2022-07-14 thomas /*
2 56324ebe 2022-07-14 thomas * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 56324ebe 2022-07-14 thomas * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
4 56324ebe 2022-07-14 thomas * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
5 56324ebe 2022-07-14 thomas *
6 56324ebe 2022-07-14 thomas * Permission to use, copy, modify, and distribute this software for any
7 56324ebe 2022-07-14 thomas * purpose with or without fee is hereby granted, provided that the above
8 56324ebe 2022-07-14 thomas * copyright notice and this permission notice appear in all copies.
9 56324ebe 2022-07-14 thomas *
10 56324ebe 2022-07-14 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 56324ebe 2022-07-14 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 56324ebe 2022-07-14 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 56324ebe 2022-07-14 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 56324ebe 2022-07-14 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 56324ebe 2022-07-14 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 56324ebe 2022-07-14 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 56324ebe 2022-07-14 thomas */
18 56324ebe 2022-07-14 thomas
19 56324ebe 2022-07-14 thomas #include <arpa/inet.h>
20 56324ebe 2022-07-14 thomas #include <sys/socket.h>
21 56324ebe 2022-07-14 thomas #include <sys/types.h>
22 56324ebe 2022-07-14 thomas
23 56324ebe 2022-07-14 thomas #include <errno.h>
24 56324ebe 2022-07-14 thomas #include <event.h>
25 56324ebe 2022-07-14 thomas #include <stdlib.h>
26 56324ebe 2022-07-14 thomas #include <stdio.h>
27 56324ebe 2022-07-14 thomas #include <string.h>
28 56324ebe 2022-07-14 thomas #include <time.h>
29 56324ebe 2022-07-14 thomas #include <unistd.h>
30 56324ebe 2022-07-14 thomas
31 56324ebe 2022-07-14 thomas #include "got_error.h"
32 d7cad54e 2022-07-14 thomas
33 d7cad54e 2022-07-14 thomas #include "got_compat.h"
34 56324ebe 2022-07-14 thomas
35 56324ebe 2022-07-14 thomas #include "proc.h"
36 56324ebe 2022-07-14 thomas #include "gotwebd.h"
37 56324ebe 2022-07-14 thomas
38 56324ebe 2022-07-14 thomas size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
39 56324ebe 2022-07-14 thomas void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
40 56324ebe 2022-07-14 thomas uint16_t);
41 56324ebe 2022-07-14 thomas void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
42 56324ebe 2022-07-14 thomas void fcgi_send_response(struct request *, struct fcgi_response *);
43 56324ebe 2022-07-14 thomas
44 56324ebe 2022-07-14 thomas void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
45 56324ebe 2022-07-14 thomas void dump_fcgi_begin_request_body(const char *,
46 56324ebe 2022-07-14 thomas struct fcgi_begin_request_body *);
47 56324ebe 2022-07-14 thomas void dump_fcgi_end_request_body(const char *,
48 56324ebe 2022-07-14 thomas struct fcgi_end_request_body *);
49 56324ebe 2022-07-14 thomas
50 56324ebe 2022-07-14 thomas extern int cgi_inflight;
51 56324ebe 2022-07-14 thomas extern volatile int client_cnt;
52 56324ebe 2022-07-14 thomas
53 56324ebe 2022-07-14 thomas void
54 56324ebe 2022-07-14 thomas fcgi_request(int fd, short events, void *arg)
55 56324ebe 2022-07-14 thomas {
56 56324ebe 2022-07-14 thomas struct request *c = arg;
57 56324ebe 2022-07-14 thomas ssize_t n;
58 56324ebe 2022-07-14 thomas size_t parsed = 0;
59 56324ebe 2022-07-14 thomas
60 56324ebe 2022-07-14 thomas n = read(fd, c->buf + c->buf_pos + c->buf_len,
61 56324ebe 2022-07-14 thomas FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
62 56324ebe 2022-07-14 thomas
63 56324ebe 2022-07-14 thomas switch (n) {
64 56324ebe 2022-07-14 thomas case -1:
65 56324ebe 2022-07-14 thomas switch (errno) {
66 56324ebe 2022-07-14 thomas case EINTR:
67 56324ebe 2022-07-14 thomas case EAGAIN:
68 56324ebe 2022-07-14 thomas return;
69 56324ebe 2022-07-14 thomas default:
70 56324ebe 2022-07-14 thomas goto fail;
71 56324ebe 2022-07-14 thomas }
72 56324ebe 2022-07-14 thomas break;
73 56324ebe 2022-07-14 thomas
74 56324ebe 2022-07-14 thomas case 0:
75 56324ebe 2022-07-14 thomas log_debug("closed connection");
76 56324ebe 2022-07-14 thomas goto fail;
77 56324ebe 2022-07-14 thomas default:
78 56324ebe 2022-07-14 thomas break;
79 56324ebe 2022-07-14 thomas }
80 56324ebe 2022-07-14 thomas
81 56324ebe 2022-07-14 thomas c->buf_len += n;
82 56324ebe 2022-07-14 thomas
83 56324ebe 2022-07-14 thomas /*
84 56324ebe 2022-07-14 thomas * Parse the records as they are received. Per the FastCGI
85 56324ebe 2022-07-14 thomas * specification, the server need only receive the FastCGI
86 56324ebe 2022-07-14 thomas * parameter records in full; it is free to begin execution
87 56324ebe 2022-07-14 thomas * at that point, which is what happens here.
88 56324ebe 2022-07-14 thomas */
89 56324ebe 2022-07-14 thomas do {
90 56324ebe 2022-07-14 thomas parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
91 56324ebe 2022-07-14 thomas if (parsed != 0) {
92 56324ebe 2022-07-14 thomas c->buf_pos += parsed;
93 56324ebe 2022-07-14 thomas c->buf_len -= parsed;
94 56324ebe 2022-07-14 thomas }
95 56324ebe 2022-07-14 thomas } while (parsed > 0 && c->buf_len > 0);
96 56324ebe 2022-07-14 thomas
97 56324ebe 2022-07-14 thomas /* Make space for further reads */
98 56324ebe 2022-07-14 thomas if (parsed != 0)
99 56324ebe 2022-07-14 thomas if (c->buf_len > 0) {
100 56324ebe 2022-07-14 thomas bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
101 56324ebe 2022-07-14 thomas c->buf_pos = 0;
102 56324ebe 2022-07-14 thomas }
103 56324ebe 2022-07-14 thomas return;
104 56324ebe 2022-07-14 thomas fail:
105 56324ebe 2022-07-14 thomas fcgi_cleanup_request(c);
106 56324ebe 2022-07-14 thomas }
107 56324ebe 2022-07-14 thomas
108 56324ebe 2022-07-14 thomas size_t
109 56324ebe 2022-07-14 thomas fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
110 56324ebe 2022-07-14 thomas {
111 56324ebe 2022-07-14 thomas struct fcgi_record_header *h;
112 56324ebe 2022-07-14 thomas
113 56324ebe 2022-07-14 thomas if (n < sizeof(struct fcgi_record_header))
114 56324ebe 2022-07-14 thomas return 0;
115 56324ebe 2022-07-14 thomas
116 56324ebe 2022-07-14 thomas h = (struct fcgi_record_header*) buf;
117 56324ebe 2022-07-14 thomas
118 56324ebe 2022-07-14 thomas dump_fcgi_record("", h);
119 56324ebe 2022-07-14 thomas
120 56324ebe 2022-07-14 thomas if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
121 56324ebe 2022-07-14 thomas + h->padding_len)
122 56324ebe 2022-07-14 thomas return 0;
123 56324ebe 2022-07-14 thomas
124 56324ebe 2022-07-14 thomas if (h->version != 1)
125 56324ebe 2022-07-14 thomas log_warn("wrong version");
126 56324ebe 2022-07-14 thomas
127 56324ebe 2022-07-14 thomas switch (h->type) {
128 56324ebe 2022-07-14 thomas case FCGI_BEGIN_REQUEST:
129 56324ebe 2022-07-14 thomas fcgi_parse_begin_request(buf +
130 56324ebe 2022-07-14 thomas sizeof(struct fcgi_record_header),
131 56324ebe 2022-07-14 thomas ntohs(h->content_len), c, ntohs(h->id));
132 56324ebe 2022-07-14 thomas break;
133 56324ebe 2022-07-14 thomas case FCGI_PARAMS:
134 56324ebe 2022-07-14 thomas fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
135 56324ebe 2022-07-14 thomas ntohs(h->content_len), c, ntohs(h->id));
136 56324ebe 2022-07-14 thomas break;
137 56324ebe 2022-07-14 thomas case FCGI_STDIN:
138 56324ebe 2022-07-14 thomas case FCGI_ABORT_REQUEST:
139 56324ebe 2022-07-14 thomas fcgi_create_end_record(c);
140 56324ebe 2022-07-14 thomas fcgi_cleanup_request(c);
141 56324ebe 2022-07-14 thomas return 0;
142 56324ebe 2022-07-14 thomas default:
143 56324ebe 2022-07-14 thomas log_warn("unimplemented type %d", h->type);
144 56324ebe 2022-07-14 thomas break;
145 56324ebe 2022-07-14 thomas }
146 56324ebe 2022-07-14 thomas
147 56324ebe 2022-07-14 thomas return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
148 56324ebe 2022-07-14 thomas + h->padding_len);
149 56324ebe 2022-07-14 thomas }
150 56324ebe 2022-07-14 thomas
151 56324ebe 2022-07-14 thomas void
152 56324ebe 2022-07-14 thomas fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
153 56324ebe 2022-07-14 thomas struct request *c, uint16_t id)
154 56324ebe 2022-07-14 thomas {
155 56324ebe 2022-07-14 thomas /* XXX -- FCGI_CANT_MPX_CONN */
156 56324ebe 2022-07-14 thomas if (c->request_started) {
157 56324ebe 2022-07-14 thomas log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
158 56324ebe 2022-07-14 thomas return;
159 56324ebe 2022-07-14 thomas }
160 56324ebe 2022-07-14 thomas
161 56324ebe 2022-07-14 thomas if (n != sizeof(struct fcgi_begin_request_body)) {
162 56324ebe 2022-07-14 thomas log_warn("wrong size %d != %lu", n,
163 56324ebe 2022-07-14 thomas sizeof(struct fcgi_begin_request_body));
164 56324ebe 2022-07-14 thomas return;
165 56324ebe 2022-07-14 thomas }
166 56324ebe 2022-07-14 thomas
167 56324ebe 2022-07-14 thomas c->request_started = 1;
168 56324ebe 2022-07-14 thomas
169 56324ebe 2022-07-14 thomas c->id = id;
170 56324ebe 2022-07-14 thomas SLIST_INIT(&c->env);
171 56324ebe 2022-07-14 thomas c->env_count = 0;
172 56324ebe 2022-07-14 thomas }
173 56324ebe 2022-07-14 thomas
174 56324ebe 2022-07-14 thomas void
175 56324ebe 2022-07-14 thomas fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
176 56324ebe 2022-07-14 thomas {
177 56324ebe 2022-07-14 thomas struct env_val *env_entry;
178 56324ebe 2022-07-14 thomas uint32_t name_len, val_len;
179 56324ebe 2022-07-14 thomas uint8_t *sd, *dr_buf;
180 56324ebe 2022-07-14 thomas
181 56324ebe 2022-07-14 thomas if (!c->request_started) {
182 56324ebe 2022-07-14 thomas log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
183 56324ebe 2022-07-14 thomas return;
184 56324ebe 2022-07-14 thomas }
185 56324ebe 2022-07-14 thomas
186 56324ebe 2022-07-14 thomas if (c->id != id) {
187 56324ebe 2022-07-14 thomas log_warn("unexpected id, ignoring");
188 56324ebe 2022-07-14 thomas return;
189 56324ebe 2022-07-14 thomas }
190 56324ebe 2022-07-14 thomas
191 56324ebe 2022-07-14 thomas if (n == 0) {
192 56324ebe 2022-07-14 thomas gotweb_process_request(c);
193 56324ebe 2022-07-14 thomas return;
194 56324ebe 2022-07-14 thomas }
195 56324ebe 2022-07-14 thomas
196 56324ebe 2022-07-14 thomas while (n > 0) {
197 56324ebe 2022-07-14 thomas if (buf[0] >> 7 == 0) {
198 56324ebe 2022-07-14 thomas name_len = buf[0];
199 56324ebe 2022-07-14 thomas n--;
200 56324ebe 2022-07-14 thomas buf++;
201 56324ebe 2022-07-14 thomas } else {
202 56324ebe 2022-07-14 thomas if (n > 3) {
203 56324ebe 2022-07-14 thomas name_len = ((buf[0] & 0x7f) << 24) +
204 56324ebe 2022-07-14 thomas (buf[1] << 16) + (buf[2] << 8) + buf[3];
205 56324ebe 2022-07-14 thomas n -= 4;
206 56324ebe 2022-07-14 thomas buf += 4;
207 56324ebe 2022-07-14 thomas } else
208 56324ebe 2022-07-14 thomas return;
209 56324ebe 2022-07-14 thomas }
210 56324ebe 2022-07-14 thomas
211 56324ebe 2022-07-14 thomas if (n > 0) {
212 56324ebe 2022-07-14 thomas if (buf[0] >> 7 == 0) {
213 56324ebe 2022-07-14 thomas val_len = buf[0];
214 56324ebe 2022-07-14 thomas n--;
215 56324ebe 2022-07-14 thomas buf++;
216 56324ebe 2022-07-14 thomas } else {
217 56324ebe 2022-07-14 thomas if (n > 3) {
218 56324ebe 2022-07-14 thomas val_len = ((buf[0] & 0x7f) << 24) +
219 56324ebe 2022-07-14 thomas (buf[1] << 16) + (buf[2] << 8) +
220 56324ebe 2022-07-14 thomas buf[3];
221 56324ebe 2022-07-14 thomas n -= 4;
222 56324ebe 2022-07-14 thomas buf += 4;
223 56324ebe 2022-07-14 thomas } else
224 56324ebe 2022-07-14 thomas return;
225 56324ebe 2022-07-14 thomas }
226 56324ebe 2022-07-14 thomas } else
227 56324ebe 2022-07-14 thomas return;
228 56324ebe 2022-07-14 thomas
229 56324ebe 2022-07-14 thomas if (n < name_len + val_len)
230 56324ebe 2022-07-14 thomas return;
231 56324ebe 2022-07-14 thomas
232 56324ebe 2022-07-14 thomas if ((env_entry = malloc(sizeof(struct env_val))) == NULL) {
233 56324ebe 2022-07-14 thomas log_warn("cannot malloc env_entry");
234 56324ebe 2022-07-14 thomas return;
235 56324ebe 2022-07-14 thomas }
236 56324ebe 2022-07-14 thomas
237 56324ebe 2022-07-14 thomas if ((env_entry->val = calloc(sizeof(char), name_len + val_len +
238 56324ebe 2022-07-14 thomas 2)) == NULL) {
239 56324ebe 2022-07-14 thomas log_warn("cannot allocate env_entry->val");
240 56324ebe 2022-07-14 thomas free(env_entry);
241 56324ebe 2022-07-14 thomas return;
242 56324ebe 2022-07-14 thomas }
243 56324ebe 2022-07-14 thomas
244 56324ebe 2022-07-14 thomas bcopy(buf, env_entry->val, name_len);
245 56324ebe 2022-07-14 thomas buf += name_len;
246 56324ebe 2022-07-14 thomas n -= name_len;
247 56324ebe 2022-07-14 thomas
248 56324ebe 2022-07-14 thomas env_entry->val[name_len] = '\0';
249 56324ebe 2022-07-14 thomas if (val_len < MAX_QUERYSTRING && strcmp(env_entry->val,
250 56324ebe 2022-07-14 thomas "QUERY_STRING") == 0 && c->querystring[0] == '\0') {
251 56324ebe 2022-07-14 thomas bcopy(buf, c->querystring, val_len);
252 56324ebe 2022-07-14 thomas c->querystring[val_len] = '\0';
253 56324ebe 2022-07-14 thomas }
254 56324ebe 2022-07-14 thomas if (val_len < GOTWEBD_MAXTEXT && strcmp(env_entry->val,
255 56324ebe 2022-07-14 thomas "HTTP_HOST") == 0 && c->http_host[0] == '\0') {
256 56324ebe 2022-07-14 thomas
257 56324ebe 2022-07-14 thomas /*
258 56324ebe 2022-07-14 thomas * lazily get subdomain
259 56324ebe 2022-07-14 thomas * will only get domain if no subdomain exists
260 56324ebe 2022-07-14 thomas * this can still work if gotweb server name is the same
261 56324ebe 2022-07-14 thomas */
262 56324ebe 2022-07-14 thomas sd = strchr(buf, '.');
263 56324ebe 2022-07-14 thomas if (sd)
264 56324ebe 2022-07-14 thomas *sd = '\0';
265 56324ebe 2022-07-14 thomas
266 56324ebe 2022-07-14 thomas bcopy(buf, c->http_host, val_len);
267 56324ebe 2022-07-14 thomas c->http_host[val_len] = '\0';
268 56324ebe 2022-07-14 thomas }
269 56324ebe 2022-07-14 thomas if (val_len < MAX_DOCUMENT_ROOT && strcmp(env_entry->val,
270 56324ebe 2022-07-14 thomas "DOCUMENT_ROOT") == 0 && c->document_root[0] == '\0') {
271 56324ebe 2022-07-14 thomas
272 56324ebe 2022-07-14 thomas /* drop first char, as it's always / */
273 56324ebe 2022-07-14 thomas dr_buf = &buf[1];
274 56324ebe 2022-07-14 thomas
275 56324ebe 2022-07-14 thomas bcopy(dr_buf, c->document_root, val_len - 1);
276 56324ebe 2022-07-14 thomas c->document_root[val_len] = '\0';
277 56324ebe 2022-07-14 thomas }
278 56324ebe 2022-07-14 thomas if (val_len < MAX_SERVER_NAME && strcmp(env_entry->val,
279 56324ebe 2022-07-14 thomas "SERVER_NAME") == 0 && c->server_name[0] == '\0') {
280 56324ebe 2022-07-14 thomas /* drop first char, as it's always / */
281 56324ebe 2022-07-14 thomas
282 56324ebe 2022-07-14 thomas bcopy(buf, c->server_name, val_len);
283 56324ebe 2022-07-14 thomas c->server_name[val_len] = '\0';
284 56324ebe 2022-07-14 thomas }
285 56324ebe 2022-07-14 thomas env_entry->val[name_len] = '=';
286 56324ebe 2022-07-14 thomas
287 56324ebe 2022-07-14 thomas bcopy(buf, (env_entry->val) + name_len + 1, val_len);
288 56324ebe 2022-07-14 thomas buf += val_len;
289 56324ebe 2022-07-14 thomas n -= val_len;
290 56324ebe 2022-07-14 thomas
291 56324ebe 2022-07-14 thomas SLIST_INSERT_HEAD(&c->env, env_entry, entry);
292 56324ebe 2022-07-14 thomas log_debug("env[%d], %s", c->env_count, env_entry->val);
293 56324ebe 2022-07-14 thomas c->env_count++;
294 56324ebe 2022-07-14 thomas }
295 56324ebe 2022-07-14 thomas }
296 56324ebe 2022-07-14 thomas
297 56324ebe 2022-07-14 thomas void
298 56324ebe 2022-07-14 thomas fcgi_timeout(int fd, short events, void *arg)
299 56324ebe 2022-07-14 thomas {
300 56324ebe 2022-07-14 thomas fcgi_cleanup_request((struct request*) arg);
301 56324ebe 2022-07-14 thomas }
302 56324ebe 2022-07-14 thomas
303 56324ebe 2022-07-14 thomas int
304 56324ebe 2022-07-14 thomas fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
305 56324ebe 2022-07-14 thomas {
306 56324ebe 2022-07-14 thomas struct fcgi_response *resp;
307 56324ebe 2022-07-14 thomas struct fcgi_record_header *header;
308 56324ebe 2022-07-14 thomas ssize_t n = 0;
309 56324ebe 2022-07-14 thomas int i;
310 56324ebe 2022-07-14 thomas
311 56324ebe 2022-07-14 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
312 56324ebe 2022-07-14 thomas return -1;
313 56324ebe 2022-07-14 thomas
314 56324ebe 2022-07-14 thomas if (data == NULL)
315 56324ebe 2022-07-14 thomas return 0;
316 56324ebe 2022-07-14 thomas
317 56324ebe 2022-07-14 thomas if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
318 56324ebe 2022-07-14 thomas log_warn("%s: cannot calloc fcgi_response", __func__);
319 56324ebe 2022-07-14 thomas return -1;
320 56324ebe 2022-07-14 thomas }
321 56324ebe 2022-07-14 thomas
322 56324ebe 2022-07-14 thomas header = (struct fcgi_record_header*) resp->data;
323 56324ebe 2022-07-14 thomas header->version = 1;
324 56324ebe 2022-07-14 thomas header->type = FCGI_STDOUT;
325 56324ebe 2022-07-14 thomas header->id = htons(c->id);
326 56324ebe 2022-07-14 thomas header->padding_len = 0;
327 56324ebe 2022-07-14 thomas header->reserved = 0;
328 56324ebe 2022-07-14 thomas
329 56324ebe 2022-07-14 thomas for (i = 0; i < len; i++) {
330 56324ebe 2022-07-14 thomas resp->data[i+8] = data[i];
331 56324ebe 2022-07-14 thomas n++;
332 56324ebe 2022-07-14 thomas }
333 56324ebe 2022-07-14 thomas
334 56324ebe 2022-07-14 thomas header->content_len = htons(n);
335 56324ebe 2022-07-14 thomas resp->data_pos = 0;
336 56324ebe 2022-07-14 thomas resp->data_len = n + sizeof(struct fcgi_record_header);
337 56324ebe 2022-07-14 thomas fcgi_send_response(c, resp);
338 56324ebe 2022-07-14 thomas
339 56324ebe 2022-07-14 thomas return 0;
340 56324ebe 2022-07-14 thomas }
341 56324ebe 2022-07-14 thomas
342 56324ebe 2022-07-14 thomas int
343 56324ebe 2022-07-14 thomas fcgi_gen_response(struct request *c, const char *data)
344 56324ebe 2022-07-14 thomas {
345 56324ebe 2022-07-14 thomas struct fcgi_response *resp;
346 56324ebe 2022-07-14 thomas struct fcgi_record_header *header;
347 56324ebe 2022-07-14 thomas ssize_t n = 0;
348 56324ebe 2022-07-14 thomas int i;
349 56324ebe 2022-07-14 thomas
350 56324ebe 2022-07-14 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
351 56324ebe 2022-07-14 thomas return -1;
352 56324ebe 2022-07-14 thomas
353 56324ebe 2022-07-14 thomas if (data == NULL)
354 56324ebe 2022-07-14 thomas return 0;
355 56324ebe 2022-07-14 thomas
356 56324ebe 2022-07-14 thomas if (strlen(data) == 0)
357 56324ebe 2022-07-14 thomas return 0;
358 56324ebe 2022-07-14 thomas
359 56324ebe 2022-07-14 thomas if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
360 56324ebe 2022-07-14 thomas log_warn("%s: cannot calloc fcgi_response", __func__);
361 56324ebe 2022-07-14 thomas return -1;
362 56324ebe 2022-07-14 thomas }
363 56324ebe 2022-07-14 thomas
364 56324ebe 2022-07-14 thomas header = (struct fcgi_record_header*) resp->data;
365 56324ebe 2022-07-14 thomas header->version = 1;
366 56324ebe 2022-07-14 thomas header->type = FCGI_STDOUT;
367 56324ebe 2022-07-14 thomas header->id = htons(c->id);
368 56324ebe 2022-07-14 thomas header->padding_len = 0;
369 56324ebe 2022-07-14 thomas header->reserved = 0;
370 56324ebe 2022-07-14 thomas
371 56324ebe 2022-07-14 thomas for (i = 0; i < strlen(data); i++) {
372 56324ebe 2022-07-14 thomas resp->data[i+8] = data[i];
373 56324ebe 2022-07-14 thomas n++;
374 56324ebe 2022-07-14 thomas }
375 56324ebe 2022-07-14 thomas
376 56324ebe 2022-07-14 thomas header->content_len = htons(n);
377 56324ebe 2022-07-14 thomas resp->data_pos = 0;
378 56324ebe 2022-07-14 thomas resp->data_len = n + sizeof(struct fcgi_record_header);
379 56324ebe 2022-07-14 thomas fcgi_send_response(c, resp);
380 56324ebe 2022-07-14 thomas
381 56324ebe 2022-07-14 thomas return 0;
382 56324ebe 2022-07-14 thomas }
383 56324ebe 2022-07-14 thomas
384 56324ebe 2022-07-14 thomas void
385 56324ebe 2022-07-14 thomas fcgi_send_response(struct request *c, struct fcgi_response *resp)
386 56324ebe 2022-07-14 thomas {
387 56324ebe 2022-07-14 thomas struct fcgi_record_header *header;
388 56324ebe 2022-07-14 thomas struct timespec ts;
389 56324ebe 2022-07-14 thomas size_t padded_len;
390 56324ebe 2022-07-14 thomas int err = 0, th = 2000;
391 56324ebe 2022-07-14 thomas
392 56324ebe 2022-07-14 thomas ts.tv_sec = 0;
393 56324ebe 2022-07-14 thomas ts.tv_nsec = 50;
394 56324ebe 2022-07-14 thomas
395 56324ebe 2022-07-14 thomas header = (struct fcgi_record_header*)resp->data;
396 56324ebe 2022-07-14 thomas
397 56324ebe 2022-07-14 thomas /* The FastCGI spec suggests to align the output buffer */
398 56324ebe 2022-07-14 thomas padded_len = FCGI_ALIGN(resp->data_len);
399 56324ebe 2022-07-14 thomas if (padded_len > resp->data_len) {
400 56324ebe 2022-07-14 thomas /* There should always be FCGI_PADDING_SIZE bytes left */
401 56324ebe 2022-07-14 thomas if (padded_len > FCGI_RECORD_SIZE)
402 56324ebe 2022-07-14 thomas log_warn("response too long");
403 56324ebe 2022-07-14 thomas header->padding_len = padded_len - resp->data_len;
404 56324ebe 2022-07-14 thomas resp->data_len = padded_len;
405 56324ebe 2022-07-14 thomas }
406 56324ebe 2022-07-14 thomas
407 56324ebe 2022-07-14 thomas dump_fcgi_record("resp ", header);
408 56324ebe 2022-07-14 thomas
409 56324ebe 2022-07-14 thomas /*
410 56324ebe 2022-07-14 thomas * XXX: add some simple write heuristics here
411 56324ebe 2022-07-14 thomas * On slower VMs, spotty connections, etc., we don't want to go right to
412 56324ebe 2022-07-14 thomas * disconnect. Let's at least try to write the data a few times before
413 56324ebe 2022-07-14 thomas * giving up.
414 56324ebe 2022-07-14 thomas */
415 56324ebe 2022-07-14 thomas while ((write(c->fd, resp->data + resp->data_pos,
416 56324ebe 2022-07-14 thomas resp->data_len)) == -1) {
417 56324ebe 2022-07-14 thomas nanosleep(&ts, NULL);
418 56324ebe 2022-07-14 thomas err++;
419 56324ebe 2022-07-14 thomas if (err == th) {
420 56324ebe 2022-07-14 thomas c->sock->client_status = CLIENT_DISCONNECT;
421 56324ebe 2022-07-14 thomas break;
422 56324ebe 2022-07-14 thomas }
423 56324ebe 2022-07-14 thomas }
424 56324ebe 2022-07-14 thomas
425 56324ebe 2022-07-14 thomas free(resp);
426 56324ebe 2022-07-14 thomas }
427 56324ebe 2022-07-14 thomas
428 56324ebe 2022-07-14 thomas void
429 56324ebe 2022-07-14 thomas fcgi_create_end_record(struct request *c)
430 56324ebe 2022-07-14 thomas {
431 56324ebe 2022-07-14 thomas struct fcgi_response *resp;
432 56324ebe 2022-07-14 thomas struct fcgi_record_header *header;
433 56324ebe 2022-07-14 thomas struct fcgi_end_request_body *end_request;
434 56324ebe 2022-07-14 thomas
435 56324ebe 2022-07-14 thomas if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
436 56324ebe 2022-07-14 thomas log_warn("cannot calloc fcgi_response");
437 56324ebe 2022-07-14 thomas return;
438 56324ebe 2022-07-14 thomas }
439 56324ebe 2022-07-14 thomas header = (struct fcgi_record_header*) resp->data;
440 56324ebe 2022-07-14 thomas header->version = 1;
441 56324ebe 2022-07-14 thomas header->type = FCGI_END_REQUEST;
442 56324ebe 2022-07-14 thomas header->id = htons(c->id);
443 56324ebe 2022-07-14 thomas header->content_len = htons(sizeof(struct
444 56324ebe 2022-07-14 thomas fcgi_end_request_body));
445 56324ebe 2022-07-14 thomas header->padding_len = 0;
446 56324ebe 2022-07-14 thomas header->reserved = 0;
447 56324ebe 2022-07-14 thomas end_request = (struct fcgi_end_request_body *) (resp->data +
448 56324ebe 2022-07-14 thomas sizeof(struct fcgi_record_header));
449 56324ebe 2022-07-14 thomas end_request->app_status = htonl(0); /* script_status */
450 56324ebe 2022-07-14 thomas end_request->protocol_status = FCGI_REQUEST_COMPLETE;
451 56324ebe 2022-07-14 thomas end_request->reserved[0] = 0;
452 56324ebe 2022-07-14 thomas end_request->reserved[1] = 0;
453 56324ebe 2022-07-14 thomas end_request->reserved[2] = 0;
454 56324ebe 2022-07-14 thomas resp->data_pos = 0;
455 56324ebe 2022-07-14 thomas resp->data_len = sizeof(struct fcgi_end_request_body) +
456 56324ebe 2022-07-14 thomas sizeof(struct fcgi_record_header);
457 56324ebe 2022-07-14 thomas fcgi_send_response(c, resp);
458 56324ebe 2022-07-14 thomas }
459 56324ebe 2022-07-14 thomas
460 56324ebe 2022-07-14 thomas void
461 56324ebe 2022-07-14 thomas fcgi_cleanup_request(struct request *c)
462 56324ebe 2022-07-14 thomas {
463 56324ebe 2022-07-14 thomas cgi_inflight--;
464 56324ebe 2022-07-14 thomas client_cnt--;
465 56324ebe 2022-07-14 thomas
466 56324ebe 2022-07-14 thomas evtimer_del(&c->tmo);
467 56324ebe 2022-07-14 thomas if (event_initialized(&c->ev))
468 56324ebe 2022-07-14 thomas event_del(&c->ev);
469 56324ebe 2022-07-14 thomas
470 56324ebe 2022-07-14 thomas close(c->fd);
471 56324ebe 2022-07-14 thomas gotweb_free_transport(c->t);
472 56324ebe 2022-07-14 thomas free(c);
473 56324ebe 2022-07-14 thomas }
474 56324ebe 2022-07-14 thomas
475 56324ebe 2022-07-14 thomas void
476 56324ebe 2022-07-14 thomas dump_fcgi_record(const char *p, struct fcgi_record_header *h)
477 56324ebe 2022-07-14 thomas {
478 56324ebe 2022-07-14 thomas dump_fcgi_record_header(p, h);
479 56324ebe 2022-07-14 thomas
480 56324ebe 2022-07-14 thomas if (h->type == FCGI_BEGIN_REQUEST)
481 56324ebe 2022-07-14 thomas dump_fcgi_begin_request_body(p,
482 56324ebe 2022-07-14 thomas (struct fcgi_begin_request_body *)(h + 1));
483 56324ebe 2022-07-14 thomas else if (h->type == FCGI_END_REQUEST)
484 56324ebe 2022-07-14 thomas dump_fcgi_end_request_body(p,
485 56324ebe 2022-07-14 thomas (struct fcgi_end_request_body *)(h + 1));
486 56324ebe 2022-07-14 thomas }
487 56324ebe 2022-07-14 thomas
488 56324ebe 2022-07-14 thomas void
489 56324ebe 2022-07-14 thomas dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
490 56324ebe 2022-07-14 thomas {
491 56324ebe 2022-07-14 thomas log_debug("%sversion: %d", p, h->version);
492 56324ebe 2022-07-14 thomas log_debug("%stype: %d", p, h->type);
493 56324ebe 2022-07-14 thomas log_debug("%srequestId: %d", p, ntohs(h->id));
494 56324ebe 2022-07-14 thomas log_debug("%scontentLength: %d", p, ntohs(h->content_len));
495 56324ebe 2022-07-14 thomas log_debug("%spaddingLength: %d", p, h->padding_len);
496 56324ebe 2022-07-14 thomas log_debug("%sreserved: %d", p, h->reserved);
497 56324ebe 2022-07-14 thomas }
498 56324ebe 2022-07-14 thomas
499 56324ebe 2022-07-14 thomas void
500 56324ebe 2022-07-14 thomas dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
501 56324ebe 2022-07-14 thomas {
502 56324ebe 2022-07-14 thomas log_debug("%srole %d", p, ntohs(b->role));
503 56324ebe 2022-07-14 thomas log_debug("%sflags %d", p, b->flags);
504 56324ebe 2022-07-14 thomas }
505 56324ebe 2022-07-14 thomas
506 56324ebe 2022-07-14 thomas void
507 56324ebe 2022-07-14 thomas dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
508 56324ebe 2022-07-14 thomas {
509 56324ebe 2022-07-14 thomas log_debug("%sappStatus: %d", p, ntohl(b->app_status));
510 56324ebe 2022-07-14 thomas log_debug("%sprotocolStatus: %d", p, b->protocol_status);
511 56324ebe 2022-07-14 thomas }