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.11 2001/06/20 00:56:51 robs Exp $";
18 #include "fcgi_config.h"
21 #define DLLAPI __declspec(dllexport)
22 #define WIN32_LEAN_AND_MEAN
26 #include <errno.h> /* for errno */
27 #include <stdarg.h> /* for va_arg */
28 #include <stdlib.h> /* for malloc */
29 #include <string.h> /* for strerror */
35 #define NO_FCGI_DEFINES
36 #include "fcgi_stdio.h"
37 #undef NO_FCGI_DEFINES
44 extern char **environ;
46 /* These definitions should be supplied by stdio.h but for some
47 * reason they get lost on certain platforms. */
49 extern int fileno(FILE *stream);
52 extern FILE *fdopen(int fildes, const char *type);
53 extern FILE *popen(const char *command, const char *type);
54 extern int pclose(FILE *stream);
59 #define pclose _pclose
71 FCGI_FILE _fcgi_sF[3];
75 *----------------------------------------------------------------------
79 * Accepts a new request from the HTTP server and creates
80 * a conventional execution environment for the request.
82 * If the application was invoked as a FastCGI server,
83 * the first call to FCGI_Accept indicates that the application
84 * has completed its initialization and is ready to accept
85 * a request. Subsequent calls to FCGI_Accept indicate that
86 * the application has completed its processing of the
87 * current request and is ready to accept a new request.
89 * If the application was invoked as a CGI program, the first
90 * call to FCGI_Accept is essentially a no-op and the second
91 * call returns EOF (-1).
94 * 0 for successful call, -1 for error (application should exit).
97 * If the application was invoked as a FastCGI server,
98 * and this is not the first call to this procedure,
99 * FCGI_Accept first performs the equivalent of FCGI_Finish.
101 * On every call, FCGI_Accept accepts the new request and
102 * reads the FCGI_PARAMS stream into an environment array,
103 * i.e. a NULL-terminated array of strings of the form
104 * ``name=value''. It assigns a pointer to this array
105 * to the global variable environ, used by the standard
106 * library function getenv. It creates new FCGI_FILE *s
107 * representing input from the HTTP server, output to the HTTP
108 * server, and error output to the HTTP server, and assigns these
109 * new files to stdin, stdout, and stderr respectively.
111 * DO NOT mutate or retain pointers to environ or any values
112 * contained in it (e.g. to the result of calling getenv(3)),
113 * since these are freed by the next call to FCGI_Finish or
114 * FCGI_Accept. In particular do not use setenv(3) or putenv(3)
115 * in conjunction with FCGI_Accept.
117 *----------------------------------------------------------------------
119 static int acceptCalled = FALSE;
120 static int isCGI = FALSE;
122 int FCGI_Accept(void)
126 * First call to FCGI_Accept. Is application running
127 * as FastCGI or as CGI?
129 isCGI = FCGX_IsCGI();
131 atexit(&FCGI_Finish);
134 * Not first call to FCGI_Accept and running as CGI means
135 * application is done.
140 FCGI_stdin->stdio_stream = stdin;
141 FCGI_stdin->fcgx_stream = NULL;
142 FCGI_stdout->stdio_stream = stdout;
143 FCGI_stdout->fcgx_stream = NULL;
144 FCGI_stderr->stdio_stream = stderr;
145 FCGI_stderr->fcgx_stream = NULL;
147 FCGX_Stream *in, *out, *error;
148 FCGX_ParamArray envp;
149 int acceptResult = FCGX_Accept(&in, &out, &error, &envp);
150 if(acceptResult < 0) {
153 FCGI_stdin->stdio_stream = NULL;
154 FCGI_stdin->fcgx_stream = in;
155 FCGI_stdout->stdio_stream = NULL;
156 FCGI_stdout->fcgx_stream = out;
157 FCGI_stderr->stdio_stream = NULL;
158 FCGI_stderr->fcgx_stream = error;
165 *----------------------------------------------------------------------
169 * Finishes the current request from the HTTP server.
173 * Flushes any buffered output to the HTTP server. Then frees
174 * all storage allocated by the previous call, including all
175 * storage reachable from the value of environ set by the previous
176 * call to FCGI_Accept.
178 * DO NOT use stdin, stdout, stderr, or environ between calling
179 * FCGI_Finish and calling FCGI_Accept.
181 * DO NOT mutate or retain pointers to environ or any values
182 * contained in it (e.g. to the result of calling getenv(3)),
183 * since these are freed by the next call to FCGI_Finish or
184 * FCGI_Accept. In particular do not use setenv(3) or putenv(3)
185 * in conjunction with FCGI_Accept.
187 *----------------------------------------------------------------------
189 void FCGI_Finish(void)
191 if(!acceptCalled || isCGI) {
195 FCGI_stdin->fcgx_stream = NULL;
196 FCGI_stdout->fcgx_stream = NULL;
197 FCGI_stderr->fcgx_stream = NULL;
202 *----------------------------------------------------------------------
204 * FCGI_StartFilterData --
207 * The current request is for the filter role, and stdin is
208 * positioned at EOF of FCGI_STDIN. The call repositions
209 * stdin to the start of FCGI_DATA.
210 * If the preconditions are not met (e.g. FCGI_STDIN has not
211 * been read to EOF), the call sets the stream error code to
212 * FCGX_CALL_SEQ_ERROR.
215 * 0 for a normal return, < 0 for error
217 *----------------------------------------------------------------------
219 int FCGI_StartFilterData(void)
221 if(FCGI_stdin->stdio_stream) {
224 return FCGX_StartFilterData(FCGI_stdin->fcgx_stream);
229 *----------------------------------------------------------------------
231 * FCGI_SetExitStatus --
233 * Sets the exit status for the current request. The exit status
234 * is the status code the request would have exited with, had
235 * the request been run as a CGI program. You can call
236 * FCGI_SetExitStatus several times during a request; the last call
237 * before the request ends (by calling FCGI_Accept) determines the
240 *----------------------------------------------------------------------
242 void FCGI_SetExitStatus(int status)
244 if(FCGI_stdin->fcgx_stream) {
245 FCGX_SetExitStatus(status, FCGI_stdin->fcgx_stream);
250 *----------------------------------------------------------------------
254 * Wrapper for function defined in H&S Section 11.2
256 *----------------------------------------------------------------------
258 void FCGI_perror(const char *str)
260 FCGI_fputs(str, FCGI_stderr);
261 FCGI_fputs(": ", FCGI_stderr);
262 FCGI_fputs(strerror(OS_Errno), FCGI_stderr);
267 *----------------------------------------------------------------------
269 * FCGI_OpenFromFILE --
271 * Constructs a new FCGI_FILE * from the FILE *stream.
274 * NULL if stream == NULL or storage could not be allocated,
275 * otherwise the new FCGI_FILE *.
277 *----------------------------------------------------------------------
279 static FCGI_FILE *FCGI_OpenFromFILE(FILE *stream)
286 fp = (FCGI_FILE *) malloc(sizeof(FCGI_FILE));
289 fp->stdio_stream = stream;
290 fp->fcgx_stream = NULL;
297 *----------------------------------------------------------------------
299 * FCGI_fopen, FCGI_fclose, FCGI_fflush, FCGI_freopen --
301 * Wrappers for functions defined in H&S Section 15.2
303 *----------------------------------------------------------------------
305 FCGI_FILE *FCGI_fopen(const char *path, const char *mode)
307 FILE * file = fopen(path, mode);
308 FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
310 if (file && !fcgi_file)
316 int FCGI_fclose(FCGI_FILE *fp)
319 if(fp->stdio_stream) {
320 n = fclose(fp->stdio_stream);
321 fp->stdio_stream = NULL;
322 } else if(fp->fcgx_stream) {
323 n = FCGX_FClose(fp->fcgx_stream);
324 fp->fcgx_stream = NULL;
326 if((fp != FCGI_stdin) && (fp != FCGI_stdout) && (fp != FCGI_stderr)) {
332 int FCGI_fflush(FCGI_FILE *fp)
337 return fflush(fp->stdio_stream);
338 else if(fp->fcgx_stream)
339 return FCGX_FFlush(fp->fcgx_stream);
343 FCGI_FILE *FCGI_freopen(const char *path, const char *mode,
346 if(fp->stdio_stream) {
347 if(freopen(path, mode, fp->stdio_stream) == NULL)
351 } else if(fp->fcgx_stream) {
352 (void) FCGX_FClose(fp->fcgx_stream);
353 fp->stdio_stream = fopen(path, mode);
354 if(fp->stdio_stream == NULL)
357 fp->fcgx_stream = NULL;
365 *----------------------------------------------------------------------
367 * FCGI_setvbuf, FCGI_setbuf --
369 * Wrappers for functions defined in H&S Section 15.3
371 *----------------------------------------------------------------------
373 int FCGI_setvbuf(FCGI_FILE *fp, char *buf, int bufmode, size_t size)
376 return setvbuf(fp->stdio_stream, buf, bufmode, size);
382 void FCGI_setbuf(FCGI_FILE *fp, char *buf)
385 setbuf(fp->stdio_stream, buf);
389 *----------------------------------------------------------------------
391 * FCGI_fseek, FCGI_ftell, FCGI_rewind, FCGI_fgetpos, FCGI_fsetpos --
393 * Wrappers for functions defined in H&S Section 15.5
395 *----------------------------------------------------------------------
397 int FCGI_fseek(FCGI_FILE *fp, long offset, int whence)
400 return fseek(fp->stdio_stream, offset, whence);
407 int FCGI_ftell(FCGI_FILE *fp)
410 return ftell(fp->stdio_stream);
417 void FCGI_rewind(FCGI_FILE *fp)
420 rewind(fp->stdio_stream);
426 int FCGI_fgetpos(FCGI_FILE *fp, fpos_t *pos)
429 return fgetpos(fp->stdio_stream, pos);
436 int FCGI_fsetpos(FCGI_FILE *fp, const fpos_t *pos)
439 return fsetpos(fp->stdio_stream, pos);
448 *----------------------------------------------------------------------
450 * FCGI_fgetc, FCGI_getchar, FCGI_ungetc --
452 * Wrappers for functions defined in H&S Section 15.6
454 * XXX: getc and getchar are generally defined as macros
455 * for performance reasons
457 *----------------------------------------------------------------------
459 int FCGI_fgetc(FCGI_FILE *fp)
462 return fgetc(fp->stdio_stream);
463 else if(fp->fcgx_stream)
464 return FCGX_GetChar(fp->fcgx_stream);
468 int FCGI_getchar(void)
470 return FCGI_fgetc(FCGI_stdin);
473 int FCGI_ungetc(int c, FCGI_FILE *fp)
476 return ungetc(c, fp->stdio_stream);
477 else if(fp->fcgx_stream)
478 return FCGX_UnGetChar(c, fp->fcgx_stream);
483 *----------------------------------------------------------------------
485 * FCGI_fgets, FCGI_gets --
487 * Wrappers for functions defined in H&S Section 15.7
489 *----------------------------------------------------------------------
491 char *FCGI_fgets(char *str, int size, FCGI_FILE *fp)
494 return fgets(str, size, fp->stdio_stream);
495 else if(fp->fcgx_stream)
496 return FCGX_GetLine(str, size, fp->fcgx_stream);
501 * There is no standard equivalent of gets that takes an explicit
502 * FILE * argument, so the following "wrapper" implements
503 * gets for both FILE * and FCGX_Stream * in terms of FCGI_getchar.
505 char *FCGI_gets(char *str)
509 for (s = str; ((c = FCGI_getchar()) != '\n');) {
523 *----------------------------------------------------------------------
525 * Wrappers for functions defined in H&S Section 15.8
527 * XXX: missing: fscanf, scanf
529 *----------------------------------------------------------------------
533 *----------------------------------------------------------------------
535 * FCGI_fputc, FCGI_putchar --
537 * Wrappers for functions defined in H&S Section 15.9
539 * XXX: putc and putchar are generally defined as macros
540 * for performance reasons
542 *----------------------------------------------------------------------
544 int FCGI_fputc(int c, FCGI_FILE *fp)
547 return fputc(c, fp->stdio_stream);
548 else if(fp->fcgx_stream)
549 return FCGX_PutChar(c, fp->fcgx_stream);
553 int FCGI_putchar(int c)
555 return FCGI_fputc(c, FCGI_stdout);
559 *----------------------------------------------------------------------
561 * FCGI_fputs, FCGI_puts
563 * Wrappers for functions defined in H&S Section 15.10
565 *----------------------------------------------------------------------
567 int FCGI_fputs(const char *str, FCGI_FILE *fp)
570 return fputs(str, fp->stdio_stream);
571 else if(fp->fcgx_stream)
572 return FCGX_PutS(str, fp->fcgx_stream);
576 int FCGI_puts(const char *str)
579 if(FCGI_stdout->stdio_stream) {
580 n = fputs(str, FCGI_stdout->stdio_stream);
584 return fputc('\n', FCGI_stdout->stdio_stream);
585 } else if(FCGI_stdout->fcgx_stream) {
586 n = FCGX_PutS(str, FCGI_stdout->fcgx_stream);
590 return FCGX_PutChar('\n', FCGI_stdout->fcgx_stream);
596 *----------------------------------------------------------------------
598 * FCGI_fprintf, FCGI_printf --
600 * Wrappers for functions defined in H&S Section 15.11
602 *----------------------------------------------------------------------
604 int FCGI_fprintf(FCGI_FILE *fp, const char *format, ...)
608 va_start(ap, format);
610 n = vfprintf(fp->stdio_stream, format, ap);
611 else if(fp->fcgx_stream)
612 n = FCGX_VFPrintF(fp->fcgx_stream, format, ap);
617 int FCGI_printf(const char *format, ...)
621 va_start(ap, format);
622 n = FCGI_vfprintf(FCGI_stdout, format, ap);
628 *----------------------------------------------------------------------
630 * FCGI_vfprintf, FCGI_vprintf --
632 * Wrappers for functions defined in H&S Section 15.12
634 *----------------------------------------------------------------------
636 int FCGI_vfprintf(FCGI_FILE *fp, const char *format, va_list ap)
639 return vfprintf(fp->stdio_stream, format, ap);
640 else if(fp->fcgx_stream)
641 return FCGX_VFPrintF(fp->fcgx_stream, format, ap);
645 int FCGI_vprintf(const char *format, va_list ap)
647 if(FCGI_stdout->stdio_stream)
648 return vfprintf(FCGI_stdout->stdio_stream, format, ap);
649 else if(FCGI_stdout->fcgx_stream)
650 return FCGX_VFPrintF(FCGI_stdout->fcgx_stream, format, ap);
655 *----------------------------------------------------------------------
657 * FCGI_fread, FCGI_fwrite --
659 * Wrappers for functions defined in H&S Section 15.13
661 *----------------------------------------------------------------------
663 size_t FCGI_fread(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
667 return fread(ptr, size, nmemb, fp->stdio_stream);
668 else if(fp->fcgx_stream) {
669 if((size * nmemb) == 0) {
672 n = FCGX_GetStr((char *) ptr, size * nmemb, fp->fcgx_stream);
678 size_t FCGI_fwrite(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
682 return fwrite(ptr, size, nmemb, fp->stdio_stream);
683 else if(fp->fcgx_stream) {
684 if((size * nmemb) == 0) {
687 n = FCGX_PutStr((char *) ptr, size * nmemb, fp->fcgx_stream);
694 *----------------------------------------------------------------------
696 * FCGI_feof, FCGI_ferror, FCGI_clearerr --
698 * Wrappers for functions defined in H&S Section 15.14
700 *----------------------------------------------------------------------
702 int FCGI_feof(FCGI_FILE *fp)
704 if(fp->stdio_stream) {
705 return feof(fp->stdio_stream);
706 } else if (fp->fcgx_stream){
707 return FCGX_HasSeenEOF(fp->fcgx_stream);
713 int FCGI_ferror(FCGI_FILE *fp)
715 if(fp->stdio_stream) {
716 return ferror(fp->stdio_stream);
717 } else if(fp->fcgx_stream) {
718 return FCGX_GetError(fp->fcgx_stream);
723 void FCGI_clearerr(FCGI_FILE *fp)
725 if(fp->stdio_stream) {
726 clearerr(fp->stdio_stream);
727 } else if(fp->fcgx_stream) {
728 FCGX_ClearError(fp->fcgx_stream);
734 *----------------------------------------------------------------------
738 * Wrappers for function defined in H&S Section 15.16
740 *----------------------------------------------------------------------
742 FCGI_FILE *FCGI_tmpfile(void)
744 FILE * file = tmpfile();
745 FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
747 if (file && !fcgi_file)
754 *----------------------------------------------------------------------
756 * FCGI_fileno, FCGI_fdopen, FCGI_popen, FCGI_pclose --
758 * Wrappers for POSIX, X/OPEN functions not in ISO C
760 *----------------------------------------------------------------------
762 int FCGI_fileno(FCGI_FILE *fp)
765 return fileno(fp->stdio_stream);
770 FCGI_FILE *FCGI_fdopen(int fd, const char *mode)
772 FILE * file = fdopen(fd, mode);
773 FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
775 if (file && !fcgi_file)
781 FCGI_FILE *FCGI_popen(const char *cmd, const char *type)
783 FILE * file = popen(cmd, type);
784 FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
786 if (file && !fcgi_file)
792 int FCGI_pclose(FCGI_FILE *fp)
795 if (fp->stdio_stream) {
796 n = pclose(fp->stdio_stream);
797 fp->stdio_stream = NULL;
798 } else if(fp->fcgx_stream) {
800 * The caller is deeply confused; don't free the storage.
804 if((fp != FCGI_stdin) && (fp != FCGI_stdout) && (fp != FCGI_stderr)) {