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 <stdint.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
29 #define FCGI_VERSION_1 1
31 /* subset of records that matters to us */
32 #define FCGI_BEGIN_REQUEST 1
33 #define FCGI_END_REQUEST 3
34 #define FCGI_PARAMS 4
35 #define FCGI_STDIN 5
36 #define FCGI_STDOUT 6
38 #define SUM(a, b) (((a) << 8) + (b))
40 struct fcgi_header {
41 uint8_t version;
42 uint8_t type;
43 uint8_t req_id1;
44 uint8_t req_id0;
45 uint8_t content_len1;
46 uint8_t content_len0;
47 uint8_t padding;
48 uint8_t reserved;
49 };
51 struct fcgi_end_req_body {
52 unsigned char app_status3;
53 unsigned char app_status2;
54 unsigned char app_status1;
55 unsigned char app_status0;
56 unsigned char proto_status;
57 unsigned char reserved[3];
58 };
60 static int
61 prepare_header(struct fcgi_header *h, int type, int id, size_t size,
62 size_t padding)
63 {
64 memset(h, 0, sizeof(*h));
66 h->version = FCGI_VERSION_1;
67 h->type = type;
68 h->req_id1 = (id >> 8);
69 h->req_id0 = (id & 0xFF);
70 h->content_len1 = (size >> 8);
71 h->content_len0 = (size & 0xFF);
72 h->padding = padding;
74 return 0;
75 }
77 static int
78 must_read(int sock, void *d, size_t len)
79 {
80 ssize_t r;
82 for (;;) {
83 switch (r = read(sock, d, len)) {
84 case -1:
85 case 0:
86 return -1;
87 default:
88 if (r == (ssize_t)len)
89 return 0;
90 len -= r;
91 d += r;
92 }
93 }
94 }
96 static int
97 consume(int fd, size_t len)
98 {
99 size_t l;
100 char buf[64];
102 while (len != 0) {
103 if ((l = len) > sizeof(buf))
104 l = sizeof(buf);
105 if (must_read(fd, buf, l) == -1)
106 return 0;
107 len -= l;
110 return 1;
113 static void
114 read_header(struct fcgi_header *hdr)
116 if (must_read(0, hdr, sizeof(*hdr)) == -1)
117 errx(1, "must_read failed");
120 /* read and consume a record of the given type */
121 static void
122 assert_record(int type)
124 struct fcgi_header hdr;
126 read_header(&hdr);
128 if (hdr.type != type)
129 errx(1, "expected record type %d; got %d",
130 type, hdr.type);
132 consume(0, SUM(hdr.content_len1, hdr.content_len0));
133 consume(0, hdr.padding);
136 int
137 main(void)
139 struct fcgi_header hdr;
140 struct fcgi_end_req_body end;
141 const char *msg;
142 size_t len;
144 msg = "20 text/gemini\r\n# Hello, world!\n";
145 len = strlen(msg);
147 for (;;) {
148 warnx("waiting for a request");
149 assert_record(FCGI_BEGIN_REQUEST);
151 /* read params */
152 for (;;) {
153 read_header(&hdr);
155 consume(0, SUM(hdr.content_len1, hdr.content_len0));
156 consume(0, hdr.padding);
158 if (hdr.type != FCGI_PARAMS)
159 errx(1, "got %d; expecting PARAMS", hdr.type);
161 if (hdr.content_len0 == 0 &&
162 hdr.content_len1 == 0 &&
163 hdr.padding == 0)
164 break;
167 assert_record(FCGI_STDIN);
169 warnx("sending the response");
171 prepare_header(&hdr, FCGI_STDOUT, 1, len, 0);
172 write(0, &hdr, sizeof(hdr));
173 write(0, msg, len);
175 warnx("closing the request");
177 prepare_header(&hdr, FCGI_END_REQUEST, 1, sizeof(end), 0);
178 write(0, &hdr, sizeof(hdr));
179 memset(&end, 0, sizeof(end));
180 write(0, &end, sizeof(end));