X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=testing%2Ffastcgi-approach%2Ffastcgi.c;h=a518c7afb21435546b197c28916306fa78b273ef;hb=67ad2d5fd59cdc5a78ce8202ccef93a8351e16cb;hp=298764d36a4b00ee3284d7e0aee328c837f656e9;hpb=4d9b62c2b9eeb1fdaec1d74f6eb3dd022c82e3ec;p=matches%2FMCTX3420.git diff --git a/testing/fastcgi-approach/fastcgi.c b/testing/fastcgi-approach/fastcgi.c index 298764d..a518c7a 100644 --- a/testing/fastcgi-approach/fastcgi.c +++ b/testing/fastcgi-approach/fastcgi.c @@ -1,14 +1,65 @@ /** * @file fastcgi.c * @purpose Runs the FCGI request loop to handle web interface requests. - * + * * fcgi_stdio.h must be included before all else so the stdio function * redirection works ok. */ - + #include +#include #include "fastcgi.h" -//#include "common.h" +#include "common.h" +#include + +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; + } + + time_t now = time(NULL); + if (force || !*loginkey || (now - timestamp > 180)) { + SHA_CTX sha1ctx; + unsigned char sha1[20]; + int i = rand(); + + SHA1_Init(&sha1ctx); + SHA1_Update(&sha1ctx, &now, sizeof(now)); + SHA1_Update(&sha1ctx, &i, sizeof(i)); + SHA1_Final(sha1, &sha1ctx); + + 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(×tamp)); + 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. @@ -111,7 +162,9 @@ void FCGI_RequestLoop (void *data) if (!strcmp("sensors", module)) { - //module_handler = Handler_Sensors; + module_handler = Handler_Sensors; + } else if (!strcmp("login", module)) { + module_handler = LoginHandler; } else if (!strcmp("actuators", module)) { } @@ -127,6 +180,8 @@ void FCGI_RequestLoop (void *data) 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(); }