Merge branch 'master' of https://github.com/szmoore/MCTX3420.git
[matches/MCTX3420.git] / testing / fastcgi-approach / fastcgi.c
index e4b5adb..a518c7a 100644 (file)
@@ -1,26 +1,65 @@
 /**
  * @file fastcgi.c
  * @purpose Runs the FCGI request loop to handle web interface requests.
- * 
- * <stdio.h> should not be included, because these functions are handled by
- * fcgi_stdio.h. If included, it must be included after fcgi_stdio.h.
+ *
+ * fcgi_stdio.h must be included before all else so the stdio function
+ * redirection works ok.
  */
+
 #include <fcgi_stdio.h>
-#include <string.h>
-#include <stdlib.h>
+#include <openssl/sha.h>
+#include "fastcgi.h"
+#include "common.h"
+#include <time.h>
+
+static void LoginHandler(void *data, char *params) {
+       static char loginkey[41] = {0}, ip[256];
+       static time_t timestamp = 0;
+       const char *key, *value;
+       int force = 0, end = 0;
+
+       while ((params = FCGI_KeyPair(params, &key, &value))) {
+               if (!strcmp(key, "force"))
+                       force = !force;
+               else if (!strcmp(key, "end"))
+                       end = !end;
+       }
+
+       if (end) {
+               *loginkey = 0;
+               FCGI_BeginJSON(200, "login");
+               FCGI_EndJSON();
+               return;
+       }
 
-/*
-       But the suggestion was: FunctionName, variable_name (local or member),
-    Structure, ENUMVALUE, Extern_FunctionName, g_global
-*/
+       time_t now = time(NULL);
+       if (force || !*loginkey || (now - timestamp > 180)) {
+               SHA_CTX sha1ctx;
+               unsigned char sha1[20];
+               int i = rand();
 
-//Replace with whatever holds the 'data'
-typedef struct Data Data;
-enum {RESPONSE_OK = 200, RESPONSE_BADREQUEST = 400,
-         RESPONSE_UNAUTHORIZED = 401};
+               SHA1_Init(&sha1ctx);
+               SHA1_Update(&sha1ctx, &now, sizeof(now));
+               SHA1_Update(&sha1ctx, &i, sizeof(i));
+               SHA1_Final(sha1, &sha1ctx);
 
-typedef void (*ModuleHandler) (Data *data, char *params);
+               timestamp = now;
+               for (i = 0; i < 20; i++)
+                       sprintf(loginkey+i*2, "%02x", sha1[i]);
+               sprintf(ip, "%s", getenv("REMOTE_ADDR"));
+               FCGI_BeginJSON(200, "login");
+               FCGI_BuildJSON("key", loginkey);
+               FCGI_EndJSON();
+       } else {
+               char buf[128];
+               strftime(buf, 128, "%H:%M:%S %d-%m-%Y",localtime(&timestamp)); 
+               FCGI_BeginJSON(401, "login");
+               FCGI_BuildJSON("description", "Already logged in");
+               FCGI_BuildJSON("user", ip); 
+               FCGI_BuildJSON("time", buf);
+               FCGI_EndJSON();
+       }
+}
 
 /**
  * Extracts a key/value pair from a request string.
@@ -31,34 +70,47 @@ typedef void (*ModuleHandler) (Data *data, char *params);
  * @return A pointer to the start of the next search location, or NULL if
  *         the EOL is reached.
  */
-static char *KeyPair(char *in, const char **key, const char **value) {
-       char *next, *split;
+char *FCGI_KeyPair(char *in, const char **key, const char **value)
+{
+       char *ptr;
        if (!in || !*in) { //Invalid input or string is EOL
                return NULL;
        }
 
        *key = in;
-       //Must be first so value will be empty if it's not specified
-       if ((next = strchr(in, '&'))) {
-               *next++ = 0;
-       } else { //Don't return NULL as current pair needs to be returned
-               next = "";
-       }
-       if ((split = strchr(in, '='))) {
-               *split++ = 0;
-               *value = split; 
-               return next;
+       //Find either = or &, whichever comes first
+       if ((ptr = strpbrk(in, "=&"))) {
+               if (*ptr == '&') { //No value specified
+                       *value = ptr;
+                       *ptr++ = 0;
+               } else {
+                       //Stopped at an '=' sign
+                       *ptr++ = 0;
+                       *value = ptr;
+                       if ((ptr = strchr(ptr,'&'))) {
+                               *ptr++ = 0;
+                       } else {
+                               ptr = "";
+                       }
+               }
+       } else { //No value specified and no other pair
+               ptr = "";
+               *value = ptr;
        }
-       //Split was not found, set to default value
-       *value = "";
-       return next ? next : "";
+       return ptr;
 }
 
-static void BeginResponse(int response_code, const char *module) {
-       switch (response_code) {
-               case RESPONSE_OK:
+/**
+ * Begins a response to the client in JSON format.
+ * @param status_code The HTTP status code to be returned.
+ * @param module The name of the module that initiated the response.
+ */
+void FCGI_BeginJSON(StatusCodes status_code, const char *module)
+{
+       switch (status_code) {
+               case STATUS_OK:
                        break;
-               case RESPONSE_UNAUTHORIZED:
+               case STATUS_UNAUTHORIZED:
                        printf("Status: 401 Unauthorized\r\n");
                        break;
                default:
@@ -69,32 +121,36 @@ static void BeginResponse(int response_code, const char *module) {
        printf("\t\"module\" : \"%s\"", module);
 }
 
-static void BuildResponse(const char *key, const char *value) {
+/**
+ * Adds a key/value pair to a JSON response. The response must have already
+ * been initiated by FCGI_BeginJSON. Note that characters are not escaped.
+ * @param key The key of the JSON entry
+ * &param value The value associated with the key.
+ */
+void FCGI_BuildJSON(const char *key, const char *value)
+{
        printf(",\r\n\t\"%s\" : \"%s\"", key, value);
 }
 
-static void EndResponse() {
+/**
+ * Ends a JSON response that was initiated by FCGI_BeginJSON.
+ */
+void FCGI_EndJSON() 
+{
        printf("\r\n}\r\n");
 }
 
-static void SensorsHandler(Data *data, char *params) {
-       const char *key, *value;
-       BeginResponse(RESPONSE_OK, "sensors");
-       while ((params = KeyPair(params, &key, &value))) {
-               BuildResponse(key, value);
-       }
-       EndResponse();
-       
-}
-
-void FCGI_RequestLoop (Data *data)
+/**
+ * Main FCGI request loop that receives/responds to client requests.
+ * @param data A data field to be passed to the selected module handler.
+ */ 
+void FCGI_RequestLoop (void *data)
 {
        int count = 0;
        while (FCGI_Accept() >= 0)   {
                ModuleHandler module_handler = NULL;
                char module[BUFSIZ], params[BUFSIZ];
-               
+
                //strncpy doesn't zero-truncate properly
                snprintf(module, BUFSIZ, "%s", getenv("DOCUMENT_URI_LOCAL"));
                snprintf(params, BUFSIZ, "%s", getenv("QUERY_STRING"));
@@ -102,13 +158,15 @@ void FCGI_RequestLoop (Data *data)
                //Remove trailing slashes (if present) from module query
                size_t lastchar = strlen(module) - 1;
                if (lastchar > 0 && module[lastchar] == '/')
-                       module[lastchar] = '\0';
+                       module[lastchar] = 0;
                
 
                if (!strcmp("sensors", module)) {
-                       module_handler = SensorsHandler;
-               } else if (!strcmp("admin", module)) {
-                       //module_handler = AdminHandlerReplace with pointer to admin handler
+                       module_handler = Handler_Sensors;
+               } else if (!strcmp("login", module)) {
+                       module_handler = LoginHandler;
+               } else if (!strcmp("actuators", module)) {
+                       
                }
 
                if (module_handler) {
@@ -116,23 +174,17 @@ void FCGI_RequestLoop (Data *data)
                } else {
                        char buf[BUFSIZ];
                        
-                       BeginResponse(400, module);
-                       BuildResponse("description", "400 Invalid response");
+                       FCGI_BeginJSON(400, module);
+                       FCGI_BuildJSON("description", "400 Invalid response");
                        snprintf(buf, BUFSIZ, "%d", count);
-                       BuildResponse("request-number", buf);
-                       BuildResponse("params", params);
-                       BuildResponse("host", getenv("SERVER_HOSTNAME"));
-                       EndResponse();
+                       FCGI_BuildJSON("request-number", buf);
+                       FCGI_BuildJSON("params", params);
+                       FCGI_BuildJSON("host", getenv("SERVER_HOSTNAME"));
+                       FCGI_BuildJSON("user", getenv("REMOTE_USER"));
+                       FCGI_BuildJSON("userip", getenv("REMOTE_ADDR"));
+                       FCGI_EndJSON();
                }
 
                count++;
-               //Debgging:
-               //printf("Module: %s, Params: %s<br>\n", module, params);
-               //printf("Request number %d, host <i>%s</i>\n",
-               //      count++, getenv("SERVER_HOSTNAME"));
        }
 }
-
-int main(int argc, char *argv[]) {
-       FCGI_RequestLoop(NULL);
-}

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