fcgiapp.c

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