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.12 2001/06/20 16:04:17 robs Exp $";
18 #include <errno.h> /* for errno */
19 #include <stdarg.h> /* for va_arg */
20 #include <stdlib.h> /* for malloc */
21 #include <string.h> /* for strerror */
23 #include "fcgi_config.h"
29 #define DLLAPI __declspec(dllexport)
31 #define NO_FCGI_DEFINES
32 #include "fcgi_stdio.h"
33 #undef NO_FCGI_DEFINES
41 extern char **environ;
43 /* These definitions should be supplied by stdio.h but for some
44 * reason they get lost on certain platforms. */
46 extern int fileno(FILE *stream);
49 extern FILE *fdopen(int fildes, const char *type);
50 extern FILE *popen(const char *command, const char *type);
51 extern int pclose(FILE *stream);
56 #define pclose _pclose
60 FCGI_FILE _fcgi_sF[3];
64 *----------------------------------------------------------------------
68 * Accepts a new request from the HTTP server and creates
69 * a conventional execution environment for the request.
71 * If the application was invoked as a FastCGI server,
72 * the first call to FCGI_Accept indicates that the application
73 * has completed its initialization and is ready to accept
74 * a request. Subsequent calls to FCGI_Accept indicate that
75 * the application has completed its processing of the
76 * current request and is ready to accept a new request.
78 * If the application was invoked as a CGI program, the first
79 * call to FCGI_Accept is essentially a no-op and the second
80 * call returns EOF (-1).
83 * 0 for successful call, -1 for error (application should exit).
86 * If the application was invoked as a FastCGI server,
87 * and this is not the first call to this procedure,
88 * FCGI_Accept first performs the equivalent of FCGI_Finish.
90 * On every call, FCGI_Accept accepts the new request and
91 * reads the FCGI_PARAMS stream into an environment array,
92 * i.e. a NULL-terminated array of strings of the form
93 * ``name=value''. It assigns a pointer to this array
94 * to the global variable environ, used by the standard
95 * library function getenv. It creates new FCGI_FILE *s
96 * representing input from the HTTP server, output to the HTTP
97 * server, and error output to the HTTP server, and assigns these
98 * new files to stdin, stdout, and stderr respectively.
100 * DO NOT mutate or retain pointers to environ or any values
101 * contained in it (e.g. to the result of calling getenv(3)),
102 * since these are freed by the next call to FCGI_Finish or
103 * FCGI_Accept. In particular do not use setenv(3) or putenv(3)
104 * in conjunction with FCGI_Accept.
106 *----------------------------------------------------------------------
108 static int acceptCalled = FALSE;
109 static int isCGI = FALSE;
111 int FCGI_Accept(void)
115 * First call to FCGI_Accept. Is application running
116 * as FastCGI or as CGI?
118 isCGI = FCGX_IsCGI();
120 atexit(&FCGI_Finish);
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 *----------------------------------------------------------------------
208 int FCGI_StartFilterData(void)
210 if(FCGI_stdin->stdio_stream) {
213 return FCGX_StartFilterData(FCGI_stdin->fcgx_stream);
218 *----------------------------------------------------------------------
220 * FCGI_SetExitStatus --
222 * Sets the exit status for the current request. The exit status
223 * is the status code the request would have exited with, had
224 * the request been run as a CGI program. You can call
225 * FCGI_SetExitStatus several times during a request; the last call
226 * before the request ends (by calling FCGI_Accept) determines the
229 *----------------------------------------------------------------------
231 void FCGI_SetExitStatus(int status)
233 if(FCGI_stdin->fcgx_stream) {
234 FCGX_SetExitStatus(status, FCGI_stdin->fcgx_stream);
239 *----------------------------------------------------------------------
243 * Wrapper for function defined in H&S Section 11.2
245 *----------------------------------------------------------------------
247 void FCGI_perror(const char *str)
249 FCGI_fputs(str, FCGI_stderr);
250 FCGI_fputs(": ", FCGI_stderr);
251 FCGI_fputs(strerror(OS_Errno), FCGI_stderr);
256 *----------------------------------------------------------------------
258 * FCGI_OpenFromFILE --
260 * Constructs a new FCGI_FILE * from the FILE *stream.
263 * NULL if stream == NULL or storage could not be allocated,
264 * otherwise the new FCGI_FILE *.
266 *----------------------------------------------------------------------
268 static FCGI_FILE *FCGI_OpenFromFILE(FILE *stream)
275 fp = (FCGI_FILE *) malloc(sizeof(FCGI_FILE));
278 fp->stdio_stream = stream;
279 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 *----------------------------------------------------------------------
294 FCGI_FILE *FCGI_fopen(const char *path, const char *mode)
296 FILE * file = fopen(path, mode);
297 FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
299 if (file && !fcgi_file)
305 int FCGI_fclose(FCGI_FILE *fp)
308 if(fp->stdio_stream) {
309 n = fclose(fp->stdio_stream);
310 fp->stdio_stream = NULL;
311 } else if(fp->fcgx_stream) {
312 n = FCGX_FClose(fp->fcgx_stream);
313 fp->fcgx_stream = NULL;
315 if((fp != FCGI_stdin) && (fp != FCGI_stdout) && (fp != FCGI_stderr)) {
321 int FCGI_fflush(FCGI_FILE *fp)
326 return fflush(fp->stdio_stream);
327 else if(fp->fcgx_stream)
328 return FCGX_FFlush(fp->fcgx_stream);
332 FCGI_FILE *FCGI_freopen(const char *path, const char *mode,
335 if(fp->stdio_stream) {
336 if(freopen(path, mode, fp->stdio_stream) == NULL)
340 } else if(fp->fcgx_stream) {
341 (void) FCGX_FClose(fp->fcgx_stream);
342 fp->stdio_stream = fopen(path, mode);
343 if(fp->stdio_stream == NULL)
346 fp->fcgx_stream = NULL;
354 *----------------------------------------------------------------------
356 * FCGI_setvbuf, FCGI_setbuf --
358 * Wrappers for functions defined in H&S Section 15.3
360 *----------------------------------------------------------------------
362 int FCGI_setvbuf(FCGI_FILE *fp, char *buf, int bufmode, size_t size)
365 return setvbuf(fp->stdio_stream, buf, bufmode, size);
371 void FCGI_setbuf(FCGI_FILE *fp, char *buf)
374 setbuf(fp->stdio_stream, buf);
378 *----------------------------------------------------------------------
380 * FCGI_fseek, FCGI_ftell, FCGI_rewind, FCGI_fgetpos, FCGI_fsetpos --
382 * Wrappers for functions defined in H&S Section 15.5
384 *----------------------------------------------------------------------
386 int FCGI_fseek(FCGI_FILE *fp, long offset, int whence)
389 return fseek(fp->stdio_stream, offset, whence);
396 int FCGI_ftell(FCGI_FILE *fp)
399 return ftell(fp->stdio_stream);
406 void FCGI_rewind(FCGI_FILE *fp)
409 rewind(fp->stdio_stream);
415 int FCGI_fgetpos(FCGI_FILE *fp, fpos_t *pos)
418 return fgetpos(fp->stdio_stream, pos);
425 int FCGI_fsetpos(FCGI_FILE *fp, const fpos_t *pos)
428 return fsetpos(fp->stdio_stream, pos);
437 *----------------------------------------------------------------------
439 * FCGI_fgetc, FCGI_getchar, FCGI_ungetc --
441 * Wrappers for functions defined in H&S Section 15.6
443 * XXX: getc and getchar are generally defined as macros
444 * for performance reasons
446 *----------------------------------------------------------------------
448 int FCGI_fgetc(FCGI_FILE *fp)
451 return fgetc(fp->stdio_stream);
452 else if(fp->fcgx_stream)
453 return FCGX_GetChar(fp->fcgx_stream);
457 int FCGI_getchar(void)
459 return FCGI_fgetc(FCGI_stdin);
462 int FCGI_ungetc(int c, FCGI_FILE *fp)
465 return ungetc(c, fp->stdio_stream);
466 else if(fp->fcgx_stream)
467 return FCGX_UnGetChar(c, fp->fcgx_stream);
472 *----------------------------------------------------------------------
474 * FCGI_fgets, FCGI_gets --
476 * Wrappers for functions defined in H&S Section 15.7
478 *----------------------------------------------------------------------
480 char *FCGI_fgets(char *str, int size, FCGI_FILE *fp)
483 return fgets(str, size, fp->stdio_stream);
484 else if(fp->fcgx_stream)
485 return FCGX_GetLine(str, size, fp->fcgx_stream);
490 * The gets() function reads characters from the standard input stream
491 * into the array pointed to by str until a newline character is read
492 * or an end-of-file condition is encountered. The newline character
493 * is discarded and the string is terminated with a null character.
495 char *FCGI_gets(char *str)
500 for (s = str; ((c = FCGI_getchar()) != '\n');) {
514 *----------------------------------------------------------------------
516 * Wrappers for functions defined in H&S Section 15.8
518 * XXX: missing: fscanf, scanf
520 *----------------------------------------------------------------------
524 *----------------------------------------------------------------------
526 * FCGI_fputc, FCGI_putchar --
528 * Wrappers for functions defined in H&S Section 15.9
530 * XXX: putc and putchar are generally defined as macros
531 * for performance reasons
533 *----------------------------------------------------------------------
535 int FCGI_fputc(int c, FCGI_FILE *fp)
538 return fputc(c, fp->stdio_stream);
539 else if(fp->fcgx_stream)
540 return FCGX_PutChar(c, fp->fcgx_stream);
544 int FCGI_putchar(int c)
546 return FCGI_fputc(c, FCGI_stdout);
550 *----------------------------------------------------------------------
552 * FCGI_fputs, FCGI_puts
554 * Wrappers for functions defined in H&S Section 15.10
556 *----------------------------------------------------------------------
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 *----------------------------------------------------------------------
595 int FCGI_fprintf(FCGI_FILE *fp, const char *format, ...)
599 va_start(ap, format);
601 n = vfprintf(fp->stdio_stream, format, ap);
602 else if(fp->fcgx_stream)
603 n = FCGX_VFPrintF(fp->fcgx_stream, format, ap);
608 int FCGI_printf(const char *format, ...)
612 va_start(ap, format);
613 n = FCGI_vfprintf(FCGI_stdout, format, ap);
619 *----------------------------------------------------------------------
621 * FCGI_vfprintf, FCGI_vprintf --
623 * Wrappers for functions defined in H&S Section 15.12
625 *----------------------------------------------------------------------
627 int FCGI_vfprintf(FCGI_FILE *fp, const char *format, va_list ap)
630 return vfprintf(fp->stdio_stream, format, ap);
631 else if(fp->fcgx_stream)
632 return FCGX_VFPrintF(fp->fcgx_stream, format, ap);
636 int FCGI_vprintf(const char *format, va_list ap)
638 if(FCGI_stdout->stdio_stream)
639 return vfprintf(FCGI_stdout->stdio_stream, format, ap);
640 else if(FCGI_stdout->fcgx_stream)
641 return FCGX_VFPrintF(FCGI_stdout->fcgx_stream, format, ap);
646 *----------------------------------------------------------------------
648 * FCGI_fread, FCGI_fwrite --
650 * Wrappers for functions defined in H&S Section 15.13
652 *----------------------------------------------------------------------
654 size_t FCGI_fread(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
658 return fread(ptr, size, nmemb, fp->stdio_stream);
659 else if(fp->fcgx_stream) {
660 if((size * nmemb) == 0) {
663 n = FCGX_GetStr((char *) ptr, size * nmemb, fp->fcgx_stream);
669 size_t FCGI_fwrite(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp)
673 return fwrite(ptr, size, nmemb, fp->stdio_stream);
674 else if(fp->fcgx_stream) {
675 if((size * nmemb) == 0) {
678 n = FCGX_PutStr((char *) ptr, size * nmemb, fp->fcgx_stream);
685 *----------------------------------------------------------------------
687 * FCGI_feof, FCGI_ferror, FCGI_clearerr --
689 * Wrappers for functions defined in H&S Section 15.14
691 *----------------------------------------------------------------------
693 int FCGI_feof(FCGI_FILE *fp)
695 if(fp->stdio_stream) {
696 return feof(fp->stdio_stream);
697 } else if (fp->fcgx_stream){
698 return FCGX_HasSeenEOF(fp->fcgx_stream);
704 int FCGI_ferror(FCGI_FILE *fp)
706 if(fp->stdio_stream) {
707 return ferror(fp->stdio_stream);
708 } else if(fp->fcgx_stream) {
709 return FCGX_GetError(fp->fcgx_stream);
714 void FCGI_clearerr(FCGI_FILE *fp)
716 if(fp->stdio_stream) {
717 clearerr(fp->stdio_stream);
718 } else if(fp->fcgx_stream) {
719 FCGX_ClearError(fp->fcgx_stream);
725 *----------------------------------------------------------------------
729 * Wrappers for function defined in H&S Section 15.16
731 *----------------------------------------------------------------------
733 FCGI_FILE *FCGI_tmpfile(void)
735 FILE * file = tmpfile();
736 FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
738 if (file && !fcgi_file)
745 *----------------------------------------------------------------------
747 * FCGI_fileno, FCGI_fdopen, FCGI_popen, FCGI_pclose --
749 * Wrappers for POSIX, X/OPEN functions not in ISO C
751 *----------------------------------------------------------------------
753 int FCGI_fileno(FCGI_FILE *fp)
756 return fileno(fp->stdio_stream);
761 FCGI_FILE *FCGI_fdopen(int fd, const char *mode)
763 FILE * file = fdopen(fd, mode);
764 FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
766 if (file && !fcgi_file)
772 FCGI_FILE *FCGI_popen(const char *cmd, const char *type)
774 FILE * file = popen(cmd, type);
775 FCGI_FILE * fcgi_file = FCGI_OpenFromFILE(file);
777 if (file && !fcgi_file)
783 int FCGI_pclose(FCGI_FILE *fp)
786 if (fp->stdio_stream) {
787 n = pclose(fp->stdio_stream);
788 fp->stdio_stream = NULL;
789 } else if(fp->fcgx_stream) {
791 * The caller is deeply confused; don't free the storage.
795 if((fp != FCGI_stdin) && (fp != FCGI_stdout) && (fp != FCGI_stderr)) {