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 1959cda3 2023-06-13 op uint8_t *data = d;
87 03fcfb79 2021-06-12 op ssize_t r;
88 03fcfb79 2021-06-12 op
89 cd1ede6d 2023-06-09 op while (len > 0) {
90 1959cda3 2023-06-13 op switch (r = read(sock, data, len)) {
91 03fcfb79 2021-06-12 op case -1:
92 cd1ede6d 2023-06-09 op err(1, "read");
93 03fcfb79 2021-06-12 op case 0:
94 cd1ede6d 2023-06-09 op errx(1, "EOF");
95 03fcfb79 2021-06-12 op default:
96 03fcfb79 2021-06-12 op len -= r;
97 1959cda3 2023-06-13 op data += r;
98 03fcfb79 2021-06-12 op }
99 03fcfb79 2021-06-12 op }
100 03fcfb79 2021-06-12 op }
101 03fcfb79 2021-06-12 op
102 cd1ede6d 2023-06-09 op static void
103 cd1ede6d 2023-06-09 op must_write(int sock, const void *d, size_t len)
104 cd1ede6d 2023-06-09 op {
105 1959cda3 2023-06-13 op const uint8_t *data = d;
106 cd1ede6d 2023-06-09 op ssize_t w;
107 cd1ede6d 2023-06-09 op
108 cd1ede6d 2023-06-09 op while (len > 0) {
109 1959cda3 2023-06-13 op switch (w = write(sock, data, len)) {
110 cd1ede6d 2023-06-09 op case -1:
111 cd1ede6d 2023-06-09 op err(1, "write");
112 cd1ede6d 2023-06-09 op case 0:
113 cd1ede6d 2023-06-09 op errx(1, "EOF");
114 cd1ede6d 2023-06-09 op default:
115 cd1ede6d 2023-06-09 op len -= w;
116 1959cda3 2023-06-13 op data += w;
117 cd1ede6d 2023-06-09 op }
118 cd1ede6d 2023-06-09 op }
119 cd1ede6d 2023-06-09 op }
120 cd1ede6d 2023-06-09 op
121 03fcfb79 2021-06-12 op static int
122 03fcfb79 2021-06-12 op consume(int fd, size_t len)
123 03fcfb79 2021-06-12 op {
124 03fcfb79 2021-06-12 op size_t l;
125 03fcfb79 2021-06-12 op char buf[64];
126 03fcfb79 2021-06-12 op
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);
131 03fcfb79 2021-06-12 op len -= l;
132 03fcfb79 2021-06-12 op }
133 03fcfb79 2021-06-12 op
134 03fcfb79 2021-06-12 op return 1;
135 03fcfb79 2021-06-12 op }
136 03fcfb79 2021-06-12 op
137 03fcfb79 2021-06-12 op static void
138 cd1ede6d 2023-06-09 op read_header(int fd, struct fcgi_header *hdr)
139 03fcfb79 2021-06-12 op {
140 cd1ede6d 2023-06-09 op must_read(fd, hdr, sizeof(*hdr));
141 03fcfb79 2021-06-12 op }
142 03fcfb79 2021-06-12 op
143 03fcfb79 2021-06-12 op /* read and consume a record of the given type */
144 03fcfb79 2021-06-12 op static void
145 cd1ede6d 2023-06-09 op assert_record(int fd, int type)
146 03fcfb79 2021-06-12 op {
147 03fcfb79 2021-06-12 op struct fcgi_header hdr;
148 03fcfb79 2021-06-12 op
149 cd1ede6d 2023-06-09 op read_header(fd, &hdr);
150 03fcfb79 2021-06-12 op
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);
154 03fcfb79 2021-06-12 op
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);
157 03fcfb79 2021-06-12 op }
158 03fcfb79 2021-06-12 op
159 03fcfb79 2021-06-12 op int
160 cd1ede6d 2023-06-09 op main(int argc, char **argv)
161 03fcfb79 2021-06-12 op {
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;
167 03fcfb79 2021-06-12 op size_t len;
168 cd1ede6d 2023-06-09 op int ch, sock, s;
169 03fcfb79 2021-06-12 op
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");
176 cd1ede6d 2023-06-09 op
177 cd1ede6d 2023-06-09 op path = argv[0];
178 cd1ede6d 2023-06-09 op
179 cd1ede6d 2023-06-09 op if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
180 cd1ede6d 2023-06-09 op err(1, "socket");
181 cd1ede6d 2023-06-09 op
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));
185 cd1ede6d 2023-06-09 op
186 cd1ede6d 2023-06-09 op if (unlink(path) == -1 && errno != ENOENT)
187 cd1ede6d 2023-06-09 op err(1, "unlink %s", path);
188 cd1ede6d 2023-06-09 op
189 cd1ede6d 2023-06-09 op if (bind(sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
190 cd1ede6d 2023-06-09 op err(1, "bind");
191 cd1ede6d 2023-06-09 op
192 cd1ede6d 2023-06-09 op if (listen(sock, 5) == -1)
193 cd1ede6d 2023-06-09 op err(1, "listen");
194 cd1ede6d 2023-06-09 op
195 03fcfb79 2021-06-12 op for (;;) {
196 cd1ede6d 2023-06-09 op if ((s = accept(sock, NULL, NULL)) == -1) {
197 cd1ede6d 2023-06-09 op warn("retrying; accept failed");
198 cd1ede6d 2023-06-09 op continue;
199 cd1ede6d 2023-06-09 op }
200 03fcfb79 2021-06-12 op
201 cd1ede6d 2023-06-09 op assert_record(s, FCGI_BEGIN_REQUEST);
202 cd1ede6d 2023-06-09 op
203 03fcfb79 2021-06-12 op /* read params */
204 03fcfb79 2021-06-12 op for (;;) {
205 cd1ede6d 2023-06-09 op read_header(s, &hdr);
206 03fcfb79 2021-06-12 op
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);
209 03fcfb79 2021-06-12 op
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);
212 03fcfb79 2021-06-12 op
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)
216 03fcfb79 2021-06-12 op break;
217 03fcfb79 2021-06-12 op }
218 03fcfb79 2021-06-12 op
219 cd1ede6d 2023-06-09 op assert_record(s, FCGI_STDIN);
220 03fcfb79 2021-06-12 op
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);
223 2247b668 2023-07-01 op
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);
227 03fcfb79 2021-06-12 op
228 2247b668 2023-07-01 op msg = "some more content in the page...\n";
229 2247b668 2023-07-01 op len = strlen(msg);
230 2247b668 2023-07-01 op
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);
234 2247b668 2023-07-01 op
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));
239 03fcfb79 2021-06-12 op }
240 03fcfb79 2021-06-12 op }