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