From 5dca779a92c7b8896232d362b03a96718ee58c7a Mon Sep 17 00:00:00 2001 From: Jeremy Tan Date: Wed, 14 Aug 2013 16:31:00 +0800 Subject: [PATCH] Update fastcgi stuff --- testing/fastcgi-approach/fastcgi_test.c | 72 +++++++++++++++---- .../fastcgi-approach/nginx_server_config.txt | 9 ++- testing/sqlite-approach/README.txt | 2 + 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/testing/fastcgi-approach/fastcgi_test.c b/testing/fastcgi-approach/fastcgi_test.c index c881a24..40543c6 100644 --- a/testing/fastcgi-approach/fastcgi_test.c +++ b/testing/fastcgi-approach/fastcgi_test.c @@ -1,23 +1,67 @@ #include "fcgi_stdio.h" /* fcgi library; put it first*/ #include +/* + 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!
"); +} + +/* + 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; - //Spawn thread to get sensor data here? - //Response loop - while (FCGI_Accept() >= 0) { - printf("Content-type: text/html\r\n" - "\r\n" - "FastCGI Hello! (C, fcgi_stdio library)" - "

FastCGI Hello! (C, fcgi_stdio library)

" - "Request number %d running on host %s\n", - count++, getenv("SERVER_HOSTNAME")); + //FCGI Accept loop + while (FCGI_Accept() >= 0) { + ModuleHandler module_handler = NULL; + const char *module = getenv("DOCUMENT_URI_LOCAL"); + const char *params = getenv("QUERY_STRING"); - char *data = getenv("QUERY_STRING"); - if (data) { - printf("
Query string is: '%s'\n", data); - } - } + 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" + "400 Bad Request\n" + "Unknown module '%s' selected.
\n", + module); + } + + //Debgging: + printf("Module: %s, Params: %s
\n", module, params); + printf("Request number %d, host %s\n", + count++, getenv("SERVER_HOSTNAME")); + } } diff --git a/testing/fastcgi-approach/nginx_server_config.txt b/testing/fastcgi-approach/nginx_server_config.txt index 6642639..c76a4e5 100644 --- a/testing/fastcgi-approach/nginx_server_config.txt +++ b/testing/fastcgi-approach/nginx_server_config.txt @@ -1,14 +1,19 @@ #Custom cgi - location /cgi/ { + location ~ ^/cgi/([^?]*) { fastcgi_pass 127.0.0.1:9005; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_HOSTNAME mctxsoft; fastcgi_param SERVER_SOFTWARE nginx; + + #Regex removed /cgi/ part, and any args + fastcgi_param DOCUMENT_URI_LOCAL $1; fastcgi_param QUERY_STRING $query_string; + + #Unused for now fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script$ fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; diff --git a/testing/sqlite-approach/README.txt b/testing/sqlite-approach/README.txt index b7d3e9c..8f2e398 100644 --- a/testing/sqlite-approach/README.txt +++ b/testing/sqlite-approach/README.txt @@ -1,3 +1,5 @@ +Note: Should be discarded in favour of the fastcgi approach + SQLite approach to interfacing with web frontend: *Main program reads in sensor data and updates the sqlite database -- 2.20.1