Merge branch 'master' of https://github.com/szmoore/MCTX3420.git
authorJeremy Tan <[email protected]>
Fri, 16 Aug 2013 12:22:00 +0000 (20:22 +0800)
committerJeremy Tan <[email protected]>
Fri, 16 Aug 2013 12:22:00 +0000 (20:22 +0800)
testing/fastcgi-approach/Makefile [new file with mode: 0644]
testing/fastcgi-approach/fastcgi.c [new file with mode: 0644]
testing/fastcgi-approach/fastcgi_test.c [deleted file]
testing/fastcgi-approach/index.html [new file with mode: 0644]
testing/fastcgi-approach/nginx_server_config.txt

diff --git a/testing/fastcgi-approach/Makefile b/testing/fastcgi-approach/Makefile
new file mode 100644 (file)
index 0000000..03be42f
--- /dev/null
@@ -0,0 +1,22 @@
+CXX = gcc
+FLAGS = -std=c99 -Wall -Werror -pedantic -g
+LIB = -lpthread -lfcgi
+OBJ = fastcgi.o
+RM = rm -f
+
+BIN = fast
+
+$(BIN) : $(OBJ)
+       $(CXX) $(FLAGS) -o $(BIN) $(OBJ) $(LIB)
+
+%.o : %.c
+       $(CXX) $(FLAGS) -c $<
+
+clean:
+       $(RM) $(BIN)
+       $(RM) *.o
+
+clean_full: #cleans up all backup files
+       $(RM) $(BIN) $(OBJ) $(LINKOBJ)
+       $(RM) *.*~
+       $(RM) *~
diff --git a/testing/fastcgi-approach/fastcgi.c b/testing/fastcgi-approach/fastcgi.c
new file mode 100644 (file)
index 0000000..c2444c8
--- /dev/null
@@ -0,0 +1,144 @@
+/**
+ * @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.
+ */
+#include <fcgi_stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+/*
+       But the suggestion was: FunctionName, variable_name (local or member),
+    Structure, ENUMVALUE, Extern_FunctionName, g_global
+*/
+
+enum {STATUS_OK = 200, STATUS_BADREQUEST = 400,
+         STATUS_UNAUTHORIZED = 401};
+
+typedef void (*ModuleHandler) (void *data, char *params);
+
+/**
+ * Extracts a key/value pair from a request string.
+ * Note that the input is modified by this function.
+ * @param in The string from which to extract the pair
+ * @param key A pointer to a variable to hold the key string
+ * @param value A pointer to a variable to hold the value string
+ * @return A pointer to the start of the next search location, or NULL if
+ *         the EOL is reached.
+ */
+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;
+       //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;
+       }
+       return ptr;
+}
+
+void FCGI_BeginJSON(int status_code, const char *module)
+{
+       switch (status_code) {
+               case STATUS_OK:
+                       break;
+               case STATUS_UNAUTHORIZED:
+                       printf("Status: 401 Unauthorized\r\n");
+                       break;
+               default:
+                       printf("Status: 400 Bad Request\r\n");
+       }
+       printf("Content-type: application/json; charset=utf-8\r\n\r\n");
+       printf("{\r\n");
+       printf("\t\"module\" : \"%s\"", module);
+}
+
+void FCGI_BuildJSON(const char *key, const char *value)
+{
+       printf(",\r\n\t\"%s\" : \"%s\"", key, value);
+}
+
+void FCGI_EndJSON() 
+{
+       printf("\r\n}\r\n");
+}
+
+static void SensorsHandler(void *data, char *params) 
+{
+       const char *key, *value;
+       
+       //Begin a request only when you know the final result
+       //E.g whether OK or not.
+       FCGI_BeginJSON(STATUS_OK, "sensors");
+       while ((params = FCGI_KeyPair(params, &key, &value))) {
+               FCGI_BuildJSON(key, value);
+       }
+       FCGI_EndJSON();
+}
+
+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"));
+               
+               //Remove trailing slashes (if present) from module query
+               size_t lastchar = strlen(module) - 1;
+               if (lastchar > 0 && module[lastchar] == '/')
+                       module[lastchar] = 0;
+               
+
+               if (!strcmp("sensors", module)) {
+                       module_handler = SensorsHandler;
+               } else if (!strcmp("admin", module)) {
+                       //module_handler = AdminHandlerReplace with pointer to admin handler
+               }
+
+               if (module_handler) {
+                       module_handler(data, params);
+               } else {
+                       char buf[BUFSIZ];
+                       
+                       FCGI_BeginJSON(400, module);
+                       FCGI_BuildJSON("description", "400 Invalid response");
+                       snprintf(buf, BUFSIZ, "%d", count);
+                       FCGI_BuildJSON("request-number", buf);
+                       FCGI_BuildJSON("params", params);
+                       FCGI_BuildJSON("host", getenv("SERVER_HOSTNAME"));
+                       FCGI_EndJSON();
+               }
+
+               count++;
+       }
+}
+
+int main(int argc, char *argv[]) {
+       FCGI_RequestLoop(NULL);
+}
diff --git a/testing/fastcgi-approach/fastcgi_test.c b/testing/fastcgi-approach/fastcgi_test.c
deleted file mode 100644 (file)
index 40543c6..0000000
+++ /dev/null
@@ -1,67 +0,0 @@
-#include "fcgi_stdio.h" /* fcgi library; put it first*/
-#include <stdlib.h>
-
-/*
-       But the suggestion was: FunctionName, variable_name (local or member),
-    Structure, ENUMVALUE, Extern_FunctionName, g_global
-*/
-
-typedef struct Data Data;
-
-typedef void (*ModuleHandler) (Data *data, const char *params);
-
-static void SensorsHandler(Data *data, const char *params) {
-    printf("Sensors module!<br>");
-}
-
-/*
-   API Schema:
-   Sensors:
-   /cgi/sensors?get=x
-   *get=x is optional. Retrieves info for sensor with id x
-   Devices:
-   /cgi/devices?status=x&power=y&id=z
-   *status and power is optional
-   *status retrieves whether device with id x is operational
-   *power tells whether or not to power on/off the device with id z
-   
-   Response format:
-   200 OK if request was ok
-   400 bad request for malformed request
-      
-*/
-int main (int argc, char *argv[])
-{
-  Data *data = NULL;
-  int count = 0;
-
-  //FCGI Accept loop
-  while (FCGI_Accept() >= 0)   {
-    ModuleHandler module_handler = NULL;
-    const char *module = getenv("DOCUMENT_URI_LOCAL");
-    const char *params = getenv("QUERY_STRING");
-
-    if (!strcmp("sensors", module)) {
-        module_handler = SensorsHandler; //Replace with pointer to sensors handler
-    } else if (!strcmp("admin"), module) {
-        module_handler = NULL; //Replace with pointer to admin handler
-        printf("Admin module selected!\n");
-    }
-    
-    if (module_handler) {
-        printf("Content-type: text/html\r\n\r\n"); //Replace with actual type
-        module_handler(data, params);
-    } else {
-        printf("Status: 400 Bad Request\r\n"
-               "Content-type: text/html\r\n\r\n"
-               "<title>400 Bad Request</title>\n"
-               "Unknown module '%s' selected.<br>\n",
-               module);   
-    }
-    
-    //Debgging:
-    printf("Module: %s, Params: %s<br>\n", module, params);
-    printf("Request number %d, host <i>%s</i>\n",
-        count++, getenv("SERVER_HOSTNAME"));
-  }
-}
diff --git a/testing/fastcgi-approach/index.html b/testing/fastcgi-approach/index.html
new file mode 100644 (file)
index 0000000..a3ff3ec
--- /dev/null
@@ -0,0 +1,82 @@
+<!DOCTYPE html>
+<html>
+       <head>
+               <meta charset="utf-8">
+               <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
+               <title>FastCGI API Test</title>
+               <style>
+                       body {
+                               font-family: "Trebuchet MS", "Verdana", "Arial", "Sans";
+                               font-size: 12px;
+                               margin: 1em;
+                       }
+                       h2 {
+                               border-bottom: 1px solid gray;
+                       }
+                       .io {
+                               border: 1px solid gray;
+                               padding: 0.5em;
+                               margin: 1em;
+                               min-height: 5em;
+                               background-color: GhostWhite;
+                       }
+               </style>
+               
+               <script>
+               $(document).ready(function()
+               {
+                       $('#inputquery').submit(function () 
+                       {
+                               $('#output').text("Submitting query...");
+                               var query = $('#inputquery').find('input[name="query"]').val();
+                               
+                               var d = new Date();
+                               var start = d.getMilliseconds();
+                               var domain = document.domain == "mctx.us.to" ? "mctx.us.to:8080" : document.domain;
+                               $.getJSON('http://'+domain+'/api/'+query, function(data) {
+                                       var items = [];
+                                       var timeDiff = d.getMilliseconds() - start; //Not precise at all, use web console
+                                       
+                                       $.each(data, function(key, val) {
+                                               items.push('<li>"' + key + '" : "' + val + '"</li>'); 
+                                       });
+                                       
+                                       
+                                       $('#output').html("Response ok (" + timeDiff + "ms)! Output:<br>");
+                                       $('<ul/>', {
+                                               html: items.join("\n")
+                                       }).appendTo('#output');
+               
+                               }).fail(function(jqXHR) {
+                                       $('#output').text("Query failed with response code: " + jqXHR.status);
+                               });
+                               return false;
+                       });
+               });
+               </script>
+       </head>
+       
+       <body>
+               <h1>FastCGI API Test</h1>
+               The API is located at: <a href="http://mctx.us.to:8080/api/">http://mctx.us.to:8080/api/</a><br>
+               <h2>Input</h2>
+               Place a query string here. Examples include:<br>
+               <ul>
+                       <li><pre>sensors?key=value&amp;key2</pre></li>
+                       <li><pre>doesntexist?f</pre></li>
+               </ul>
+               Response times are inaccurate via JavaScript. Use the web console of
+               your browser to determine how long the query takes.<br>
+               Hopefully this doesn't break!
+               <div class="io">
+                       <form id="inputquery" name="input" action="#">
+                               Query string: <input type="text" name="query"><br>
+                               <input type="submit" value="Submit">
+                       </form>
+               </div>
+               
+               <h2>Output</h2>
+               <div id="output" class="io">
+               </div>
+       </body>
+</html>
index c76a4e5..76be0f0 100644 (file)
@@ -1,5 +1,5 @@
         #Custom cgi
-        location ~ ^/cgi/([^?]*) {
+        location ~ ^/api/?([^?]*) {
                 fastcgi_pass 127.0.0.1:9005;
                 fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
                 fastcgi_param  SERVER_HOSTNAME    mctxsoft;

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