4 * FastCGI-stdio compatibility package
7 * Copyright (c) 1996 Open Market, Inc.
9 * See the file "LICENSE.TERMS" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 static const char rcsid[] = "$Id: fcgi_stdio.c,v 1.1 1997/09/16 15:36:33 stanleyg Exp $";
19 #define DLLAPI __declspec(dllexport)
22 #define NO_FCGI_DEFINES
23 #include "fcgi_stdio.h"
24 #undef NO_FCGI_DEFINES
52 FCGI_FILE _fcgi_sF[3];
54 FCGI_FILE _fcgi_sF[3] = {{stdin, NULL}, {stdout, NULL}, {stderr, NULL}};
62 *----------------------------------------------------------------------
66 * Accepts a new request from the HTTP server and creates
67 * a conventional execution environment for the request.
69 * If the application was invoked as a FastCGI server,
70 * the first call to FCGI_Accept indicates that the application
71 * has completed its initialization and is ready to accept
72 * a request. Subsequent calls to FCGI_Accept indicate that
73 * the application has completed its processing of the
74 * current request and is ready to accept a new request.
76 * If the application was invoked as a CGI program, the first
77 * call to FCGI_Accept is essentially a no-op and the second
78 * call returns EOF (-1).
81 * 0 for successful call, -1 for error (application should exit).
84 * If the application was invoked as a FastCGI server,
85 * and this is not the first call to this procedure,
86 * FCGI_Accept first performs the equivalent of FCGI_Finish.
88 * On every call, FCGI_Accept accepts the new request and
89 * reads the FCGI_PARAMS stream into an environment array,
90 * i.e. a NULL-terminated array of strings of the form
91 * ``name=value''. It assigns a pointer to this array
92 * to the global variable environ, used by the standard
93 * library function getenv. It creates new FCGI_FILE *s
94 * representing input from the HTTP server, output to the HTTP
95 * server, and error output to the HTTP server, and assigns these
96 * new files to stdin, stdout, and stderr respectively.
98 * DO NOT mutate or retain pointers to environ or any values
99 * contained in it (e.g. to the result of calling getenv(3)),
100 * since these are freed by the next call to FCGI_Finish or
101 * FCGI_Accept. In particular do not use setenv(3) or putenv(3)
102 * in conjunction with FCGI_Accept.
104 *----------------------------------------------------------------------
106 static int acceptCalled = FALSE;
107 static int isCGI = FALSE;
109 extern char **environ;
112 int FCGI_Accept(void)
116 * First call to FCGI_Accept. Is application running
117 * as FastCGI or as CGI?
119 isCGI = FCGX_IsCGI();
123 * Not first call to FCGI_Accept and running as CGI means
124 * application is done.
129 FCGI_stdin->stdio_stream = stdin;
130 FCGI_stdin->fcgx_stream = NULL;
131 FCGI_stdout->stdio_stream = stdout;
132 FCGI_stdout->fcgx_stream = NULL;
133 FCGI_stderr->stdio_stream = stderr;
134 FCGI_stderr->fcgx_stream = NULL;
136 FCGX_Stream *in, *out, *error;
137 FCGX_ParamArray envp;
138 int acceptResult = FCGX_Accept(&in, &out, &error, &envp);
139 if(acceptResult < 0) {
142 FCGI_stdin->stdio_stream = NULL;
143 FCGI_stdin->fcgx_stream = in;
144 FCGI_stdout->stdio_stream = NULL;
145 FCGI_stdout->fcgx_stream = out;
146 FCGI_stderr->stdio_stream = NULL;
147 FCGI_stderr->fcgx_stream = error;
154 *----------------------------------------------------------------------
158 * Finishes the current request from the HTTP server.
162 * Flushes any buffered output to the HTTP server. Then frees
163 * all storage allocated by the previous call, including all
164 * storage reachable from the value of environ set by the previous
165 * call to FCGI_Accept.
167 * DO NOT use stdin, stdout, stderr, or environ between calling
168 * FCGI_Finish and calling FCGI_Accept.
170 * DO NOT mutate or retain pointers to environ or any values
171 * contained in it (e.g. to the result of calling getenv(3)),
172 * since these are freed by the next call to FCGI_Finish or
173 * FCGI_Accept. In particular do not use setenv(3) or putenv(3)
174 * in conjunction with FCGI_Accept.
176 *----------------------------------------------------------------------
178 void FCGI_Finish(void)
180 if(!acceptCalled || isCGI) {
184 FCGI_stdin->fcgx_stream = NULL;
185 FCGI_stdout->fcgx_stream = NULL;
186 FCGI_stderr->fcgx_stream = NULL;
191 *----------------------------------------------------------------------
193 * FCGI_StartFilterData --
196 * The current request is for the filter role, and stdin is
197 * positioned at EOF of FCGI_STDIN. The call repositions
198 * stdin to the start of FCGI_DATA.
199 * If the preconditions are not met (e.g. FCGI_STDIN has not
200 * been read to EOF), the call sets the stream error code to
201 * FCGX_CALL_SEQ_ERROR.
204 * 0 for a normal return, < 0 for error
206 *----------------------------------------------------------------------
209 int FCGI_StartFilterData(void)
211 if(FCGI_stdin->stdio_stream) {
214 return FCGX_StartFilterData(FCGI_stdin->fcgx_stream);
219 *----------------------------------------------------------------------
221 * FCGI_SetExitStatus --
223 * Sets the exit status for the current request. The exit status
224 * is the status code the request would have exited with, had
225 * the request been run as a CGI program. You can call
226 * FCGI_SetExitStatus several times during a request; the last call
227 * before the request ends (by calling FCGI_Accept) determines the
230 *----------------------------------------------------------------------
233 void FCGI_SetExitStatus(int status)
235 if(FCGI_stdin->fcgx_stream) {
236 FCGX_SetExitStatus(status, FCGI_stdin->fcgx_stream);
241 *----------------------------------------------------------------------
245 * Wrapper for function defined in H&S Section 11.2
247 *----------------------------------------------------------------------
250 void FCGI_perror(const char *str)
252 FCGI_fputs(str, FCGI_stderr);
253 FCGI_fputs(": ", FCGI_stderr);
254 FCGI_fputs(strerror(OS_Errno), FCGI_stderr);
259 *----------------------------------------------------------------------
261 * FCGI_OpenFromFILE --
263 * Constructs a new FCGI_FILE * from the FILE *stream.
266 * NULL if stream == NULL or storage could not be allocated,
267 * otherwise the new FCGI_FILE *.
269 *----------------------------------------------------------------------
272 static FCGI_FILE *FCGI_OpenFromFILE(FILE *stream)
277 fp = malloc(sizeof(FCGI_FILE));
280 fp->stdio_stream = stream;
281 fp->fcgx_stream = NULL;
286 *----------------------------------------------------------------------
288 * FCGI_fopen, FCGI_fclose, FCGI_fflush, FCGI_freopen --
290 * Wrappers for functions defined in H&S Section 15.2
292 *----------------------------------------------------------------------
295 FCGI_FILE *FCGI_fopen(const char *path, const char *mode)
297 return FCGI_OpenFromFILE(fopen(path, mode));
300 int FCGI_fclose(FCGI_FILE *fp)
303 if(fp->stdio_stream) {
304 n = fclose(fp->stdio_stream);
305 fp->stdio_stream = NULL;
306 } else if(fp->fcgx_stream) {
307 n = FCGX_FClose(fp->fcgx_stream);
308 fp->fcgx_stream = NULL;
310 if((fp != FCGI_stdin) && (fp != FCGI_stdout) && (fp != FCGI_stderr)) {
316 int FCGI_fflush(FCGI_FILE *fp)
319 return fflush(fp->stdio_stream);
320 else if(fp->fcgx_stream)
321 return FCGX_FFlush(fp->fcgx_stream);
325 FCGI_FILE *FCGI_freopen(const char *path, const char *mode,
328 if(fp->stdio_stream) {
329 if(freopen(path, mode, fp->stdio_stream) == NULL)
333 } else if(fp->fcgx_stream) {
334 (void) FCGX_FClose(fp->fcgx_stream);
335 fp->stdio_stream = fopen(path, mode);
336 if(fp->stdio_stream == NULL)
339 fp->fcgx_stream = NULL;
347 *----------------------------------------------------------------------
349 * FCGI_setvbuf, FCGI_setbuf --
351 * Wrappers for functions defined in H&S Section 15.3
353 *----------------------------------------------------------------------
355 int FCGI_setvbuf(FCGI_FILE *fp, char *buf, int bufmode, size_t size)
358 return setvbuf(fp->stdio_stream, buf, bufmode, size);
364 void FCGI_setbuf(FCGI_FILE *fp, char *buf)
367 setbuf(fp->stdio_stream, buf);
371 *----------------------------------------------------------------------
373 * FCGI_fseek, FCGI_ftell, FCGI_rewind, FCGI_fgetpos, FCGI_fsetpos --
375 * Wrappers for functions defined in H&S Section 15.5
377 *----------------------------------------------------------------------
380 int FCGI_fseek(FCGI_FILE *fp, long offset, int whence)
383 return fseek(fp->stdio_stream, offset, whence);
390 int FCGI_ftell(FCGI_FILE *fp)
393 return ftell(fp->stdio_stream);
400 void FCGI_rewind(FCGI_FILE *fp)
403 rewind(fp->stdio_stream);
409 int FCGI_fgetpos(FCGI_FILE *fp, fpos_t *pos)
412 return fgetpos(fp->stdio_stream, pos);
419 int FCGI_fsetpos(FCGI_FILE *fp, const fpos_t *pos)
422 return fsetpos(fp->stdio_stream, pos);
431 *----------------------------------------------------------------------
433 * FCGI_fgetc, FCGI_getchar, FCGI_ungetc --
435 * Wrappers for functions defined in H&S Section 15.6
437 * XXX: getc and getchar are generally defined as macros
438 * for performance reasons
440 *----------------------------------------------------------------------
443 int FCGI_fgetc(FCGI_FILE *fp)
446 return fgetc(fp->stdio_stream);
447 else if(fp->fcgx_stream)
448 return FCGX_GetChar(fp->fcgx_stream);
452 int FCGI_getchar(void)
454 return FCGI_fgetc(FCGI_stdin);
457 int FCGI_ungetc(int c, FCGI_FILE *fp)
460 return ungetc(c, fp->stdio_stream);
461 else if(fp->fcgx_stream)
462 return FCGX_UnGetChar(c, fp->fcgx_stream);
467 *----------------------------------------------------------------------
469 * FCGI_fgets, FCGI_gets --
471 * Wrappers for functions defined in H&S Section 15.7
473 *----------------------------------------------------------------------
476 char *FCGI_fgets(char *str, int size, FCGI_FILE *fp)
479 return fgets(str, size, fp->stdio_stream);
480 else if(fp->fcgx_stream)
481 return FCGX_GetLine(str, size, fp->fcgx_stream);
486 * There is no standard equivalent of gets that takes an explicit
487 * FILE * argument, so the following "wrapper" implements
488 * gets for both FILE * and FCGX_Stream * in terms of FCGI_getchar.
491 char *FCGI_gets(char *str)
495 for (s = str; ((c = FCGI_getchar()) != '\n');) {
509 *----------------------------------------------------------------------
513 * Wrappers for functions defined in H&S Section 15.8
515 * XXX: missing: fscanf, scanf
517 *----------------------------------------------------------------------
522 *----------------------------------------------------------------------
524 * FCGI_fputc, FCGI_putchar --
526 * Wrappers for functions defined in H&S Section 15.9
528 * XXX: putc and putchar are generally defined as macros
529 * for performance reasons
531 *----------------------------------------------------------------------
534 int FCGI_fputc(int c, FCGI_FILE *fp)
537 return fputc(c, fp->stdio_stream);
538 else if(fp->fcgx_stream)
539 return FCGX_PutChar(c, fp->fcgx_stream);
543 int FCGI_putchar(int c)
545 return FCGI_fputc(c, FCGI_stdout);
549 *----------------------------------------------------------------------
551 * FCGI_fputs, FCGI_puts
553 * Wrappers for functions defined in H&S Section 15.10
555 *----------------------------------------------------------------------
558 int FCGI_fputs(const char *str, FCGI_FILE *fp)
561 return fputs(str, fp->stdio_stream);
562 else if(fp->fcgx_stream)
563 return FCGX_PutS(str, fp->fcgx_stream);
567 int FCGI_puts(const char *str)
570 if(FCGI_stdout->stdio_stream) {
571 n = fputs(str, FCGI_stdout->stdio_stream);
575 return fputc('\n', FCGI_stdout->stdio_stream);
576 } else if(FCGI_stdout->fcgx_stream) {
577 n = FCGX_PutS(str, FCGI_stdout->fcgx_stream);
581 return FCGX_PutChar('\n', FCGI_stdout->fcgx_stream);
587 *----------------------------------------------------------------------
589 * FCGI_fprintf, FCGI_printf --
591 * Wrappers for functions defined in H&S Section 15.11
593 *----------------------------------------------------------------------
596 int FCGI_fprintf(FCGI_FILE *fp, const char *format, ...)
600 va_start(ap, format);
602 n = vfprintf(fp->stdio_stream, format, ap);
603 else if(fp->fcgx_stream)
604 n = FCGX_VFPrintF(fp->fcgx_stream, format, ap);
609 int FCGI_printf(const char *format, ...)
613 va_start(ap, format);
614 n = FCGI_vfprintf(FCGI_stdout, format, ap);
620 *----------------------------------------------------------------------
622 * FCGI_vfprintf, FCGI_vprintf --
624 * Wrappers for functions defined in H&S Section 15.12
626 *----------------------------------------------------------------------
629 int FCGI_vfprintf(FCGI_FILE *fp, const char *format, va_list ap)
632 return vfprintf(fp->stdio_stream, format, ap);
633 else if(fp->fcgx_stream)
634 return FCGX_VFPrintF(fp->fcgx_stream, format, ap);
638 int FCGI_vprintf(const char *format, va_list ap)
640 if(FCGI_stdout->stdio_stream)
641 return vfprintf(FCGI_stdout->stdio_stream, format, ap);
642 else if(FCGI_stdout->fcgx_stream)
643 return FCGX_VFPrintF(FCGI_stdout->fcgx_stream, format, ap);
648 *----------------------------------------------------------------------
650 * FCGI_fread, FCGI_fwrite --
652 * Wrappers for functions defined in H&S Section 15.13
654 *----------------------------------------------------------------------
657 size_t FCGI_fread(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
661 return fread(ptr, size, nmemb, fp->stdio_stream);
662 else if(fp->fcgx_stream) {
663 if((size * nmemb) == 0) {
666 n = FCGX_GetStr((char *) ptr, size * nmemb, fp->fcgx_stream);
672 size_t FCGI_fwrite(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
676 return fwrite(ptr, size, nmemb, fp->stdio_stream);
677 else if(fp->fcgx_stream) {
678 if((size * nmemb) == 0) {
681 n = FCGX_PutStr((char *) ptr, size * nmemb, fp->fcgx_stream);
688 *----------------------------------------------------------------------
690 * FCGI_feof, FCGI_ferror, FCGI_clearerr --
692 * Wrappers for functions defined in H&S Section 15.14
694 *----------------------------------------------------------------------
696 int FCGI_feof(FCGI_FILE *fp)
698 if(fp->stdio_stream) {
699 return feof(fp->stdio_stream);
700 } else if (fp->fcgx_stream){
701 return FCGX_HasSeenEOF(fp->fcgx_stream);
707 int FCGI_ferror(FCGI_FILE *fp)
709 if(fp->stdio_stream) {
710 return ferror(fp->stdio_stream);
711 } else if(fp->fcgx_stream) {
712 return FCGX_GetError(fp->fcgx_stream);
717 void FCGI_clearerr(FCGI_FILE *fp)
719 if(fp->stdio_stream) {
720 clearerr(fp->stdio_stream);
721 } else if(fp->fcgx_stream) {
722 FCGX_ClearError(fp->fcgx_stream);
728 *----------------------------------------------------------------------
730 * FCGI_fileno, FCGI_fdopen, FCGI_popen, FCGI_pclose --
732 * Wrappers for POSIX, X/OPEN functions not in ISO C
734 *----------------------------------------------------------------------
738 * These definitions should be supplied by stdio.h but for some
739 * reason they get lost on certain platforms.
742 * XXX: Need to find the right way to handle this for NT
746 extern int fileno(FILE *stream);
748 extern FILE *fdopen(int fildes, const char *type);
749 extern FILE *popen(const char *command, const char *type);
750 extern int pclose(FILE *stream);
753 int FCGI_fileno(FCGI_FILE *fp)
756 return fileno(fp->stdio_stream);
761 FCGI_FILE *FCGI_fdopen(int fd, const char *mode)
764 return FCGI_OpenFromFILE(fdopen(fd, mode));
770 FCGI_FILE *FCGI_popen(const char *cmd, const char *type)
772 return FCGI_OpenFromFILE(popen(cmd, type));
775 int FCGI_pclose(FCGI_FILE *fp)
778 if(fp->stdio_stream) {
780 n = pclose(fp->stdio_stream);
782 fp->stdio_stream = NULL;
783 } else if(fp->fcgx_stream) {
785 * The caller is deeply confused; don't free the storage.
789 if((fp != FCGI_stdin) && (fp != FCGI_stdout) && (fp != FCGI_stderr)) {
796 *----------------------------------------------------------------------