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)
89 switch (r = read(sock, d, len)) {
102 must_write(int sock, const void *d, size_t len)
107 switch (w = write(sock, d, len)) {
120 consume(int fd, size_t len)
126 if ((l = len) > sizeof(buf))
128 must_read(fd, buf, l);
136 read_header(int fd, struct fcgi_header *hdr)
138 must_read(fd, hdr, sizeof(*hdr));
141 /* read and consume a record of the given type */
143 assert_record(int fd, int type)
145 struct fcgi_header hdr;
147 read_header(fd, &hdr);
149 if (hdr.type != type)
150 errx(1, "expected record type %d; got %d",
153 consume(fd, SUM(hdr.content_len1, hdr.content_len0));
154 consume(fd, hdr.padding);
158 main(int argc, char **argv)
160 struct fcgi_header hdr;
161 struct fcgi_end_req_body end;
162 struct sockaddr_un sun;
168 msg = "20 text/gemini\r\n# hello from fastcgi!\n";
171 while ((ch = getopt(argc, argv, "")) != -1)
172 errx(1, "wrong usage");
176 errx(1, "wrong usage");
180 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
183 memset(&sun, 0, sizeof(sun));
184 sun.sun_family = AF_UNIX;
185 (void)strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
187 if (unlink(path) == -1 && errno != ENOENT)
188 err(1, "unlink %s", path);
190 if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
193 if (listen(sock, 5) == -1)
197 if ((s = accept(sock, NULL, NULL)) == -1) {
198 warn("retrying; accept failed");
202 assert_record(s, FCGI_BEGIN_REQUEST);
206 read_header(s, &hdr);
208 consume(s, SUM(hdr.content_len1, hdr.content_len0));
209 consume(s, hdr.padding);
211 if (hdr.type != FCGI_PARAMS)
212 errx(1, "got %d; expecting PARAMS", hdr.type);
214 if (hdr.content_len0 == 0 &&
215 hdr.content_len1 == 0 &&
220 assert_record(s, FCGI_STDIN);
222 prepare_header(&hdr, FCGI_STDOUT, 1, len, 0);
223 must_write(s, &hdr, sizeof(hdr));
224 must_write(s, msg, len);
226 prepare_header(&hdr, FCGI_END_REQUEST, 1, sizeof(end), 0);
227 write(s, &hdr, sizeof(hdr));
228 memset(&end, 0, sizeof(end));
229 write(s, &end, sizeof(end));