4 * Definitions for FastCGI application server programs
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.
12 * $Id: fcgiapp.h,v 1.5 2000/11/05 17:09:36 robs Exp $
18 /* Hack to see if we are building TCL - TCL needs varargs not stdarg */
25 #if defined (c_plusplus) || defined (__cplusplus)
32 #define DLLAPI __declspec(dllimport)
42 * Error codes. Assigned to avoid conflict with EOF and errno(2).
44 #define FCGX_UNSUPPORTED_VERSION -2
45 #define FCGX_PROTOCOL_ERROR -3
46 #define FCGX_PARAMS_ERROR -4
47 #define FCGX_CALL_SEQ_ERROR -5
50 * This structure defines the state of a FastCGI stream.
51 * Streams are modeled after the FILE type defined in stdio.h.
52 * (We wouldn't need our own if platform vendors provided a
53 * standard way to subclass theirs.)
54 * The state of a stream is private and should only be accessed
55 * by the procedures defined below.
57 typedef struct FCGX_Stream {
58 unsigned char *rdNext; /* reader: first valid byte
59 * writer: equals stop */
60 unsigned char *wrNext; /* writer: first free byte
61 * reader: equals stop */
62 unsigned char *stop; /* reader: last valid byte + 1
63 * writer: last free byte + 1 */
64 unsigned char *stopUnget; /* reader: first byte of current buffer
65 * fragment, for ungetc
66 * writer: undefined */
70 int FCGI_errno; /* error status */
71 void (*fillBuffProc) (struct FCGX_Stream *stream);
72 void (*emptyBuffProc) (struct FCGX_Stream *stream, int doClose);
77 * An environment (as defined by environ(7)): A NULL-terminated array
78 * of strings, each string having the form name=value.
80 typedef char **FCGX_ParamArray;
85 * Setting FCGI_FAIL_ACCEPT_ON_INTR prevents FCGX_Accept() from
86 * restarting upon being interrupted.
88 #define FCGI_FAIL_ACCEPT_ON_INTR 1
91 * FCGX_Request -- State associated with a request.
93 * Its exposed for API simplicity, I expect parts of it to change!
95 typedef struct FCGX_Request {
96 int requestId; /* valid if isBeginProcessed */
103 /* Don't use anything below here */
105 struct Params *paramsPtr;
106 int ipcFd; /* < 0 means no connection */
107 int isBeginProcessed; /* FCGI_BEGIN_REQUEST seen */
108 int keepConnection; /* don't close ipcFd at end of request */
110 int nWriters; /* number of open writers (0..2) */
117 *======================================================================
119 *======================================================================
123 *----------------------------------------------------------------------
127 * Returns TRUE iff this process appears to be a CGI process
128 * rather than a FastCGI process.
130 *----------------------------------------------------------------------
132 DLLAPI int FCGX_IsCGI(void);
135 *----------------------------------------------------------------------
139 * Initialize the FCGX library. Call in multi-threaded apps
140 * before calling FCGX_Accept_r().
142 * Returns 0 upon success.
144 *----------------------------------------------------------------------
146 DLLAPI int FCGX_Init(void);
149 *----------------------------------------------------------------------
153 * Create a FastCGI listen socket.
155 * path is the Unix domain socket (named pipe for WinNT), or a colon
156 * followed by a port number. e.g. "/tmp/fastcgi/mysocket", ":5000"
158 * backlog is the listen queue depth used in the listen() call.
160 * Returns the socket's file descriptor or -1 on error.
162 *----------------------------------------------------------------------
164 DLLAPI int FCGX_OpenSocket(const char *path, int backlog);
167 *----------------------------------------------------------------------
169 * FCGX_InitRequest --
171 * Initialize a FCGX_Request for use with FCGX_Accept_r().
173 * sock is a file descriptor returned by FCGX_OpenSocket() or 0 (default).
174 * The only supported flag at this time is FCGI_FAIL_ON_INTR.
176 * Returns 0 upon success.
177 *----------------------------------------------------------------------
179 DLLAPI int FCGX_InitRequest(FCGX_Request *request, int sock, int flags);
182 *----------------------------------------------------------------------
186 * Accept a new request (multi-thread safe). Be sure to call
190 * 0 for successful call, -1 for error.
194 * Finishes the request accepted by (and frees any
195 * storage allocated by) the previous call to FCGX_Accept.
196 * Creates input, output, and error streams and
197 * assigns them to *in, *out, and *err respectively.
198 * Creates a parameters data structure to be accessed
199 * via getenv(3) (if assigned to environ) or by FCGX_GetParam
200 * and assigns it to *envp.
202 * DO NOT retain pointers to the envp array or any strings
203 * contained in it (e.g. to the result of calling FCGX_GetParam),
204 * since these will be freed by the next call to FCGX_Finish
207 * DON'T use the FCGX_Request, its structure WILL change.
209 *----------------------------------------------------------------------
211 DLLAPI int FCGX_Accept_r(FCGX_Request *request);
214 *----------------------------------------------------------------------
218 * Finish the request (multi-thread safe).
222 * Finishes the request accepted by (and frees any
223 * storage allocated by) the previous call to FCGX_Accept.
225 * DO NOT retain pointers to the envp array or any strings
226 * contained in it (e.g. to the result of calling FCGX_GetParam),
227 * since these will be freed by the next call to FCGX_Finish
230 *----------------------------------------------------------------------
232 DLLAPI void FCGX_Finish_r(FCGX_Request *request);
235 *----------------------------------------------------------------------
239 * Free the memory and IPC FD associated with the request (multi-thread safe).
241 *----------------------------------------------------------------------
243 DLLAPI void FCGX_Free(FCGX_Request * request);
246 *----------------------------------------------------------------------
250 * Accept a new request (NOT multi-thread safe).
253 * 0 for successful call, -1 for error.
257 * Finishes the request accepted by (and frees any
258 * storage allocated by) the previous call to FCGX_Accept.
259 * Creates input, output, and error streams and
260 * assigns them to *in, *out, and *err respectively.
261 * Creates a parameters data structure to be accessed
262 * via getenv(3) (if assigned to environ) or by FCGX_GetParam
263 * and assigns it to *envp.
265 * DO NOT retain pointers to the envp array or any strings
266 * contained in it (e.g. to the result of calling FCGX_GetParam),
267 * since these will be freed by the next call to FCGX_Finish
270 *----------------------------------------------------------------------
272 DLLAPI int FCGX_Accept(
276 FCGX_ParamArray *envp);
279 *----------------------------------------------------------------------
283 * Finish the current request (NOT multi-thread safe).
287 * Finishes the request accepted by (and frees any
288 * storage allocated by) the previous call to FCGX_Accept.
290 * DO NOT retain pointers to the envp array or any strings
291 * contained in it (e.g. to the result of calling FCGX_GetParam),
292 * since these will be freed by the next call to FCGX_Finish
295 *----------------------------------------------------------------------
297 DLLAPI void FCGX_Finish(void);
300 *----------------------------------------------------------------------
302 * FCGX_StartFilterData --
304 * stream is an input stream for a FCGI_FILTER request.
305 * stream is positioned at EOF on FCGI_STDIN.
306 * Repositions stream to the start of FCGI_DATA.
307 * If the preconditions are not met (e.g. FCGI_STDIN has not
308 * been read to EOF) sets the stream error code to
309 * FCGX_CALL_SEQ_ERROR.
312 * 0 for a normal return, < 0 for error
314 *----------------------------------------------------------------------
316 DLLAPI int FCGX_StartFilterData(FCGX_Stream *stream);
319 *----------------------------------------------------------------------
321 * FCGX_SetExitStatus --
323 * Sets the exit status for stream's request. The exit status
324 * is the status code the request would have exited with, had
325 * the request been run as a CGI program. You can call
326 * SetExitStatus several times during a request; the last call
327 * before the request ends determines the value.
329 *----------------------------------------------------------------------
331 DLLAPI void FCGX_SetExitStatus(int status, FCGX_Stream *stream);
334 *======================================================================
336 *======================================================================
340 *----------------------------------------------------------------------
342 * FCGX_GetParam -- obtain value of FCGI parameter in environment
346 * Value bound to name, NULL if name not present in the
347 * environment envp. Caller must not mutate the result
348 * or retain it past the end of this request.
350 *----------------------------------------------------------------------
352 DLLAPI char *FCGX_GetParam(const char *name, FCGX_ParamArray envp);
355 *======================================================================
357 *======================================================================
361 *----------------------------------------------------------------------
365 * Reads a byte from the input stream and returns it.
368 * The byte, or EOF (-1) if the end of input has been reached.
370 *----------------------------------------------------------------------
372 DLLAPI int FCGX_GetChar(FCGX_Stream *stream);
375 *----------------------------------------------------------------------
379 * Pushes back the character c onto the input stream. One
380 * character of pushback is guaranteed once a character
381 * has been read. No pushback is possible for EOF.
384 * Returns c if the pushback succeeded, EOF if not.
386 *----------------------------------------------------------------------
388 DLLAPI int FCGX_UnGetChar(int c, FCGX_Stream *stream);
391 *----------------------------------------------------------------------
395 * Reads up to n consecutive bytes from the input stream
396 * into the character array str. Performs no interpretation
397 * of the input bytes.
400 * Number of bytes read. If result is smaller than n,
401 * the end of input has been reached.
403 *----------------------------------------------------------------------
405 DLLAPI int FCGX_GetStr(char *str, int n, FCGX_Stream *stream);
408 *----------------------------------------------------------------------
412 * Reads up to n-1 consecutive bytes from the input stream
413 * into the character array str. Stops before n-1 bytes
414 * have been read if '\n' or EOF is read. The terminating '\n'
415 * is copied to str. After copying the last byte into str,
416 * stores a '\0' terminator.
419 * NULL if EOF is the first thing read from the input stream,
422 *----------------------------------------------------------------------
424 DLLAPI char *FCGX_GetLine(char *str, int n, FCGX_Stream *stream);
427 *----------------------------------------------------------------------
431 * Returns EOF if end-of-file has been detected while reading
432 * from stream; otherwise returns 0.
434 * Note that FCGX_HasSeenEOF(s) may return 0, yet an immediately
435 * following FCGX_GetChar(s) may return EOF. This function, like
436 * the standard C stdio function feof, does not provide the
437 * ability to peek ahead.
440 * EOF if end-of-file has been detected, 0 if not.
442 *----------------------------------------------------------------------
445 DLLAPI int FCGX_HasSeenEOF(FCGX_Stream *stream);
448 *======================================================================
450 *======================================================================
454 *----------------------------------------------------------------------
458 * Writes a byte to the output stream.
461 * The byte, or EOF (-1) if an error occurred.
463 *----------------------------------------------------------------------
465 DLLAPI int FCGX_PutChar(int c, FCGX_Stream *stream);
468 *----------------------------------------------------------------------
472 * Writes n consecutive bytes from the character array str
473 * into the output stream. Performs no interpretation
474 * of the output bytes.
477 * Number of bytes written (n) for normal return,
478 * EOF (-1) if an error occurred.
480 *----------------------------------------------------------------------
482 DLLAPI int FCGX_PutStr(const char *str, int n, FCGX_Stream *stream);
485 *----------------------------------------------------------------------
489 * Writes a null-terminated character string to the output stream.
492 * number of bytes written for normal return,
493 * EOF (-1) if an error occurred.
495 *----------------------------------------------------------------------
497 DLLAPI int FCGX_PutS(const char *str, FCGX_Stream *stream);
500 *----------------------------------------------------------------------
502 * FCGX_FPrintF, FCGX_VFPrintF --
504 * Performs printf-style output formatting and writes the results
505 * to the output stream.
508 * number of bytes written for normal return,
509 * EOF (-1) if an error occurred.
511 *----------------------------------------------------------------------
513 DLLAPI int FCGX_FPrintF(FCGX_Stream *stream, const char *format, ...);
515 DLLAPI int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg);
518 *----------------------------------------------------------------------
522 * Flushes any buffered output.
524 * Server-push is a legitimate application of FCGX_FFlush.
525 * Otherwise, FCGX_FFlush is not very useful, since FCGX_Accept
526 * does it implicitly. Calling FCGX_FFlush in non-push applications
527 * results in extra writes and therefore reduces performance.
530 * EOF (-1) if an error occurred.
532 *----------------------------------------------------------------------
534 DLLAPI int FCGX_FFlush(FCGX_Stream *stream);
537 *======================================================================
538 * Both Readers and Writers
539 *======================================================================
543 *----------------------------------------------------------------------
547 * Closes the stream. For writers, flushes any buffered
550 * Close is not a very useful operation since FCGX_Accept
551 * does it implicitly. Closing the out stream before the
552 * err stream results in an extra write if there's nothing
553 * in the err stream, and therefore reduces performance.
556 * EOF (-1) if an error occurred.
558 *----------------------------------------------------------------------
560 DLLAPI int FCGX_FClose(FCGX_Stream *stream);
563 *----------------------------------------------------------------------
567 * Return the stream error code. 0 means no error, > 0
568 * is an errno(2) error, < 0 is an FastCGI error.
570 *----------------------------------------------------------------------
572 DLLAPI int FCGX_GetError(FCGX_Stream *stream);
575 *----------------------------------------------------------------------
579 * Clear the stream error code and end-of-file indication.
581 *----------------------------------------------------------------------
583 DLLAPI void FCGX_ClearError(FCGX_Stream *stream);
586 #if defined (__cplusplus) || defined (c_plusplus)
587 } /* terminate extern "C" { */
590 #endif /* _FCGIAPP_H */