Commit | Line | Data |
ae0319bf |
1 | /* |
0198fd3c |
2 | * fcgiapp.c -- |
3 | * |
4 | * FastCGI application library: request-at-a-time |
5 | * |
6 | * |
7 | * Copyright (c) 1996 Open Market, Inc. |
8 | * |
9 | * See the file "LICENSE.TERMS" for information on usage and redistribution |
10 | * of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
11 | * |
12 | */ |
0198fd3c |
13 | #ifndef lint |
55d1f206 |
14 | static const char rcsid[] = "$Id: fcgiapp.c,v 1.24 2001/06/22 09:12:03 skimo Exp $"; |
0198fd3c |
15 | #endif /* not lint */ |
16 | |
6ad90ad2 |
17 | #include <assert.h> |
18 | #include <errno.h> |
19 | #include <fcntl.h> /* for fcntl */ |
20 | #include <math.h> |
21 | #include <memory.h> /* for memchr() */ |
22 | #include <stdarg.h> |
0198fd3c |
23 | #include <stdio.h> |
6ad90ad2 |
24 | #include <stdlib.h> |
25 | #include <string.h> |
0198fd3c |
26 | #include <sys/types.h> |
6ad90ad2 |
27 | |
fc0ac9b7 |
28 | #include "fcgi_config.h" |
29 | |
6ad90ad2 |
30 | #ifdef HAVE_SYS_SOCKET_H |
31 | #include <sys/socket.h> /* for getpeername */ |
32 | #endif |
33 | |
0198fd3c |
34 | #ifdef HAVE_SYS_TIME_H |
35 | #include <sys/time.h> |
36 | #endif |
37 | |
0198fd3c |
38 | #ifdef HAVE_UNISTD_H |
39 | #include <unistd.h> |
40 | #endif |
41 | |
75ccbd96 |
42 | #ifdef HAVE_LIMITS_H |
43 | #include <limits.h> |
44 | #endif |
45 | |
fc0ac9b7 |
46 | #ifdef _WIN32 |
47 | #define DLLAPI __declspec(dllexport) |
48 | #endif |
49 | |
50 | #include "fcgiapp.h" |
e57ed029 |
51 | #include "fcgiappmisc.h" |
fc0ac9b7 |
52 | |
0198fd3c |
53 | #include "fcgimisc.h" |
0198fd3c |
54 | #include "fastcgi.h" |
55 | #include "fcgios.h" |
56 | |
57 | /* |
ae0319bf |
58 | * This is a workaround for one version of the HP C compiler |
0198fd3c |
59 | * (c89 on HP-UX 9.04, also Stratus FTX), which will dump core |
60 | * if given 'long double' for varargs. |
61 | */ |
62 | #ifdef HAVE_VA_ARG_LONG_DOUBLE_BUG |
63 | #define LONG_DOUBLE double |
64 | #else |
65 | #define LONG_DOUBLE long double |
66 | #endif |
67 | |
5a7cc494 |
68 | /* |
ae0319bf |
69 | * Globals |
5a7cc494 |
70 | */ |
71 | static int libInitialized = 0; |
165bb74e |
72 | static int isFastCGI = -1; |
5a7cc494 |
73 | static char *webServerAddressList = NULL; |
faca4b3a |
74 | static FCGX_Request the_request; |
5a7cc494 |
75 | |
0198fd3c |
76 | static void *Malloc(size_t size) |
77 | { |
78 | void *result = malloc(size); |
79 | ASSERT(size == 0 || result != NULL); |
80 | return result; |
81 | } |
82 | |
83 | static char *StringCopy(char *str) |
84 | { |
85 | int strLen = strlen(str); |
aadcc3c8 |
86 | char *newString = (char *)Malloc(strLen + 1); |
0198fd3c |
87 | memcpy(newString, str, strLen); |
88 | newString[strLen] = '\000'; |
89 | return newString; |
90 | } |
91 | |
92 | \f |
93 | /* |
94 | *---------------------------------------------------------------------- |
95 | * |
96 | * FCGX_GetChar -- |
97 | * |
98 | * Reads a byte from the input stream and returns it. |
99 | * |
100 | * Results: |
101 | * The byte, or EOF (-1) if the end of input has been reached. |
102 | * |
103 | *---------------------------------------------------------------------- |
104 | */ |
105 | int FCGX_GetChar(FCGX_Stream *stream) |
106 | { |
107 | if(stream->rdNext != stream->stop) |
108 | return *stream->rdNext++; |
109 | if(stream->isClosed || !stream->isReader) |
110 | return EOF; |
111 | stream->fillBuffProc(stream); |
112 | stream->stopUnget = stream->rdNext; |
113 | if(stream->rdNext != stream->stop) |
114 | return *stream->rdNext++; |
115 | ASSERT(stream->isClosed); /* bug in fillBufProc if not */ |
116 | return EOF; |
117 | } |
118 | \f |
119 | /* |
120 | *---------------------------------------------------------------------- |
121 | * |
122 | * FCGX_GetStr -- |
123 | * |
124 | * Reads up to n consecutive bytes from the input stream |
125 | * into the character array str. Performs no interpretation |
126 | * of the input bytes. |
127 | * |
128 | * Results: |
129 | * Number of bytes read. If result is smaller than n, |
130 | * the end of input has been reached. |
131 | * |
132 | *---------------------------------------------------------------------- |
133 | */ |
134 | int FCGX_GetStr(char *str, int n, FCGX_Stream *stream) |
135 | { |
136 | int m, bytesMoved; |
137 | |
138 | if(n <= 0) { |
139 | return 0; |
140 | } |
141 | /* |
142 | * Fast path: n bytes are already available |
143 | */ |
144 | if(n <= (stream->stop - stream->rdNext)) { |
145 | memcpy(str, stream->rdNext, n); |
146 | stream->rdNext += n; |
147 | return n; |
148 | } |
149 | /* |
150 | * General case: stream is closed or buffer fill procedure |
151 | * needs to be called |
152 | */ |
153 | bytesMoved = 0; |
154 | for (;;) { |
155 | if(stream->rdNext != stream->stop) { |
156 | m = min(n - bytesMoved, stream->stop - stream->rdNext); |
157 | memcpy(str, stream->rdNext, m); |
158 | bytesMoved += m; |
159 | stream->rdNext += m; |
160 | if(bytesMoved == n) |
161 | return bytesMoved; |
162 | str += m; |
163 | } |
164 | if(stream->isClosed || !stream->isReader) |
165 | return bytesMoved; |
166 | stream->fillBuffProc(stream); |
167 | stream->stopUnget = stream->rdNext; |
168 | } |
169 | } |
170 | \f |
171 | /* |
172 | *---------------------------------------------------------------------- |
173 | * |
174 | * FCGX_GetLine -- |
175 | * |
176 | * Reads up to n-1 consecutive bytes from the input stream |
177 | * into the character array str. Stops before n-1 bytes |
178 | * have been read if '\n' or EOF is read. The terminating '\n' |
179 | * is copied to str. After copying the last byte into str, |
180 | * stores a '\0' terminator. |
181 | * |
182 | * Results: |
183 | * NULL if EOF is the first thing read from the input stream, |
184 | * str otherwise. |
185 | * |
186 | *---------------------------------------------------------------------- |
187 | */ |
188 | char *FCGX_GetLine(char *str, int n, FCGX_Stream *stream) |
189 | { |
190 | int c; |
191 | char *p = str; |
611bc63f |
192 | |
0198fd3c |
193 | n--; |
194 | while (n > 0) { |
195 | c = FCGX_GetChar(stream); |
196 | if(c == EOF) { |
197 | if(p == str) |
198 | return NULL; |
ae0319bf |
199 | else |
0198fd3c |
200 | break; |
ae0319bf |
201 | } |
611bc63f |
202 | *p++ = (char) c; |
0198fd3c |
203 | n--; |
204 | if(c == '\n') |
205 | break; |
206 | } |
207 | *p = '\0'; |
208 | return str; |
209 | } |
210 | \f |
211 | /* |
212 | *---------------------------------------------------------------------- |
213 | * |
214 | * FCGX_UnGetChar -- |
215 | * |
216 | * Pushes back the character c onto the input stream. One |
217 | * character of pushback is guaranteed once a character |
218 | * has been read. No pushback is possible for EOF. |
219 | * |
220 | * Results: |
221 | * Returns c if the pushback succeeded, EOF if not. |
222 | * |
223 | *---------------------------------------------------------------------- |
224 | */ |
225 | int FCGX_UnGetChar(int c, FCGX_Stream *stream) { |
226 | if(c == EOF |
227 | || stream->isClosed |
228 | || !stream->isReader |
229 | || stream->rdNext == stream->stopUnget) |
230 | return EOF; |
231 | --(stream->rdNext); |
611bc63f |
232 | *stream->rdNext = (unsigned char) c; |
0198fd3c |
233 | return c; |
234 | } |
235 | |
236 | /* |
237 | *---------------------------------------------------------------------- |
238 | * |
239 | * FCGX_HasSeenEOF -- |
240 | * |
241 | * Returns EOF if end-of-file has been detected while reading |
242 | * from stream; otherwise returns 0. |
243 | * |
244 | * Note that FCGX_HasSeenEOF(s) may return 0, yet an immediately |
245 | * following FCGX_GetChar(s) may return EOF. This function, like |
246 | * the standard C stdio function feof, does not provide the |
247 | * ability to peek ahead. |
248 | * |
249 | * Results: |
250 | * EOF if end-of-file has been detected, 0 if not. |
251 | * |
252 | *---------------------------------------------------------------------- |
253 | */ |
254 | int FCGX_HasSeenEOF(FCGX_Stream *stream) { |
255 | return (stream->isClosed) ? EOF : 0; |
256 | } |
257 | \f |
258 | /* |
259 | *---------------------------------------------------------------------- |
260 | * |
261 | * FCGX_PutChar -- |
262 | * |
263 | * Writes a byte to the output stream. |
264 | * |
265 | * Results: |
266 | * The byte, or EOF (-1) if an error occurred. |
ae0319bf |
267 | * |
0198fd3c |
268 | *---------------------------------------------------------------------- |
269 | */ |
270 | int FCGX_PutChar(int c, FCGX_Stream *stream) |
271 | { |
272 | if(stream->wrNext != stream->stop) |
611bc63f |
273 | return (*stream->wrNext++ = (unsigned char) c); |
0198fd3c |
274 | if(stream->isClosed || stream->isReader) |
275 | return EOF; |
276 | stream->emptyBuffProc(stream, FALSE); |
277 | if(stream->wrNext != stream->stop) |
611bc63f |
278 | return (*stream->wrNext++ = (unsigned char) c); |
0198fd3c |
279 | ASSERT(stream->isClosed); /* bug in emptyBuffProc if not */ |
280 | return EOF; |
281 | } |
282 | \f |
283 | /* |
284 | *---------------------------------------------------------------------- |
285 | * |
286 | * FCGX_PutStr -- |
287 | * |
288 | * Writes n consecutive bytes from the character array str |
289 | * into the output stream. Performs no interpretation |
290 | * of the output bytes. |
291 | * |
292 | * Results: |
293 | * Number of bytes written (n) for normal return, |
294 | * EOF (-1) if an error occurred. |
ae0319bf |
295 | * |
0198fd3c |
296 | *---------------------------------------------------------------------- |
297 | */ |
298 | int FCGX_PutStr(const char *str, int n, FCGX_Stream *stream) |
299 | { |
300 | int m, bytesMoved; |
301 | |
302 | /* |
303 | * Fast path: room for n bytes in the buffer |
304 | */ |
305 | if(n <= (stream->stop - stream->wrNext)) { |
306 | memcpy(stream->wrNext, str, n); |
307 | stream->wrNext += n; |
308 | return n; |
309 | } |
310 | /* |
311 | * General case: stream is closed or buffer empty procedure |
312 | * needs to be called |
313 | */ |
314 | bytesMoved = 0; |
315 | for (;;) { |
316 | if(stream->wrNext != stream->stop) { |
317 | m = min(n - bytesMoved, stream->stop - stream->wrNext); |
318 | memcpy(stream->wrNext, str, m); |
319 | bytesMoved += m; |
320 | stream->wrNext += m; |
321 | if(bytesMoved == n) |
322 | return bytesMoved; |
323 | str += m; |
324 | } |
325 | if(stream->isClosed || stream->isReader) |
326 | return -1; |
327 | stream->emptyBuffProc(stream, FALSE); |
328 | } |
329 | } |
330 | \f |
331 | /* |
332 | *---------------------------------------------------------------------- |
333 | * |
334 | * FCGX_PutS -- |
335 | * |
336 | * Writes a character string to the output stream. |
337 | * |
338 | * Results: |
339 | * number of bytes written for normal return, |
340 | * EOF (-1) if an error occurred. |
ae0319bf |
341 | * |
0198fd3c |
342 | *---------------------------------------------------------------------- |
343 | */ |
344 | int FCGX_PutS(const char *str, FCGX_Stream *stream) |
345 | { |
346 | return FCGX_PutStr(str, strlen(str), stream); |
347 | } |
348 | \f |
349 | /* |
350 | *---------------------------------------------------------------------- |
351 | * |
352 | * FCGX_FPrintF -- |
353 | * |
354 | * Performs output formatting and writes the results |
355 | * to the output stream. |
356 | * |
357 | * Results: |
358 | * number of bytes written for normal return, |
359 | * EOF (-1) if an error occurred. |
ae0319bf |
360 | * |
0198fd3c |
361 | *---------------------------------------------------------------------- |
362 | */ |
363 | int FCGX_FPrintF(FCGX_Stream *stream, const char *format, ...) |
364 | { |
365 | int result; |
366 | va_list ap; |
367 | va_start(ap, format); |
368 | result = FCGX_VFPrintF(stream, format, ap); |
369 | va_end(ap); |
370 | return result; |
371 | } |
372 | |
373 | /* |
374 | *---------------------------------------------------------------------- |
375 | * |
376 | * FCGX_VFPrintF -- |
377 | * |
378 | * Performs output formatting and writes the results |
379 | * to the output stream. |
380 | * |
381 | * Results: |
382 | * number of bytes written for normal return, |
383 | * EOF (-1) if an error occurred. |
ae0319bf |
384 | * |
0198fd3c |
385 | *---------------------------------------------------------------------- |
386 | */ |
387 | |
388 | #define PRINTF_BUFFLEN 100 |
389 | /* |
390 | * More than sufficient space for all unmodified conversions |
391 | * except %s and %f. |
392 | */ |
393 | #define FMT_BUFFLEN 25 |
394 | /* |
395 | * Max size of a format specifier is 1 + 5 + 7 + 7 + 2 + 1 + slop |
396 | */ |
397 | static void CopyAndAdvance(char **destPtr, char **srcPtr, int n); |
398 | |
399 | int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg) |
400 | { |
401 | char *f, *fStop, *percentPtr, *p, *fmtBuffPtr, *buffPtr; |
611bc63f |
402 | int op, performedOp, sizeModifier, buffCount = 0, buffLen, specifierLength; |
403 | int fastPath, n, auxBuffLen = 0, buffReqd, minWidth, precision, exp; |
0198fd3c |
404 | char *auxBuffPtr = NULL; |
405 | int streamCount = 0; |
406 | char fmtBuff[FMT_BUFFLEN]; |
407 | char buff[PRINTF_BUFFLEN]; |
408 | |
409 | int intArg; |
410 | short shortArg; |
411 | long longArg; |
412 | unsigned unsignedArg; |
413 | unsigned long uLongArg; |
414 | unsigned short uShortArg; |
611bc63f |
415 | char *charPtrArg = NULL; |
0198fd3c |
416 | void *voidPtrArg; |
417 | int *intPtrArg; |
418 | long *longPtrArg; |
419 | short *shortPtrArg; |
611bc63f |
420 | double doubleArg = 0.0; |
421 | LONG_DOUBLE lDoubleArg = 0.0L; |
0198fd3c |
422 | |
423 | fmtBuff[0] = '%'; |
424 | f = (char *) format; |
425 | fStop = f + strlen(f); |
426 | while (f != fStop) { |
aadcc3c8 |
427 | percentPtr = (char *)memchr(f, '%', fStop - f); |
0198fd3c |
428 | if(percentPtr == NULL) percentPtr = fStop; |
429 | if(percentPtr != f) { |
59bfcb87 |
430 | if(FCGX_PutStr(f, percentPtr - f, stream) < 0) |
431 | goto ErrorReturn; |
0198fd3c |
432 | streamCount += percentPtr - f; |
433 | f = percentPtr; |
434 | if(f == fStop) break; |
435 | } |
436 | fastPath = TRUE; |
437 | /* |
438 | * The following loop always executes either once or twice. |
439 | */ |
440 | for (;;) { |
441 | if(fastPath) { |
442 | /* |
443 | * Fast path: Scan optimistically, hoping that no flags, |
444 | * minimum field width, or precision are specified. |
445 | * Use the preallocated buffer, which is large enough |
446 | * for all fast path cases. If the conversion specifier |
447 | * is really more complex, run the loop a second time |
448 | * using the slow path. |
449 | * Note that fast path execution of %s bypasses the buffer |
450 | * and %f is not attempted on the fast path due to |
451 | * its large buffering requirements. |
452 | */ |
453 | op = *(percentPtr + 1); |
454 | switch(op) { |
455 | case 'l': |
456 | case 'L': |
457 | case 'h': |
458 | sizeModifier = op; |
459 | op = *(percentPtr + 2); |
611bc63f |
460 | fmtBuff[1] = (char) sizeModifier; |
461 | fmtBuff[2] = (char) op; |
0198fd3c |
462 | fmtBuff[3] = '\0'; |
463 | specifierLength = 3; |
464 | break; |
465 | default: |
466 | sizeModifier = ' '; |
611bc63f |
467 | fmtBuff[1] = (char) op; |
0198fd3c |
468 | fmtBuff[2] = '\0'; |
469 | specifierLength = 2; |
470 | break; |
471 | } |
472 | buffPtr = buff; |
473 | buffLen = PRINTF_BUFFLEN; |
474 | } else { |
475 | /* |
476 | * Slow path: Scan the conversion specifier and construct |
477 | * a new format string, compute an upper bound on the |
478 | * amount of buffering that sprintf will require, |
479 | * and allocate a larger buffer if necessary. |
480 | */ |
481 | p = percentPtr + 1; |
482 | fmtBuffPtr = &fmtBuff[1]; |
483 | /* |
484 | * Scan flags |
485 | */ |
486 | n = strspn(p, "-0+ #"); |
59bfcb87 |
487 | if(n > 5) |
488 | goto ErrorReturn; |
0198fd3c |
489 | CopyAndAdvance(&fmtBuffPtr, &p, n); |
490 | /* |
491 | * Scan minimum field width |
492 | */ |
493 | n = strspn(p, "0123456789"); |
494 | if(n == 0) { |
495 | if(*p == '*') { |
496 | minWidth = va_arg(arg, int); |
59bfcb87 |
497 | if(abs(minWidth) > 999999) |
498 | goto ErrorReturn; |
0198fd3c |
499 | /* |
500 | * The following use of strlen rather than the |
501 | * value returned from sprintf is because SUNOS4 |
502 | * returns a char * instead of an int count. |
503 | */ |
504 | sprintf(fmtBuffPtr, "%d", minWidth); |
505 | fmtBuffPtr += strlen(fmtBuffPtr); |
506 | p++; |
507 | } else { |
508 | minWidth = 0; |
509 | } |
510 | } else if(n <= 6) { |
511 | minWidth = strtol(p, NULL, 10); |
512 | CopyAndAdvance(&fmtBuffPtr, &p, n); |
513 | } else { |
514 | goto ErrorReturn; |
515 | } |
516 | /* |
517 | * Scan precision |
518 | */ |
519 | if(*p == '.') { |
520 | CopyAndAdvance(&fmtBuffPtr, &p, 1); |
521 | n = strspn(p, "0123456789"); |
522 | if(n == 0) { |
523 | if(*p == '*') { |
524 | precision = va_arg(arg, int); |
525 | if(precision < 0) precision = 0; |
59bfcb87 |
526 | if(precision > 999999) |
527 | goto ErrorReturn; |
0198fd3c |
528 | /* |
529 | * The following use of strlen rather than the |
530 | * value returned from sprintf is because SUNOS4 |
531 | * returns a char * instead of an int count. |
532 | */ |
533 | sprintf(fmtBuffPtr, "%d", precision); |
534 | fmtBuffPtr += strlen(fmtBuffPtr); |
535 | p++; |
536 | } else { |
537 | precision = 0; |
538 | } |
539 | } else if(n <= 6) { |
540 | precision = strtol(p, NULL, 10); |
541 | CopyAndAdvance(&fmtBuffPtr, &p, n); |
542 | } else { |
543 | goto ErrorReturn; |
544 | } |
545 | } else { |
546 | precision = -1; |
547 | } |
548 | /* |
549 | * Scan size modifier and conversion operation |
550 | */ |
551 | switch(*p) { |
552 | case 'l': |
553 | case 'L': |
554 | case 'h': |
555 | sizeModifier = *p; |
556 | CopyAndAdvance(&fmtBuffPtr, &p, 1); |
557 | break; |
558 | default: |
559 | sizeModifier = ' '; |
560 | break; |
561 | } |
562 | op = *p; |
563 | CopyAndAdvance(&fmtBuffPtr, &p, 1); |
564 | ASSERT(fmtBuffPtr - fmtBuff < FMT_BUFFLEN); |
565 | *fmtBuffPtr = '\0'; |
566 | specifierLength = p - percentPtr; |
567 | /* |
568 | * Bound the required buffer size. For s and f |
569 | * conversions this requires examining the argument. |
570 | */ |
571 | switch(op) { |
572 | case 'd': |
573 | case 'i': |
574 | case 'u': |
575 | case 'o': |
576 | case 'x': |
577 | case 'X': |
578 | case 'c': |
579 | case 'p': |
580 | buffReqd = max(precision, 46); |
581 | break; |
582 | case 's': |
583 | charPtrArg = va_arg(arg, char *); |
a25dc891 |
584 | if (!charPtrArg) charPtrArg = "(null)"; |
0198fd3c |
585 | if(precision == -1) { |
586 | buffReqd = strlen(charPtrArg); |
587 | } else { |
aadcc3c8 |
588 | p = (char *)memchr(charPtrArg, '\0', precision); |
0198fd3c |
589 | buffReqd = |
590 | (p == NULL) ? precision : p - charPtrArg; |
591 | } |
592 | break; |
593 | case 'f': |
594 | switch(sizeModifier) { |
595 | case ' ': |
596 | doubleArg = va_arg(arg, double); |
611bc63f |
597 | frexp(doubleArg, &exp); |
0198fd3c |
598 | break; |
599 | case 'L': |
600 | lDoubleArg = va_arg(arg, LONG_DOUBLE); |
611bc63f |
601 | /* XXX Need to check for the presence of |
602 | * frexpl() and use it if available */ |
603 | frexp((double) lDoubleArg, &exp); |
0198fd3c |
604 | break; |
605 | default: |
606 | goto ErrorReturn; |
607 | } |
608 | if(precision == -1) precision = 6; |
609 | buffReqd = precision + 3 + ((exp > 0) ? exp/3 : 0); |
610 | break; |
611 | case 'e': |
612 | case 'E': |
613 | case 'g': |
614 | case 'G': |
615 | if(precision == -1) precision = 6; |
616 | buffReqd = precision + 8; |
617 | break; |
618 | case 'n': |
619 | case '%': |
620 | default: |
621 | goto ErrorReturn; |
622 | break; |
623 | } |
624 | buffReqd = max(buffReqd + 10, minWidth); |
625 | /* |
626 | * Allocate the buffer |
627 | */ |
628 | if(buffReqd <= PRINTF_BUFFLEN) { |
629 | buffPtr = buff; |
630 | buffLen = PRINTF_BUFFLEN; |
631 | } else { |
632 | if(auxBuffPtr == NULL || buffReqd > auxBuffLen) { |
633 | if(auxBuffPtr != NULL) free(auxBuffPtr); |
aadcc3c8 |
634 | auxBuffPtr = (char *)Malloc(buffReqd); |
0198fd3c |
635 | auxBuffLen = buffReqd; |
59bfcb87 |
636 | if(auxBuffPtr == NULL) |
637 | goto ErrorReturn; |
0198fd3c |
638 | } |
639 | buffPtr = auxBuffPtr; |
640 | buffLen = auxBuffLen; |
641 | } |
642 | } |
643 | /* |
644 | * This giant switch statement requires the following variables |
645 | * to be set up: op, sizeModifier, arg, buffPtr, fmtBuff. |
646 | * When fastPath == FALSE and op == 's' or 'f', the argument |
647 | * has been read into charPtrArg, doubleArg, or lDoubleArg. |
648 | * The statement produces the boolean performedOp, TRUE iff |
649 | * the op/sizeModifier were executed and argument consumed; |
650 | * if performedOp, the characters written into buffPtr[] |
651 | * and the character count buffCount (== EOF meaning error). |
652 | * |
653 | * The switch cases are arranged in the same order as in the |
654 | * description of fprintf in section 15.11 of Harbison and Steele. |
655 | */ |
656 | performedOp = TRUE; |
657 | switch(op) { |
658 | case 'd': |
659 | case 'i': |
660 | switch(sizeModifier) { |
661 | case ' ': |
662 | intArg = va_arg(arg, int); |
663 | sprintf(buffPtr, fmtBuff, intArg); |
664 | buffCount = strlen(buffPtr); |
665 | break; |
666 | case 'l': |
667 | longArg = va_arg(arg, long); |
668 | sprintf(buffPtr, fmtBuff, longArg); |
669 | buffCount = strlen(buffPtr); |
670 | break; |
671 | case 'h': |
ab4f3d6f |
672 | shortArg = (short) va_arg(arg, int); |
673 | sprintf(buffPtr, fmtBuff, shortArg); |
0198fd3c |
674 | buffCount = strlen(buffPtr); |
675 | break; |
676 | default: |
677 | goto ErrorReturn; |
678 | } |
679 | break; |
680 | case 'u': |
681 | case 'o': |
682 | case 'x': |
683 | case 'X': |
684 | switch(sizeModifier) { |
685 | case ' ': |
686 | unsignedArg = va_arg(arg, unsigned); |
687 | sprintf(buffPtr, fmtBuff, unsignedArg); |
688 | buffCount = strlen(buffPtr); |
689 | break; |
690 | case 'l': |
691 | uLongArg = va_arg(arg, unsigned long); |
692 | sprintf(buffPtr, fmtBuff, uLongArg); |
693 | buffCount = strlen(buffPtr); |
694 | break; |
695 | case 'h': |
ab4f3d6f |
696 | uShortArg = (unsigned short) va_arg(arg, int); |
697 | sprintf(buffPtr, fmtBuff, uShortArg); |
0198fd3c |
698 | buffCount = strlen(buffPtr); |
699 | break; |
700 | default: |
701 | goto ErrorReturn; |
702 | } |
703 | break; |
704 | case 'c': |
705 | switch(sizeModifier) { |
706 | case ' ': |
707 | intArg = va_arg(arg, int); |
708 | sprintf(buffPtr, fmtBuff, intArg); |
709 | buffCount = strlen(buffPtr); |
710 | break; |
711 | case 'l': |
712 | /* |
713 | * XXX: Allowed by ISO C Amendment 1, but |
714 | * many platforms don't yet support wint_t |
715 | */ |
716 | goto ErrorReturn; |
717 | default: |
718 | goto ErrorReturn; |
719 | } |
720 | break; |
721 | case 's': |
722 | switch(sizeModifier) { |
723 | case ' ': |
724 | if(fastPath) { |
725 | buffPtr = va_arg(arg, char *); |
726 | buffCount = strlen(buffPtr); |
727 | buffLen = buffCount + 1; |
728 | } else { |
729 | sprintf(buffPtr, fmtBuff, charPtrArg); |
730 | buffCount = strlen(buffPtr); |
731 | } |
732 | break; |
733 | case 'l': |
734 | /* |
735 | * XXX: Don't know how to convert a sequence |
736 | * of wide characters into a byte stream, or |
737 | * even how to predict the buffering required. |
738 | */ |
739 | goto ErrorReturn; |
740 | default: |
741 | goto ErrorReturn; |
742 | } |
743 | break; |
744 | case 'p': |
59bfcb87 |
745 | if(sizeModifier != ' ') |
746 | goto ErrorReturn; |
0198fd3c |
747 | voidPtrArg = va_arg(arg, void *); |
748 | sprintf(buffPtr, fmtBuff, voidPtrArg); |
749 | buffCount = strlen(buffPtr); |
750 | break; |
751 | case 'n': |
752 | switch(sizeModifier) { |
753 | case ' ': |
754 | intPtrArg = va_arg(arg, int *); |
755 | *intPtrArg = streamCount; |
756 | break; |
757 | case 'l': |
758 | longPtrArg = va_arg(arg, long *); |
759 | *longPtrArg = streamCount; |
760 | break; |
761 | case 'h': |
611bc63f |
762 | shortPtrArg = (short *) va_arg(arg, short *); |
763 | *shortPtrArg = (short) streamCount; |
0198fd3c |
764 | break; |
765 | default: |
766 | goto ErrorReturn; |
767 | } |
768 | buffCount = 0; |
769 | break; |
770 | case 'f': |
771 | if(fastPath) { |
772 | performedOp = FALSE; |
773 | break; |
774 | } |
775 | switch(sizeModifier) { |
776 | case ' ': |
777 | sprintf(buffPtr, fmtBuff, doubleArg); |
778 | buffCount = strlen(buffPtr); |
779 | break; |
780 | case 'L': |
781 | sprintf(buffPtr, fmtBuff, lDoubleArg); |
782 | buffCount = strlen(buffPtr); |
783 | break; |
784 | default: |
785 | goto ErrorReturn; |
786 | } |
787 | break; |
788 | case 'e': |
789 | case 'E': |
790 | case 'g': |
791 | case 'G': |
792 | switch(sizeModifier) { |
793 | case ' ': |
794 | doubleArg = va_arg(arg, double); |
795 | sprintf(buffPtr, fmtBuff, doubleArg); |
796 | buffCount = strlen(buffPtr); |
797 | break; |
798 | case 'L': |
799 | lDoubleArg = va_arg(arg, LONG_DOUBLE); |
800 | sprintf(buffPtr, fmtBuff, lDoubleArg); |
801 | buffCount = strlen(buffPtr); |
802 | break; |
803 | default: |
804 | goto ErrorReturn; |
805 | } |
806 | break; |
807 | case '%': |
808 | if(sizeModifier != ' ') |
809 | goto ErrorReturn; |
810 | buff[0] = '%'; |
811 | buffCount = 1; |
812 | break; |
813 | case '\0': |
814 | goto ErrorReturn; |
815 | default: |
816 | performedOp = FALSE; |
817 | break; |
818 | } /* switch(op) */ |
819 | if(performedOp) break; |
59bfcb87 |
820 | if(!fastPath) |
821 | goto ErrorReturn; |
0198fd3c |
822 | fastPath = FALSE; |
823 | } /* for (;;) */ |
824 | ASSERT(buffCount < buffLen); |
825 | if(buffCount > 0) { |
826 | if(FCGX_PutStr(buffPtr, buffCount, stream) < 0) |
827 | goto ErrorReturn; |
828 | streamCount += buffCount; |
829 | } else if(buffCount < 0) { |
830 | goto ErrorReturn; |
831 | } |
832 | f += specifierLength; |
833 | } /* while(f != fStop) */ |
834 | goto NormalReturn; |
835 | ErrorReturn: |
836 | streamCount = -1; |
837 | NormalReturn: |
838 | if(auxBuffPtr != NULL) free(auxBuffPtr); |
839 | return streamCount; |
840 | } |
841 | |
842 | /* |
843 | * Copy n characters from *srcPtr to *destPtr, then increment |
844 | * both *srcPtr and *destPtr by n. |
845 | */ |
846 | static void CopyAndAdvance(char **destPtr, char **srcPtr, int n) |
847 | { |
848 | char *dest = *destPtr; |
849 | char *src = *srcPtr; |
850 | int i; |
851 | for (i = 0; i < n; i++) |
852 | *dest++ = *src++; |
853 | *destPtr = dest; |
854 | *srcPtr = src; |
855 | } |
856 | \f |
857 | /* |
858 | *---------------------------------------------------------------------- |
859 | * |
860 | * FCGX_FFlush -- |
861 | * |
862 | * Flushes any buffered output. |
863 | * |
864 | * Server-push is a legitimate application of FCGX_FFlush. |
865 | * Otherwise, FCGX_FFlush is not very useful, since FCGX_Accept |
866 | * does it implicitly. FCGX_FFlush may reduce performance |
867 | * by increasing the total number of operating system calls |
868 | * the application makes. |
869 | * |
870 | * Results: |
871 | * EOF (-1) if an error occurred. |
ae0319bf |
872 | * |
0198fd3c |
873 | *---------------------------------------------------------------------- |
874 | */ |
875 | int FCGX_FFlush(FCGX_Stream *stream) |
876 | { |
877 | if(stream->isClosed || stream->isReader) |
878 | return 0; |
879 | stream->emptyBuffProc(stream, FALSE); |
880 | return (stream->isClosed) ? -1 : 0; |
881 | } |
882 | \f |
883 | /* |
884 | *---------------------------------------------------------------------- |
885 | * |
886 | * FCGX_FClose -- |
887 | * |
888 | * Performs FCGX_FFlush and closes the stream. |
889 | * |
890 | * This is not a very useful operation, since FCGX_Accept |
891 | * does it implicitly. Closing the out stream before the |
892 | * err stream results in an extra write if there's nothing |
893 | * in the err stream, and therefore reduces performance. |
894 | * |
895 | * Results: |
896 | * EOF (-1) if an error occurred. |
ae0319bf |
897 | * |
0198fd3c |
898 | *---------------------------------------------------------------------- |
899 | */ |
900 | int FCGX_FClose(FCGX_Stream *stream) |
901 | { |
59bfcb87 |
902 | if (stream == NULL) return 0; |
903 | |
0198fd3c |
904 | if(!stream->wasFCloseCalled) { |
905 | if(!stream->isReader) { |
906 | stream->emptyBuffProc(stream, TRUE); |
907 | } |
908 | stream->wasFCloseCalled = TRUE; |
909 | stream->isClosed = TRUE; |
910 | if(stream->isReader) { |
911 | stream->wrNext = stream->stop = stream->rdNext; |
912 | } else { |
913 | stream->rdNext = stream->stop = stream->wrNext; |
914 | } |
915 | } |
916 | return (stream->FCGI_errno == 0) ? 0 : EOF; |
917 | } |
918 | \f |
919 | /* |
920 | *---------------------------------------------------------------------- |
921 | * |
922 | * SetError -- |
923 | * |
924 | * An error has occurred; save the error code in the stream |
925 | * for diagnostic purposes and set the stream state so that |
926 | * reads return EOF and writes have no effect. |
927 | * |
928 | *---------------------------------------------------------------------- |
929 | */ |
930 | static void SetError(FCGX_Stream *stream, int FCGI_errno) |
931 | { |
932 | /* |
933 | * Preserve only the first error. |
934 | */ |
935 | if(stream->FCGI_errno == 0) { |
936 | stream->FCGI_errno = FCGI_errno; |
937 | stream->isClosed = TRUE; |
938 | } |
939 | } |
940 | |
941 | /* |
942 | *---------------------------------------------------------------------- |
943 | * |
944 | * FCGX_GetError -- |
945 | * |
946 | * Return the stream error code. 0 means no error, > 0 |
947 | * is an errno(2) error, < 0 is an FCGX_errno error. |
948 | * |
949 | *---------------------------------------------------------------------- |
950 | */ |
951 | int FCGX_GetError(FCGX_Stream *stream) { |
952 | return stream->FCGI_errno; |
953 | } |
954 | |
955 | /* |
956 | *---------------------------------------------------------------------- |
957 | * |
958 | * FCGX_ClearError -- |
959 | * |
960 | * Clear the stream error code and end-of-file indication. |
961 | * |
962 | *---------------------------------------------------------------------- |
963 | */ |
964 | void FCGX_ClearError(FCGX_Stream *stream) { |
965 | stream->FCGI_errno = 0; |
966 | /* |
967 | * stream->isClosed = FALSE; |
968 | * XXX: should clear isClosed but work is needed to make it safe |
969 | * to do so. For example, if an application calls FClose, gets |
970 | * an I/O error on the write, calls ClearError and retries |
971 | * the FClose, FClose (really EmptyBuffProc) will write a second |
972 | * EOF record. If an application calls PutChar instead of FClose |
973 | * after the ClearError, the application will write more data. |
974 | * The stream's state must discriminate between various states |
975 | * of the stream that are now all lumped under isClosed. |
976 | */ |
977 | } |
978 | \f |
979 | /* |
980 | *====================================================================== |
981 | * Parameters |
982 | *====================================================================== |
983 | */ |
984 | |
985 | /* |
986 | * A vector of pointers representing the parameters received |
987 | * by a FastCGI application server, with the vector's length |
988 | * and last valid element so adding new parameters is efficient. |
989 | */ |
990 | |
991 | typedef struct Params { |
992 | FCGX_ParamArray vec; /* vector of strings */ |
993 | int length; /* number of string vec can hold */ |
994 | char **cur; /* current item in vec; *cur == NULL */ |
995 | } Params; |
996 | typedef Params *ParamsPtr; |
997 | \f |
998 | /* |
999 | *---------------------------------------------------------------------- |
1000 | * |
1001 | * NewParams -- |
1002 | * |
1003 | * Creates a new Params structure. |
1004 | * |
1005 | * Results: |
1006 | * Pointer to the new structure. |
1007 | * |
1008 | *---------------------------------------------------------------------- |
1009 | */ |
1010 | static ParamsPtr NewParams(int length) |
1011 | { |
1012 | ParamsPtr result; |
aadcc3c8 |
1013 | result = (Params *)Malloc(sizeof(Params)); |
1014 | result->vec = (char **)Malloc(length * sizeof(char *)); |
0198fd3c |
1015 | result->length = length; |
1016 | result->cur = result->vec; |
1017 | *result->cur = NULL; |
1018 | return result; |
1019 | } |
1020 | \f |
1021 | /* |
1022 | *---------------------------------------------------------------------- |
1023 | * |
1024 | * FreeParams -- |
1025 | * |
1026 | * Frees a Params structure and all the parameters it contains. |
1027 | * |
1028 | * Side effects: |
0b7c9662 |
1029 | * env becomes invalid. |
0198fd3c |
1030 | * |
1031 | *---------------------------------------------------------------------- |
1032 | */ |
1033 | static void FreeParams(ParamsPtr *paramsPtrPtr) |
1034 | { |
1035 | ParamsPtr paramsPtr = *paramsPtrPtr; |
1036 | char **p; |
1037 | if(paramsPtr == NULL) { |
1038 | return; |
1039 | } |
1040 | for (p = paramsPtr->vec; p < paramsPtr->cur; p++) { |
1041 | free(*p); |
1042 | } |
1043 | free(paramsPtr->vec); |
1044 | free(paramsPtr); |
1045 | *paramsPtrPtr = NULL; |
1046 | } |
1047 | \f |
1048 | /* |
1049 | *---------------------------------------------------------------------- |
1050 | * |
1051 | * PutParam -- |
1052 | * |
1053 | * Add a name/value pair to a Params structure. |
1054 | * |
1055 | * Results: |
1056 | * None. |
1057 | * |
ae0319bf |
1058 | * Side effects: |
0198fd3c |
1059 | * Parameters structure updated. |
1060 | * |
1061 | *---------------------------------------------------------------------- |
ae0319bf |
1062 | */ |
0198fd3c |
1063 | static void PutParam(ParamsPtr paramsPtr, char *nameValue) |
1064 | { |
1065 | int size; |
1066 | |
1067 | *paramsPtr->cur++ = nameValue; |
1068 | size = paramsPtr->cur - paramsPtr->vec; |
1069 | if(size >= paramsPtr->length) { |
1070 | paramsPtr->length *= 2; |
aadcc3c8 |
1071 | paramsPtr->vec = (FCGX_ParamArray)realloc(paramsPtr->vec, paramsPtr->length * sizeof(char *)); |
0198fd3c |
1072 | paramsPtr->cur = paramsPtr->vec + size; |
1073 | } |
1074 | *paramsPtr->cur = NULL; |
1075 | } |
1076 | \f |
1077 | /* |
1078 | *---------------------------------------------------------------------- |
1079 | * |
1080 | * FCGX_GetParam -- obtain value of FCGI parameter in environment |
1081 | * |
1082 | * |
1083 | * Results: |
1084 | * Value bound to name, NULL if name not present in the |
1085 | * environment envp. Caller must not mutate the result |
1086 | * or retain it past the end of this request. |
1087 | * |
1088 | *---------------------------------------------------------------------- |
1089 | */ |
1090 | char *FCGX_GetParam(const char *name, FCGX_ParamArray envp) |
1091 | { |
1092 | int len; |
1093 | char **p; |
2fe790fc |
1094 | |
1095 | if (name == NULL || envp == NULL) return NULL; |
1096 | |
0198fd3c |
1097 | len = strlen(name); |
2fe790fc |
1098 | |
1099 | for (p = envp; *p; ++p) { |
0198fd3c |
1100 | if((strncmp(name, *p, len) == 0) && ((*p)[len] == '=')) { |
1101 | return *p+len+1; |
1102 | } |
1103 | } |
1104 | return NULL; |
1105 | } |
1106 | \f |
1107 | /* |
1108 | *---------------------------------------------------------------------- |
1109 | * |
1110 | * Start of FastCGI-specific code |
1111 | * |
1112 | *---------------------------------------------------------------------- |
1113 | */ |
1114 | \f |
1115 | /* |
1116 | *---------------------------------------------------------------------- |
1117 | * |
1118 | * ReadParams -- |
1119 | * |
1120 | * Reads FastCGI name-value pairs from stream until EOF. Converts |
1121 | * each pair to name=value format and adds it to Params structure. |
1122 | * |
1123 | *---------------------------------------------------------------------- |
1124 | */ |
1125 | static int ReadParams(Params *paramsPtr, FCGX_Stream *stream) |
1126 | { |
1127 | int nameLen, valueLen; |
1128 | unsigned char lenBuff[3]; |
1129 | char *nameValue; |
1130 | |
1131 | while((nameLen = FCGX_GetChar(stream)) != EOF) { |
1132 | /* |
1133 | * Read name length (one or four bytes) and value length |
1134 | * (one or four bytes) from stream. |
1135 | */ |
1136 | if((nameLen & 0x80) != 0) { |
1137 | if(FCGX_GetStr((char *) &lenBuff[0], 3, stream) != 3) { |
1138 | SetError(stream, FCGX_PARAMS_ERROR); |
1139 | return -1; |
1140 | } |
1141 | nameLen = ((nameLen & 0x7f) << 24) + (lenBuff[0] << 16) |
1142 | + (lenBuff[1] << 8) + lenBuff[2]; |
1143 | } |
1144 | if((valueLen = FCGX_GetChar(stream)) == EOF) { |
1145 | SetError(stream, FCGX_PARAMS_ERROR); |
1146 | return -1; |
1147 | } |
1148 | if((valueLen & 0x80) != 0) { |
1149 | if(FCGX_GetStr((char *) &lenBuff[0], 3, stream) != 3) { |
1150 | SetError(stream, FCGX_PARAMS_ERROR); |
1151 | return -1; |
1152 | } |
1153 | valueLen = ((valueLen & 0x7f) << 24) + (lenBuff[0] << 16) |
1154 | + (lenBuff[1] << 8) + lenBuff[2]; |
1155 | } |
1156 | /* |
1157 | * nameLen and valueLen are now valid; read the name and value |
1158 | * from stream and construct a standard environment entry. |
1159 | */ |
aadcc3c8 |
1160 | nameValue = (char *)Malloc(nameLen + valueLen + 2); |
0198fd3c |
1161 | if(FCGX_GetStr(nameValue, nameLen, stream) != nameLen) { |
1162 | SetError(stream, FCGX_PARAMS_ERROR); |
1163 | free(nameValue); |
1164 | return -1; |
1165 | } |
1166 | *(nameValue + nameLen) = '='; |
1167 | if(FCGX_GetStr(nameValue + nameLen + 1, valueLen, stream) |
1168 | != valueLen) { |
1169 | SetError(stream, FCGX_PARAMS_ERROR); |
1170 | free(nameValue); |
1171 | return -1; |
1172 | } |
1173 | *(nameValue + nameLen + valueLen + 1) = '\0'; |
1174 | PutParam(paramsPtr, nameValue); |
1175 | } |
1176 | return 0; |
1177 | } |
1178 | \f |
1179 | /* |
1180 | *---------------------------------------------------------------------- |
1181 | * |
1182 | * MakeHeader -- |
1183 | * |
1184 | * Constructs an FCGI_Header struct. |
1185 | * |
1186 | *---------------------------------------------------------------------- |
1187 | */ |
1188 | static FCGI_Header MakeHeader( |
1189 | int type, |
1190 | int requestId, |
1191 | int contentLength, |
1192 | int paddingLength) |
1193 | { |
1194 | FCGI_Header header; |
1195 | ASSERT(contentLength >= 0 && contentLength <= FCGI_MAX_LENGTH); |
1196 | ASSERT(paddingLength >= 0 && paddingLength <= 0xff); |
1197 | header.version = FCGI_VERSION_1; |
611bc63f |
1198 | header.type = (unsigned char) type; |
1199 | header.requestIdB1 = (unsigned char) ((requestId >> 8) & 0xff); |
1200 | header.requestIdB0 = (unsigned char) ((requestId ) & 0xff); |
1201 | header.contentLengthB1 = (unsigned char) ((contentLength >> 8) & 0xff); |
1202 | header.contentLengthB0 = (unsigned char) ((contentLength ) & 0xff); |
1203 | header.paddingLength = (unsigned char) paddingLength; |
0198fd3c |
1204 | header.reserved = 0; |
1205 | return header; |
1206 | } |
1207 | \f |
1208 | /* |
1209 | *---------------------------------------------------------------------- |
1210 | * |
1211 | * MakeEndRequestBody -- |
1212 | * |
1213 | * Constructs an FCGI_EndRequestBody struct. |
1214 | * |
1215 | *---------------------------------------------------------------------- |
1216 | */ |
1217 | static FCGI_EndRequestBody MakeEndRequestBody( |
1218 | int appStatus, |
1219 | int protocolStatus) |
1220 | { |
1221 | FCGI_EndRequestBody body; |
611bc63f |
1222 | body.appStatusB3 = (unsigned char) ((appStatus >> 24) & 0xff); |
1223 | body.appStatusB2 = (unsigned char) ((appStatus >> 16) & 0xff); |
1224 | body.appStatusB1 = (unsigned char) ((appStatus >> 8) & 0xff); |
1225 | body.appStatusB0 = (unsigned char) ((appStatus ) & 0xff); |
1226 | body.protocolStatus = (unsigned char) protocolStatus; |
0198fd3c |
1227 | memset(body.reserved, 0, sizeof(body.reserved)); |
1228 | return body; |
1229 | } |
1230 | \f |
1231 | /* |
1232 | *---------------------------------------------------------------------- |
1233 | * |
1234 | * MakeUnknownTypeBody -- |
1235 | * |
1236 | * Constructs an FCGI_MakeUnknownTypeBody struct. |
1237 | * |
1238 | *---------------------------------------------------------------------- |
1239 | */ |
1240 | static FCGI_UnknownTypeBody MakeUnknownTypeBody( |
1241 | int type) |
1242 | { |
1243 | FCGI_UnknownTypeBody body; |
611bc63f |
1244 | body.type = (unsigned char) type; |
0198fd3c |
1245 | memset(body.reserved, 0, sizeof(body.reserved)); |
1246 | return body; |
1247 | } |
1248 | \f |
1249 | /* |
1250 | *---------------------------------------------------------------------- |
1251 | * |
1252 | * AlignInt8 -- |
1253 | * |
1254 | * Returns the smallest integer greater than or equal to n |
1255 | * that's a multiple of 8. |
1256 | * |
1257 | *---------------------------------------------------------------------- |
1258 | */ |
1259 | static int AlignInt8(unsigned n) { |
1260 | return (n + 7) & (UINT_MAX - 7); |
1261 | } |
1262 | |
1263 | /* |
1264 | *---------------------------------------------------------------------- |
1265 | * |
1266 | * AlignPtr8 -- |
1267 | * |
1268 | * Returns the smallest pointer greater than or equal to p |
1269 | * that's a multiple of 8. |
1270 | * |
1271 | *---------------------------------------------------------------------- |
1272 | */ |
1273 | static unsigned char *AlignPtr8(unsigned char *p) { |
1274 | unsigned long u = (unsigned long) p; |
1275 | u = ((u + 7) & (ULONG_MAX - 7)) - u; |
1276 | return p + u; |
1277 | } |
1278 | \f |
0198fd3c |
1279 | |
1280 | /* |
1281 | * State associated with a stream |
1282 | */ |
1283 | typedef struct FCGX_Stream_Data { |
1284 | unsigned char *buff; /* buffer after alignment */ |
1285 | int bufflen; /* number of bytes buff can store */ |
1286 | unsigned char *mBuff; /* buffer as returned by Malloc */ |
1287 | unsigned char *buffStop; /* reader: last valid byte + 1 of entire buffer. |
1288 | * stop generally differs from buffStop for |
1289 | * readers because of record structure. |
1290 | * writer: buff + bufflen */ |
1291 | int type; /* reader: FCGI_PARAMS or FCGI_STDIN |
1292 | * writer: FCGI_STDOUT or FCGI_STDERR */ |
1293 | int eorStop; /* reader: stop stream at end-of-record */ |
1294 | int skip; /* reader: don't deliver content bytes */ |
1295 | int contentLen; /* reader: bytes of unread content */ |
1296 | int paddingLen; /* reader: bytes of unread padding */ |
1297 | int isAnythingWritten; /* writer: data has been written to ipcFd */ |
1298 | int rawWrite; /* writer: write data without stream headers */ |
5a7cc494 |
1299 | FCGX_Request *reqDataPtr; /* request data not specific to one stream */ |
0198fd3c |
1300 | } FCGX_Stream_Data; |
1301 | \f |
1302 | /* |
1303 | *---------------------------------------------------------------------- |
1304 | * |
1305 | * WriteCloseRecords -- |
1306 | * |
1307 | * Writes an EOF record for the stream content if necessary. |
1308 | * If this is the last writer to close, writes an FCGI_END_REQUEST |
1309 | * record. |
1310 | * |
1311 | *---------------------------------------------------------------------- |
1312 | */ |
1313 | static void WriteCloseRecords(struct FCGX_Stream *stream) |
1314 | { |
aadcc3c8 |
1315 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
1316 | /* |
1317 | * Enter rawWrite mode so final records won't be encapsulated as |
1318 | * stream data. |
1319 | */ |
1320 | data->rawWrite = TRUE; |
1321 | /* |
1322 | * Generate EOF for stream content if needed. |
1323 | */ |
1324 | if(!(data->type == FCGI_STDERR |
1325 | && stream->wrNext == data->buff |
1326 | && !data->isAnythingWritten)) { |
1327 | FCGI_Header header; |
1328 | header = MakeHeader(data->type, data->reqDataPtr->requestId, 0, 0); |
1329 | FCGX_PutStr((char *) &header, sizeof(header), stream); |
1330 | }; |
1331 | /* |
1332 | * Generate FCGI_END_REQUEST record if needed. |
1333 | */ |
1334 | if(data->reqDataPtr->nWriters == 1) { |
1335 | FCGI_EndRequestRecord endRequestRecord; |
1336 | endRequestRecord.header = MakeHeader(FCGI_END_REQUEST, |
1337 | data->reqDataPtr->requestId, |
1338 | sizeof(endRequestRecord.body), 0); |
1339 | endRequestRecord.body = MakeEndRequestBody( |
1340 | data->reqDataPtr->appStatus, FCGI_REQUEST_COMPLETE); |
1341 | FCGX_PutStr((char *) &endRequestRecord, |
1342 | sizeof(endRequestRecord), stream); |
1343 | } |
1344 | data->reqDataPtr->nWriters--; |
1345 | } |
1346 | \f |
19d98b91 |
1347 | |
1348 | |
1349 | static int write_it_all(int fd, char *buf, int len) |
1350 | { |
1351 | int wrote; |
ae0319bf |
1352 | |
19d98b91 |
1353 | while (len) { |
1354 | wrote = OS_Write(fd, buf, len); |
1355 | if (wrote < 0) |
1356 | return wrote; |
1357 | len -= wrote; |
1358 | buf += wrote; |
1359 | } |
1360 | return len; |
1361 | } |
1362 | |
0198fd3c |
1363 | /* |
1364 | *---------------------------------------------------------------------- |
1365 | * |
1366 | * EmptyBuffProc -- |
1367 | * |
1368 | * Encapsulates any buffered stream content in a FastCGI |
1369 | * record. Writes the data, making the buffer empty. |
1370 | * |
1371 | *---------------------------------------------------------------------- |
1372 | */ |
1373 | static void EmptyBuffProc(struct FCGX_Stream *stream, int doClose) |
1374 | { |
aadcc3c8 |
1375 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
1376 | int cLen, eLen; |
1377 | /* |
1378 | * If the buffer contains stream data, fill in the header. |
1379 | * Pad the record to a multiple of 8 bytes in length. Padding |
1380 | * can't overflow the buffer because the buffer is a multiple |
1381 | * of 8 bytes in length. If the buffer contains no stream |
1382 | * data, reclaim the space reserved for the header. |
1383 | */ |
1384 | if(!data->rawWrite) { |
1385 | cLen = stream->wrNext - data->buff - sizeof(FCGI_Header); |
1386 | if(cLen > 0) { |
1387 | eLen = AlignInt8(cLen); |
1388 | /* |
1389 | * Giving the padding a well-defined value keeps Purify happy. |
1390 | */ |
1391 | memset(stream->wrNext, 0, eLen - cLen); |
1392 | stream->wrNext += eLen - cLen; |
1393 | *((FCGI_Header *) data->buff) |
1394 | = MakeHeader(data->type, |
1395 | data->reqDataPtr->requestId, cLen, eLen - cLen); |
1396 | } else { |
1397 | stream->wrNext = data->buff; |
1398 | } |
1399 | } |
1400 | if(doClose) { |
1401 | WriteCloseRecords(stream); |
1402 | }; |
19d98b91 |
1403 | if (stream->wrNext != data->buff) { |
0198fd3c |
1404 | data->isAnythingWritten = TRUE; |
19d98b91 |
1405 | if (write_it_all(data->reqDataPtr->ipcFd, (char *)data->buff, stream->wrNext - data->buff) < 0) { |
0198fd3c |
1406 | SetError(stream, OS_Errno); |
1407 | return; |
1408 | } |
1409 | stream->wrNext = data->buff; |
1410 | } |
1411 | /* |
1412 | * The buffer is empty. |
1413 | */ |
1414 | if(!data->rawWrite) { |
1415 | stream->wrNext += sizeof(FCGI_Header); |
1416 | } |
1417 | } |
1418 | \f |
1419 | /* |
1420 | * Return codes for Process* functions |
1421 | */ |
1422 | #define STREAM_RECORD 0 |
1423 | #define SKIP 1 |
1424 | #define BEGIN_RECORD 2 |
1425 | #define MGMT_RECORD 3 |
1426 | |
1427 | /* |
1428 | *---------------------------------------------------------------------- |
1429 | * |
1430 | * ProcessManagementRecord -- |
1431 | * |
1432 | * Reads and responds to a management record. The only type of |
1433 | * management record this library understands is FCGI_GET_VALUES. |
1434 | * The only variables that this library's FCGI_GET_VALUES |
1435 | * understands are FCGI_MAX_CONNS, FCGI_MAX_REQS, and FCGI_MPXS_CONNS. |
1436 | * Ignore other FCGI_GET_VALUES variables; respond to other |
1437 | * management records with a FCGI_UNKNOWN_TYPE record. |
1438 | * |
1439 | *---------------------------------------------------------------------- |
1440 | */ |
1441 | static int ProcessManagementRecord(int type, FCGX_Stream *stream) |
1442 | { |
aadcc3c8 |
1443 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
1444 | ParamsPtr paramsPtr = NewParams(3); |
1445 | char **pPtr; |
1446 | char response[64]; /* 64 = 8 + 3*(1+1+14+1)* + padding */ |
1447 | char *responseP = &response[FCGI_HEADER_LEN]; |
611bc63f |
1448 | char *name, value = '\0'; |
0198fd3c |
1449 | int len, paddedLen; |
1450 | if(type == FCGI_GET_VALUES) { |
1451 | ReadParams(paramsPtr, stream); |
1452 | if((FCGX_GetError(stream) != 0) || (data->contentLen != 0)) { |
1453 | FreeParams(¶msPtr); |
1454 | return FCGX_PROTOCOL_ERROR; |
1455 | } |
1456 | for (pPtr = paramsPtr->vec; pPtr < paramsPtr->cur; pPtr++) { |
1457 | name = *pPtr; |
1458 | *(strchr(name, '=')) = '\0'; |
1459 | if(strcmp(name, FCGI_MAX_CONNS) == 0) { |
1460 | value = '1'; |
1461 | } else if(strcmp(name, FCGI_MAX_REQS) == 0) { |
1462 | value = '1'; |
1463 | } else if(strcmp(name, FCGI_MPXS_CONNS) == 0) { |
1464 | value = '0'; |
1465 | } else { |
1466 | name = NULL; |
1467 | } |
1468 | if(name != NULL) { |
1469 | len = strlen(name); |
1470 | sprintf(responseP, "%c%c%s%c", len, 1, name, value); |
1471 | responseP += len + 3; |
1472 | } |
1473 | } |
1474 | len = responseP - &response[FCGI_HEADER_LEN]; |
1475 | paddedLen = AlignInt8(len); |
1476 | *((FCGI_Header *) response) |
1477 | = MakeHeader(FCGI_GET_VALUES_RESULT, FCGI_NULL_REQUEST_ID, |
1478 | len, paddedLen - len); |
1479 | FreeParams(¶msPtr); |
1480 | } else { |
1481 | paddedLen = len = sizeof(FCGI_UnknownTypeBody); |
1482 | ((FCGI_UnknownTypeRecord *) response)->header |
1483 | = MakeHeader(FCGI_UNKNOWN_TYPE, FCGI_NULL_REQUEST_ID, |
1484 | len, 0); |
1485 | ((FCGI_UnknownTypeRecord *) response)->body |
1486 | = MakeUnknownTypeBody(type); |
1487 | } |
19d98b91 |
1488 | if (write_it_all(data->reqDataPtr->ipcFd, response, FCGI_HEADER_LEN + paddedLen) < 0) { |
0198fd3c |
1489 | SetError(stream, OS_Errno); |
1490 | return -1; |
1491 | } |
19d98b91 |
1492 | |
0198fd3c |
1493 | return MGMT_RECORD; |
1494 | } |
1495 | \f |
1496 | /* |
1497 | *---------------------------------------------------------------------- |
1498 | * |
1499 | * ProcessBeginRecord -- |
1500 | * |
1501 | * Reads an FCGI_BEGIN_REQUEST record. |
1502 | * |
1503 | * Results: |
1504 | * BEGIN_RECORD for normal return. FCGX_PROTOCOL_ERROR for |
1505 | * protocol error. SKIP for attempt to multiplex |
1506 | * connection. -1 for error from write (errno in stream). |
1507 | * |
1508 | * Side effects: |
1509 | * In case of BEGIN_RECORD return, stores requestId, role, |
1510 | * keepConnection values, and sets isBeginProcessed = TRUE. |
1511 | * |
1512 | *---------------------------------------------------------------------- |
1513 | */ |
1514 | static int ProcessBeginRecord(int requestId, FCGX_Stream *stream) |
1515 | { |
aadcc3c8 |
1516 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
1517 | FCGI_BeginRequestBody body; |
1518 | if(requestId == 0 || data->contentLen != sizeof(body)) { |
1519 | return FCGX_PROTOCOL_ERROR; |
1520 | } |
1521 | if(data->reqDataPtr->isBeginProcessed) { |
1522 | /* |
1523 | * The Web server is multiplexing the connection. This library |
1524 | * doesn't know how to handle multiplexing, so respond with |
1525 | * FCGI_END_REQUEST{protocolStatus = FCGI_CANT_MPX_CONN} |
1526 | */ |
1527 | FCGI_EndRequestRecord endRequestRecord; |
1528 | endRequestRecord.header = MakeHeader(FCGI_END_REQUEST, |
1529 | requestId, sizeof(endRequestRecord.body), 0); |
1530 | endRequestRecord.body |
1531 | = MakeEndRequestBody(0, FCGI_CANT_MPX_CONN); |
19d98b91 |
1532 | if (write_it_all(data->reqDataPtr->ipcFd, (char *)&endRequestRecord, sizeof(endRequestRecord)) < 0) { |
0198fd3c |
1533 | SetError(stream, OS_Errno); |
1534 | return -1; |
1535 | } |
19d98b91 |
1536 | |
0198fd3c |
1537 | return SKIP; |
1538 | } |
1539 | /* |
1540 | * Accept this new request. Read the record body. |
1541 | */ |
1542 | data->reqDataPtr->requestId = requestId; |
1543 | if(FCGX_GetStr((char *) &body, sizeof(body), stream) |
1544 | != sizeof(body)) { |
1545 | return FCGX_PROTOCOL_ERROR; |
1546 | } |
1547 | data->reqDataPtr->keepConnection = (body.flags & FCGI_KEEP_CONN); |
1548 | data->reqDataPtr->role = (body.roleB1 << 8) + body.roleB0; |
1549 | data->reqDataPtr->isBeginProcessed = TRUE; |
1550 | return BEGIN_RECORD; |
1551 | } |
1552 | \f |
1553 | /* |
1554 | *---------------------------------------------------------------------- |
1555 | * |
1556 | * ProcessHeader -- |
1557 | * |
1558 | * Interprets FCGI_Header. Processes FCGI_BEGIN_REQUEST and |
1559 | * management records here; extracts information from stream |
1560 | * records (FCGI_PARAMS, FCGI_STDIN) into stream. |
1561 | * |
1562 | * Results: |
1563 | * >= 0 for a normal return, < 0 for error |
1564 | * |
1565 | * Side effects: |
1566 | * XXX: Many (more than there used to be). |
1567 | * If !stream->isRequestIdSet, ProcessHeader initializes |
1568 | * stream->requestId from header and sets stream->isRequestIdSet |
1569 | * to TRUE. ProcessHeader also sets stream->contentLen to header's |
1570 | * contentLength, and sets stream->paddingLen to the header's |
1571 | * paddingLength. |
1572 | * |
1573 | *---------------------------------------------------------------------- |
1574 | */ |
1575 | static int ProcessHeader(FCGI_Header header, FCGX_Stream *stream) |
1576 | { |
aadcc3c8 |
1577 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
1578 | int requestId; |
1579 | if(header.version != FCGI_VERSION_1) { |
1580 | return FCGX_UNSUPPORTED_VERSION; |
1581 | } |
1582 | requestId = (header.requestIdB1 << 8) |
1583 | + header.requestIdB0; |
1584 | data->contentLen = (header.contentLengthB1 << 8) |
1585 | + header.contentLengthB0; |
1586 | data->paddingLen = header.paddingLength; |
1587 | if(header.type == FCGI_BEGIN_REQUEST) { |
1588 | return ProcessBeginRecord(requestId, stream); |
1589 | } |
1590 | if(requestId == FCGI_NULL_REQUEST_ID) { |
1591 | return ProcessManagementRecord(header.type, stream); |
1592 | } |
1593 | if(requestId != data->reqDataPtr->requestId) { |
1594 | return SKIP; |
1595 | } |
1596 | if(header.type != data->type) { |
1597 | return FCGX_PROTOCOL_ERROR; |
1598 | } |
1599 | return STREAM_RECORD; |
1600 | } |
1601 | \f |
1602 | /* |
1603 | *---------------------------------------------------------------------- |
1604 | * |
1605 | * FillBuffProc -- |
1606 | * |
1607 | * Reads bytes from the ipcFd, supplies bytes to a stream client. |
1608 | * |
1609 | *---------------------------------------------------------------------- |
1610 | */ |
1611 | static void FillBuffProc(FCGX_Stream *stream) |
1612 | { |
aadcc3c8 |
1613 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
1614 | FCGI_Header header; |
1615 | int headerLen = 0; |
1616 | int status, count; |
1617 | |
1618 | for (;;) { |
1619 | /* |
1620 | * If data->buff is empty, do a read. |
1621 | */ |
1622 | if(stream->rdNext == data->buffStop) { |
1623 | count = OS_Read(data->reqDataPtr->ipcFd, (char *)data->buff, |
1624 | data->bufflen); |
1625 | if(count <= 0) { |
1626 | SetError(stream, (count == 0 ? FCGX_PROTOCOL_ERROR : OS_Errno)); |
1627 | return; |
1628 | } |
1629 | stream->rdNext = data->buff; |
1630 | data->buffStop = data->buff + count; |
1631 | } |
1632 | /* |
1633 | * Now data->buff is not empty. If the current record contains |
1634 | * more content bytes, deliver all that are present in data->buff. |
1635 | */ |
1636 | if(data->contentLen > 0) { |
1637 | count = min(data->contentLen, data->buffStop - stream->rdNext); |
1638 | data->contentLen -= count; |
1639 | if(!data->skip) { |
1640 | stream->wrNext = stream->stop = stream->rdNext + count; |
1641 | return; |
1642 | } else { |
1643 | stream->rdNext += count; |
1644 | if(data->contentLen > 0) { |
1645 | continue; |
1646 | } else { |
1647 | data->skip = FALSE; |
1648 | } |
1649 | } |
1650 | } |
1651 | /* |
1652 | * If the current record (whose content has been fully consumed by |
1653 | * the client) was padded, skip over the padding bytes. |
1654 | */ |
1655 | if(data->paddingLen > 0) { |
1656 | count = min(data->paddingLen, data->buffStop - stream->rdNext); |
1657 | data->paddingLen -= count; |
1658 | stream->rdNext += count; |
1659 | if(data->paddingLen > 0) { |
1660 | continue; |
1661 | } |
1662 | } |
1663 | /* |
1664 | * All done with the current record, including the padding. |
1665 | * If we're in a recursive call from ProcessHeader, deliver EOF. |
1666 | */ |
1667 | if(data->eorStop) { |
1668 | stream->stop = stream->rdNext; |
1669 | stream->isClosed = TRUE; |
1670 | return; |
1671 | } |
1672 | /* |
1673 | * Fill header with bytes from the input buffer. |
1674 | */ |
1675 | count = min((int)sizeof(header) - headerLen, |
1676 | data->buffStop - stream->rdNext); |
1677 | memcpy(((char *)(&header)) + headerLen, stream->rdNext, count); |
1678 | headerLen += count; |
1679 | stream->rdNext += count; |
1680 | if(headerLen < sizeof(header)) { |
1681 | continue; |
1682 | }; |
1683 | headerLen = 0; |
1684 | /* |
1685 | * Interpret header. eorStop prevents ProcessHeader from reading |
1686 | * past the end-of-record when using stream to read content. |
1687 | */ |
1688 | data->eorStop = TRUE; |
1689 | stream->stop = stream->rdNext; |
1690 | status = ProcessHeader(header, stream); |
1691 | data->eorStop = FALSE; |
1692 | stream->isClosed = FALSE; |
1693 | switch(status) { |
1694 | case STREAM_RECORD: |
1695 | /* |
1696 | * If this stream record header marked the end of stream |
1697 | * data deliver EOF to the stream client, otherwise loop |
1698 | * and deliver data. |
1699 | * |
1700 | * XXX: If this is final stream and |
1701 | * stream->rdNext != data->buffStop, buffered |
1702 | * data is next request (server pipelining)? |
1703 | */ |
1704 | if(data->contentLen == 0) { |
1705 | stream->wrNext = stream->stop = stream->rdNext; |
1706 | stream->isClosed = TRUE; |
1707 | return; |
1708 | } |
1709 | break; |
1710 | case SKIP: |
1711 | data->skip = TRUE; |
1712 | break; |
1713 | case BEGIN_RECORD: |
1714 | /* |
1715 | * If this header marked the beginning of a new |
1716 | * request, return role information to caller. |
1717 | */ |
1718 | return; |
1719 | break; |
1720 | case MGMT_RECORD: |
1721 | break; |
1722 | default: |
1723 | ASSERT(status < 0); |
1724 | SetError(stream, status); |
1725 | return; |
1726 | break; |
1727 | } |
1728 | } |
1729 | } |
1730 | \f |
1731 | /* |
1732 | *---------------------------------------------------------------------- |
1733 | * |
1734 | * NewStream -- |
1735 | * |
1736 | * Creates a stream to read or write from an open ipcFd. |
1737 | * The stream performs reads/writes of up to bufflen bytes. |
1738 | * |
1739 | *---------------------------------------------------------------------- |
1740 | */ |
1741 | static FCGX_Stream *NewStream( |
5a7cc494 |
1742 | FCGX_Request *reqDataPtr, int bufflen, int isReader, int streamType) |
0198fd3c |
1743 | { |
1744 | /* |
1745 | * XXX: It would be a lot cleaner to have a NewStream that only |
1746 | * knows about the type FCGX_Stream, with all other |
1747 | * necessary data passed in. It appears that not just |
1748 | * data and the two procs are needed for initializing stream, |
1749 | * but also data->buff and data->buffStop. This has implications |
1750 | * for procs that want to swap buffers, too. |
1751 | */ |
aadcc3c8 |
1752 | FCGX_Stream *stream = (FCGX_Stream *)Malloc(sizeof(FCGX_Stream)); |
1753 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)Malloc(sizeof(FCGX_Stream_Data)); |
0198fd3c |
1754 | data->reqDataPtr = reqDataPtr; |
1755 | bufflen = AlignInt8(min(max(bufflen, 32), FCGI_MAX_LENGTH + 1)); |
1756 | data->bufflen = bufflen; |
aadcc3c8 |
1757 | data->mBuff = (unsigned char *)Malloc(bufflen); |
0198fd3c |
1758 | data->buff = AlignPtr8(data->mBuff); |
1759 | if(data->buff != data->mBuff) { |
1760 | data->bufflen -= 8; |
1761 | } |
1762 | if(isReader) { |
1763 | data->buffStop = data->buff; |
1764 | } else { |
1765 | data->buffStop = data->buff + data->bufflen; |
1766 | } |
1767 | data->type = streamType; |
1768 | data->eorStop = FALSE; |
1769 | data->skip = FALSE; |
1770 | data->contentLen = 0; |
1771 | data->paddingLen = 0; |
1772 | data->isAnythingWritten = FALSE; |
1773 | data->rawWrite = FALSE; |
1774 | |
1775 | stream->data = data; |
1776 | stream->isReader = isReader; |
1777 | stream->isClosed = FALSE; |
1778 | stream->wasFCloseCalled = FALSE; |
1779 | stream->FCGI_errno = 0; |
1780 | if(isReader) { |
1781 | stream->fillBuffProc = FillBuffProc; |
1782 | stream->emptyBuffProc = NULL; |
1783 | stream->rdNext = data->buff; |
1784 | stream->stop = stream->rdNext; |
1785 | stream->stopUnget = data->buff; |
1786 | stream->wrNext = stream->stop; |
1787 | } else { |
1788 | stream->fillBuffProc = NULL; |
1789 | stream->emptyBuffProc = EmptyBuffProc; |
1790 | stream->wrNext = data->buff + sizeof(FCGI_Header); |
1791 | stream->stop = data->buffStop; |
1792 | stream->stopUnget = NULL; |
1793 | stream->rdNext = stream->stop; |
1794 | } |
1795 | return stream; |
1796 | } |
1797 | \f |
1798 | /* |
1799 | *---------------------------------------------------------------------- |
1800 | * |
1801 | * FreeStream -- |
1802 | * |
1803 | * Frees all storage allocated when *streamPtr was created, |
1804 | * and nulls out *streamPtr. |
1805 | * |
1806 | *---------------------------------------------------------------------- |
1807 | */ |
1808 | void FreeStream(FCGX_Stream **streamPtr) |
1809 | { |
1810 | FCGX_Stream *stream = *streamPtr; |
1811 | FCGX_Stream_Data *data; |
1812 | if(stream == NULL) { |
1813 | return; |
1814 | } |
aadcc3c8 |
1815 | data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
1816 | data->reqDataPtr = NULL; |
1817 | free(data->mBuff); |
1818 | free(data); |
1819 | free(stream); |
1820 | *streamPtr = NULL; |
1821 | } |
1822 | \f |
1823 | /* |
1824 | *---------------------------------------------------------------------- |
1825 | * |
1826 | * SetReaderType -- |
1827 | * |
1828 | * Re-initializes the stream to read data of the specified type. |
1829 | * |
1830 | *---------------------------------------------------------------------- |
1831 | */ |
1832 | static FCGX_Stream *SetReaderType(FCGX_Stream *stream, int streamType) |
1833 | { |
aadcc3c8 |
1834 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
1835 | ASSERT(stream->isReader); |
1836 | data->type = streamType; |
1837 | data->eorStop = FALSE; |
1838 | data->skip = FALSE; |
1839 | data->contentLen = 0; |
1840 | data->paddingLen = 0; |
1841 | stream->wrNext = stream->stop = stream->rdNext; |
1842 | stream->isClosed = FALSE; |
1843 | return stream; |
1844 | } |
1845 | \f |
1846 | /* |
1847 | *---------------------------------------------------------------------- |
1848 | * |
1849 | * NewReader -- |
1850 | * |
1851 | * Creates a stream to read streamType records for the given |
1852 | * request. The stream performs OS reads of up to bufflen bytes. |
1853 | * |
1854 | *---------------------------------------------------------------------- |
1855 | */ |
5a7cc494 |
1856 | static FCGX_Stream *NewReader(FCGX_Request *reqDataPtr, int bufflen, int streamType) |
0198fd3c |
1857 | { |
1858 | return NewStream(reqDataPtr, bufflen, TRUE, streamType); |
1859 | } |
1860 | |
1861 | |
1862 | /* |
1863 | *---------------------------------------------------------------------- |
1864 | * |
1865 | * NewWriter -- |
1866 | * |
1867 | * Creates a stream to write streamType FastCGI records, using |
1868 | * the ipcFd and RequestId contained in *reqDataPtr. |
1869 | * The stream performs OS writes of up to bufflen bytes. |
1870 | * |
1871 | *---------------------------------------------------------------------- |
1872 | */ |
5a7cc494 |
1873 | static FCGX_Stream *NewWriter(FCGX_Request *reqDataPtr, int bufflen, int streamType) |
0198fd3c |
1874 | { |
1875 | return NewStream(reqDataPtr, bufflen, FALSE, streamType); |
1876 | } |
1877 | |
1878 | |
1879 | /* |
1880 | *---------------------------------------------------------------------- |
1881 | * |
1882 | * CreateWriter -- |
1883 | * |
1884 | * Creates a stream to write streamType FastCGI records, using |
1885 | * the given ipcFd and request Id. This function is provided |
1886 | * for use by cgi-fcgi. In order to be defensive against misuse, |
1887 | * this function leaks a little storage; cgi-fcgi doesn't care. |
1888 | * |
1889 | *---------------------------------------------------------------------- |
1890 | */ |
1891 | FCGX_Stream *CreateWriter( |
1892 | int ipcFd, |
1893 | int requestId, |
1894 | int bufflen, |
1895 | int streamType) |
1896 | { |
aadcc3c8 |
1897 | FCGX_Request *reqDataPtr = (FCGX_Request *)Malloc(sizeof(FCGX_Request)); |
0198fd3c |
1898 | reqDataPtr->ipcFd = ipcFd; |
1899 | reqDataPtr->requestId = requestId; |
1900 | /* |
1901 | * Suppress writing an FCGI_END_REQUEST record. |
1902 | */ |
1903 | reqDataPtr->nWriters = 2; |
1904 | return NewWriter(reqDataPtr, bufflen, streamType); |
1905 | } |
1906 | \f |
1907 | /* |
1908 | *====================================================================== |
1909 | * Control |
1910 | *====================================================================== |
1911 | */ |
1912 | |
0198fd3c |
1913 | /* |
1914 | *---------------------------------------------------------------------- |
1915 | * |
1916 | * FCGX_IsCGI -- |
1917 | * |
1918 | * This routine determines if the process is running as a CGI or |
1919 | * FastCGI process. The distinction is made by determining whether |
1920 | * FCGI_LISTENSOCK_FILENO is a listener ipcFd or the end of a |
1921 | * pipe (ie. standard in). |
1922 | * |
1923 | * Results: |
1924 | * TRUE if the process is a CGI process, FALSE if FastCGI. |
1925 | * |
0198fd3c |
1926 | *---------------------------------------------------------------------- |
1927 | */ |
1928 | int FCGX_IsCGI(void) |
1929 | { |
5a7cc494 |
1930 | if (isFastCGI != -1) { |
1931 | return !isFastCGI; |
0198fd3c |
1932 | } |
ae0319bf |
1933 | |
5a7cc494 |
1934 | if (!libInitialized) { |
1935 | int rc = FCGX_Init(); |
1936 | if (rc) { |
1937 | /* exit() isn't great, but hey */ |
1938 | exit((rc < 0) ? rc : -rc); |
1939 | } |
ae0319bf |
1940 | } |
0198fd3c |
1941 | |
0b7c9662 |
1942 | isFastCGI = OS_IsFcgi(FCGI_LISTENSOCK_FILENO); |
5a7cc494 |
1943 | |
1944 | return !isFastCGI; |
0198fd3c |
1945 | } |
1946 | \f |
1947 | /* |
1948 | *---------------------------------------------------------------------- |
1949 | * |
1950 | * FCGX_Finish -- |
1951 | * |
1952 | * Finishes the current request from the HTTP server. |
1953 | * |
1954 | * Side effects: |
1955 | * |
1956 | * Finishes the request accepted by (and frees any |
1957 | * storage allocated by) the previous call to FCGX_Accept. |
1958 | * |
1959 | * DO NOT retain pointers to the envp array or any strings |
1960 | * contained in it (e.g. to the result of calling FCGX_GetParam), |
1961 | * since these will be freed by the next call to FCGX_Finish |
1962 | * or FCGX_Accept. |
1963 | * |
1964 | *---------------------------------------------------------------------- |
1965 | */ |
0198fd3c |
1966 | |
1967 | void FCGX_Finish(void) |
1968 | { |
faca4b3a |
1969 | FCGX_Finish_r(&the_request); |
5a7cc494 |
1970 | } |
1971 | |
1972 | /* |
1973 | *---------------------------------------------------------------------- |
1974 | * |
1975 | * FCGX_Finish_r -- |
1976 | * |
1977 | * Finishes the current request from the HTTP server. |
1978 | * |
1979 | * Side effects: |
1980 | * |
1981 | * Finishes the request accepted by (and frees any |
1982 | * storage allocated by) the previous call to FCGX_Accept. |
1983 | * |
1984 | * DO NOT retain pointers to the envp array or any strings |
1985 | * contained in it (e.g. to the result of calling FCGX_GetParam), |
1986 | * since these will be freed by the next call to FCGX_Finish |
1987 | * or FCGX_Accept. |
1988 | * |
1989 | *---------------------------------------------------------------------- |
1990 | */ |
1991 | void FCGX_Finish_r(FCGX_Request *reqDataPtr) |
1992 | { |
1993 | if (reqDataPtr == NULL) { |
1994 | return; |
1995 | } |
1996 | |
59bfcb87 |
1997 | /* This should probably use a 'status' member instead of 'in' */ |
0b7c9662 |
1998 | if (reqDataPtr->in) { |
1999 | int errStatus = FCGX_FClose(reqDataPtr->err); |
2000 | int outStatus = FCGX_FClose(reqDataPtr->out); |
5a7cc494 |
2001 | |
8462b1ec |
2002 | if (errStatus || outStatus || FCGX_GetError(reqDataPtr->in)) |
5a7cc494 |
2003 | { |
2004 | OS_IpcClose(reqDataPtr->ipcFd); |
a7ad2f46 |
2005 | reqDataPtr->ipcFd = -1; |
5a7cc494 |
2006 | } |
8462b1ec |
2007 | } |
5a7cc494 |
2008 | |
8462b1ec |
2009 | FCGX_Free(reqDataPtr); |
2010 | } |
59bfcb87 |
2011 | |
8462b1ec |
2012 | void FCGX_Free(FCGX_Request * request) |
2013 | { |
2014 | if (request == NULL) |
2015 | return; |
5a7cc494 |
2016 | |
8462b1ec |
2017 | FreeStream(&request->in); |
2018 | FreeStream(&request->out); |
2019 | FreeStream(&request->err); |
2020 | FreeParams(&request->paramsPtr); |
5a7cc494 |
2021 | |
8462b1ec |
2022 | if (!request->keepConnection) |
2023 | { |
2024 | OS_IpcClose(request->ipcFd); |
2025 | request->ipcFd = -1; |
0198fd3c |
2026 | } |
2027 | } |
5a7cc494 |
2028 | |
0b7c9662 |
2029 | int FCGX_OpenSocket(const char *path, int backlog) |
2030 | { |
165bb74e |
2031 | int rc = OS_CreateLocalIpcFd(path, backlog); |
2032 | if (rc == FCGI_LISTENSOCK_FILENO && isFastCGI == 0) { |
2033 | /* XXX probably need to call OS_LibInit() again for Win */ |
2034 | isFastCGI = 1; |
2035 | } |
3da6011f |
2036 | return rc; |
0b7c9662 |
2037 | } |
2038 | |
2039 | int FCGX_InitRequest(FCGX_Request *request, int sock, int flags) |
5a7cc494 |
2040 | { |
2041 | memset(request, 0, sizeof(FCGX_Request)); |
0b7c9662 |
2042 | |
2043 | /* @@@ Should check that sock is open and listening */ |
2044 | request->listen_sock = sock; |
2045 | |
2046 | /* @@@ Should validate against "known" flags */ |
2047 | request->flags = flags; |
2048 | |
8c74d6f3 |
2049 | request->ipcFd = -1; |
2050 | |
0b7c9662 |
2051 | return 0; |
5a7cc494 |
2052 | } |
2053 | |
2054 | /* |
2055 | *---------------------------------------------------------------------- |
2056 | * |
2057 | * FCGX_Init -- |
2058 | * |
2059 | * Initilize the FCGX library. This is called by FCGX_Accept() |
2060 | * but must be called by the user when using FCGX_Accept_r(). |
2061 | * |
2062 | * Results: |
2063 | * 0 for successful call. |
2064 | * |
2065 | *---------------------------------------------------------------------- |
2066 | */ |
2067 | int FCGX_Init(void) |
2068 | { |
2069 | char *p; |
ae0319bf |
2070 | |
5a7cc494 |
2071 | if (libInitialized) { |
2072 | return 0; |
2073 | } |
2074 | |
2075 | /* If our compiler doesn't play by the ISO rules for struct layout, halt. */ |
2076 | ASSERT(sizeof(FCGI_Header) == FCGI_HEADER_LEN); |
2077 | |
faca4b3a |
2078 | FCGX_InitRequest(&the_request, FCGI_LISTENSOCK_FILENO, 0); |
5a7cc494 |
2079 | |
2080 | if (OS_LibInit(NULL) == -1) { |
2081 | return OS_Errno ? OS_Errno : -9997; |
2082 | } |
2083 | |
2084 | p = getenv("FCGI_WEB_SERVER_ADDRS"); |
faca4b3a |
2085 | webServerAddressList = p ? StringCopy(p) : NULL; |
ae0319bf |
2086 | |
5a7cc494 |
2087 | libInitialized = 1; |
2088 | return 0; |
2089 | } |
2090 | |
0198fd3c |
2091 | /* |
2092 | *---------------------------------------------------------------------- |
2093 | * |
2094 | * FCGX_Accept -- |
2095 | * |
2096 | * Accepts a new request from the HTTP server. |
2097 | * |
2098 | * Results: |
2099 | * 0 for successful call, -1 for error. |
2100 | * |
2101 | * Side effects: |
2102 | * |
2103 | * Finishes the request accepted by (and frees any |
2104 | * storage allocated by) the previous call to FCGX_Accept. |
2105 | * Creates input, output, and error streams and |
2106 | * assigns them to *in, *out, and *err respectively. |
2107 | * Creates a parameters data structure to be accessed |
2108 | * via getenv(3) (if assigned to environ) or by FCGX_GetParam |
2109 | * and assigns it to *envp. |
2110 | * |
2111 | * DO NOT retain pointers to the envp array or any strings |
2112 | * contained in it (e.g. to the result of calling FCGX_GetParam), |
2113 | * since these will be freed by the next call to FCGX_Finish |
2114 | * or FCGX_Accept. |
2115 | * |
2116 | *---------------------------------------------------------------------- |
2117 | */ |
0198fd3c |
2118 | |
2119 | int FCGX_Accept( |
2120 | FCGX_Stream **in, |
2121 | FCGX_Stream **out, |
2122 | FCGX_Stream **err, |
2123 | FCGX_ParamArray *envp) |
2124 | { |
165bb74e |
2125 | int rc; |
2126 | |
611bc63f |
2127 | if (! libInitialized) { |
2128 | rc = FCGX_Init(); |
2129 | if (rc) { |
2130 | return rc; |
5a7cc494 |
2131 | } |
ae0319bf |
2132 | } |
0198fd3c |
2133 | |
faca4b3a |
2134 | rc = FCGX_Accept_r(&the_request); |
165bb74e |
2135 | |
faca4b3a |
2136 | *in = the_request.in; |
2137 | *out = the_request.out; |
2138 | *err = the_request.err; |
2139 | *envp = the_request.envp; |
165bb74e |
2140 | |
2141 | return rc; |
5a7cc494 |
2142 | } |
0198fd3c |
2143 | |
5a7cc494 |
2144 | /* |
2145 | *---------------------------------------------------------------------- |
2146 | * |
2147 | * FCGX_Accept_r -- |
2148 | * |
2149 | * Accepts a new request from the HTTP server. |
2150 | * |
2151 | * Results: |
2152 | * 0 for successful call, -1 for error. |
2153 | * |
2154 | * Side effects: |
2155 | * |
2156 | * Finishes the request accepted by (and frees any |
2157 | * storage allocated by) the previous call to FCGX_Accept. |
2158 | * Creates input, output, and error streams and |
2159 | * assigns them to *in, *out, and *err respectively. |
2160 | * Creates a parameters data structure to be accessed |
2161 | * via getenv(3) (if assigned to environ) or by FCGX_GetParam |
2162 | * and assigns it to *envp. |
2163 | * |
2164 | * DO NOT retain pointers to the envp array or any strings |
2165 | * contained in it (e.g. to the result of calling FCGX_GetParam), |
2166 | * since these will be freed by the next call to FCGX_Finish |
2167 | * or FCGX_Accept. |
2168 | * |
2169 | *---------------------------------------------------------------------- |
2170 | */ |
0b7c9662 |
2171 | int FCGX_Accept_r(FCGX_Request *reqDataPtr) |
5a7cc494 |
2172 | { |
2173 | if (!libInitialized) { |
2174 | return -9998; |
0198fd3c |
2175 | } |
5a7cc494 |
2176 | |
2177 | /* Finish the current request, if any. */ |
2178 | FCGX_Finish_r(reqDataPtr); |
2179 | |
2180 | for (;;) { |
0198fd3c |
2181 | /* |
2182 | * If a connection isn't open, accept a new connection (blocking). |
2183 | * If an OS error occurs in accepting the connection, |
2184 | * return -1 to the caller, who should exit. |
2185 | */ |
5a7cc494 |
2186 | if (reqDataPtr->ipcFd < 0) { |
0b7c9662 |
2187 | int fail_on_intr = reqDataPtr->flags & FCGI_FAIL_ACCEPT_ON_INTR; |
2188 | |
2189 | reqDataPtr->ipcFd = OS_Accept(reqDataPtr->listen_sock, fail_on_intr, webServerAddressList); |
5a7cc494 |
2190 | if (reqDataPtr->ipcFd < 0) { |
2191 | return (errno > 0) ? (0 - errno) : -9999; |
2192 | } |
2193 | } |
0198fd3c |
2194 | /* |
2195 | * A connection is open. Read from the connection in order to |
2196 | * get the request's role and environment. If protocol or other |
2197 | * errors occur, close the connection and try again. |
2198 | */ |
2199 | reqDataPtr->isBeginProcessed = FALSE; |
0b7c9662 |
2200 | reqDataPtr->in = NewReader(reqDataPtr, 8192, 0); |
2201 | FillBuffProc(reqDataPtr->in); |
0198fd3c |
2202 | if(!reqDataPtr->isBeginProcessed) { |
2203 | goto TryAgain; |
2204 | } |
2205 | { |
2206 | char *roleStr; |
2207 | switch(reqDataPtr->role) { |
2208 | case FCGI_RESPONDER: |
2209 | roleStr = "FCGI_ROLE=RESPONDER"; |
2210 | break; |
2211 | case FCGI_AUTHORIZER: |
2212 | roleStr = "FCGI_ROLE=AUTHORIZER"; |
2213 | break; |
2214 | case FCGI_FILTER: |
2215 | roleStr = "FCGI_ROLE=FILTER"; |
2216 | break; |
2217 | default: |
2218 | goto TryAgain; |
2219 | } |
2220 | reqDataPtr->paramsPtr = NewParams(30); |
2221 | PutParam(reqDataPtr->paramsPtr, StringCopy(roleStr)); |
2222 | } |
0b7c9662 |
2223 | SetReaderType(reqDataPtr->in, FCGI_PARAMS); |
2224 | if(ReadParams(reqDataPtr->paramsPtr, reqDataPtr->in) >= 0) { |
0198fd3c |
2225 | /* |
2226 | * Finished reading the environment. No errors occurred, so |
2227 | * leave the connection-retry loop. |
2228 | */ |
2229 | break; |
2230 | } |
eff1a9bb |
2231 | |
0198fd3c |
2232 | /* |
2233 | * Close the connection and try again. |
2234 | */ |
eff1a9bb |
2235 | TryAgain: |
55d1f206 |
2236 | reqDataPtr->keepConnection = 0; |
8462b1ec |
2237 | FCGX_Free(reqDataPtr); |
eff1a9bb |
2238 | |
0198fd3c |
2239 | } /* for (;;) */ |
2240 | /* |
2241 | * Build the remaining data structures representing the new |
2242 | * request and return successfully to the caller. |
2243 | */ |
0b7c9662 |
2244 | SetReaderType(reqDataPtr->in, FCGI_STDIN); |
2245 | reqDataPtr->out = NewWriter(reqDataPtr, 8192, FCGI_STDOUT); |
2246 | reqDataPtr->err = NewWriter(reqDataPtr, 512, FCGI_STDERR); |
0198fd3c |
2247 | reqDataPtr->nWriters = 2; |
165bb74e |
2248 | reqDataPtr->envp = reqDataPtr->paramsPtr->vec; |
0198fd3c |
2249 | return 0; |
2250 | } |
2251 | \f |
2252 | /* |
2253 | *---------------------------------------------------------------------- |
2254 | * |
2255 | * FCGX_StartFilterData -- |
2256 | * |
2257 | * stream is an input stream for a FCGI_FILTER request. |
2258 | * stream is positioned at EOF on FCGI_STDIN. |
2259 | * Repositions stream to the start of FCGI_DATA. |
2260 | * If the preconditions are not met (e.g. FCGI_STDIN has not |
2261 | * been read to EOF) sets the stream error code to |
2262 | * FCGX_CALL_SEQ_ERROR. |
2263 | * |
2264 | * Results: |
ae0319bf |
2265 | * 0 for a normal return, < 0 for error |
0198fd3c |
2266 | * |
2267 | *---------------------------------------------------------------------- |
2268 | */ |
2269 | |
2270 | int FCGX_StartFilterData(FCGX_Stream *stream) |
2271 | { |
aadcc3c8 |
2272 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
2273 | if(data->reqDataPtr->role != FCGI_FILTER |
2274 | || !stream->isReader |
2275 | || !stream->isClosed |
2276 | || data->type != FCGI_STDIN) { |
2277 | SetError(stream, FCGX_CALL_SEQ_ERROR); |
2278 | return -1; |
2279 | } |
faca4b3a |
2280 | SetReaderType(stream, FCGI_DATA); |
0198fd3c |
2281 | return 0; |
2282 | } |
2283 | \f |
2284 | /* |
2285 | *---------------------------------------------------------------------- |
2286 | * |
2287 | * FCGX_SetExitStatus -- |
2288 | * |
2289 | * Sets the exit status for stream's request. The exit status |
2290 | * is the status code the request would have exited with, had |
2291 | * the request been run as a CGI program. You can call |
2292 | * SetExitStatus several times during a request; the last call |
2293 | * before the request ends determines the value. |
2294 | * |
2295 | *---------------------------------------------------------------------- |
2296 | */ |
2297 | |
2298 | void FCGX_SetExitStatus(int status, FCGX_Stream *stream) |
2299 | { |
aadcc3c8 |
2300 | FCGX_Stream_Data *data = (FCGX_Stream_Data *)stream->data; |
0198fd3c |
2301 | data->reqDataPtr->appStatus = status; |
2302 | } |
5a7cc494 |
2303 | |