2 03fcfb79 2021-06-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 03fcfb79 2021-06-12 op * Permission to use, copy, modify, and distribute this software for any
5 03fcfb79 2021-06-12 op * purpose with or without fee is hereby granted, provided that the above
6 03fcfb79 2021-06-12 op * copyright notice and this permission notice appear in all copies.
8 03fcfb79 2021-06-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 03fcfb79 2021-06-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 03fcfb79 2021-06-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 03fcfb79 2021-06-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 03fcfb79 2021-06-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 03fcfb79 2021-06-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 03fcfb79 2021-06-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 03fcfb79 2021-06-12 op * Test program for fastcgi. It speaks the protocol over stdin.
19 03fcfb79 2021-06-12 op * Can't handle more than one request at the same time.
22 03fcfb79 2021-06-12 op #include "../config.h"
24 cd1ede6d 2023-06-09 op #include <sys/socket.h>
25 cd1ede6d 2023-06-09 op #include <sys/stat.h>
26 cd1ede6d 2023-06-09 op #include <sys/un.h>
28 cd1ede6d 2023-06-09 op #include <errno.h>
29 03fcfb79 2021-06-12 op #include <stdint.h>
30 03fcfb79 2021-06-12 op #include <stdio.h>
31 89c110fe 2021-06-15 op #include <stdlib.h>
32 03fcfb79 2021-06-12 op #include <string.h>
33 03fcfb79 2021-06-12 op #include <unistd.h>
35 03fcfb79 2021-06-12 op #define FCGI_VERSION_1 1
37 03fcfb79 2021-06-12 op /* subset of records that matters to us */
38 03fcfb79 2021-06-12 op #define FCGI_BEGIN_REQUEST 1
39 03fcfb79 2021-06-12 op #define FCGI_END_REQUEST 3
40 03fcfb79 2021-06-12 op #define FCGI_PARAMS 4
41 03fcfb79 2021-06-12 op #define FCGI_STDIN 5
42 03fcfb79 2021-06-12 op #define FCGI_STDOUT 6
44 03fcfb79 2021-06-12 op #define SUM(a, b) (((a) << 8) + (b))
46 03fcfb79 2021-06-12 op struct fcgi_header {
47 03fcfb79 2021-06-12 op uint8_t version;
49 03fcfb79 2021-06-12 op uint8_t req_id1;
50 03fcfb79 2021-06-12 op uint8_t req_id0;
51 03fcfb79 2021-06-12 op uint8_t content_len1;
52 03fcfb79 2021-06-12 op uint8_t content_len0;
53 03fcfb79 2021-06-12 op uint8_t padding;
54 03fcfb79 2021-06-12 op uint8_t reserved;
57 03fcfb79 2021-06-12 op struct fcgi_end_req_body {
58 03fcfb79 2021-06-12 op unsigned char app_status3;
59 03fcfb79 2021-06-12 op unsigned char app_status2;
60 03fcfb79 2021-06-12 op unsigned char app_status1;
61 03fcfb79 2021-06-12 op unsigned char app_status0;
62 03fcfb79 2021-06-12 op unsigned char proto_status;
63 03fcfb79 2021-06-12 op unsigned char reserved[3];
67 03fcfb79 2021-06-12 op prepare_header(struct fcgi_header *h, int type, int id, size_t size,
68 03fcfb79 2021-06-12 op size_t padding)
70 03fcfb79 2021-06-12 op memset(h, 0, sizeof(*h));
72 03fcfb79 2021-06-12 op h->version = FCGI_VERSION_1;
73 03fcfb79 2021-06-12 op h->type = type;
74 03fcfb79 2021-06-12 op h->req_id1 = (id >> 8);
75 03fcfb79 2021-06-12 op h->req_id0 = (id & 0xFF);
76 03fcfb79 2021-06-12 op h->content_len1 = (size >> 8);
77 03fcfb79 2021-06-12 op h->content_len0 = (size & 0xFF);
78 03fcfb79 2021-06-12 op h->padding = padding;
84 03fcfb79 2021-06-12 op must_read(int sock, void *d, size_t len)
88 cd1ede6d 2023-06-09 op while (len > 0) {
89 03fcfb79 2021-06-12 op switch (r = read(sock, d, len)) {
91 cd1ede6d 2023-06-09 op err(1, "read");
93 cd1ede6d 2023-06-09 op errx(1, "EOF");
102 cd1ede6d 2023-06-09 op must_write(int sock, const void *d, size_t len)
106 cd1ede6d 2023-06-09 op while (len > 0) {
107 cd1ede6d 2023-06-09 op switch (w = write(sock, d, len)) {
109 cd1ede6d 2023-06-09 op err(1, "write");
111 cd1ede6d 2023-06-09 op errx(1, "EOF");
120 03fcfb79 2021-06-12 op consume(int fd, size_t len)
123 03fcfb79 2021-06-12 op char buf[64];
125 03fcfb79 2021-06-12 op while (len != 0) {
126 03fcfb79 2021-06-12 op if ((l = len) > sizeof(buf))
127 03fcfb79 2021-06-12 op l = sizeof(buf);
128 cd1ede6d 2023-06-09 op must_read(fd, buf, l);
136 cd1ede6d 2023-06-09 op read_header(int fd, struct fcgi_header *hdr)
138 cd1ede6d 2023-06-09 op must_read(fd, hdr, sizeof(*hdr));
141 03fcfb79 2021-06-12 op /* read and consume a record of the given type */
143 cd1ede6d 2023-06-09 op assert_record(int fd, int type)
145 03fcfb79 2021-06-12 op struct fcgi_header hdr;
147 cd1ede6d 2023-06-09 op read_header(fd, &hdr);
149 03fcfb79 2021-06-12 op if (hdr.type != type)
150 03fcfb79 2021-06-12 op errx(1, "expected record type %d; got %d",
151 03fcfb79 2021-06-12 op type, hdr.type);
153 cd1ede6d 2023-06-09 op consume(fd, SUM(hdr.content_len1, hdr.content_len0));
154 cd1ede6d 2023-06-09 op consume(fd, hdr.padding);
158 cd1ede6d 2023-06-09 op main(int argc, char **argv)
160 03fcfb79 2021-06-12 op struct fcgi_header hdr;
161 03fcfb79 2021-06-12 op struct fcgi_end_req_body end;
162 cd1ede6d 2023-06-09 op struct sockaddr_un sun;
163 cd1ede6d 2023-06-09 op const char *path;
164 03fcfb79 2021-06-12 op const char *msg;
166 cd1ede6d 2023-06-09 op int ch, sock, s;
168 cd1ede6d 2023-06-09 op msg = "20 text/gemini\r\n# hello from fastcgi!\n";
169 03fcfb79 2021-06-12 op len = strlen(msg);
171 cd1ede6d 2023-06-09 op while ((ch = getopt(argc, argv, "")) != -1)
172 cd1ede6d 2023-06-09 op errx(1, "wrong usage");
173 cd1ede6d 2023-06-09 op argc -= optind;
174 cd1ede6d 2023-06-09 op argv += optind;
175 cd1ede6d 2023-06-09 op if (argc != 1)
176 cd1ede6d 2023-06-09 op errx(1, "wrong usage");
178 cd1ede6d 2023-06-09 op path = argv[0];
180 cd1ede6d 2023-06-09 op if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
181 cd1ede6d 2023-06-09 op err(1, "socket");
183 cd1ede6d 2023-06-09 op memset(&sun, 0, sizeof(sun));
184 cd1ede6d 2023-06-09 op sun.sun_family = AF_UNIX;
185 cd1ede6d 2023-06-09 op (void)strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
187 cd1ede6d 2023-06-09 op if (unlink(path) == -1 && errno != ENOENT)
188 cd1ede6d 2023-06-09 op err(1, "unlink %s", path);
190 cd1ede6d 2023-06-09 op if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
191 cd1ede6d 2023-06-09 op err(1, "bind");
193 cd1ede6d 2023-06-09 op if (listen(sock, 5) == -1)
194 cd1ede6d 2023-06-09 op err(1, "listen");
197 cd1ede6d 2023-06-09 op if ((s = accept(sock, NULL, NULL)) == -1) {
198 cd1ede6d 2023-06-09 op warn("retrying; accept failed");
202 cd1ede6d 2023-06-09 op assert_record(s, FCGI_BEGIN_REQUEST);
204 03fcfb79 2021-06-12 op /* read params */
206 cd1ede6d 2023-06-09 op read_header(s, &hdr);
208 cd1ede6d 2023-06-09 op consume(s, SUM(hdr.content_len1, hdr.content_len0));
209 cd1ede6d 2023-06-09 op consume(s, hdr.padding);
211 03fcfb79 2021-06-12 op if (hdr.type != FCGI_PARAMS)
212 03fcfb79 2021-06-12 op errx(1, "got %d; expecting PARAMS", hdr.type);
214 03fcfb79 2021-06-12 op if (hdr.content_len0 == 0 &&
215 03fcfb79 2021-06-12 op hdr.content_len1 == 0 &&
216 03fcfb79 2021-06-12 op hdr.padding == 0)
220 cd1ede6d 2023-06-09 op assert_record(s, FCGI_STDIN);
222 03fcfb79 2021-06-12 op prepare_header(&hdr, FCGI_STDOUT, 1, len, 0);
223 cd1ede6d 2023-06-09 op must_write(s, &hdr, sizeof(hdr));
224 cd1ede6d 2023-06-09 op must_write(s, msg, len);
226 03fcfb79 2021-06-12 op prepare_header(&hdr, FCGI_END_REQUEST, 1, sizeof(end), 0);
227 cd1ede6d 2023-06-09 op write(s, &hdr, sizeof(hdr));
228 03fcfb79 2021-06-12 op memset(&end, 0, sizeof(end));
229 cd1ede6d 2023-06-09 op write(s, &end, sizeof(end));