/**
* @file fastcgi.h
- * @purpose Headers for the fastcgi web interface
+ * @brief Headers for the fastcgi web interface
*/
#ifndef _FASTCGI_H
#define _FASTCGI_H
-/**(HTTP) Status codes that fcgi module handlers can return**/
+/**
+ * Status codes that fcgi module handlers can return
+ * Success status codes have values > 0
+ * Failure status codes have values <(=) 0
+ * Note: 0 is counted as an error code to minimise confusion
+ * with in-browser JSON parsing error codes
+ */
typedef enum StatusCodes {
- STATUS_OK = 200,
- STATUS_ERROR = 400,
- STATUS_UNAUTHORIZED = 401
+ STATUS_OK = 1,
+ STATUS_ERROR = -1,
+ STATUS_UNAUTHORIZED = -2,
+ STATUS_NOTRUNNING = -3,
+ STATUS_ALREADYEXISTS = -4
} StatusCodes;
-typedef struct FCGIContext FCGIContext;
+#define FCGI_PARAM_REQUIRED (1 << 0)
+#define FCGI_PARAM_RECEIVED (1 << 1)
+#define FCGI_BOOL_T (1 << 2)
+#define FCGI_INT_T (1 << 3)
+#define FCGI_LONG_T (1 << 4)
+#define FCGI_DOUBLE_T (1 << 5)
+#define FCGI_STRING_T (1 << 6)
+#define FCGI_REQUIRED(x) ((x) | FCGI_PARAM_REQUIRED)
+#define FCGI_IS_REQUIRED(x) ((x) & FCGI_PARAM_REQUIRED)
+#define FCGI_RECEIVED(x) ((x) & FCGI_PARAM_RECEIVED)
+#define FCGI_TYPE(x) ((x) & ~(FCGI_PARAM_REQUIRED | FCGI_PARAM_RECEIVED))
+
+typedef struct FCGIValue {
+ const char *key;
+ void *value;
+ unsigned flags;
+} FCGIValue;
+
+/**Contextual information related to FCGI requests*/
+typedef struct
+{
+ /**The time of last valid user access possessing the control key**/
+ time_t control_timestamp;
+ /**A SHA-1 hash that is the control key, determining who is logged in**/
+ char control_key[41];
+ /**The IPv4 address of the logged-in user**/
+ char control_ip[16];
+ /**A friendly name for the logged-in user. Max length 30**/
+ char friendly_name[31];
+ /**The name of the current module**/
+ const char *current_module;
+ /**For debugging purposes?**/
+ int response_number;
+} FCGIContext;
+
typedef void (*ModuleHandler) (FCGIContext *context, char *params);
-extern void FCGI_BeginControl(FCGIContext *context, bool force);
-extern void FCGI_EndControl(FCGIContext *context);
+extern bool FCGI_LockControl(FCGIContext *context, bool force);
+extern void FCGI_ReleaseControl(FCGIContext *context);
extern bool FCGI_HasControl(FCGIContext *context, const char *key);
extern char *FCGI_KeyPair(char *in, const char **key, const char **value);
+extern bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], size_t count);
extern void FCGI_BeginJSON(FCGIContext *context, StatusCodes status_code);
+extern void FCGI_AcceptJSON(FCGIContext *context, const char *description, const char *cookie);
extern void FCGI_JSONPair(const char *key, const char *value);
extern void FCGI_JSONLong(const char *key, long value);
extern void FCGI_JSONDouble(const char *key, double value);
extern void FCGI_JSONBool(const char *key, bool value);
extern void FCGI_JSONKey(const char *key);
-extern void FCGI_JSONValue(const char *format, ...);
+extern void FCGI_PrintRaw(const char *format, ...);
extern void FCGI_EndJSON();
-extern void FCGI_RejectJSON(FCGIContext *context);
extern void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *description);
-extern void * FCGI_RequestLoop (void *data);
-#define FCGI_PrintRaw FCGI_JSONValue // Functionality is identical
+extern char *FCGI_EscapeText(char *buf);
+extern void *FCGI_RequestLoop (void *data);
+
+extern void FCGI_WriteBinary(void * data, size_t size, size_t num_elem);
+
+/**
+ * Shortcut to calling FCGI_RejectJSONEx. Sets the error code
+ * to STATUS_ERROR.
+ * @param context The context to work in
+ * @param description A short description of why the request was rejected.
+ * @see FCGI_RejectJSONEx
+ */
+#define FCGI_RejectJSON(context, description) FCGI_RejectJSONEx(context, STATUS_ERROR, description)
+
+/**
+ * Custom formatting function for the JSON value. To be used in
+ * conjunction with FCGI_JSONKey. Care should be taken to ensure
+ * that valid JSON is produced.
+ * @see FCGI_PrintRaw for calling syntax
+ */
+#define FCGI_JSONValue FCGI_PrintRaw
#endif