comparison src/c/fastcgi.c @ 859:60240acd15b9

Successfully starting FastCGI sessions with Apache
author Adam Chlipala <adamc@hcoop.net>
date Sat, 27 Jun 2009 12:38:23 -0400
parents
children a738002d5b4d
comparison
equal deleted inserted replaced
858:346cf1908a17 859:60240acd15b9
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netdb.h>
9 #include <netinet/in.h>
10 #include <unistd.h>
11 #include <signal.h>
12 #include <stdarg.h>
13
14 #include <pthread.h>
15
16 #include "urweb.h"
17 #include "request.h"
18 #include "queue.h"
19
20 #include "fastcgi.h"
21
22 typedef struct {
23 unsigned char version;
24 unsigned char type;
25 unsigned char requestIdB1;
26 unsigned char requestIdB0;
27 unsigned char contentLengthB1;
28 unsigned char contentLengthB0;
29 unsigned char paddingLength;
30 unsigned char reserved;
31 unsigned char contentData[65535];
32 } FCGI_Record;
33
34 typedef struct {
35 FCGI_Record r;
36 int sock;
37 } FCGI_Output;
38
39 typedef struct {
40 char buf[sizeof(FCGI_Record) + 255];
41 int available, used, sock;
42 } FCGI_Input;
43
44 static FCGI_Output *fastcgi_output() {
45 FCGI_Output *o = malloc(sizeof(FCGI_Output));
46
47 o->r.version = FCGI_VERSION_1;
48 o->r.paddingLength = 0;
49 o->r.reserved = 0;
50
51 return o;
52 }
53
54 static FCGI_Input *fastcgi_input() {
55 FCGI_Input *i = malloc(sizeof(FCGI_Input));
56
57 i->available = i->used = 0;
58
59 return i;
60 }
61
62 static void fastcgi_input_reset(FCGI_Input *i) {
63 i->available = i->used = 0;
64 }
65
66 static int fastcgi_send(FCGI_Output *o,
67 unsigned char type,
68 unsigned short requestId,
69 unsigned short contentLength) {
70 o->r.type = type;
71 o->r.requestIdB1 = requestId >> 8;
72 o->r.requestIdB0 = requestId & 255;
73 o->r.contentLengthB1 = contentLength >> 8;
74 o->r.contentLengthB0 = contentLength & 255;
75 return uw_really_send(o->sock, &o->r, sizeof(o->r) - (65535 - contentLength));
76 }
77
78 #define LATEST(i) ((FCGI_Record *)(i->buf + i->used))
79
80 static FCGI_Record *fastcgi_recv(FCGI_Input *i) {
81 while (1) {
82 ssize_t n;
83
84 if (i->available >= i->used + sizeof(FCGI_Record) - 65535
85 && i->available >= i->used + sizeof(FCGI_Record) - 65535
86 + ((LATEST(i)->contentLengthB1 << 8) & LATEST(i)->contentLengthB0)
87 + LATEST(i)->paddingLength) {
88 FCGI_Record *r = LATEST(i);
89
90 i->used += sizeof(FCGI_Record) - 65535
91 + ((LATEST(i)->contentLengthB1 << 8) & LATEST(i)->contentLengthB0)
92 + LATEST(i)->paddingLength;
93
94 return r;
95 }
96
97 if (i->used > 0) {
98 memmove(i->buf, i->buf + i->used, i->available - i->used);
99 i->available -= i->used;
100 i->used = 0;
101 }
102
103 n = recv(i->sock, i->buf + i->available, sizeof(i->buf) - i->available, 0);
104
105 if (n <= 0)
106 return NULL;
107
108 i->available += n;
109 }
110 }
111
112 static char *get_header(void *data, const char *h) {
113 return NULL;
114 }
115
116 static void on_success(uw_context ctx) { }
117
118 static void on_failure(uw_context ctx) {
119 uw_write_header(ctx, "Status: 500 Internal Server Error\r\n");
120 }
121
122 static void write_stderr(FCGI_Output *o, const char *fmt, ...) {
123 int len;
124 va_list ap;
125 va_start(ap, fmt);
126
127 len = vsnprintf(o->r.contentData, 65535, fmt, ap);
128 if (len < 0)
129 fprintf(stderr, "vsnprintf() failed in log_error().\n");
130 else if (fastcgi_send(o, FCGI_STDERR, FCGI_NULL_REQUEST_ID, len))
131 fprintf(stderr, "fastcgi_send() failed in log_error().\n");
132 }
133
134 static void log_error(void *data, const char *fmt, ...) {
135 FCGI_Output *o = (FCGI_Output *)data;
136 va_list ap;
137 va_start(ap, fmt);
138
139 if (o) {
140 int len = vsnprintf(o->r.contentData, 65535, fmt, ap);
141 if (len < 0)
142 fprintf(stderr, "vsnprintf() failed in log_error().\n");
143 else if (fastcgi_send(o, FCGI_STDERR, FCGI_NULL_REQUEST_ID, len))
144 fprintf(stderr, "fastcgi_send() failed in log_error().\n");
145 } else
146 vfprintf(stderr, fmt, ap);
147 }
148
149 static void log_debug(void *data, const char *fmt, ...) {
150 }
151
152 static void *worker(void *data) {
153 int me = *(int *)data;
154 FCGI_Input *in = fastcgi_input();
155 FCGI_Output *out = fastcgi_output();
156 uw_context ctx = uw_request_new_context(out, log_error, log_debug);
157 uw_request_context rc = uw_new_request_context();
158
159 while (1) {
160 FCGI_Record *r;
161
162 in->sock = out->sock = uw_dequeue();
163
164 if (!(r = fastcgi_recv(in))) {
165 fprintf(stderr, "Error receiving initial message\n");
166 return NULL;
167 }
168
169 if (r->type != FCGI_BEGIN_REQUEST) {
170 write_stderr(out, "First message is not BEGIN_REQUEST\n");
171 goto done;
172 } else if (((FCGI_BeginRequestBody *)&r->contentData)->roleB0 != FCGI_RESPONDER) {
173 write_stderr(out, "First message is not BEGIN_REQUEST\n");
174 goto done;
175 }
176
177 if (!(r = fastcgi_recv(in))) {
178 fprintf(stderr, "Error receiving second message\n");
179 return NULL;
180 }
181 write_stderr(out, "Next message code: %d\n", r->type);
182
183 done:
184 close(in->sock);
185 fastcgi_input_reset(in);
186 uw_reset(ctx);
187 }
188 }
189
190 static void help(char *cmd) {
191 printf("Usage: %s [-t <thread-count>]\n", cmd);
192 }
193
194 static void sigint(int signum) {
195 printf("Exiting....\n");
196 exit(0);
197 }
198
199 static loggers ls = {NULL, log_error, log_debug};
200
201 int main(int argc, char *argv[]) {
202 // The skeleton for this function comes from Beej's sockets tutorial.
203 struct sockaddr_in their_addr; // connector's address information
204 int sin_size, yes = 1;
205 int nthreads = 1, i, *names, opt;
206 char *fwsa = getenv("FCGI_WEB_SERVER_ADDRS"), *nthreads_s = getenv("URWEB_NUM_THREADS");
207
208 if (nthreads_s) {
209 nthreads = atoi(nthreads_s);
210 if (nthreads <= 0) {
211 fprintf(stderr, "Bad URWEB_NUM_THREADS value\n");
212 return 1;
213 }
214 }
215
216 signal(SIGINT, sigint);
217 signal(SIGPIPE, SIG_IGN);
218 signal(SIGUSR1, sigint);
219 signal(SIGTERM, sigint);
220
221 while ((opt = getopt(argc, argv, "ht:")) != -1) {
222 switch (opt) {
223 case '?':
224 fprintf(stderr, "Unknown command-line option");
225 help(argv[0]);
226 return 1;
227
228 case 'h':
229 help(argv[0]);
230 return 0;
231
232 case 't':
233 nthreads = atoi(optarg);
234 if (nthreads <= 0) {
235 fprintf(stderr, "Invalid thread count\n");
236 help(argv[0]);
237 return 1;
238 }
239 break;
240
241 default:
242 fprintf(stderr, "Unexpected getopt() behavior\n");
243 return 1;
244 }
245 }
246
247 uw_request_init(NULL, log_error, log_debug);
248
249 names = calloc(nthreads, sizeof(int));
250
251 sin_size = sizeof their_addr;
252
253 {
254 pthread_t thread;
255 int name;
256
257 if (pthread_create(&thread, NULL, client_pruner, &ls)) {
258 fprintf(stderr, "Error creating pruner thread\n");
259 return 1;
260 }
261 }
262
263 for (i = 0; i < nthreads; ++i) {
264 pthread_t thread;
265 names[i] = i;
266 if (pthread_create(&thread, NULL, worker, &names[i])) {
267 fprintf(stderr, "Error creating worker thread #%d\n", i);
268 return 1;
269 }
270 }
271
272 while (1) {
273 int new_fd = accept(FCGI_LISTENSOCK_FILENO, (struct sockaddr *)&their_addr, &sin_size);
274
275 if (new_fd < 0) {
276 fprintf(stderr, "Socket accept failed\n");
277 return 1;
278 }
279
280 if (fwsa) {
281 char host[100], matched = 0;
282 char *ips, *sep;
283
284 if (getnameinfo((struct sockaddr *)&their_addr, sin_size, host, sizeof host, NULL, 0, NI_NUMERICHOST)) {
285 fprintf(stderr, "Remote IP determination failed\n");
286 return 1;
287 }
288
289 for (ips = fwsa; sep = strchr(ips, ','); ips = sep+1) {
290 if (!strncmp(ips, host, sep - ips)) {
291 matched = 1;
292 break;
293 }
294 }
295
296 if (!matched && strcmp(ips, host)) {
297 fprintf(stderr, "Remote address is not in FCGI_WEB_SERVER_ADDRS");
298 return 1;
299 }
300 }
301
302 uw_enqueue(new_fd);
303 }
304 }