Merge pull request #32 from jtanx/master
[matches/MCTX3420.git] / server / fastcgi.h
1 /**
2  * @file fastcgi.h
3  * @brief Headers for the fastcgi web interface
4  */
5  
6 #ifndef _FASTCGI_H
7 #define _FASTCGI_H
8  
9 /**
10  * Status codes that fcgi module handlers can return
11  * Success status codes have values > 0
12  * Failure status codes have values <(=) 0 
13  * Note: 0 is counted as an error code to minimise confusion
14  * with in-browser JSON parsing error codes
15  */
16 typedef enum StatusCodes {
17         STATUS_OK = 1,
18         STATUS_ERROR = -1,
19         STATUS_UNAUTHORIZED = -2,
20         STATUS_OUTOFRANGE = -3
21 } StatusCodes;
22
23 #define FCGI_PARAM_REQUIRED (1 << 0)
24 #define FCGI_PARAM_RECEIVED (1 << 1)
25 #define FCGI_BOOL_T (1 << 2)
26 #define FCGI_LONG_T (1 << 3)
27 #define FCGI_DOUBLE_T (1 << 4)
28 #define FCGI_STRING_T (1 << 5)
29 #define FCGI_REQUIRED(x) ((x) | FCGI_PARAM_REQUIRED)
30 #define FCGI_IS_REQUIRED(x) ((x) & FCGI_PARAM_REQUIRED)
31 #define FCGI_RECEIVED(x) ((x) & FCGI_PARAM_RECEIVED)
32 #define FCGI_TYPE(x) ((x) & ~(FCGI_PARAM_REQUIRED | FCGI_PARAM_RECEIVED))
33
34 typedef struct FCGIValue {
35         const char *key;
36         void *value;
37         unsigned flags;
38 } FCGIValue;
39
40 typedef struct FCGIContext FCGIContext;
41 typedef void (*ModuleHandler) (FCGIContext *context, char *params);
42
43 extern void FCGI_BeginControl(FCGIContext *context, bool force);
44 extern void FCGI_EndControl(FCGIContext *context);
45 extern bool FCGI_HasControl(FCGIContext *context, const char *key);
46 extern char *FCGI_KeyPair(char *in, const char **key, const char **value);
47 extern bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], size_t count);
48 extern void FCGI_BeginJSON(FCGIContext *context, StatusCodes status_code);
49 extern void FCGI_JSONPair(const char *key, const char *value);
50 extern void FCGI_JSONLong(const char *key, long value);
51 extern void FCGI_JSONDouble(const char *key, double value);
52 extern void FCGI_JSONBool(const char *key, bool value);
53 extern void FCGI_JSONKey(const char *key);
54 extern void FCGI_PrintRaw(const char *format, ...);
55 extern void FCGI_EndJSON();
56 extern char *FCGI_EscapeJSON(char *buf);
57 extern void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *description);
58 extern void *FCGI_RequestLoop (void *data);
59
60 /**
61  * Shortcut to calling FCGI_RejectJSONEx. Sets the error code
62  * to STATUS_ERROR.
63  * @param context The context to work in
64  * @param description A short description of why the request was rejected.
65  * @see FCGI_RejectJSONEx
66  */
67 #define FCGI_RejectJSON(context, description) FCGI_RejectJSONEx(context, STATUS_ERROR, description)
68
69 /**
70  * Custom formatting function for the JSON value. To be used in 
71  * conjunction with FCGI_JSONKey. Care should be taken to ensure
72  * that valid JSON is produced.
73  * @see FCGI_PrintRaw for calling syntax
74  */
75 #define FCGI_JSONValue FCGI_PrintRaw
76
77 #endif
78
79

UCC git Repository :: git.ucc.asn.au