Blame


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