warn handler fix; 0.56 release
[catagits/fcgi2.git] / include / fcgiapp.h
CommitLineData
344bf056 1/*
0198fd3c 2 * fcgiapp.h --
3 *
4 * Definitions for FastCGI application server programs
5 *
6 *
7 * Copyright (c) 1996 Open Market, Inc.
8 *
9 * See the file "LICENSE.TERMS" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
0b7c9662 12 * $Id: fcgiapp.h,v 1.4 1999/08/05 21:25:52 roberts Exp $
0198fd3c 13 */
14
15#ifndef _FCGIAPP_H
16#define _FCGIAPP_H
17
344bf056 18/* Hack to see if we are building TCL - TCL needs varargs not stdarg */
19#ifndef TCL_LIBRARY
0198fd3c 20#include <stdarg.h>
21#else
22#include <varargs.h>
344bf056 23#endif
0198fd3c 24
25#if defined (c_plusplus) || defined (__cplusplus)
26extern "C" {
27#endif
28
344bf056 29#ifdef _WIN32
30
31#ifndef DLLAPI
32#define DLLAPI __declspec(dllimport)
33#endif
34
35#else /* !_WIN32 */
36
37#define DLLAPI
38
39#endif /* !_WIN32 */
40
0198fd3c 41/*
42 * Error codes. Assigned to avoid conflict with EOF and errno(2).
43 */
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
48
49/*
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.
56 */
57typedef 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 */
67 int isReader;
68 int isClosed;
69 int wasFCloseCalled;
70 int FCGI_errno; /* error status */
71 void (*fillBuffProc) (struct FCGX_Stream *stream);
72 void (*emptyBuffProc) (struct FCGX_Stream *stream, int doClose);
73 void *data;
74} FCGX_Stream;
75
76/*
77 * An environment (as defined by environ(7)): A NULL-terminated array
78 * of strings, each string having the form name=value.
79 */
80typedef char **FCGX_ParamArray;
81
5a7cc494 82/*
0b7c9662 83 * FCGX_Request Flags
5a7cc494 84 *
0b7c9662 85 * Setting FCGI_FAIL_ACCEPT_ON_INTR prevents FCGX_Accept() from
86 * restarting upon being interrupted.
87 */
88#define FCGI_FAIL_ACCEPT_ON_INTR 1
89
90/*
91 * FCGX_Request -- State associated with a request.
92 *
93 * Its exposed for API simplicity, I expect parts of it to change!
5a7cc494 94 */
95typedef struct FCGX_Request {
0b7c9662 96 int requestId; /* valid if isBeginProcessed */
97 int role;
98 FCGX_Stream *in;
99 FCGX_Stream *out;
100 FCGX_Stream *err;
101 char **envp;
102
103 /* Don't use anything below here */
104
105 struct Params *paramsPtr;
5a7cc494 106 int ipcFd; /* < 0 means no connection */
107 int isBeginProcessed; /* FCGI_BEGIN_REQUEST seen */
5a7cc494 108 int keepConnection; /* don't close ipcFd at end of request */
5a7cc494 109 int appStatus;
110 int nWriters; /* number of open writers (0..2) */
0b7c9662 111 int flags;
112 int listen_sock;
5a7cc494 113} FCGX_Request;
114
0198fd3c 115\f
116/*
117 *======================================================================
118 * Control
119 *======================================================================
120 */
121
122/*
123 *----------------------------------------------------------------------
124 *
125 * FCGX_IsCGI --
126 *
127 * Returns TRUE iff this process appears to be a CGI process
128 * rather than a FastCGI process.
129 *
130 *----------------------------------------------------------------------
131 */
132DLLAPI int FCGX_IsCGI(void);
133
134/*
135 *----------------------------------------------------------------------
136 *
5a7cc494 137 * FCGX_Init --
138 *
139 * Initialize the FCGX library. Call in multi-threaded apps
140 * before calling FCGX_Accept_r().
141 *
142 * Returns 0 upon success.
143 *
144 *----------------------------------------------------------------------
145 */
146DLLAPI int FCGX_Init(void);
147
148/*
149 *----------------------------------------------------------------------
150 *
0b7c9662 151 * FCGX_OpenSocket --
152 *
153 * Create a FastCGI listen socket.
154 *
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"
157 *
158 * backlog is the listen queue depth used in the listen() call.
159 *
160 * Returns the socket's file descriptor or -1 on error.
161 *
162 *----------------------------------------------------------------------
163 */
164DLLAPI int FCGX_OpenSocket(const char *path, int backlog);
165
166/*
167 *----------------------------------------------------------------------
168 *
5a7cc494 169 * FCGX_InitRequest --
170 *
0b7c9662 171 * Initialize a FCGX_Request for use with FCGX_Accept_r().
172 *
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.
5a7cc494 175 *
0b7c9662 176 * Returns 0 upon success.
5a7cc494 177 *----------------------------------------------------------------------
178 */
0b7c9662 179DLLAPI int FCGX_InitRequest(FCGX_Request *request, int sock, int flags);
5a7cc494 180
181/*
182 *----------------------------------------------------------------------
183 *
184 * FCGX_Accept_r --
185 *
344bf056 186 * Accept a new request (multi-thread safe). Be sure to call
5a7cc494 187 * FCGX_Init() first.
188 *
189 * Results:
190 * 0 for successful call, -1 for error.
191 *
192 * Side effects:
193 *
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.
201 *
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
205 * or FCGX_Accept.
206 *
207 * DON'T use the FCGX_Request, its structure WILL change.
208 *
209 *----------------------------------------------------------------------
210 */
0b7c9662 211DLLAPI int FCGX_Accept_r(FCGX_Request *request);
5a7cc494 212
213/*
214 *----------------------------------------------------------------------
215 *
216 * FCGX_Finish_r --
217 *
218 * Finish the request (multi-thread safe).
219 *
220 * Side effects:
221 *
222 * Finishes the request accepted by (and frees any
223 * storage allocated by) the previous call to FCGX_Accept.
224 *
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
228 * or FCGX_Accept.
229 *
230 *----------------------------------------------------------------------
231 */
232DLLAPI void FCGX_Finish_r(FCGX_Request *request);
233
234/*
235 *----------------------------------------------------------------------
236 *
0198fd3c 237 * FCGX_Accept --
238 *
5a7cc494 239 * Accept a new request (NOT multi-thread safe).
0198fd3c 240 *
241 * Results:
242 * 0 for successful call, -1 for error.
243 *
244 * Side effects:
245 *
246 * Finishes the request accepted by (and frees any
247 * storage allocated by) the previous call to FCGX_Accept.
248 * Creates input, output, and error streams and
249 * assigns them to *in, *out, and *err respectively.
250 * Creates a parameters data structure to be accessed
251 * via getenv(3) (if assigned to environ) or by FCGX_GetParam
252 * and assigns it to *envp.
253 *
254 * DO NOT retain pointers to the envp array or any strings
255 * contained in it (e.g. to the result of calling FCGX_GetParam),
256 * since these will be freed by the next call to FCGX_Finish
257 * or FCGX_Accept.
258 *
259 *----------------------------------------------------------------------
260 */
261DLLAPI int FCGX_Accept(
262 FCGX_Stream **in,
263 FCGX_Stream **out,
264 FCGX_Stream **err,
265 FCGX_ParamArray *envp);
266
267/*
268 *----------------------------------------------------------------------
269 *
270 * FCGX_Finish --
271 *
5a7cc494 272 * Finish the current request (NOT multi-thread safe).
0198fd3c 273 *
274 * Side effects:
275 *
276 * Finishes the request accepted by (and frees any
277 * storage allocated by) the previous call to FCGX_Accept.
278 *
279 * DO NOT retain pointers to the envp array or any strings
280 * contained in it (e.g. to the result of calling FCGX_GetParam),
281 * since these will be freed by the next call to FCGX_Finish
282 * or FCGX_Accept.
283 *
284 *----------------------------------------------------------------------
285 */
286DLLAPI void FCGX_Finish(void);
287
288/*
289 *----------------------------------------------------------------------
290 *
291 * FCGX_StartFilterData --
292 *
293 * stream is an input stream for a FCGI_FILTER request.
294 * stream is positioned at EOF on FCGI_STDIN.
295 * Repositions stream to the start of FCGI_DATA.
296 * If the preconditions are not met (e.g. FCGI_STDIN has not
297 * been read to EOF) sets the stream error code to
298 * FCGX_CALL_SEQ_ERROR.
299 *
300 * Results:
344bf056 301 * 0 for a normal return, < 0 for error
0198fd3c 302 *
303 *----------------------------------------------------------------------
304 */
305DLLAPI int FCGX_StartFilterData(FCGX_Stream *stream);
306
307/*
308 *----------------------------------------------------------------------
309 *
310 * FCGX_SetExitStatus --
311 *
312 * Sets the exit status for stream's request. The exit status
313 * is the status code the request would have exited with, had
314 * the request been run as a CGI program. You can call
315 * SetExitStatus several times during a request; the last call
316 * before the request ends determines the value.
317 *
318 *----------------------------------------------------------------------
319 */
320DLLAPI void FCGX_SetExitStatus(int status, FCGX_Stream *stream);
321\f
322/*
323 *======================================================================
324 * Parameters
325 *======================================================================
326 */
327
328/*
329 *----------------------------------------------------------------------
330 *
331 * FCGX_GetParam -- obtain value of FCGI parameter in environment
332 *
333 *
334 * Results:
335 * Value bound to name, NULL if name not present in the
336 * environment envp. Caller must not mutate the result
337 * or retain it past the end of this request.
338 *
339 *----------------------------------------------------------------------
340 */
341DLLAPI char *FCGX_GetParam(const char *name, FCGX_ParamArray envp);
342\f
343/*
344 *======================================================================
345 * Readers
346 *======================================================================
347 */
348
349/*
350 *----------------------------------------------------------------------
351 *
352 * FCGX_GetChar --
353 *
354 * Reads a byte from the input stream and returns it.
355 *
356 * Results:
357 * The byte, or EOF (-1) if the end of input has been reached.
358 *
359 *----------------------------------------------------------------------
360 */
361DLLAPI int FCGX_GetChar(FCGX_Stream *stream);
362
363/*
364 *----------------------------------------------------------------------
365 *
366 * FCGX_UnGetChar --
367 *
368 * Pushes back the character c onto the input stream. One
369 * character of pushback is guaranteed once a character
370 * has been read. No pushback is possible for EOF.
371 *
372 * Results:
373 * Returns c if the pushback succeeded, EOF if not.
374 *
375 *----------------------------------------------------------------------
376 */
377DLLAPI int FCGX_UnGetChar(int c, FCGX_Stream *stream);
378
379/*
380 *----------------------------------------------------------------------
381 *
382 * FCGX_GetStr --
383 *
384 * Reads up to n consecutive bytes from the input stream
385 * into the character array str. Performs no interpretation
386 * of the input bytes.
387 *
388 * Results:
389 * Number of bytes read. If result is smaller than n,
390 * the end of input has been reached.
391 *
392 *----------------------------------------------------------------------
393 */
394DLLAPI int FCGX_GetStr(char *str, int n, FCGX_Stream *stream);
395
396/*
397 *----------------------------------------------------------------------
398 *
399 * FCGX_GetLine --
400 *
401 * Reads up to n-1 consecutive bytes from the input stream
402 * into the character array str. Stops before n-1 bytes
403 * have been read if '\n' or EOF is read. The terminating '\n'
404 * is copied to str. After copying the last byte into str,
405 * stores a '\0' terminator.
406 *
407 * Results:
408 * NULL if EOF is the first thing read from the input stream,
409 * str otherwise.
410 *
411 *----------------------------------------------------------------------
412 */
413DLLAPI char *FCGX_GetLine(char *str, int n, FCGX_Stream *stream);
414
415/*
416 *----------------------------------------------------------------------
417 *
418 * FCGX_HasSeenEOF --
419 *
420 * Returns EOF if end-of-file has been detected while reading
421 * from stream; otherwise returns 0.
422 *
423 * Note that FCGX_HasSeenEOF(s) may return 0, yet an immediately
424 * following FCGX_GetChar(s) may return EOF. This function, like
425 * the standard C stdio function feof, does not provide the
426 * ability to peek ahead.
427 *
428 * Results:
429 * EOF if end-of-file has been detected, 0 if not.
430 *
431 *----------------------------------------------------------------------
432 */
433
434DLLAPI int FCGX_HasSeenEOF(FCGX_Stream *stream);
435\f
436/*
437 *======================================================================
438 * Writers
439 *======================================================================
440 */
441
442/*
443 *----------------------------------------------------------------------
444 *
445 * FCGX_PutChar --
446 *
447 * Writes a byte to the output stream.
448 *
449 * Results:
450 * The byte, or EOF (-1) if an error occurred.
344bf056 451 *
0198fd3c 452 *----------------------------------------------------------------------
453 */
454DLLAPI int FCGX_PutChar(int c, FCGX_Stream *stream);
455
456/*
457 *----------------------------------------------------------------------
458 *
459 * FCGX_PutStr --
460 *
461 * Writes n consecutive bytes from the character array str
462 * into the output stream. Performs no interpretation
463 * of the output bytes.
464 *
465 * Results:
466 * Number of bytes written (n) for normal return,
467 * EOF (-1) if an error occurred.
344bf056 468 *
0198fd3c 469 *----------------------------------------------------------------------
470 */
471DLLAPI int FCGX_PutStr(const char *str, int n, FCGX_Stream *stream);
472
473/*
474 *----------------------------------------------------------------------
475 *
476 * FCGX_PutS --
477 *
478 * Writes a null-terminated character string to the output stream.
479 *
480 * Results:
481 * number of bytes written for normal return,
482 * EOF (-1) if an error occurred.
344bf056 483 *
0198fd3c 484 *----------------------------------------------------------------------
485 */
486DLLAPI int FCGX_PutS(const char *str, FCGX_Stream *stream);
487
488/*
489 *----------------------------------------------------------------------
490 *
491 * FCGX_FPrintF, FCGX_VFPrintF --
492 *
493 * Performs printf-style output formatting and writes the results
494 * to the output stream.
495 *
496 * Results:
497 * number of bytes written for normal return,
498 * EOF (-1) if an error occurred.
344bf056 499 *
0198fd3c 500 *----------------------------------------------------------------------
501 */
502DLLAPI int FCGX_FPrintF(FCGX_Stream *stream, const char *format, ...);
503
504DLLAPI int FCGX_VFPrintF(FCGX_Stream *stream, const char *format, va_list arg);
505
506/*
507 *----------------------------------------------------------------------
508 *
509 * FCGX_FFlush --
510 *
511 * Flushes any buffered output.
512 *
513 * Server-push is a legitimate application of FCGX_FFlush.
514 * Otherwise, FCGX_FFlush is not very useful, since FCGX_Accept
515 * does it implicitly. Calling FCGX_FFlush in non-push applications
516 * results in extra writes and therefore reduces performance.
517 *
518 * Results:
519 * EOF (-1) if an error occurred.
344bf056 520 *
0198fd3c 521 *----------------------------------------------------------------------
522 */
523DLLAPI int FCGX_FFlush(FCGX_Stream *stream);
524\f
525/*
526 *======================================================================
527 * Both Readers and Writers
528 *======================================================================
529 */
530
531/*
532 *----------------------------------------------------------------------
533 *
534 * FCGX_FClose --
535 *
536 * Closes the stream. For writers, flushes any buffered
537 * output.
538 *
539 * Close is not a very useful operation since FCGX_Accept
540 * does it implicitly. Closing the out stream before the
541 * err stream results in an extra write if there's nothing
542 * in the err stream, and therefore reduces performance.
543 *
544 * Results:
545 * EOF (-1) if an error occurred.
344bf056 546 *
0198fd3c 547 *----------------------------------------------------------------------
548 */
549DLLAPI int FCGX_FClose(FCGX_Stream *stream);
550
551/*
552 *----------------------------------------------------------------------
553 *
554 * FCGX_GetError --
555 *
556 * Return the stream error code. 0 means no error, > 0
557 * is an errno(2) error, < 0 is an FastCGI error.
558 *
559 *----------------------------------------------------------------------
560 */
561DLLAPI int FCGX_GetError(FCGX_Stream *stream);
562
563/*
564 *----------------------------------------------------------------------
565 *
566 * FCGX_ClearError --
567 *
568 * Clear the stream error code and end-of-file indication.
569 *
570 *----------------------------------------------------------------------
571 */
572DLLAPI void FCGX_ClearError(FCGX_Stream *stream);
573
574
575#if defined (__cplusplus) || defined (c_plusplus)
576} /* terminate extern "C" { */
577#endif
578
579#endif /* _FCGIAPP_H */