Merge branch 'master' of https://github.com/szmoore/MCTX3420.git
[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_NOTRUNNING = -3,
21         STATUS_ALREADYEXISTS = -4
22 } StatusCodes;
23
24 #define FCGI_PARAM_REQUIRED (1 << 0)
25 #define FCGI_PARAM_RECEIVED (1 << 1)
26 #define FCGI_BOOL_T (1 << 2)
27 #define FCGI_INT_T      (1 << 3)
28 #define FCGI_LONG_T (1 << 4)
29 #define FCGI_DOUBLE_T (1 << 5)
30 #define FCGI_STRING_T (1 << 6)
31 #define FCGI_REQUIRED(x) ((x) | FCGI_PARAM_REQUIRED)
32 #define FCGI_IS_REQUIRED(x) ((x) & FCGI_PARAM_REQUIRED)
33 #define FCGI_RECEIVED(x) ((x) & FCGI_PARAM_RECEIVED)
34 #define FCGI_TYPE(x) ((x) & ~(FCGI_PARAM_REQUIRED | FCGI_PARAM_RECEIVED))
35
36 typedef struct FCGIValue {
37         const char *key;
38         void *value;
39         unsigned flags;
40 } FCGIValue;
41
42 typedef enum {USER_UNAUTH, USER_NORMAL, USER_ADMIN} UserType;
43
44 /**Contextual information related to FCGI requests*/
45 typedef struct  
46 {
47         /**The time of last valid user access possessing the control key**/
48         time_t control_timestamp;
49         /**A SHA-1 hash that is the control key, determining who is logged in**/
50         char control_key[41]; 
51         /**The IPv4 address of the logged-in user**/
52         char control_ip[16];
53         /**Determines if the user is an admin or not**/
54         UserType user_type;
55         /**Name of the logged in user**/
56         char user_name[31];
57         /**The name of the current module**/
58         const char *current_module;
59         /**For debugging purposes?**/
60         int response_number;
61 } FCGIContext;
62
63 typedef void (*ModuleHandler) (FCGIContext *context, char *params);
64
65 extern bool FCGI_LockControl(FCGIContext *context, const char * user_name, UserType user_type);
66 extern void FCGI_ReleaseControl(FCGIContext *context);
67 extern bool FCGI_HasControl(FCGIContext *context, const char *key);
68 extern char *FCGI_KeyPair(char *in, const char **key, const char **value);
69 extern bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], size_t count);
70 extern void FCGI_BeginJSON(FCGIContext *context, StatusCodes status_code);
71 extern void FCGI_AcceptJSON(FCGIContext *context, const char *description, const char *cookie);
72 extern void FCGI_JSONPair(const char *key, const char *value);
73 extern void FCGI_JSONLong(const char *key, long value);
74 extern void FCGI_JSONDouble(const char *key, double value);
75 extern void FCGI_JSONBool(const char *key, bool value);
76 extern void FCGI_JSONKey(const char *key);
77 extern void FCGI_PrintRaw(const char *format, ...);
78 extern void FCGI_EndJSON();
79 extern void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *description);
80 extern char *FCGI_EscapeText(char *buf);
81 extern void *FCGI_RequestLoop (void *data);
82
83 extern void FCGI_WriteBinary(void * data, size_t size, size_t num_elem);
84
85 /**
86  * Shortcut to calling FCGI_RejectJSONEx. Sets the error code
87  * to STATUS_ERROR.
88  * @param context The context to work in
89  * @param description A short description of why the request was rejected.
90  * @see FCGI_RejectJSONEx
91  */
92 #define FCGI_RejectJSON(context, description) FCGI_RejectJSONEx(context, STATUS_ERROR, description)
93
94 /**
95  * Custom formatting function for the JSON value. To be used in 
96  * conjunction with FCGI_JSONKey. Care should be taken to ensure
97  * that valid JSON is produced.
98  * @see FCGI_PrintRaw for calling syntax
99  */
100 #define FCGI_JSONValue FCGI_PrintRaw
101
102 #endif
103
104

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