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 ssize_t r;
88 while (len > 0) {
89 switch (r = read(sock, d, len)) {
90 case -1:
91 err(1, "read");
92 case 0:
93 errx(1, "EOF");
94 default:
95 len -= r;
96 d += r;
97 }
98 }
99 }
101 static void
102 must_write(int sock, const void *d, size_t len)
104 ssize_t w;
106 while (len > 0) {
107 switch (w = write(sock, d, len)) {
108 case -1:
109 err(1, "write");
110 case 0:
111 errx(1, "EOF");
112 default:
113 len -= w;
114 d += w;
119 static int
120 consume(int fd, size_t len)
122 size_t l;
123 char buf[64];
125 while (len != 0) {
126 if ((l = len) > sizeof(buf))
127 l = sizeof(buf);
128 must_read(fd, buf, l);
129 len -= l;
132 return 1;
135 static void
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 */
142 static void
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",
151 type, hdr.type);
153 consume(fd, SUM(hdr.content_len1, hdr.content_len0));
154 consume(fd, hdr.padding);
157 int
158 main(int argc, char **argv)
160 struct fcgi_header hdr;
161 struct fcgi_end_req_body end;
162 struct sockaddr_un sun;
163 const char *path;
164 const char *msg;
165 size_t len;
166 int ch, sock, s;
168 msg = "20 text/gemini\r\n# hello from fastcgi!\n";
169 len = strlen(msg);
171 while ((ch = getopt(argc, argv, "")) != -1)
172 errx(1, "wrong usage");
173 argc -= optind;
174 argv += optind;
175 if (argc != 1)
176 errx(1, "wrong usage");
178 path = argv[0];
180 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
181 err(1, "socket");
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)
191 err(1, "bind");
193 if (listen(sock, 5) == -1)
194 err(1, "listen");
196 for (;;) {
197 if ((s = accept(sock, NULL, NULL)) == -1) {
198 warn("retrying; accept failed");
199 continue;
202 assert_record(s, FCGI_BEGIN_REQUEST);
204 /* read params */
205 for (;;) {
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 &&
216 hdr.padding == 0)
217 break;
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));