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