X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=testing%2Ffastcgi-approach%2Ffastcgi_test.c;h=40543c6c1fc42b4182e91aeb57c9395c7786eb18;hb=5bd6aced71333d0dc8d142f593073c68811e5435;hp=c881a24d76c667e6c08a8a9b814be26d09521afa;hpb=dc5fd23089d23f18649df40bec82fa01af7782f4;p=matches%2FMCTX3420.git 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")); + } }