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