Blame


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