2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Test program for fastcgi. It speaks the protocol over stdin.
19 * Can't handle more than one request at the same time.
22 #include "../config.h"
24 #include <sys/socket.h>
35 #define FCGI_VERSION_1 1
37 /* subset of records that matters to us */
38 #define FCGI_BEGIN_REQUEST 1
39 #define FCGI_END_REQUEST 3
44 #define SUM(a, b) (((a) << 8) + (b))
57 struct fcgi_end_req_body {
58 unsigned char app_status3;
59 unsigned char app_status2;
60 unsigned char app_status1;
61 unsigned char app_status0;
62 unsigned char proto_status;
63 unsigned char reserved[3];
67 prepare_header(struct fcgi_header *h, int type, int id, size_t size,
70 memset(h, 0, sizeof(*h));
72 h->version = FCGI_VERSION_1;
74 h->req_id1 = (id >> 8);
75 h->req_id0 = (id & 0xFF);
76 h->content_len1 = (size >> 8);
77 h->content_len0 = (size & 0xFF);
84 must_read(int sock, void *d, size_t len)
90 switch (r = read(sock, data, len)) {
103 must_write(int sock, const void *d, size_t len)
105 const uint8_t *data = d;
109 switch (w = write(sock, data, len)) {
122 consume(int fd, size_t len)
128 if ((l = len) > sizeof(buf))
130 must_read(fd, buf, l);
138 read_header(int fd, struct fcgi_header *hdr)
140 must_read(fd, hdr, sizeof(*hdr));
143 /* read and consume a record of the given type */
145 assert_record(int fd, int type)
147 struct fcgi_header hdr;
149 read_header(fd, &hdr);
151 if (hdr.type != type)
152 errx(1, "expected record type %d; got %d",
155 consume(fd, SUM(hdr.content_len1, hdr.content_len0));
156 consume(fd, hdr.padding);
160 main(int argc, char **argv)
162 struct fcgi_header hdr;
163 struct fcgi_end_req_body end;
164 struct sockaddr_un sun;
170 while ((ch = getopt(argc, argv, "")) != -1)
171 errx(1, "wrong usage");
175 errx(1, "wrong usage");
179 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
182 memset(&sun, 0, sizeof(sun));
183 sun.sun_family = AF_UNIX;
184 (void)strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
186 if (unlink(path) == -1 && errno != ENOENT)
187 err(1, "unlink %s", path);
189 if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
192 if (listen(sock, 5) == -1)
196 if ((s = accept(sock, NULL, NULL)) == -1) {
197 warn("retrying; accept failed");
201 assert_record(s, FCGI_BEGIN_REQUEST);
205 read_header(s, &hdr);
207 consume(s, SUM(hdr.content_len1, hdr.content_len0));
208 consume(s, hdr.padding);
210 if (hdr.type != FCGI_PARAMS)
211 errx(1, "got %d; expecting PARAMS", hdr.type);
213 if (hdr.content_len0 == 0 &&
214 hdr.content_len1 == 0 &&
219 assert_record(s, FCGI_STDIN);
221 msg = "20 text/gemini\r\n# hello from fastcgi!\n";
224 prepare_header(&hdr, FCGI_STDOUT, 1, len, 0);
225 must_write(s, &hdr, sizeof(hdr));
226 must_write(s, msg, len);
228 msg = "some more content in the page...\n";
231 prepare_header(&hdr, FCGI_STDOUT, 1, len, 0);
232 must_write(s, &hdr, sizeof(hdr));
233 must_write(s, msg, len);
235 prepare_header(&hdr, FCGI_END_REQUEST, 1, sizeof(end), 0);
236 write(s, &hdr, sizeof(hdr));
237 memset(&end, 0, sizeof(end));
238 write(s, &end, sizeof(end));