Check for socklen_t
[catagits/fcgi2.git] / cgi-fcgi / cgi-fcgi.c
CommitLineData
e3fe7c0c 1/*
0198fd3c 2 * cgifcgi.c --
3 *
4 * CGI to FastCGI bridge
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
3d683188 14static const char rcsid[] = "$Id: cgi-fcgi.c,v 1.7 1999/07/28 00:38:34 roberts Exp $";
0198fd3c 15#endif /* not lint */
16
3d683188 17#include "fcgi_config.h"
18
0198fd3c 19#include <assert.h>
3d683188 20#include <ctype.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <stdio.h>
0198fd3c 24#include <stdlib.h>
25#include <string.h>
3d683188 26
0198fd3c 27#ifdef HAVE_NETDB_H
28#include <netdb.h>
29#endif
3d683188 30
31#ifdef _WIN32
32#include <stdlib.h>
33#else
34extern char **environ;
0198fd3c 35#endif
3d683188 36
0198fd3c 37#ifdef HAVE_SYS_PARAM_H
38#include <sys/param.h>
39#endif
3d683188 40
41#ifdef HAVE_SYS_TIME_H
42#include <sys/time.h>
43#endif
44
45#if defined HAVE_UNISTD_H
46#include <unistd.h>
47#endif
48
0198fd3c 49#include "fcgimisc.h"
50#include "fcgiapp.h"
51#include "fcgiappmisc.h"
52#include "fastcgi.h"
0198fd3c 53#include "fcgios.h"
54
04d12200 55
0198fd3c 56static int wsReadPending = 0;
0198fd3c 57static int fcgiReadPending = 0;
58static int fcgiWritePending = 0;
59
60static void ScheduleIo(void);
61
62\f
63/*
64 * Simple buffer (not ring buffer) type, used by all event handlers.
65 */
66#define BUFFLEN 8192
67typedef struct {
68 char *next;
69 char *stop;
70 char buff[BUFFLEN];
71} Buffer;
72
73/*
74 *----------------------------------------------------------------------
75 *
76 * GetPtr --
77 *
78 * Returns a count of the number of characters available
79 * in the buffer (at most n) and advances past these
80 * characters. Stores a pointer to the first of these
81 * characters in *ptr.
82 *
83 *----------------------------------------------------------------------
84 */
85
86static int GetPtr(char **ptr, int n, Buffer *pBuf)
87{
88 int result;
89 *ptr = pBuf->next;
90 result = min(n, pBuf->stop - pBuf->next);
91 pBuf->next += result;
92 return result;
93}
94\f
95/*
96 *----------------------------------------------------------------------
97 *
98 * MakeHeader --
99 *
100 * Constructs an FCGI_Header struct.
101 *
102 *----------------------------------------------------------------------
103 */
104static FCGI_Header MakeHeader(
105 int type,
106 int requestId,
107 int contentLength,
108 int paddingLength)
109{
110 FCGI_Header header;
111 ASSERT(contentLength >= 0 && contentLength <= FCGI_MAX_LENGTH);
112 ASSERT(paddingLength >= 0 && paddingLength <= 0xff);
113 header.version = FCGI_VERSION_1;
114 header.type = type;
115 header.requestIdB1 = (requestId >> 8) & 0xff;
116 header.requestIdB0 = (requestId ) & 0xff;
117 header.contentLengthB1 = (contentLength >> 8) & 0xff;
118 header.contentLengthB0 = (contentLength ) & 0xff;
119 header.paddingLength = paddingLength;
120 header.reserved = 0;
121 return header;
122}
123\f
124/*
125 *----------------------------------------------------------------------
126 *
127 * MakeBeginRequestBody --
128 *
129 * Constructs an FCGI_BeginRequestBody record.
130 *
131 *----------------------------------------------------------------------
132 */
133static FCGI_BeginRequestBody MakeBeginRequestBody(
134 int role,
135 int keepConnection)
136{
137 FCGI_BeginRequestBody body;
138 ASSERT((role >> 16) == 0);
139 body.roleB1 = (role >> 8) & 0xff;
140 body.roleB0 = (role ) & 0xff;
141 body.flags = (keepConnection) ? FCGI_KEEP_CONN : 0;
142 memset(body.reserved, 0, sizeof(body.reserved));
143 return body;
144}
145
146\f
147static int bytesToRead; /* number of bytes to read from Web Server */
148static int appServerSock = -1; /* Socket connected to FastCGI application,
149 * used by AppServerReadHandler and
150 * AppServerWriteHandler. */
151static Buffer fromAS; /* Bytes read from the FCGI application server. */
152static FCGI_Header header; /* Header of the current record. Is global
153 * since read may return a partial header. */
154static int headerLen = 0; /* Number of valid bytes contained in header.
155 * If headerLen < sizeof(header),
156 * AppServerReadHandler is reading a record header;
157 * otherwise it is reading bytes of record content
158 * or padding. */
159static int contentLen; /* If headerLen == sizeof(header), contentLen
160 * is the number of content bytes still to be
161 * read. */
162static int paddingLen; /* If headerLen == sizeof(header), paddingLen
163 * is the number of padding bytes still
164 * to be read. */
165static int requestId; /* RequestId of the current request.
166 * Set by main. */
167static FCGI_EndRequestBody erBody;
168static int readingEndRequestBody = FALSE;
169 /* If readingEndRequestBody, erBody contains
170 * partial content: contentLen more bytes need
171 * to be read. */
172static int exitStatus = 0;
173static int exitStatusSet = FALSE;
174
175static int stdinFds[3];
176
177\f
178/*
179 *----------------------------------------------------------------------
180 *
181 * FCGIexit --
182 *
183 * FCGIexit provides a single point of exit. It's main use is for
184 * application debug when porting to other operating systems.
185 *
186 *----------------------------------------------------------------------
187 */
188static void FCGIexit(int exitCode)
189{
190 if(appServerSock != -1) {
191 OS_Close(appServerSock);
192 appServerSock = -1;
193 }
194 OS_LibShutdown();
195 exit(exitCode);
196}
197
198#undef exit
199#define exit FCGIexit
200
201\f
202/*
203 *----------------------------------------------------------------------
204 *
205 * AppServerReadHandler --
206 *
207 * Reads data from the FCGI application server and (blocking)
208 * writes all of it to the Web server. Exits the program upon
209 * reading EOF from the FCGI application server. Called only when
210 * there's data ready to read from the application server.
211 *
212 *----------------------------------------------------------------------
213 */
214
215static void AppServerReadHandler(ClientData clientData, int bytesRead)
216{
217 int count, outFD;
218 char *ptr;
219
220 assert(fcgiReadPending == TRUE);
221 fcgiReadPending = FALSE;
222 count = bytesRead;
223
224 if(count <= 0) {
225 if(count < 0) {
226 exit(OS_Errno);
227 }
228 if(headerLen > 0 || paddingLen > 0) {
229 exit(FCGX_PROTOCOL_ERROR);
230 }
231 if(appServerSock != -1) {
232 OS_Close(appServerSock);
233 appServerSock = -1;
234 }
235 /*
236 * XXX: Shouldn't be here if exitStatusSet.
237 */
238 exit((exitStatusSet) ? exitStatus : FCGX_PROTOCOL_ERROR);
239 }
240 fromAS.stop = fromAS.next + count;
241 while(fromAS.next != fromAS.stop) {
242 /*
243 * fromAS is not empty. What to do with the contents?
244 */
245 if(headerLen < sizeof(header)) {
246 /*
247 * First priority is to complete the header.
248 */
249 count = GetPtr(&ptr, sizeof(header) - headerLen, &fromAS);
250 assert(count > 0);
251 memcpy(&header + headerLen, ptr, count);
252 headerLen += count;
253 if(headerLen < sizeof(header)) {
254 break;
255 }
256 if(header.version != FCGI_VERSION_1) {
257 exit(FCGX_UNSUPPORTED_VERSION);
258 }
259 if((header.requestIdB1 << 8) + header.requestIdB0 != requestId) {
260 exit(FCGX_PROTOCOL_ERROR);
261 }
262 contentLen = (header.contentLengthB1 << 8)
263 + header.contentLengthB0;
264 paddingLen = header.paddingLength;
265 } else {
266 /*
267 * Header is complete (possibly from previous call). What now?
268 */
269 switch(header.type) {
270 case FCGI_STDOUT:
271 case FCGI_STDERR:
272 /*
273 * Write the buffered content to stdout or stderr.
274 * Blocking writes are OK here; can't prevent a slow
275 * client from tying up the app server without buffering
276 * output in temporary files.
277 */
278 count = GetPtr(&ptr, contentLen, &fromAS);
279 contentLen -= count;
280 if(count > 0) {
281 outFD = (header.type == FCGI_STDOUT) ?
282 STDOUT_FILENO : STDERR_FILENO;
283 if(OS_Write(outFD, ptr, count) < 0) {
284 exit(OS_Errno);
285 }
286 }
287 break;
288 case FCGI_END_REQUEST:
289 if(!readingEndRequestBody) {
290 if(contentLen != sizeof(erBody)) {
291 exit(FCGX_PROTOCOL_ERROR);
292 }
293 readingEndRequestBody = TRUE;
294 }
295 count = GetPtr(&ptr, contentLen, &fromAS);
296 if(count > 0) {
297 memcpy(&erBody + sizeof(erBody) - contentLen,
298 ptr, count);
299 contentLen -= count;
300 }
301 if(contentLen == 0) {
302 if(erBody.protocolStatus != FCGI_REQUEST_COMPLETE) {
303 /*
304 * XXX: What to do with FCGI_OVERLOADED?
305 */
306 exit(FCGX_PROTOCOL_ERROR);
307 }
308 exitStatus = (erBody.appStatusB3 << 24)
309 + (erBody.appStatusB2 << 16)
310 + (erBody.appStatusB1 << 8)
311 + (erBody.appStatusB0 );
312 exitStatusSet = TRUE;
313 readingEndRequestBody = FALSE;
314 }
315 break;
316 case FCGI_GET_VALUES_RESULT:
317 /* coming soon */
318 case FCGI_UNKNOWN_TYPE:
319 /* coming soon */
320 default:
321 exit(FCGX_PROTOCOL_ERROR);
322 }
323 if(contentLen == 0) {
324 if(paddingLen > 0) {
325 paddingLen -= GetPtr(&ptr, paddingLen, &fromAS);
326 }
327 /*
328 * If we've processed all the data and skipped all the
329 * padding, discard the header and look for the next one.
330 */
331 if(paddingLen == 0) {
332 headerLen = 0;
333 }
334 }
335 } /* headerLen >= sizeof(header) */
336 } /*while*/
337 ScheduleIo();
338}
339\f
340static Buffer fromWS; /* Buffer for data read from Web server
341 * and written to FastCGI application. Used
342 * by WebServerReadHandler and
343 * AppServerWriteHandler. */
344static int webServerReadHandlerEOF;
345 /* TRUE iff WebServerReadHandler has read EOF from
346 * the Web server. Used in main to prevent
347 * rescheduling WebServerReadHandler. */
348
349/*
350 *----------------------------------------------------------------------
351 *
352 * WebServerReadHandler --
353 *
354 * Non-blocking reads data from the Web server into the fromWS
355 * buffer. Called only when fromWS is empty, no EOF has been
356 * received from the Web server, and there's data available to read.
357 *
358 *----------------------------------------------------------------------
359 */
360
361static void WebServerReadHandler(ClientData clientData, int bytesRead)
362{
363 assert(fromWS.next == fromWS.stop);
364 assert(fromWS.next == &fromWS.buff[0]);
365 assert(wsReadPending == TRUE);
366 wsReadPending = FALSE;
367
368 if(bytesRead < 0) {
369 exit(OS_Errno);
370 }
371 *((FCGI_Header *) &fromWS.buff[0])
372 = MakeHeader(FCGI_STDIN, requestId, bytesRead, 0);
373 bytesToRead -= bytesRead;
374 fromWS.stop = &fromWS.buff[sizeof(FCGI_Header) + bytesRead];
375 webServerReadHandlerEOF = (bytesRead == 0);
376 ScheduleIo();
377}
378\f
379/*
380 *----------------------------------------------------------------------
381 *
382 * AppServerWriteHandler --
383 *
384 * Non-blocking writes data from the fromWS buffer to the FCGI
385 * application server. Called only when fromWS is non-empty
386 * and the socket is ready to accept some data.
387 *
388 *----------------------------------------------------------------------
389 */
390
391static void AppServerWriteHandler(ClientData clientData, int bytesWritten)
392{
393 int length = fromWS.stop - fromWS.next;
394
395 assert(length > 0);
396 assert(fcgiWritePending == TRUE);
397
398 fcgiWritePending = FALSE;
399 if(bytesWritten < 0) {
400 exit(OS_Errno);
401 }
402 if((int)bytesWritten < length) {
403 fromWS.next += bytesWritten;
404 } else {
405 fromWS.stop = fromWS.next = &fromWS.buff[0];
406 }
407
408 ScheduleIo();
e3fe7c0c 409}
0198fd3c 410
411\f
412/*
413 * ScheduleIo --
414 *
415 * This functions is responsible for scheduling all I/O to move
416 * data between a web server and a FastCGI application.
417 *
418 * Results:
419 * None.
420 *
421 * Side effects:
422 * This routine will signal the ioEvent upon completion.
e3fe7c0c 423 *
0198fd3c 424 */
425static void ScheduleIo(void)
426{
427 int length;
428
429 /*
430 * Move data between standard in and the FastCGI connection.
431 */
432 if(!fcgiWritePending && appServerSock != -1 &&
433 ((length = fromWS.stop - fromWS.next) != 0)) {
434 if(OS_AsyncWrite(appServerSock, 0, fromWS.next, length,
435 AppServerWriteHandler,
436 (ClientData)appServerSock) == -1) {
437 FCGIexit(OS_Errno);
438 } else {
439 fcgiWritePending = TRUE;
440 }
441 }
442
443 /*
444 * Schedule a read from the FastCGI application if there's not
445 * one pending and there's room in the buffer.
446 */
447 if(!fcgiReadPending && appServerSock != -1) {
448 fromAS.next = &fromAS.buff[0];
449
e3fe7c0c 450 if(OS_AsyncRead(appServerSock, 0, fromAS.next, BUFFLEN,
0198fd3c 451 AppServerReadHandler,
452 (ClientData)appServerSock) == -1) {
453 FCGIexit(OS_Errno);
454 } else {
455 fcgiReadPending = TRUE;
456 }
457 }
458
459 /*
460 * Schedule a read from standard in if necessary.
461 */
462 if((bytesToRead > 0) && !webServerReadHandlerEOF && !wsReadPending &&
463 !fcgiWritePending &&
464 fromWS.next == &fromWS.buff[0]) {
465 if(OS_AsyncReadStdin(fromWS.next + sizeof(FCGI_Header),
e3fe7c0c 466 BUFFLEN - sizeof(FCGI_Header),
0198fd3c 467 WebServerReadHandler, STDIN_FILENO)== -1) {
468 FCGIexit(OS_Errno);
469 } else {
470 wsReadPending = TRUE;
471 }
472 }
473}
474
475\f
476/*
477 *----------------------------------------------------------------------
478 *
479 * FCGI_Start --
480 *
481 * Starts nServers copies of FCGI application appPath, all
482 * listening to a Unix Domain socket at bindPath.
483 *
484 *----------------------------------------------------------------------
485 */
486
487static void FCGI_Start(char *bindPath, char *appPath, int nServers)
488{
489 int listenFd, i;
0198fd3c 490
491 if((listenFd = OS_CreateLocalIpcFd(bindPath)) == -1) {
492 exit(OS_Errno);
493 }
e3fe7c0c 494
0198fd3c 495 if(access(appPath, X_OK) == -1) {
496 fprintf(stderr, "%s is not executable\n", appPath);
497 exit(1);
498 }
499
500 /*
501 * Create the server processes
502 */
503 for(i = 0; i < nServers; i++) {
504 if(OS_SpawnChild(appPath, listenFd) == -1) {
505 exit(OS_Errno);
506 }
507 }
508 OS_Close(listenFd);
509}
510\f
511/*
512 *----------------------------------------------------------------------
513 *
514 * FCGIUtil_BuildNameValueHeader --
515 *
516 * Builds a name-value pair header from the name length
517 * and the value length. Stores the header into *headerBuffPtr,
518 * and stores the length of the header into *headerLenPtr.
519 *
520 * Side effects:
521 * Stores header's length (at most 8) into *headerLenPtr,
522 * and stores the header itself into
523 * headerBuffPtr[0 .. *headerLenPtr - 1].
524 *
525 *----------------------------------------------------------------------
526 */
0198fd3c 527static void FCGIUtil_BuildNameValueHeader(
528 int nameLen,
529 int valueLen,
530 unsigned char *headerBuffPtr,
531 int *headerLenPtr) {
532 unsigned char *startHeaderBuffPtr = headerBuffPtr;
533
534 ASSERT(nameLen >= 0);
3d683188 535 if (nameLen < 0x80) {
0198fd3c 536 *headerBuffPtr++ = nameLen;
537 } else {
538 *headerBuffPtr++ = (nameLen >> 24) | 0x80;
539 *headerBuffPtr++ = (nameLen >> 16);
540 *headerBuffPtr++ = (nameLen >> 8);
541 *headerBuffPtr++ = nameLen;
542 }
543 ASSERT(valueLen >= 0);
3d683188 544 if (valueLen < 0x80) {
0198fd3c 545 *headerBuffPtr++ = valueLen;
546 } else {
547 *headerBuffPtr++ = (valueLen >> 24) | 0x80;
548 *headerBuffPtr++ = (valueLen >> 16);
549 *headerBuffPtr++ = (valueLen >> 8);
550 *headerBuffPtr++ = valueLen;
551 }
552 *headerLenPtr = headerBuffPtr - startHeaderBuffPtr;
0198fd3c 553}
554\f
555
556#define MAXARGS 16
557static int ParseArgs(int argc, char *argv[],
558 int *doBindPtr, int *doStartPtr,
559 char *connectPathPtr, char *appPathPtr, int *nServersPtr) {
560 int i,
561 x,
562 err = 0,
563 ac;
564 char *tp1,
565 *tp2,
566 *av[MAXARGS];
567 FILE *fp;
568 char line[BUFSIZ];
569
570 *doBindPtr = TRUE;
571 *doStartPtr = TRUE;
572 *connectPathPtr = '\0';
573 *appPathPtr = '\0';
574 *nServersPtr = 0;
575
576 for(i = 0; i < MAXARGS; i++)
577 av[i] = NULL;
578 for(i = 1; i < argc; i++) {
579 if(argv[i][0] == '-') {
580 if(!strcmp(argv[i], "-f")) {
581 if(++i == argc) {
582 fprintf(stderr,
583 "Missing command file name after -f\n");
584 return 1;
585 }
586 if((fp = fopen(argv[i], "r")) == NULL) {
587 fprintf(stderr, "Cannot open command file %s\n", argv[i]);
588 return 1;
589 }
590 ac = 1;
591 while(fgets(line, BUFSIZ, fp)) {
592 if(line[0] == '#') {
593 continue;
594 }
595 if((tp1 = (char *) strrchr(line,'\n')) != NULL) {
596 *tp1-- = 0;
597 while(*tp1 == ' ' || *tp1 =='\t') {
598 *tp1-- = 0;
599 }
600 } else {
601 fprintf(stderr, "Line to long\n");
602 return 1;
603 }
604 tp1 = line;
605 while(tp1) {
606 if((tp2 = strchr(tp1, ' ')) != NULL) {
607 *tp2++ = 0;
608 }
609 if(ac >= MAXARGS) {
610 fprintf(stderr,
611 "To many arguments, "
612 "%d is max from a file\n", MAXARGS);
613 exit(-1);
614 }
3d683188 615 if((av[ac] = (char *)malloc(strlen(tp1)+1)) == NULL) {
0198fd3c 616 fprintf(stderr, "Cannot allocate %d bytes\n",
617 strlen(tp1)+1);
618 exit(-1);
619 }
620 strcpy(av[ac++], tp1);
621 tp1 = tp2;
622 }
623 }
624 err = ParseArgs(ac, av, doBindPtr, doStartPtr,
625 connectPathPtr, appPathPtr, nServersPtr);
626 for(x = 1; x < ac; x++) {
627 ASSERT(av[x] != NULL);
628 free(av[x]);
629 }
630 return err;
631#ifdef _WIN32
632 } else if (!strcmp(argv[i], "-jitcgi")) {
633 DebugBreak();
634 } else if (!strcmp(argv[i], "-dbgfcgi")) {
635 putenv("DEBUG_FCGI=TRUE");
636#endif
637 } else if(!strcmp(argv[i], "-start")) {
638 *doBindPtr = FALSE;
639 } else if(!strcmp(argv[i], "-bind")) {
640 *doStartPtr = FALSE;
641 } else if(!strcmp(argv[i], "-connect")) {
642 if(++i == argc) {
643 fprintf(stderr,
644 "Missing connection name after -connect\n");
645 err++;
646 } else {
647 strcpy(connectPathPtr, argv[i]);
648 }
649 } else {
650 fprintf(stderr, "Unknown option %s\n", argv[i]);
651 err++;
652 }
653 } else if(*appPathPtr == '\0') {
654 strcpy(appPathPtr, argv[i]);
655 } else if(isdigit(argv[i][0]) && *nServersPtr == 0) {
656 *nServersPtr = atoi(argv[i]);
657 if(*nServersPtr <= 0) {
658 fprintf(stderr, "Number of servers must be greater than 0\n");
659 err++;
660 }
661 } else {
662 fprintf(stderr, "Unknown argument %s\n", argv[i]);
663 err++;
664 }
665 }
666 if(*doStartPtr && *appPathPtr == 0) {
667 fprintf(stderr, "Missing application pathname\n");
668 err++;
669 }
670 if(*connectPathPtr == 0) {
671 fprintf(stderr, "Missing -connect <connName>\n");
672 err++;
673 } else if(strchr(connectPathPtr, ':')) {
674/*
675 * XXX: Test to see if we can use IP connect locally...
676 This hack lets me test the ability to create a local process listening
677 to a TCP/IP port for connections and subsequently connect to the app
678 like we do for Unix domain and named pipes.
e3fe7c0c 679
0198fd3c 680 if(*doStartPtr && *doBindPtr) {
681 fprintf(stderr,
682 "<connName> of form hostName:portNumber "
683 "requires -start or -bind\n");
684 err++;
685 }
686 */
687 }
688 if(*nServersPtr == 0) {
689 *nServersPtr = 1;
690 }
691 return err;
692}
693\f
04d12200 694int main(int argc, char **argv)
0198fd3c 695{
04d12200 696 char **envp = environ;
0198fd3c 697 int count;
698 FCGX_Stream *paramsStream;
699 int numFDs;
700 unsigned char headerBuff[8];
701 int headerLen, valueLen;
702 char *equalPtr;
703 FCGI_BeginRequestRecord beginRecord;
704 int doBind, doStart, nServers;
705 char appPath[MAXPATHLEN], bindPath[MAXPATHLEN];
706
707 if(ParseArgs(argc, argv, &doBind, &doStart,
708 (char *) &bindPath, (char *) &appPath, &nServers)) {
709 fprintf(stderr,
710"Usage:\n"
711" cgi-fcgi -f <cmdPath> , or\n"
712" cgi-fcgi -connect <connName> <appPath> [<nServers>] , or\n"
713" cgi-fcgi -start -connect <connName> <appPath> [<nServers>] , or\n"
714" cgi-fcgi -bind -connect <connName> ,\n"
715"where <connName> is either the pathname of a UNIX domain socket\n"
716"or (if -bind is given) a hostName:portNumber specification\n"
717"or (if -start is given) a :portNumber specification (uses local host).\n");
718 exit(1);
719 }
720
721 if(OS_LibInit(stdinFds)) {
722 fprintf(stderr, "Error initializing OS library: %d\n", OS_Errno);
723 exit(0);
724 }
725
726 equalPtr = getenv("CONTENT_LENGTH");
727 if(equalPtr != NULL) {
728 bytesToRead = atoi(equalPtr);
729 } else {
730 bytesToRead = 0;
731 }
e3fe7c0c 732
0198fd3c 733 if(doBind) {
734 appServerSock = OS_FcgiConnect(bindPath);
735 }
736 if(doStart && (!doBind || appServerSock < 0)) {
737 FCGI_Start(bindPath, appPath, nServers);
738 if(!doBind) {
739 exit(0);
740 } else {
741 appServerSock = OS_FcgiConnect(bindPath);
742 }
743 }
744 if(appServerSock < 0) {
745 fprintf(stderr, "Could not connect to %s\n", bindPath);
746 exit(OS_Errno);
747 }
748 /*
749 * Set an arbitrary non-null FCGI RequestId
750 */
751 requestId = 1;
752 /*
753 * XXX: Send FCGI_GET_VALUES
754 */
755
756 /*
757 * XXX: Receive FCGI_GET_VALUES_RESULT
758 */
759
760 /*
761 * Send FCGI_BEGIN_REQUEST (XXX: hack, separate write)
762 */
763 beginRecord.header = MakeHeader(FCGI_BEGIN_REQUEST, requestId,
764 sizeof(beginRecord.body), 0);
765 beginRecord.body = MakeBeginRequestBody(FCGI_RESPONDER, FALSE);
766 count = OS_Write(appServerSock, (char *)&beginRecord, sizeof(beginRecord));
767 if(count != sizeof(beginRecord)) {
768 exit(OS_Errno);
769 }
770 /*
771 * Send environment to the FCGI application server
772 */
773 paramsStream = CreateWriter(appServerSock, requestId, 8192, FCGI_PARAMS);
774 for( ; *envp != NULL; envp++) {
775 equalPtr = strchr(*envp, '=');
776 if(equalPtr == NULL) {
777 exit(1000);
778 }
779 valueLen = strlen(equalPtr + 1);
780 FCGIUtil_BuildNameValueHeader(
781 equalPtr - *envp,
782 valueLen,
783 &headerBuff[0],
784 &headerLen);
785 if(FCGX_PutStr((char *) &headerBuff[0], headerLen, paramsStream) < 0
786 || FCGX_PutStr(*envp, equalPtr - *envp, paramsStream) < 0
787 || FCGX_PutStr(equalPtr + 1, valueLen, paramsStream) < 0) {
788 exit(FCGX_GetError(paramsStream));
789 }
790 }
791 FCGX_FClose(paramsStream);
792 FreeStream(&paramsStream);
793 /*
794 * Perform the event loop until AppServerReadHander sees FCGI_END_REQUEST
795 */
796 fromWS.stop = fromWS.next = &fromWS.buff[0];
797 webServerReadHandlerEOF = FALSE;
798 /*
799 * XXX: might want to use numFDs in the os library.
800 */
801 numFDs = max(appServerSock, STDIN_FILENO) + 1;
802 OS_SetFlags(appServerSock, O_NONBLOCK);
803
804 ScheduleIo();
805 while(!exitStatusSet) {
806 /*
807 * NULL = wait forever (or at least until there's something
808 * to do.
809 */
810 OS_DoIo(NULL);
811 }
812 if(exitStatusSet) {
813 FCGIexit(exitStatus);
814 } else {
815 FCGIexit(999);
816 }
283822e9 817
04d12200 818 return 0;
0198fd3c 819}