Blame


1 03fcfb79 2021-06-12 op /*
2 03fcfb79 2021-06-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 03fcfb79 2021-06-12 op *
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.
7 03fcfb79 2021-06-12 op *
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.
15 03fcfb79 2021-06-12 op */
16 03fcfb79 2021-06-12 op
17 03fcfb79 2021-06-12 op /*
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.
20 03fcfb79 2021-06-12 op */
21 03fcfb79 2021-06-12 op
22 03fcfb79 2021-06-12 op #include "../config.h"
23 03fcfb79 2021-06-12 op
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>
27 cd1ede6d 2023-06-09 op
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>
34 03fcfb79 2021-06-12 op
35 03fcfb79 2021-06-12 op #define FCGI_VERSION_1 1
36 03fcfb79 2021-06-12 op
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
43 03fcfb79 2021-06-12 op
44 03fcfb79 2021-06-12 op #define SUM(a, b) (((a) << 8) + (b))
45 03fcfb79 2021-06-12 op
46 03fcfb79 2021-06-12 op struct fcgi_header {
47 03fcfb79 2021-06-12 op uint8_t version;
48 03fcfb79 2021-06-12 op uint8_t type;
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;
55 03fcfb79 2021-06-12 op };
56 03fcfb79 2021-06-12 op
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];
64 03fcfb79 2021-06-12 op };
65 03fcfb79 2021-06-12 op
66 03fcfb79 2021-06-12 op static int
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)
69 03fcfb79 2021-06-12 op {
70 03fcfb79 2021-06-12 op memset(h, 0, sizeof(*h));
71 03fcfb79 2021-06-12 op
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;
79 03fcfb79 2021-06-12 op
80 03fcfb79 2021-06-12 op return 0;
81 03fcfb79 2021-06-12 op }
82 03fcfb79 2021-06-12 op
83 cd1ede6d 2023-06-09 op static void
84 03fcfb79 2021-06-12 op must_read(int sock, void *d, size_t len)
85 03fcfb79 2021-06-12 op {
86 03fcfb79 2021-06-12 op ssize_t r;
87 03fcfb79 2021-06-12 op
88 cd1ede6d 2023-06-09 op while (len > 0) {
89 03fcfb79 2021-06-12 op switch (r = read(sock, d, len)) {
90 03fcfb79 2021-06-12 op case -1:
91 cd1ede6d 2023-06-09 op err(1, "read");
92 03fcfb79 2021-06-12 op case 0:
93 cd1ede6d 2023-06-09 op errx(1, "EOF");
94 03fcfb79 2021-06-12 op default:
95 03fcfb79 2021-06-12 op len -= r;
96 03fcfb79 2021-06-12 op d += r;
97 03fcfb79 2021-06-12 op }
98 03fcfb79 2021-06-12 op }
99 03fcfb79 2021-06-12 op }
100 03fcfb79 2021-06-12 op
101 cd1ede6d 2023-06-09 op static void
102 cd1ede6d 2023-06-09 op must_write(int sock, const void *d, size_t len)
103 cd1ede6d 2023-06-09 op {
104 cd1ede6d 2023-06-09 op ssize_t w;
105 cd1ede6d 2023-06-09 op
106 cd1ede6d 2023-06-09 op while (len > 0) {
107 cd1ede6d 2023-06-09 op switch (w = write(sock, d, len)) {
108 cd1ede6d 2023-06-09 op case -1:
109 cd1ede6d 2023-06-09 op err(1, "write");
110 cd1ede6d 2023-06-09 op case 0:
111 cd1ede6d 2023-06-09 op errx(1, "EOF");
112 cd1ede6d 2023-06-09 op default:
113 cd1ede6d 2023-06-09 op len -= w;
114 cd1ede6d 2023-06-09 op d += w;
115 cd1ede6d 2023-06-09 op }
116 cd1ede6d 2023-06-09 op }
117 cd1ede6d 2023-06-09 op }
118 cd1ede6d 2023-06-09 op
119 03fcfb79 2021-06-12 op static int
120 03fcfb79 2021-06-12 op consume(int fd, size_t len)
121 03fcfb79 2021-06-12 op {
122 03fcfb79 2021-06-12 op size_t l;
123 03fcfb79 2021-06-12 op char buf[64];
124 03fcfb79 2021-06-12 op
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);
129 03fcfb79 2021-06-12 op len -= l;
130 03fcfb79 2021-06-12 op }
131 03fcfb79 2021-06-12 op
132 03fcfb79 2021-06-12 op return 1;
133 03fcfb79 2021-06-12 op }
134 03fcfb79 2021-06-12 op
135 03fcfb79 2021-06-12 op static void
136 cd1ede6d 2023-06-09 op read_header(int fd, struct fcgi_header *hdr)
137 03fcfb79 2021-06-12 op {
138 cd1ede6d 2023-06-09 op must_read(fd, hdr, sizeof(*hdr));
139 03fcfb79 2021-06-12 op }
140 03fcfb79 2021-06-12 op
141 03fcfb79 2021-06-12 op /* read and consume a record of the given type */
142 03fcfb79 2021-06-12 op static void
143 cd1ede6d 2023-06-09 op assert_record(int fd, int type)
144 03fcfb79 2021-06-12 op {
145 03fcfb79 2021-06-12 op struct fcgi_header hdr;
146 03fcfb79 2021-06-12 op
147 cd1ede6d 2023-06-09 op read_header(fd, &hdr);
148 03fcfb79 2021-06-12 op
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);
152 03fcfb79 2021-06-12 op
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);
155 03fcfb79 2021-06-12 op }
156 03fcfb79 2021-06-12 op
157 03fcfb79 2021-06-12 op int
158 cd1ede6d 2023-06-09 op main(int argc, char **argv)
159 03fcfb79 2021-06-12 op {
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;
165 03fcfb79 2021-06-12 op size_t len;
166 cd1ede6d 2023-06-09 op int ch, sock, s;
167 03fcfb79 2021-06-12 op
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);
170 03fcfb79 2021-06-12 op
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");
177 cd1ede6d 2023-06-09 op
178 cd1ede6d 2023-06-09 op path = argv[0];
179 cd1ede6d 2023-06-09 op
180 cd1ede6d 2023-06-09 op if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
181 cd1ede6d 2023-06-09 op err(1, "socket");
182 cd1ede6d 2023-06-09 op
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));
186 cd1ede6d 2023-06-09 op
187 cd1ede6d 2023-06-09 op if (unlink(path) == -1 && errno != ENOENT)
188 cd1ede6d 2023-06-09 op err(1, "unlink %s", path);
189 cd1ede6d 2023-06-09 op
190 cd1ede6d 2023-06-09 op if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
191 cd1ede6d 2023-06-09 op err(1, "bind");
192 cd1ede6d 2023-06-09 op
193 cd1ede6d 2023-06-09 op if (listen(sock, 5) == -1)
194 cd1ede6d 2023-06-09 op err(1, "listen");
195 cd1ede6d 2023-06-09 op
196 03fcfb79 2021-06-12 op for (;;) {
197 cd1ede6d 2023-06-09 op if ((s = accept(sock, NULL, NULL)) == -1) {
198 cd1ede6d 2023-06-09 op warn("retrying; accept failed");
199 cd1ede6d 2023-06-09 op continue;
200 cd1ede6d 2023-06-09 op }
201 03fcfb79 2021-06-12 op
202 cd1ede6d 2023-06-09 op assert_record(s, FCGI_BEGIN_REQUEST);
203 cd1ede6d 2023-06-09 op
204 03fcfb79 2021-06-12 op /* read params */
205 03fcfb79 2021-06-12 op for (;;) {
206 cd1ede6d 2023-06-09 op read_header(s, &hdr);
207 03fcfb79 2021-06-12 op
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);
210 03fcfb79 2021-06-12 op
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);
213 03fcfb79 2021-06-12 op
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)
217 03fcfb79 2021-06-12 op break;
218 03fcfb79 2021-06-12 op }
219 03fcfb79 2021-06-12 op
220 cd1ede6d 2023-06-09 op assert_record(s, FCGI_STDIN);
221 03fcfb79 2021-06-12 op
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);
225 03fcfb79 2021-06-12 op
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));
230 03fcfb79 2021-06-12 op }
231 03fcfb79 2021-06-12 op }