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