#if !defined(NET_WANDERINGHORSE_CWAL_TH1ISH_CGI_H_INCLUDED)
#define NET_WANDERINGHORSE_CWAL_TH1ISH_CGI_H_INCLUDED
/**
   This file houses the th1ish_cgi API, a far-from-complete CGI
   application framework based on th1ish.
*/
#if defined(TH1ISH_AMALGAMATION_BUILD)
#  include "th1ish_amalgamation.h"
#else
#  include "th1ish.h"
#endif

#if defined(__cplusplus)
extern "C" {
#endif

/**
   State container for the th1ish CGI module. It holds all kinds of
   stuff related to CGI state.
*/
struct th1ish_cgi {
    cwal_engine * e;
    th1ish_interp * ie;
    /**
       Holds info related to the request.
    */
    struct {
        /**
           POST data. The library handles JSON and transforms
           form/url-encoded input to a JSON form here.
        */
        cwal_value * post;
        /**
           An Object holding values from the HTTP GET parameters
           (i.e. from getenv("QUERY_STRING")).
        */
        cwal_value * get;
        /**
           An Object holding values from the HTTP cookies
           (i.e. from getenv("HTTP_COOKIE")).
        */
        cwal_value * cookies;
        /**
           An Object holding the raw (currently not unencoded)
           values from the environ(7) environment
           (i.e. from getenv(3) and friends).
         */
        cwal_value * env;
        /**
           stdin for reading POST data. Currently hard-wired
           to stdin.
        */
        FILE * input;
    } request;
    struct {
        /**
           Object of HTTP headers as key/value pairs.
        */
        cwal_value * headers;
        /**
           Object of HTTP cookies as key/value pairs.
        */
        cwal_value * cookies;
        /**
           Response content type.
        */
        char const * contentType;
        /**
           Response HTTP code type.
        */
        int httpCode;
        /**
           Response HTTP status message:

           Status: httpCode httpStatus
        */
        char * httpStatus;
        /**
           Specifies whether or not HTTP headers (including cookies)
           should be output.
        
           0==no header output

           >0==output headers

           <0==determine automatically if headers
           should be output.
        */
        char headerMode;
    } response;
    /**
       The scope under which this object was originally
       initialized. It MUST NOT wander up to a higher scope than this.
     */
    cwal_scope * topScope;
    /**
       TODO. Config file (JSON).
     */
    cwal_value * config;
    /**
       Scratchpad buffer.
     */
    cwal_buffer tmpBuf;
    /**
       Second scratchpad buffer, for when tmpBuf is currently being
       used.
     */
    cwal_buffer tmpBuf2;
    /**
       Holds non-HTTP error information.
    */
    struct {
        /**
           Underlying result code. Hopefully one
           of the CWAL_RC_xxx values.
         */
        int rc;
        /**
           _Might_ hold an error string in the first msg.used bytes of
           msg.mem.
        */
        cwal_buffer msg;
    } err;
};
typedef struct th1ish_cgi th1ish_cgi;
/**
   "Empty" th1ish_cgi object, intended for use
   in copy-constructing.
*/
#define th1ish_cgi_empty_m {                    \
        NULL/*e*/, \
        NULL/*ie*/,                         \
        {/*request*/\
            NULL/*post*/,\
            NULL/*get*/,     \
            NULL/*cookies*/,                 \
            NULL/*input*/ \
        },                        \
        {/*response*/\
            NULL/*headers*/,\
            NULL/*cookies*/,     \
            NULL/*contentType*/,            \
            200/*httpCode*/,\
            NULL/*httpStatus*/,\
            1/*headerMode*/\
        },                        \
        NULL/*topScope*/,\
        NULL/*config*/,       \
        cwal_buffer_empty_m/*tmpBuf*/,      \
        cwal_buffer_empty_m/*tmpBuf2*/,            \
        {/*err*/ 0/*rc*/, cwal_buffer_empty_m/*msg*/} \
    }

/**
   "Empty" th1ish_cgi object, intended for use
   in copy-constructing.
*/
extern const th1ish_cgi th1ish_cgi_empty;

/** @def TH1ISH_CGI_GETENV_DEFAULT

    The default environment name(s) to use for cwal_cgi_getenv().
*/
#define TH1ISH_CGI_GETENV_DEFAULT "gpc"

/**
   Initializes tgt to use ie as its interpreter/engine and
   sets up tgt's state based on the contents of the CGI-related
   environment variables.

   tgt must be a cleanly-initialized th1ish_cgi instance,
   and it may be stack allocated (copy th1ish_cgi_empty over
   it to cleanly initialize it).

   Ownership of ie and tgt are not changed and ie must live tgt.

   Returns 0 on success.

   On error, if either ie or tgt are NULL then there are no side
   effects, otherwise tgt might be partially initialized, so the
   client must eventually pass tgt to th1ish_cgi_cleanup() before ie
   is cleaned up.
*/
int th1ish_cgi_init( th1ish_interp * ie, th1ish_cgi * tgt );

/**
   Cleans up memory owned by the given cgi instance but does not free
   cgi. The caller is responsible for deallocating cgi in a manner
   complementary to its allocation mechanism.
*/
void th1ish_cgi_cleanup( th1ish_cgi * cgi );

/**
   Sets the HTTP response code and message string. The msg bytes
   are copied by this function, and need not be static. The default
   response code is 200. If msg is NULL or empty then some built-in
   string is used, depending on the value of httpCode.
*/
int th1ish_cgi_response_status_set(th1ish_cgi * cx, int httpCode,
                                   char const * msg);


/**
   
*/
cwal_value * th1ish_cgi_env_get( th1ish_cgi * cgi,
                                 char which,
                                 char createIfNeeded );

/**
   
 */
cwal_value * th1ish_cgi_getenv( th1ish_cgi * cx,
                                char const * fromWhere,
                                char const * key,
                                cwal_size_t keyLen);

/**
   
 */
char const * th1ish_cgi_getenv_cstr( th1ish_cgi * cx, char const * where,
                                     char const * key, cwal_size_t keyLen,
                                     cwal_size_t* strLen );
/**
   Sets a variable in one of cx's environment objects.

   whichEnv must be the conventional character representation
   (case-insensitive) for on of the following environment objects:

   - g = GET
   - p = POST
   - e = ENV (i.e. getenv(3))
   - c = COOKIE
   - r = REQUEST (==GET, POST, COOKIE)

   On success 0 is returned and ownership of v is transfered to (or
   shared with) the appropriate environment object. On error non-zero
   is returned and ownership of v is not modified.
*/
int th1ish_cgi_setenv_v( th1ish_cgi * cx, char whichEnv,
                         char const * key,
                         cwal_size_t keyLen,
                         cwal_value * v );

/**
   Convenience form of th1ish_cgi_setenv_v() which sets
   the property in the 'e' environment.
*/
int th1ish_cgi_setenv( th1ish_cgi * cx, char const * key,
                       cwal_size_t keyLen, cwal_value * v );

/**
   
 */
int th1ish_cgi_cookie_set( th1ish_cgi * cx,
                           char const * key,
                           cwal_size_t keyLen,
                           cwal_value * v );

/**
   
 */
int th1ish_cgi_cookie_set2( th1ish_cgi * cx,
                            char const * key, cwal_size_t keyLen,
                            cwal_value * v,
                            char const * domain, char const * path,
                            unsigned int expires, char secure,
                            char httponly );

/**
   
 */
int th1ish_cgi_response_header_add( th1ish_cgi * cx, char const * key,
                                    cwal_size_t keyLen, cwal_value * v );

/**
   This outputs cx's HTTP headers, flushes all pending output (buffered via
   the api.ob API), and cleans up any cleans up any output buffers
 */
int th1ish_cgi_response_output_all(th1ish_cgi * cx);
/**
   URL-decodes a string inline. sLen may be NULL, but if it is not
   then *sLen must initially be the length of str which should be
   decoded. On returning, if sLen is not NULL then it will contain the
   new virtual length of str (which may be shorter than its original
   length but never longer). str will be NUL-terminated by this
   function. If sLen is NULL then the equivalent of strlen() is used
   to determine how many bytes to decode.

   The only error conditions are if the arguments are invalid, in
   which case it returns without side effects (if it can determine the
   arguments are in error) or invokes undefined behaviour (if *sLen is
   longer than str's valid memory).

*/
void th1ish_cgi_urldecode_inline( char * str, cwal_size_t * sLen );

/**
   If the environment variable PATH_INFO is found during
   initialization, it is split into its component dir/file parts as an
   array. This routine returns the Nth element in that array, or NULL
   if out of bounds or no PATH_INFO is availble. The array itself is
   available via th1ish_cgi_getenv(cx,"e","PATH_INFO_SPLIT", 15).
*/
cwal_value * th1ish_cgi_path_part( th1ish_cgi * cx,
                                   unsigned short ndx );
/**
   Convenience form of th1ish_cgi_path_part() with fetches the value
   as a C-string. If strLen is not NULL then it will be set to the
   length (in bytes) of the returned string pointer.  Returns NULL on
   error or if it can't figure out what to do.
*/
char const * th1ish_cgi_path_part_cstr( th1ish_cgi * cx, unsigned short ndx,
                                        cwal_size_t * strLen );

/**
   URL-encodes src to dest and NUL-terminates it. dest is expanded as
   needed.

   Returns 0 on success. On error dest might be partially populated.
*/
int th1ish_cgi_urlencode( th1ish_cgi * cx, char const * src,
                          cwal_size_t srcLen,
                          cwal_buffer * dest );

    
#if defined(__cplusplus)
} /*extern "C"*/
#endif

#endif
/* NET_WANDERINGHORSE_CWAL_TH1ISH_CGI_H_INCLUDED */
