Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 /*
18 * Test program for fastcgi. It speaks the protocol over stdin.
19 * Can't handle more than one request at the same time.
20 */
22 #include "../config.h"
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/un.h>
28 #include <errno.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.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
40 #define FCGI_PARAMS 4
41 #define FCGI_STDIN 5
42 #define FCGI_STDOUT 6
44 #define SUM(a, b) (((a) << 8) + (b))
46 struct fcgi_header {
47 uint8_t version;
48 uint8_t type;
49 uint8_t req_id1;
50 uint8_t req_id0;
51 uint8_t content_len1;
52 uint8_t content_len0;
53 uint8_t padding;
54 uint8_t reserved;
55 };
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];
64 };
66 static int
67 prepare_header(struct fcgi_header *h, int type, int id, size_t size,
68 size_t padding)
69 {
70 memset(h, 0, sizeof(*h));
72 h->version = FCGI_VERSION_1;
73 h->type = type;
74 h->req_id1 = (id >> 8);
75 h->req_id0 = (id & 0xFF);
76 h->content_len1 = (size >> 8);
77 h->content_len0 = (size & 0xFF);
78 h->padding = padding;
80 return 0;
81 }
83 static void
84 must_read(int sock, void *d, size_t len)
85 {
86 uint8_t *data = d;
87 ssize_t r;
89 while (len > 0) {
90 switch (r = read(sock, data, len)) {
91 case -1:
92 err(1, "read");
93 case 0:
94 errx(1, "EOF");
95 default:
96 len -= r;
97 data += r;
98 }
99 }
102 static void
103 must_write(int sock, const void *d, size_t len)
105 const uint8_t *data = d;
106 ssize_t w;
108 while (len > 0) {
109 switch (w = write(sock, data, len)) {
110 case -1:
111 err(1, "write");
112 case 0:
113 errx(1, "EOF");
114 default:
115 len -= w;
116 data += w;
121 static int
122 consume(int fd, size_t len)
124 size_t l;
125 char buf[64];
127 while (len != 0) {
128 if ((l = len) > sizeof(buf))
129 l = sizeof(buf);
130 must_read(fd, buf, l);
131 len -= l;
134 return 1;
137 static void
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 */
144 static void
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",
153 type, hdr.type);
155 consume(fd, SUM(hdr.content_len1, hdr.content_len0));
156 consume(fd, hdr.padding);
159 int
160 main(int argc, char **argv)
162 struct fcgi_header hdr;
163 struct fcgi_end_req_body end;
164 struct sockaddr_un sun;
165 const char *path;
166 const char *msg;
167 size_t len;
168 int ch, sock, s;
170 while ((ch = getopt(argc, argv, "")) != -1)
171 errx(1, "wrong usage");
172 argc -= optind;
173 argv += optind;
174 if (argc != 1)
175 errx(1, "wrong usage");
177 path = argv[0];
179 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
180 err(1, "socket");
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)
190 err(1, "bind");
192 if (listen(sock, 5) == -1)
193 err(1, "listen");
195 for (;;) {
196 if ((s = accept(sock, NULL, NULL)) == -1) {
197 warn("retrying; accept failed");
198 continue;
201 assert_record(s, FCGI_BEGIN_REQUEST);
203 /* read params */
204 for (;;) {
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 &&
215 hdr.padding == 0)
216 break;
219 assert_record(s, FCGI_STDIN);
221 msg = "20 text/gemini\r\n# hello from fastcgi!\n";
222 len = strlen(msg);
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";
229 len = strlen(msg);
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));