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.8 2001/06/22 14:19:23 robs Exp $
18 /* Hack to see if we are building TCL - TCL needs varargs not stdarg */
25 #if defined (c_plusplus) || defined (__cplusplus)
31 #define DLLAPI __declspec(dllimport)
38 * Error codes. Assigned to avoid conflict with EOF and errno(2).
40 #define FCGX_UNSUPPORTED_VERSION -2
41 #define FCGX_PROTOCOL_ERROR -3
42 #define FCGX_PARAMS_ERROR -4
43 #define FCGX_CALL_SEQ_ERROR -5
46 * This structure defines the state of a FastCGI stream.
47 * Streams are modeled after the FILE type defined in stdio.h.
48 * (We wouldn't need our own if platform vendors provided a
49 * standard way to subclass theirs.)
50 * The state of a stream is private and should only be accessed
51 * by the procedures defined below.
53 typedef struct FCGX_Stream {
54 unsigned char *rdNext; /* reader: first valid byte
55 * writer: equals stop */
56 unsigned char *wrNext; /* writer: first free byte
57 * reader: equals stop */
58 unsigned char *stop; /* reader: last valid byte + 1
59 * writer: last free byte + 1 */
60 unsigned char *stopUnget; /* reader: first byte of current buffer
61 * fragment, for ungetc
62 * writer: undefined */
66 int FCGI_errno; /* error status */
67 void (*fillBuffProc) (struct FCGX_Stream *stream);
68 void (*emptyBuffProc) (struct FCGX_Stream *stream, int doClose);
73 * An environment (as defined by environ(7)): A NULL-terminated array
74 * of strings, each string having the form name=value.
76 typedef char **FCGX_ParamArray;
81 * Setting FCGI_FAIL_ACCEPT_ON_INTR prevents FCGX_Accept() from
82 * restarting upon being interrupted.
84 #define FCGI_FAIL_ACCEPT_ON_INTR 1
87 * FCGX_Request -- State associated with a request.
89 * Its exposed for API simplicity, I expect parts of it to change!
91 typedef struct FCGX_Request {
92 int requestId; /* valid if isBeginProcessed */
99 /* Don't use anything below here */
101 struct Params *paramsPtr;
102 int ipcFd; /* < 0 means no connection */
103 int isBeginProcessed; /* FCGI_BEGIN_REQUEST seen */
104 int keepConnection; /* don't close ipcFd at end of request */
106 int nWriters; /* number of open writers (0..2) */
113 *======================================================================
115 *======================================================================
119 *----------------------------------------------------------------------
123 * Returns TRUE iff this process appears to be a CGI process
124 * rather than a FastCGI process.
126 *----------------------------------------------------------------------
128 DLLAPI int FCGX_IsCGI(void);
131 *----------------------------------------------------------------------
135 * Initialize the FCGX library. Call in multi-threaded apps
136 * before calling FCGX_Accept_r().
138 * Returns 0 upon success.
140 *----------------------------------------------------------------------
142 DLLAPI int FCGX_Init(void);
145 *----------------------------------------------------------------------
149 * Create a FastCGI listen socket.
151 * path is the Unix domain socket (named pipe for WinNT), or a colon
152 * followed by a port number. e.g. "/tmp/fastcgi/mysocket", ":5000"
154 * backlog is the listen queue depth used in the listen() call.
156 * Returns the socket's file descriptor or -1 on error.
158 *----------------------------------------------------------------------
160 DLLAPI int FCGX_OpenSocket(const char *path, int backlog);
163 *----------------------------------------------------------------------
165 * FCGX_InitRequest --
167 * Initialize a FCGX_Request for use with FCGX_Accept_r().
169 * sock is a file descriptor returned by FCGX_OpenSocket() or 0 (default).
170 * The only supported flag at this time is FCGI_FAIL_ON_INTR.
172 * Returns 0 upon success.
173 *----------------------------------------------------------------------
175 DLLAPI int FCGX_InitRequest(FCGX_Request *request, int sock, int flags);
178 *----------------------------------------------------------------------
182 * Accept a new request (multi-thread safe). Be sure to call
186 * 0 for successful call, -1 for error.
190 * Finishes the request accepted by (and frees any
191 * storage allocated by) the previous call to FCGX_Accept.
192 * Creates input, output, and error streams and
193 * assigns them to *in, *out, and *err respectively.
194 * Creates a parameters data structure to be accessed
195 * via getenv(3) (if assigned to environ) or by FCGX_GetParam
196 * and assigns it to *envp.
198 * DO NOT retain pointers to the envp array or any strings
199 * contained in it (e.g. to the result of calling FCGX_GetParam),
200 * since these will be freed by the next call to FCGX_Finish
203 * DON'T use the FCGX_Request, its structure WILL change.
205 *----------------------------------------------------------------------
207 DLLAPI int FCGX_Accept_r(FCGX_Request *request);
210 *----------------------------------------------------------------------
214 * Finish the request (multi-thread safe).
218 * Finishes the request accepted by (and frees any
219 * storage allocated by) the previous call to FCGX_Accept.
221 * DO NOT retain pointers to the envp array or any strings
222 * contained in it (e.g. to the result of calling FCGX_GetParam),
223 * since these will be freed by the next call to FCGX_Finish
226 *----------------------------------------------------------------------
228 DLLAPI void FCGX_Finish_r(FCGX_Request *request);
231 *----------------------------------------------------------------------
235 * Free the memory and, if close is true,
236 * IPC FD associated with the request (multi-thread safe).
238 *----------------------------------------------------------------------
240 DLLAPI void FCGX_Free(FCGX_Request * request, int close);
243 *----------------------------------------------------------------------
247 * Accept a new request (NOT multi-thread safe).
250 * 0 for successful call, -1 for error.
254 * Finishes the request accepted by (and frees any
255 * storage allocated by) the previous call to FCGX_Accept.
256 * Creates input, output, and error streams and
257 * assigns them to *in, *out, and *err respectively.
258 * Creates a parameters data structure to be accessed
259 * via getenv(3) (if assigned to environ) or by FCGX_GetParam
260 * and assigns it to *envp.
262 * DO NOT retain pointers to the envp array or any strings
263 * contained in it (e.g. to the result of calling FCGX_GetParam),
264 * since these will be freed by the next call to FCGX_Finish
267 *----------------------------------------------------------------------
269 DLLAPI int FCGX_Accept(
273 FCGX_ParamArray *envp);
276 *----------------------------------------------------------------------
280 * Finish the current request (NOT multi-thread safe).
284 * Finishes the request accepted by (and frees any
285 * storage allocated by) the previous call to FCGX_Accept.
287 * DO NOT retain pointers to the envp array or any strings
288 * contained in it (e.g. to the result of calling FCGX_GetParam),
289 * since these will be freed by the next call to FCGX_Finish
292 *----------------------------------------------------------------------
294 DLLAPI void FCGX_Finish(void);
297 *----------------------------------------------------------------------
299 * FCGX_StartFilterData --
301 * stream is an input stream for a FCGI_FILTER request.
302 * stream is positioned at EOF on FCGI_STDIN.
303 * Repositions stream to the start of FCGI_DATA.
304 * If the preconditions are not met (e.g. FCGI_STDIN has not
305 * been read to EOF) sets the stream error code to
306 * FCGX_CALL_SEQ_ERROR.
309 * 0 for a normal return, < 0 for error
311 *----------------------------------------------------------------------
313 DLLAPI int FCGX_StartFilterData(FCGX_Stream *stream);
316 *----------------------------------------------------------------------
318 * FCGX_SetExitStatus --
320 * Sets the exit status for stream's request. The exit status
321 * is the status code the request would have exited with, had
322 * the request been run as a CGI program. You can call
323 * SetExitStatus several times during a request; the last call
324 * before the request ends determines the value.
326 *----------------------------------------------------------------------
328 DLLAPI void FCGX_SetExitStatus(int status, FCGX_Stream *stream);
331 *======================================================================
333 *======================================================================
337 *----------------------------------------------------------------------
339 * FCGX_GetParam -- obtain value of FCGI parameter in environment
343 * Value bound to name, NULL if name not present in the
344 * environment envp. Caller must not mutate the result
345 * or retain it past the end of this request.
347 *----------------------------------------------------------------------
349 DLLAPI char *FCGX_GetParam(const char *name, FCGX_ParamArray envp);
352 *======================================================================
354 *======================================================================
358 *----------------------------------------------------------------------
362 * Reads a byte from the input stream and returns it.
365 * The byte, or EOF (-1) if the end of input has been reached.
367 *----------------------------------------------------------------------
369 DLLAPI int FCGX_GetChar(FCGX_Stream *stream);
372 *----------------------------------------------------------------------
376 * Pushes back the character c onto the input stream. One
377 * character of pushback is guaranteed once a character
378 * has been read. No pushback is possible for EOF.
381 * Returns c if the pushback succeeded, EOF if not.
383 *----------------------------------------------------------------------
385 DLLAPI int FCGX_UnGetChar(int c, FCGX_Stream *stream);
388 *----------------------------------------------------------------------
392 * Reads up to n consecutive bytes from the input stream
393 * into the character array str. Performs no interpretation
394 * of the input bytes.
397 * Number of bytes read. If result is smaller than n,
398 * the end of input has been reached.
400 *----------------------------------------------------------------------
402 DLLAPI int FCGX_GetStr(char *str, int n, FCGX_Stream *stream);
405 *----------------------------------------------------------------------
409 * Reads up to n-1 consecutive bytes from the input stream
410 * into the character array str. Stops before n-1 bytes
411 * have been read if '\n' or EOF is read. The terminating '\n'
412 * is copied to str. After copying the last byte into str,
413 * stores a '\0' terminator.
416 * NULL if EOF is the first thing read from the input stream,
419 *----------------------------------------------------------------------
421 DLLAPI char *FCGX_GetLine(char *str, int n, FCGX_Stream *stream);
424 *----------------------------------------------------------------------
428 * Returns EOF if end-of-file has been detected while reading
429 * from stream; otherwise returns 0.
431 * Note that FCGX_HasSeenEOF(s) may return 0, yet an immediately
432 * following FCGX_GetChar(s) may return EOF. This function, like
433 * the standard C stdio function feof, does not provide the
434 * ability to peek ahead.
437 * EOF if end-of-file has been detected, 0 if not.
439 *----------------------------------------------------------------------
442 DLLAPI int FCGX_HasSeenEOF(FCGX_Stream *stream);
445 *======================================================================
447 *======================================================================
451 *----------------------------------------------------------------------
455 * Writes a byte to the output stream.
458 * The byte, or EOF (-1) if an error occurred.
460 *----------------------------------------------------------------------
462 DLLAPI int FCGX_PutChar(int c, FCGX_Stream *stream);
465 *----------------------------------------------------------------------
469 * Writes n consecutive bytes from the character array str
470 * into the output stream. Performs no interpretation
471 * of the output bytes.
474 * Number of bytes written (n) for normal return,
475 * EOF (-1) if an error occurred.
477 *----------------------------------------------------------------------
479 DLLAPI int FCGX_PutStr(const char *str, int n, FCGX_Stream *stream);
482 *----------------------------------------------------------------------
486 * Writes a null-terminated character string to the output stream.
489 * number of bytes written for normal return,
490 * EOF (-1) if an error occurred.
492 *----------------------------------------------------------------------
494 DLLAPI int FCGX_PutS(const char *str, FCGX_Stream *stream);
497 *----------------------------------------------------------------------
499 * FCGX_FPrintF, FCGX_VFPrintF --
501 * Performs printf-style output formatting and writes the results
502 * to the output stream.
505 * number of bytes written for normal return,
506 * EOF (-1) if an error occurred.
508 *----------------------------------------------------------------------
510 DLLAPI int FCGX_FPrintF(FCGX_Stream *stream, const char *format, ...);
512 DLLAPI int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg);
515 *----------------------------------------------------------------------
519 * Flushes any buffered output.
521 * Server-push is a legitimate application of FCGX_FFlush.
522 * Otherwise, FCGX_FFlush is not very useful, since FCGX_Accept
523 * does it implicitly. Calling FCGX_FFlush in non-push applications
524 * results in extra writes and therefore reduces performance.
527 * EOF (-1) if an error occurred.
529 *----------------------------------------------------------------------
531 DLLAPI int FCGX_FFlush(FCGX_Stream *stream);
534 *======================================================================
535 * Both Readers and Writers
536 *======================================================================
540 *----------------------------------------------------------------------
544 * Closes the stream. For writers, flushes any buffered
547 * Close is not a very useful operation since FCGX_Accept
548 * does it implicitly. Closing the out stream before the
549 * err stream results in an extra write if there's nothing
550 * in the err stream, and therefore reduces performance.
553 * EOF (-1) if an error occurred.
555 *----------------------------------------------------------------------
557 DLLAPI int FCGX_FClose(FCGX_Stream *stream);
560 *----------------------------------------------------------------------
564 * Return the stream error code. 0 means no error, > 0
565 * is an errno(2) error, < 0 is an FastCGI error.
567 *----------------------------------------------------------------------
569 DLLAPI int FCGX_GetError(FCGX_Stream *stream);
572 *----------------------------------------------------------------------
576 * Clear the stream error code and end-of-file indication.
578 *----------------------------------------------------------------------
580 DLLAPI void FCGX_ClearError(FCGX_Stream *stream);
583 *----------------------------------------------------------------------
585 * FCGX_CreateWriter --
587 * Create a FCGX_Stream (used by cgi-fcgi). This shouldn't
588 * be needed by a FastCGI applictaion.
590 *----------------------------------------------------------------------
592 DLLAPI FCGX_Stream *FCGX_CreateWriter(
599 *----------------------------------------------------------------------
603 * Free a FCGX_Stream (used by cgi-fcgi). This shouldn't
604 * be needed by a FastCGI applictaion.
606 *----------------------------------------------------------------------
608 DLLAPI void FCGX_FreeStream(FCGX_Stream **stream);
611 #if defined (__cplusplus) || defined (c_plusplus)
612 } /* terminate extern "C" { */
615 #endif /* _FCGIAPP_H */