Home | History | Annotate | Download | only in cgi
      1 /*
      2  * Copyright 2001-2004 Brandon Long
      3  * All Rights Reserved.
      4  *
      5  * ClearSilver Templating System
      6  *
      7  * This code is made available under the terms of the ClearSilver License.
      8  * http://www.clearsilver.net/license.hdf
      9  *
     10  */
     11 
     12 /*
     13  * cgiwrap.h
     14  * The purpose of the cgiwrap is to abstract the CGI interface to allow
     15  * for other than the default implementation.  The default
     16  * implementation is of course based on environment variables and stdio,
     17  * but this can be used with server modules which can substitute their
     18  * own implementation of these functions.
     19  */
     20 
     21 
     22 #ifndef __CGIWRAP_H_
     23 #define __CGIWRAP_H_ 1
     24 
     25 #include <stdarg.h>
     26 #include "util/neo_err.h"
     27 
     28 __BEGIN_DECLS
     29 
     30 typedef int (*READ_FUNC)(void *, char *, int);
     31 typedef int (*WRITEF_FUNC)(void *, const char *, va_list);
     32 typedef int (*WRITE_FUNC)(void *, const char *, int);
     33 typedef char *(*GETENV_FUNC)(void *, const char *);
     34 typedef int (*PUTENV_FUNC)(void *, const char *, const char *);
     35 typedef int (*ITERENV_FUNC)(void *, int, char **, char **);
     36 
     37 /*
     38  * Function: cgiwrap_init_std - Initialize cgiwrap with default functions
     39  * Description: cgiwrap_init_std will initialize the cgiwrap subsystem
     40  *              to use the default CGI functions, ie
     41  *              getenv/putenv/stdio.  In reality, all this is doing is
     42  *              setting up the data for the cgiwrap_iterenv() function.
     43  * Input: the arguments to main, namely argc/argv/envp
     44  * Output: None
     45  * Returns: None
     46  */
     47 void cgiwrap_init_std (int argc, char **argv, char **envp);
     48 
     49 /*
     50  * Function: cgiwrap_init_emu - initialize cgiwrap for emulated use
     51  * Description: cgiwrap_init_emu sets up the cgiwrap subsystem for use
     52  *              in an emulated environment where you are providing
     53  *              routines to use in place of the standard routines, ie
     54  *              when used to interface with a server or scripting
     55  *              language.
     56  *              See cgi/cgiwrap.h for the exact definitions of the
     57  *              callback functions.
     58  * Input: data - user data to be passed to the specified callbacks
     59  *        read_cb - a cb to replace fread(stdin)
     60  *        writef_cb - a cb to repalce fprintf(stdout)
     61  *        write_cb - a cb to replace fwrite(stdout)
     62  *        getenv_cb - a cb to replace getenv
     63  *        putenv_cb - a cb to replace putenv
     64  *        iterenv_cb - a cb to replace the default environment iteration
     65  *                     function (which just wraps walking the envp array)
     66  * Output: None
     67  * Returns: None
     68  */
     69 void cgiwrap_init_emu (void *data, READ_FUNC read_cb,
     70     WRITEF_FUNC writef_cb, WRITE_FUNC write_cb, GETENV_FUNC getenv_cb,
     71     PUTENV_FUNC putenv_cb, ITERENV_FUNC iterenv_cb);
     72 
     73 /*
     74  * Function: cgiwrap_getenv - the wrapper for getenv
     75  * Description: cgiwrap_getenv wraps the getenv function for access to
     76  *              environment variables, which are used to pass data to
     77  *              CGI scripts.  This version differs from the system
     78  *              getenv in that it makes a copy of the value it returns,
     79  *              which gets around problems when wrapping this routine in
     80  *              garbage collected/reference counted languages by
     81  *              moving the ownership of the data to the calling
     82  *              function.
     83  * Input: k - the environment variable to lookup
     84  * Output: v - a newly allocated copy of the value of that variable, or
     85  *             NULL if not found.
     86  * Returns: NERR_NOMEM if there isn't memory available to allocate the result
     87  */
     88 NEOERR *cgiwrap_getenv (const char *k, char **v);
     89 
     90 /*
     91  * Function: cgiwrap_putenv - wrap the putenv call
     92  * Description: cgiwrap_putenv wraps the putenv call.  This is mostly
     93  *              used by the cgi_debug_init function to create an
     94  *              artificial environment.  This version differs from the
     95  *              system version by having separate arguments for the
     96  *              variable name and value, which makes life easier for the
     97  *              caller (usually), and keeps most wrapping callbacks from
     98  *              having to implement a parser to separate them.
     99  * Input: k - the env var name
    100  *        v - the new value for env var k
    101  * Output: None
    102  * Returns: NERR_NOMEM
    103  */
    104 NEOERR *cgiwrap_putenv (const char *k, const char *v);
    105 
    106 /*
    107  * Function: cgiwrap_iterenv - iterater for env vars
    108  * Description: cgiwrap_iterenv allows a program to iterate over all the
    109  *              environment variables.  This is probably mostly used by
    110  *              the default debug output.
    111  * Input: n - variable to return.  This should start at 0 and increment
    112  *            until you receive a NULL return value.
    113  * Output: k - a malloc'd copy of the variable name
    114  *         v - a malloc'd copy of the variable value
    115  * Returns: NERR_NOMEM
    116  */
    117 NEOERR *cgiwrap_iterenv (int n, char **k, char **v);
    118 
    119 /*
    120  * Function: cgiwrap_writef - a wrapper for printf
    121  * Description: cgiwrap_writef is the formatted output command that
    122  *              replaces printf or fprintf(stdout) in a standard CGI
    123  * Input: fmt - standard printf fmt string and args
    124  * Output: None
    125  * Returns: NERR_SYSTEM
    126  */
    127 NEOERR *cgiwrap_writef (const char *fmt, ...)
    128                         ATTRIBUTE_PRINTF(1,2);
    129 
    130 /*
    131  * Function: cgiwrap_writevf - a wrapper for vprintf
    132  * Description: cgiwrap_writevf is the formatted output command that
    133  *              replaces vprintf or fvprintf(stdout) in a standard CGI
    134  *              It is also used by cgiwrap_writef (the actual wrapped
    135  *              function is a v type function)
    136  * Input: fmt - standard printf fmt string
    137  *        ap - stdarg argument pointer
    138  * Output: None
    139  * Returns: NERR_SYSTEM
    140  */
    141 NEOERR *cgiwrap_writevf (const char *fmt, va_list ap);
    142 
    143 /*
    144  * Function: cgiwrap_write - wrapper for the fwrite(stdout)
    145  * Description: cgiwrap_write is the block data output function for
    146  *              cgiwrap that replaces fwrite(stdout) in regular CGIs
    147  * Input: buf - a character buffer
    148  *        buf_len - the length of the buffer in buf
    149  * Output: None
    150  * Returns: NERR_IO
    151  */
    152 NEOERR *cgiwrap_write (const char *buf, int buf_len);
    153 
    154 /*
    155  * Function: cgiwrap_read - cgiwrap input function
    156  * Description: cgiwrap_read is used to read incoming data from the
    157  *              client, usually from a POST or PUT HTTP request.  It
    158  *              wraps the part of fread(stdin).
    159  * Input: buf - a pre-allocated buffer to read the data into
    160  *        buf_len - the size of the pre-allocated buffer
    161  * Output: read_len - the number of bytes read into buf
    162  * Returns: None
    163  */
    164 void cgiwrap_read (char *buf, int buf_len, int *read_len);
    165 
    166 __END_DECLS
    167 
    168 #endif /* __CGIWRAP_H_ */
    169