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)
86 1959cda3 2023-06-13 op uint8_t *data = d;
89 cd1ede6d 2023-06-09 op while (len > 0) {
90 1959cda3 2023-06-13 op switch (r = read(sock, data, len)) {
92 cd1ede6d 2023-06-09 op err(1, "read");
94 cd1ede6d 2023-06-09 op errx(1, "EOF");
103 cd1ede6d 2023-06-09 op must_write(int sock, const void *d, size_t len)
105 1959cda3 2023-06-13 op const uint8_t *data = d;
108 cd1ede6d 2023-06-09 op while (len > 0) {
109 1959cda3 2023-06-13 op switch (w = write(sock, data, len)) {
111 cd1ede6d 2023-06-09 op err(1, "write");
113 cd1ede6d 2023-06-09 op errx(1, "EOF");
122 03fcfb79 2021-06-12 op consume(int fd, size_t len)
125 03fcfb79 2021-06-12 op char buf[64];
127 03fcfb79 2021-06-12 op while (len != 0) {
128 03fcfb79 2021-06-12 op if ((l = len) > sizeof(buf))
129 03fcfb79 2021-06-12 op l = sizeof(buf);
130 cd1ede6d 2023-06-09 op must_read(fd, buf, l);
138 cd1ede6d 2023-06-09 op read_header(int fd, struct fcgi_header *hdr)
140 cd1ede6d 2023-06-09 op must_read(fd, hdr, sizeof(*hdr));
143 03fcfb79 2021-06-12 op /* read and consume a record of the given type */
145 cd1ede6d 2023-06-09 op assert_record(int fd, int type)
147 03fcfb79 2021-06-12 op struct fcgi_header hdr;
149 cd1ede6d 2023-06-09 op read_header(fd, &hdr);
151 03fcfb79 2021-06-12 op if (hdr.type != type)
152 03fcfb79 2021-06-12 op errx(1, "expected record type %d; got %d",
153 03fcfb79 2021-06-12 op type, hdr.type);
155 cd1ede6d 2023-06-09 op consume(fd, SUM(hdr.content_len1, hdr.content_len0));
156 cd1ede6d 2023-06-09 op consume(fd, hdr.padding);
160 cd1ede6d 2023-06-09 op main(int argc, char **argv)
162 03fcfb79 2021-06-12 op struct fcgi_header hdr;
163 03fcfb79 2021-06-12 op struct fcgi_end_req_body end;
164 cd1ede6d 2023-06-09 op struct sockaddr_un sun;
165 cd1ede6d 2023-06-09 op const char *path;
166 03fcfb79 2021-06-12 op const char *msg;
168 cd1ede6d 2023-06-09 op int ch, sock, s;
170 cd1ede6d 2023-06-09 op while ((ch = getopt(argc, argv, "")) != -1)
171 cd1ede6d 2023-06-09 op errx(1, "wrong usage");
172 cd1ede6d 2023-06-09 op argc -= optind;
173 cd1ede6d 2023-06-09 op argv += optind;
174 cd1ede6d 2023-06-09 op if (argc != 1)
175 cd1ede6d 2023-06-09 op errx(1, "wrong usage");
177 cd1ede6d 2023-06-09 op path = argv[0];
179 cd1ede6d 2023-06-09 op if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
180 cd1ede6d 2023-06-09 op err(1, "socket");
182 cd1ede6d 2023-06-09 op memset(&sun, 0, sizeof(sun));
183 cd1ede6d 2023-06-09 op sun.sun_family = AF_UNIX;
184 cd1ede6d 2023-06-09 op (void)strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
186 cd1ede6d 2023-06-09 op if (unlink(path) == -1 && errno != ENOENT)
187 cd1ede6d 2023-06-09 op err(1, "unlink %s", path);
189 cd1ede6d 2023-06-09 op if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
190 cd1ede6d 2023-06-09 op err(1, "bind");
192 cd1ede6d 2023-06-09 op if (listen(sock, 5) == -1)
193 cd1ede6d 2023-06-09 op err(1, "listen");
196 cd1ede6d 2023-06-09 op if ((s = accept(sock, NULL, NULL)) == -1) {
197 cd1ede6d 2023-06-09 op warn("retrying; accept failed");
201 cd1ede6d 2023-06-09 op assert_record(s, FCGI_BEGIN_REQUEST);
203 03fcfb79 2021-06-12 op /* read params */
205 cd1ede6d 2023-06-09 op read_header(s, &hdr);
207 cd1ede6d 2023-06-09 op consume(s, SUM(hdr.content_len1, hdr.content_len0));
208 cd1ede6d 2023-06-09 op consume(s, hdr.padding);
210 03fcfb79 2021-06-12 op if (hdr.type != FCGI_PARAMS)
211 03fcfb79 2021-06-12 op errx(1, "got %d; expecting PARAMS", hdr.type);
213 03fcfb79 2021-06-12 op if (hdr.content_len0 == 0 &&
214 03fcfb79 2021-06-12 op hdr.content_len1 == 0 &&
215 03fcfb79 2021-06-12 op hdr.padding == 0)
219 cd1ede6d 2023-06-09 op assert_record(s, FCGI_STDIN);
221 2247b668 2023-07-01 op msg = "20 text/gemini\r\n# hello from fastcgi!\n";
222 2247b668 2023-07-01 op len = strlen(msg);
224 03fcfb79 2021-06-12 op prepare_header(&hdr, FCGI_STDOUT, 1, len, 0);
225 cd1ede6d 2023-06-09 op must_write(s, &hdr, sizeof(hdr));
226 cd1ede6d 2023-06-09 op must_write(s, msg, len);
228 2247b668 2023-07-01 op msg = "some more content in the page...\n";
229 2247b668 2023-07-01 op len = strlen(msg);
231 2247b668 2023-07-01 op prepare_header(&hdr, FCGI_STDOUT, 1, len, 0);
232 2247b668 2023-07-01 op must_write(s, &hdr, sizeof(hdr));
233 2247b668 2023-07-01 op must_write(s, msg, len);
235 03fcfb79 2021-06-12 op prepare_header(&hdr, FCGI_END_REQUEST, 1, sizeof(end), 0);
236 cd1ede6d 2023-06-09 op write(s, &hdr, sizeof(hdr));
237 03fcfb79 2021-06-12 op memset(&end, 0, sizeof(end));
238 cd1ede6d 2023-06-09 op write(s, &end, sizeof(end));