adamc@859
|
1 #define _GNU_SOURCE
|
adamc@859
|
2
|
adamc@859
|
3 #include <stdio.h>
|
adamc@859
|
4 #include <string.h>
|
adamc@859
|
5 #include <stdlib.h>
|
adamc@859
|
6 #include <sys/types.h>
|
adamc@859
|
7 #include <sys/socket.h>
|
adamc@859
|
8 #include <netdb.h>
|
adamc@859
|
9 #include <netinet/in.h>
|
adamc@859
|
10 #include <unistd.h>
|
adamc@859
|
11 #include <signal.h>
|
adamc@859
|
12 #include <stdarg.h>
|
adamc@1094
|
13 #include <ctype.h>
|
adamc@859
|
14
|
adamc@859
|
15 #include <pthread.h>
|
adamc@859
|
16
|
adamc@859
|
17 #include "urweb.h"
|
adamc@859
|
18 #include "request.h"
|
adamc@859
|
19 #include "queue.h"
|
adamc@859
|
20
|
adamc@859
|
21 #include "fastcgi.h"
|
adamc@859
|
22
|
adamc@1094
|
23 extern uw_app uw_application;
|
adamc@1094
|
24
|
adamc@859
|
25 typedef struct {
|
adamc@859
|
26 unsigned char version;
|
adamc@859
|
27 unsigned char type;
|
adamc@859
|
28 unsigned char requestIdB1;
|
adamc@859
|
29 unsigned char requestIdB0;
|
adamc@859
|
30 unsigned char contentLengthB1;
|
adamc@859
|
31 unsigned char contentLengthB0;
|
adamc@859
|
32 unsigned char paddingLength;
|
adamc@859
|
33 unsigned char reserved;
|
adamc@859
|
34 unsigned char contentData[65535];
|
adamc@859
|
35 } FCGI_Record;
|
adamc@859
|
36
|
adamc@859
|
37 typedef struct {
|
adamc@859
|
38 FCGI_Record r;
|
adamc@859
|
39 int sock;
|
adamc@859
|
40 } FCGI_Output;
|
adamc@859
|
41
|
adamc@859
|
42 typedef struct {
|
adamc@859
|
43 char buf[sizeof(FCGI_Record) + 255];
|
adamc@859
|
44 int available, used, sock;
|
adamc@859
|
45 } FCGI_Input;
|
adamc@859
|
46
|
adamc@859
|
47 static FCGI_Output *fastcgi_output() {
|
adamc@859
|
48 FCGI_Output *o = malloc(sizeof(FCGI_Output));
|
adamc@859
|
49
|
adamc@859
|
50 o->r.version = FCGI_VERSION_1;
|
adamc@859
|
51 o->r.paddingLength = 0;
|
adamc@859
|
52 o->r.reserved = 0;
|
adamc@859
|
53
|
adamc@859
|
54 return o;
|
adamc@859
|
55 }
|
adamc@859
|
56
|
adamc@859
|
57 static FCGI_Input *fastcgi_input() {
|
adamc@859
|
58 FCGI_Input *i = malloc(sizeof(FCGI_Input));
|
adamc@859
|
59
|
adamc@859
|
60 i->available = i->used = 0;
|
adamc@859
|
61
|
adamc@859
|
62 return i;
|
adamc@859
|
63 }
|
adamc@859
|
64
|
adamc@859
|
65 static void fastcgi_input_reset(FCGI_Input *i) {
|
adamc@859
|
66 i->available = i->used = 0;
|
adamc@859
|
67 }
|
adamc@859
|
68
|
adamc@859
|
69 static int fastcgi_send(FCGI_Output *o,
|
adamc@859
|
70 unsigned char type,
|
adamc@859
|
71 unsigned short contentLength) {
|
adamc@859
|
72 o->r.type = type;
|
adamc@860
|
73 o->r.requestIdB1 = o->r.requestIdB0 = 0;
|
adamc@859
|
74 o->r.contentLengthB1 = contentLength >> 8;
|
adamc@859
|
75 o->r.contentLengthB0 = contentLength & 255;
|
adamc@861
|
76 return uw_really_send(o->sock, &o->r, sizeof(o->r) - 65535 + contentLength);
|
adamc@859
|
77 }
|
adamc@859
|
78
|
adamc@859
|
79 #define LATEST(i) ((FCGI_Record *)(i->buf + i->used))
|
adamc@859
|
80
|
adamc@859
|
81 static FCGI_Record *fastcgi_recv(FCGI_Input *i) {
|
adamc@859
|
82 while (1) {
|
adamc@859
|
83 ssize_t n;
|
adamc@859
|
84
|
adamc@859
|
85 if (i->available >= i->used + sizeof(FCGI_Record) - 65535
|
adamc@859
|
86 && i->available >= i->used + sizeof(FCGI_Record) - 65535
|
adamc@860
|
87 + ((LATEST(i)->contentLengthB1 << 8) | LATEST(i)->contentLengthB0)
|
adamc@859
|
88 + LATEST(i)->paddingLength) {
|
adamc@859
|
89 FCGI_Record *r = LATEST(i);
|
adamc@859
|
90
|
adamc@859
|
91 i->used += sizeof(FCGI_Record) - 65535
|
adamc@860
|
92 + ((LATEST(i)->contentLengthB1 << 8) | LATEST(i)->contentLengthB0)
|
adamc@859
|
93 + LATEST(i)->paddingLength;
|
adamc@859
|
94
|
adamc@859
|
95 return r;
|
adamc@859
|
96 }
|
adamc@859
|
97
|
adamc@859
|
98 if (i->used > 0) {
|
adamc@859
|
99 memmove(i->buf, i->buf + i->used, i->available - i->used);
|
adamc@859
|
100 i->available -= i->used;
|
adamc@859
|
101 i->used = 0;
|
adamc@859
|
102 }
|
adamc@859
|
103
|
adamc@859
|
104 n = recv(i->sock, i->buf + i->available, sizeof(i->buf) - i->available, 0);
|
adamc@859
|
105
|
adamc@859
|
106 if (n <= 0)
|
adamc@859
|
107 return NULL;
|
adamc@859
|
108
|
adamc@859
|
109 i->available += n;
|
adamc@859
|
110 }
|
adamc@859
|
111 }
|
adamc@859
|
112
|
adamc@859
|
113 static void on_success(uw_context ctx) { }
|
adamc@859
|
114
|
adamc@859
|
115 static void on_failure(uw_context ctx) {
|
adamc@859
|
116 uw_write_header(ctx, "Status: 500 Internal Server Error\r\n");
|
adamc@859
|
117 }
|
adamc@859
|
118
|
adamc@863
|
119 static int write_stdout(void *data, const char *buf, size_t len) {
|
adamc@860
|
120 FCGI_Output *o = (FCGI_Output *)data;
|
adamc@860
|
121 while (len > 0) {
|
adamc@860
|
122 size_t len2 = len;
|
adamc@860
|
123 if (len2 > 65535)
|
adamc@860
|
124 len2 = 65535;
|
adamc@860
|
125 memcpy(o->r.contentData, buf, len2);
|
adamc@860
|
126 if (fastcgi_send(o, FCGI_STDOUT, len2)) {
|
adamc@860
|
127 fprintf(stderr, "fastcgi_send() failed in write_stdout().\n");
|
adamc@860
|
128 return -1;
|
adamc@860
|
129 }
|
adamc@860
|
130 buf += len2;
|
adamc@860
|
131 len -= len2;
|
adamc@860
|
132 }
|
adamc@860
|
133
|
adamc@860
|
134 return 0;
|
adamc@860
|
135 }
|
adamc@860
|
136
|
adamc@859
|
137 static void write_stderr(FCGI_Output *o, const char *fmt, ...) {
|
adamc@859
|
138 int len;
|
adamc@859
|
139 va_list ap;
|
adamc@859
|
140 va_start(ap, fmt);
|
adamc@859
|
141
|
adamc@1134
|
142 len = vsnprintf((char *)o->r.contentData, 65535, fmt, ap);
|
adamc@859
|
143 if (len < 0)
|
adamc@860
|
144 fprintf(stderr, "vsnprintf() failed in write_stderr().\n");
|
adamc@860
|
145 else if (fastcgi_send(o, FCGI_STDERR, len))
|
adamc@860
|
146 fprintf(stderr, "fastcgi_send() failed in write_stderr().\n");
|
adamc@860
|
147 }
|
adamc@860
|
148
|
adamc@860
|
149 static void close_stream(FCGI_Output *o, unsigned char type) {
|
adamc@860
|
150 if (fastcgi_send(o, type, 0))
|
adamc@860
|
151 fprintf(stderr, "fastcgi_send() failed in close_stream().\n");
|
adamc@859
|
152 }
|
adamc@859
|
153
|
adamc@859
|
154 static void log_error(void *data, const char *fmt, ...) {
|
adamc@859
|
155 FCGI_Output *o = (FCGI_Output *)data;
|
adamc@859
|
156 va_list ap;
|
adamc@859
|
157 va_start(ap, fmt);
|
adamc@859
|
158
|
adamc@859
|
159 if (o) {
|
adamc@1134
|
160 int len = vsnprintf((char *)o->r.contentData, 65535, fmt, ap);
|
adamc@859
|
161 if (len < 0)
|
adamc@859
|
162 fprintf(stderr, "vsnprintf() failed in log_error().\n");
|
adamc@860
|
163 else if (fastcgi_send(o, FCGI_STDERR, len))
|
adamc@859
|
164 fprintf(stderr, "fastcgi_send() failed in log_error().\n");
|
adamc@859
|
165 } else
|
adamc@859
|
166 vfprintf(stderr, fmt, ap);
|
adamc@859
|
167 }
|
adamc@859
|
168
|
adamc@859
|
169 static void log_debug(void *data, const char *fmt, ...) {
|
adamc@859
|
170 }
|
adamc@859
|
171
|
adamc@860
|
172 typedef struct {
|
adamc@860
|
173 char *name, *value;
|
adamc@860
|
174 unsigned name_len, value_len;
|
adamc@860
|
175 } nvp;
|
adamc@860
|
176
|
adamc@860
|
177 static char *search_nvps(nvp *nvps, const char *h) {
|
adamc@862
|
178 for (; nvps->name[0]; ++nvps)
|
adamc@860
|
179 if (!strcmp(h, nvps->name))
|
adamc@860
|
180 return nvps->value;
|
adamc@860
|
181
|
adamc@860
|
182 return NULL;
|
adamc@860
|
183 }
|
adamc@860
|
184
|
adamc@860
|
185 typedef struct {
|
adamc@860
|
186 nvp *nvps;
|
adamc@860
|
187 char *uppercased;
|
adamc@860
|
188 int n_nvps, uppercased_len;
|
adamc@860
|
189 } headers;
|
adamc@860
|
190
|
adamc@860
|
191 static char *get_header(void *data, const char *h) {
|
adamc@860
|
192 headers *hs = (headers *)data;
|
adamc@860
|
193 size_t len = strlen(h);
|
adamc@1134
|
194 char *s;
|
adamc@860
|
195 const char *saved_h = h;
|
adamc@860
|
196
|
adamc@860
|
197 if (len > hs->uppercased_len) {
|
adamc@860
|
198 hs->uppercased_len = len;
|
adamc@860
|
199 hs->uppercased = realloc(hs->uppercased, len + 6);
|
adamc@860
|
200 }
|
adamc@860
|
201
|
adamc@860
|
202 strcpy(hs->uppercased, "HTTP_");
|
adamc@860
|
203 for (s = hs->uppercased+5; *h; ++h)
|
adamc@860
|
204 *s++ = *h == '-' ? '_' : toupper(*h);
|
adamc@860
|
205 *s = 0;
|
adamc@860
|
206
|
adamc@860
|
207 if (!strcasecmp(saved_h, "Content-length")
|
adamc@864
|
208 || !strcasecmp(saved_h, "Content-type")) {
|
adamc@1134
|
209 if ((s = search_nvps(hs->nvps, hs->uppercased + 5)))
|
adamc@864
|
210 return s;
|
adamc@864
|
211 }
|
adamc@864
|
212
|
adamc@864
|
213 return search_nvps(hs->nvps, hs->uppercased);
|
adamc@860
|
214 }
|
adamc@860
|
215
|
adamc@864
|
216 static int read_funny_len(unsigned char **buf, int *len) {
|
adamc@861
|
217 if (*len <= 0)
|
adamc@861
|
218 return -1;
|
adamc@861
|
219
|
adamc@861
|
220 if ((*buf)[0] >> 7 == 0) {
|
adamc@861
|
221 int r = (*buf)[0];
|
adamc@861
|
222 ++*buf;
|
adamc@861
|
223 --*len;
|
adamc@861
|
224 return r;
|
adamc@861
|
225 }
|
adamc@861
|
226 else if (*len < 4)
|
adamc@861
|
227 return -1;
|
adamc@861
|
228 else {
|
adamc@1049
|
229 int r = (((*buf)[0] & 0x7f) << 24) + ((*buf)[1] << 16) + ((*buf)[2] << 8) + (*buf)[3];
|
adamc@861
|
230 *buf += 4;
|
adamc@861
|
231 *len -= 4;
|
adamc@861
|
232 return r;
|
adamc@861
|
233 }
|
adamc@861
|
234 }
|
adamc@861
|
235
|
adamc@864
|
236 static int read_nvp(unsigned char **buf, int len, nvp *nv) {
|
adamc@860
|
237 int nameLength, valueLength;
|
adamc@860
|
238
|
adamc@864
|
239 if ((nameLength = read_funny_len(buf, &len)) < 0)
|
adamc@860
|
240 return -1;
|
adamc@864
|
241 if ((valueLength = read_funny_len(buf, &len)) < 0)
|
adamc@1049
|
242 return -2;
|
adamc@860
|
243 if (len < nameLength + valueLength)
|
adamc@1049
|
244 return -3;
|
adamc@860
|
245
|
adamc@862
|
246 if (nameLength+1 > nv->name_len) {
|
adamc@860
|
247 nv->name_len = nameLength+1;
|
adamc@862
|
248 nv->name = realloc(nv->name, nv->name_len);
|
adamc@860
|
249 }
|
adamc@862
|
250 if (valueLength+1 > nv->value_len) {
|
adamc@860
|
251 nv->value_len = valueLength+1;
|
adamc@862
|
252 nv->value = realloc(nv->value, nv->value_len);
|
adamc@860
|
253 }
|
adamc@860
|
254
|
adamc@864
|
255 memcpy(nv->name, *buf, nameLength);
|
adamc@860
|
256 nv->name[nameLength] = 0;
|
adamc@860
|
257
|
adamc@864
|
258 memcpy(nv->value, *buf + nameLength, valueLength);
|
adamc@860
|
259 nv->value[valueLength] = 0;
|
adamc@860
|
260
|
adamc@864
|
261 *buf += nameLength + valueLength;
|
adamc@864
|
262
|
adamc@860
|
263 return 0;
|
adamc@860
|
264 }
|
adamc@860
|
265
|
adamc@863
|
266 static int fastcgi_close_with(FCGI_Output *out, request_result rr) {
|
adamc@863
|
267 FCGI_EndRequestBody *erb = (FCGI_EndRequestBody *)out->r.contentData;
|
adamc@863
|
268
|
adamc@863
|
269 close_stream(out, FCGI_STDOUT);
|
adamc@863
|
270 close_stream(out, FCGI_STDERR);
|
adamc@863
|
271
|
adamc@863
|
272 if (rr == SERVED)
|
adamc@863
|
273 erb->appStatusB3 = erb->appStatusB2 = erb->appStatusB1 = erb->appStatusB0 = 0;
|
adamc@863
|
274 else
|
adamc@863
|
275 erb->appStatusB3 = erb->appStatusB2 = erb->appStatusB1 = erb->appStatusB0 = 0xFF;
|
adamc@863
|
276
|
adamc@863
|
277 erb->protocolStatus = FCGI_REQUEST_COMPLETE;
|
adamc@863
|
278 fastcgi_send(out, FCGI_END_REQUEST, sizeof(FCGI_EndRequestBody));
|
adamc@863
|
279 return close(out->sock);
|
adamc@863
|
280 }
|
adamc@863
|
281
|
adamc@863
|
282 static int fastcgi_close(int sock) {
|
adamc@863
|
283 FCGI_Output out;
|
adamc@863
|
284 out.sock = sock;
|
adamc@863
|
285 out.r.version = FCGI_VERSION_1;
|
adamc@863
|
286 out.r.paddingLength = 0;
|
adamc@863
|
287 out.r.reserved = 0;
|
adamc@863
|
288
|
adamc@863
|
289 return fastcgi_close_with(&out, SERVED);
|
adamc@863
|
290 }
|
adamc@863
|
291
|
adamc@863
|
292 int fastcgi_send_normal(int sock, const void *buf, ssize_t len) {
|
adamc@863
|
293 FCGI_Output out;
|
adamc@863
|
294 out.sock = sock;
|
adamc@863
|
295 out.r.version = FCGI_VERSION_1;
|
adamc@863
|
296 out.r.paddingLength = 0;
|
adamc@863
|
297 out.r.reserved = 0;
|
adamc@863
|
298
|
adamc@863
|
299 return write_stdout(&out, buf, len);
|
adamc@863
|
300 }
|
adamc@863
|
301
|
adamc@859
|
302 static void *worker(void *data) {
|
adamc@859
|
303 FCGI_Input *in = fastcgi_input();
|
adamc@859
|
304 FCGI_Output *out = fastcgi_output();
|
adamc@1094
|
305 uw_context ctx = uw_request_new_context(&uw_application, out, log_error, log_debug);
|
adamc@859
|
306 uw_request_context rc = uw_new_request_context();
|
adamc@860
|
307 headers hs;
|
adamc@860
|
308 size_t body_size = 0;
|
adamc@860
|
309 char *body = malloc(0);
|
adamc@860
|
310 size_t path_size = 0;
|
adamc@860
|
311 char *path_buf = malloc(0);
|
adamc@860
|
312
|
adamc@860
|
313 hs.uppercased = malloc(0);
|
adamc@860
|
314 hs.uppercased_len = 0;
|
adamc@860
|
315 hs.nvps = malloc(sizeof(nvp));
|
adamc@860
|
316 hs.n_nvps = 1;
|
adamc@862
|
317 hs.nvps[0].name = malloc(1);
|
adamc@862
|
318 hs.nvps[0].name_len = 1;
|
adamc@862
|
319 hs.nvps[0].value = malloc(0);
|
adamc@862
|
320 hs.nvps[0].value_len = 0;
|
adamc@859
|
321
|
adamc@859
|
322 while (1) {
|
adamc@859
|
323 FCGI_Record *r;
|
adamc@860
|
324 size_t used_nvps = 0;
|
adamc@860
|
325 int body_len, body_read;
|
adamc@860
|
326 char *s;
|
adamc@860
|
327 char *method, *path, *path_info, *query_string;
|
adamc@859
|
328
|
adamc@859
|
329 in->sock = out->sock = uw_dequeue();
|
adamc@859
|
330
|
adamc@859
|
331 if (!(r = fastcgi_recv(in))) {
|
adamc@859
|
332 fprintf(stderr, "Error receiving initial message\n");
|
adamc@860
|
333 goto done;
|
adamc@859
|
334 }
|
adamc@861
|
335
|
adamc@859
|
336 if (r->type != FCGI_BEGIN_REQUEST) {
|
adamc@859
|
337 write_stderr(out, "First message is not BEGIN_REQUEST\n");
|
adamc@859
|
338 goto done;
|
adamc@859
|
339 } else if (((FCGI_BeginRequestBody *)&r->contentData)->roleB0 != FCGI_RESPONDER) {
|
adamc@859
|
340 write_stderr(out, "First message is not BEGIN_REQUEST\n");
|
adamc@859
|
341 goto done;
|
adamc@859
|
342 }
|
adamc@859
|
343
|
adamc@860
|
344 while (1) {
|
adamc@864
|
345 unsigned char *buf;
|
adamc@864
|
346 int len;
|
adamc@860
|
347
|
adamc@860
|
348 if (!(r = fastcgi_recv(in))) {
|
adamc@860
|
349 write_stderr(out, "Error receiving environment variables\n");
|
adamc@860
|
350 goto done;
|
adamc@860
|
351 }
|
adamc@860
|
352
|
adamc@860
|
353 if (r->type != FCGI_PARAMS) {
|
adamc@860
|
354 write_stderr(out, "Expected FCGI_PARAMS but got %d\n", r->type);
|
adamc@860
|
355 goto done;
|
adamc@860
|
356 }
|
adamc@860
|
357
|
adamc@860
|
358 if (r->contentLengthB1 == 0 && r->contentLengthB0 == 0)
|
adamc@860
|
359 break;
|
adamc@860
|
360
|
adamc@864
|
361 len = (r->contentLengthB1 << 8) | r->contentLengthB0;
|
adamc@864
|
362
|
adamc@864
|
363 for (buf = r->contentData; buf < r->contentData + len; ) {
|
adamc@864
|
364 if (used_nvps == hs.n_nvps-1) {
|
adamc@864
|
365 ++hs.n_nvps;
|
adamc@864
|
366 hs.nvps = realloc(hs.nvps, hs.n_nvps * sizeof(nvp));
|
adamc@864
|
367 hs.nvps[hs.n_nvps-1].name = malloc(1);
|
adamc@864
|
368 hs.nvps[hs.n_nvps-1].value = malloc(0);
|
adamc@864
|
369 hs.nvps[hs.n_nvps-1].name_len = 1;
|
adamc@864
|
370 hs.nvps[hs.n_nvps-1].value_len = 0;
|
adamc@864
|
371 }
|
adamc@864
|
372
|
adamc@864
|
373 if (read_nvp(&buf, len - (buf - r->contentData), &hs.nvps[used_nvps]) < 0) {
|
adamc@864
|
374 write_stderr(out, "Error reading FCGI_PARAMS name-value pair\n");
|
adamc@864
|
375 goto done;
|
adamc@864
|
376 }
|
adamc@864
|
377
|
adamc@1049
|
378 //write_stderr(out, "PARAM: %s -> %s\n", hs.nvps[used_nvps].name, hs.nvps[used_nvps].value);
|
adamc@864
|
379
|
adamc@864
|
380 ++used_nvps;
|
adamc@860
|
381 }
|
adamc@859
|
382 }
|
adamc@860
|
383
|
adamc@862
|
384 hs.nvps[used_nvps].name[0] = 0;
|
adamc@860
|
385
|
adamc@1134
|
386 if ((s = get_header(&hs, "Content-Length"))) {
|
adamc@860
|
387 body_len = atoi(s);
|
adamc@860
|
388 if (body_len < 0) {
|
adamc@860
|
389 write_stderr(out, "Invalid Content-Length\n");
|
adamc@860
|
390 goto done;
|
adamc@860
|
391 }
|
adamc@860
|
392 } else
|
adamc@860
|
393 body_len = 0;
|
adamc@860
|
394
|
adamc@861
|
395 if (body_len+1 > body_size) {
|
adamc@861
|
396 body_size = body_len+1;
|
adamc@860
|
397 body = realloc(body, body_size);
|
adamc@860
|
398 }
|
adamc@860
|
399
|
adamc@860
|
400 for (body_read = 0; body_read < body_len; ) {
|
adamc@860
|
401 int this_len;
|
adamc@860
|
402
|
adamc@860
|
403 if (!(r = fastcgi_recv(in))) {
|
adamc@860
|
404 write_stderr(out, "Error receiving STDIN\n");
|
adamc@860
|
405 goto done;
|
adamc@860
|
406 }
|
adamc@860
|
407
|
adamc@860
|
408 if (r->type != FCGI_STDIN) {
|
adamc@860
|
409 write_stderr(out, "Expected FCGI_STDIN but got %d\n", r->type);
|
adamc@860
|
410 goto done;
|
adamc@860
|
411 }
|
adamc@860
|
412
|
adamc@860
|
413 if (r->contentLengthB1 == 0 && r->contentLengthB0 == 0) {
|
adamc@860
|
414 write_stderr(out, "End of STDIN\n");
|
adamc@860
|
415 break;
|
adamc@860
|
416 }
|
adamc@860
|
417
|
adamc@860
|
418 this_len = (r->contentLengthB1 << 8) | r->contentLengthB0;
|
adamc@860
|
419
|
adamc@860
|
420 if (body_read + this_len > body_len) {
|
adamc@860
|
421 write_stderr(out, "Too much STDIN\n");
|
adamc@860
|
422 goto done;
|
adamc@860
|
423 }
|
adamc@860
|
424
|
adamc@860
|
425 memcpy(&body[body_read], r->contentData, this_len);
|
adamc@860
|
426 body_read += this_len;
|
adamc@860
|
427 }
|
adamc@860
|
428
|
adamc@861
|
429 body[body_read] = 0;
|
adamc@861
|
430
|
adamc@860
|
431 if (!(method = search_nvps(hs.nvps, "REQUEST_METHOD"))) {
|
adamc@860
|
432 write_stderr(out, "REQUEST_METHOD not set\n");
|
adamc@860
|
433 goto done;
|
adamc@860
|
434 }
|
adamc@860
|
435
|
adamc@860
|
436 if (!(path = search_nvps(hs.nvps, "SCRIPT_NAME"))) {
|
adamc@860
|
437 write_stderr(out, "SCRIPT_NAME not set\n");
|
adamc@860
|
438 goto done;
|
adamc@860
|
439 }
|
adamc@861
|
440
|
adamc@1134
|
441 if ((path_info = search_nvps(hs.nvps, "PATH_INFO"))) {
|
adamc@860
|
442 int len1 = strlen(path), len2 = strlen(path_info);
|
adamc@860
|
443 int len = len1 + len2 + 1;
|
adamc@860
|
444
|
adamc@860
|
445 if (len > path_size) {
|
adamc@860
|
446 path_size = len;
|
adamc@860
|
447 path_buf = realloc(path_buf, path_size);
|
adamc@860
|
448 }
|
adamc@860
|
449
|
adamc@860
|
450 sprintf(path_buf, "%s%s", path, path_info);
|
adamc@860
|
451 path = path_buf;
|
adamc@860
|
452 }
|
adamc@860
|
453
|
adamc@860
|
454 if (!(query_string = search_nvps(hs.nvps, "QUERY_STRING")))
|
adamc@860
|
455 query_string = "";
|
adamc@860
|
456
|
adamc@860
|
457 uw_set_headers(ctx, get_header, &hs);
|
adamc@861
|
458
|
adamc@860
|
459 {
|
adamc@860
|
460 request_result rr;
|
adamc@860
|
461
|
adamc@860
|
462 rr = uw_request(rc, ctx, method, path, query_string, body, body_read,
|
adamc@860
|
463 on_success, on_failure,
|
adamc@860
|
464 out, log_error, log_debug,
|
adamc@863
|
465 in->sock, fastcgi_send_normal, fastcgi_close);
|
adamc@860
|
466
|
adamc@860
|
467 if (rr == KEEP_OPEN)
|
adamc@860
|
468 goto done2;
|
adamc@860
|
469
|
adamc@860
|
470 uw_output(ctx, write_stdout, out);
|
adamc@863
|
471 fastcgi_close_with(out, rr);
|
adamc@863
|
472 goto done2;
|
adamc@860
|
473 }
|
adamc@859
|
474
|
adamc@859
|
475 done:
|
adamc@859
|
476 close(in->sock);
|
adamc@860
|
477 done2:
|
adamc@859
|
478 fastcgi_input_reset(in);
|
adamc@859
|
479 uw_reset(ctx);
|
adamc@859
|
480 }
|
adamc@859
|
481 }
|
adamc@859
|
482
|
adamc@859
|
483 static void help(char *cmd) {
|
adamc@859
|
484 printf("Usage: %s [-t <thread-count>]\n", cmd);
|
adamc@859
|
485 }
|
adamc@859
|
486
|
adamc@859
|
487 static void sigint(int signum) {
|
adamc@859
|
488 printf("Exiting....\n");
|
adamc@859
|
489 exit(0);
|
adamc@859
|
490 }
|
adamc@859
|
491
|
adamc@1094
|
492 static loggers ls = {&uw_application, NULL, log_error, log_debug};
|
adamc@859
|
493
|
adamc@859
|
494 int main(int argc, char *argv[]) {
|
adamc@859
|
495 // The skeleton for this function comes from Beej's sockets tutorial.
|
adamc@859
|
496 struct sockaddr_in their_addr; // connector's address information
|
adamc@1134
|
497 socklen_t sin_size;
|
adamc@859
|
498 int nthreads = 1, i, *names, opt;
|
adamc@859
|
499 char *fwsa = getenv("FCGI_WEB_SERVER_ADDRS"), *nthreads_s = getenv("URWEB_NUM_THREADS");
|
adamc@859
|
500
|
adamc@859
|
501 if (nthreads_s) {
|
adamc@859
|
502 nthreads = atoi(nthreads_s);
|
adamc@859
|
503 if (nthreads <= 0) {
|
adamc@859
|
504 fprintf(stderr, "Bad URWEB_NUM_THREADS value\n");
|
adamc@859
|
505 return 1;
|
adamc@859
|
506 }
|
adamc@859
|
507 }
|
adamc@859
|
508
|
adamc@859
|
509 signal(SIGINT, sigint);
|
adamc@859
|
510 signal(SIGPIPE, SIG_IGN);
|
adamc@859
|
511 signal(SIGUSR1, sigint);
|
adamc@859
|
512 signal(SIGTERM, sigint);
|
adamc@859
|
513
|
adamc@859
|
514 while ((opt = getopt(argc, argv, "ht:")) != -1) {
|
adamc@859
|
515 switch (opt) {
|
adamc@859
|
516 case '?':
|
adamc@859
|
517 fprintf(stderr, "Unknown command-line option");
|
adamc@859
|
518 help(argv[0]);
|
adamc@859
|
519 return 1;
|
adamc@859
|
520
|
adamc@859
|
521 case 'h':
|
adamc@859
|
522 help(argv[0]);
|
adamc@859
|
523 return 0;
|
adamc@859
|
524
|
adamc@859
|
525 case 't':
|
adamc@859
|
526 nthreads = atoi(optarg);
|
adamc@859
|
527 if (nthreads <= 0) {
|
adamc@859
|
528 fprintf(stderr, "Invalid thread count\n");
|
adamc@859
|
529 help(argv[0]);
|
adamc@859
|
530 return 1;
|
adamc@859
|
531 }
|
adamc@859
|
532 break;
|
adamc@859
|
533
|
adamc@859
|
534 default:
|
adamc@859
|
535 fprintf(stderr, "Unexpected getopt() behavior\n");
|
adamc@859
|
536 return 1;
|
adamc@859
|
537 }
|
adamc@859
|
538 }
|
adamc@859
|
539
|
adamc@860
|
540 uw_set_on_success("");
|
adamc@1094
|
541 uw_request_init(&uw_application, NULL, log_error, log_debug);
|
adamc@859
|
542
|
adamc@859
|
543 names = calloc(nthreads, sizeof(int));
|
adamc@859
|
544
|
adamc@859
|
545 sin_size = sizeof their_addr;
|
adamc@859
|
546
|
adamc@859
|
547 {
|
adamc@859
|
548 pthread_t thread;
|
adamc@859
|
549
|
adamc@859
|
550 if (pthread_create(&thread, NULL, client_pruner, &ls)) {
|
adamc@859
|
551 fprintf(stderr, "Error creating pruner thread\n");
|
adamc@859
|
552 return 1;
|
adamc@859
|
553 }
|
adamc@859
|
554 }
|
adamc@859
|
555
|
adamc@859
|
556 for (i = 0; i < nthreads; ++i) {
|
adamc@859
|
557 pthread_t thread;
|
adamc@859
|
558 names[i] = i;
|
adamc@859
|
559 if (pthread_create(&thread, NULL, worker, &names[i])) {
|
adamc@859
|
560 fprintf(stderr, "Error creating worker thread #%d\n", i);
|
adamc@859
|
561 return 1;
|
adamc@859
|
562 }
|
adamc@859
|
563 }
|
adamc@859
|
564
|
adamc@859
|
565 while (1) {
|
adamc@859
|
566 int new_fd = accept(FCGI_LISTENSOCK_FILENO, (struct sockaddr *)&their_addr, &sin_size);
|
adamc@859
|
567
|
adamc@859
|
568 if (new_fd < 0) {
|
adamc@859
|
569 fprintf(stderr, "Socket accept failed\n");
|
adamc@859
|
570 return 1;
|
adamc@859
|
571 }
|
adamc@859
|
572
|
adamc@859
|
573 if (fwsa) {
|
adamc@859
|
574 char host[100], matched = 0;
|
adamc@859
|
575 char *ips, *sep;
|
adamc@859
|
576
|
adamc@859
|
577 if (getnameinfo((struct sockaddr *)&their_addr, sin_size, host, sizeof host, NULL, 0, NI_NUMERICHOST)) {
|
adamc@859
|
578 fprintf(stderr, "Remote IP determination failed\n");
|
adamc@859
|
579 return 1;
|
adamc@859
|
580 }
|
adamc@859
|
581
|
adamc@1134
|
582 for (ips = fwsa; (sep = strchr(ips, ',')); ips = sep+1) {
|
adamc@859
|
583 if (!strncmp(ips, host, sep - ips)) {
|
adamc@859
|
584 matched = 1;
|
adamc@859
|
585 break;
|
adamc@859
|
586 }
|
adamc@859
|
587 }
|
adamc@859
|
588
|
adamc@859
|
589 if (!matched && strcmp(ips, host)) {
|
adamc@859
|
590 fprintf(stderr, "Remote address is not in FCGI_WEB_SERVER_ADDRS");
|
adamc@859
|
591 return 1;
|
adamc@859
|
592 }
|
adamc@859
|
593 }
|
adamc@859
|
594
|
adamc@859
|
595 uw_enqueue(new_fd);
|
adamc@859
|
596 }
|
adamc@859
|
597 }
|
adamc@1121
|
598
|
adamc@1121
|
599 void *uw_init_client_data() {
|
adamc@1121
|
600 return NULL;
|
adamc@1121
|
601 }
|
adamc@1121
|
602
|
adamc@1121
|
603 void uw_free_client_data(void *data) {
|
adamc@1121
|
604 }
|
adamc@1121
|
605
|
adamc@1121
|
606 void uw_copy_client_data(void *dst, void *src) {
|
adamc@1121
|
607 }
|
adamc@1121
|
608
|
adamc@1121
|
609 void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) {
|
adamc@1121
|
610 if (uw_get_app(ctx)->db_begin(ctx))
|
adamc@1121
|
611 uw_error(ctx, FATAL, "Error running SQL BEGIN");
|
adamc@1121
|
612 uw_get_app(ctx)->expunger(ctx, cli);
|
adamc@1121
|
613 if (uw_get_app(ctx)->db_commit(ctx))
|
adamc@1121
|
614 uw_error(ctx, FATAL, "Error running SQL COMMIT");
|
adamc@1121
|
615 }
|
adamc@1121
|
616
|
adamc@1121
|
617 void uw_post_expunge(uw_context ctx, void *data) {
|
adamc@1121
|
618 }
|