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