X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=testing%2Ffastcgi-approach%2Ffastcgi.c;h=a518c7afb21435546b197c28916306fa78b273ef;hb=67ad2d5fd59cdc5a78ce8202ccef93a8351e16cb;hp=c2444c8b82f4bfe299802dee38861ccb2c0bd352;hpb=81f1b8b245bc3820974ae5e270efe05fca0f89db;p=matches%2FMCTX3420.git diff --git a/testing/fastcgi-approach/fastcgi.c b/testing/fastcgi-approach/fastcgi.c index c2444c8..a518c7a 100644 --- a/testing/fastcgi-approach/fastcgi.c +++ b/testing/fastcgi-approach/fastcgi.c @@ -1,24 +1,65 @@ /** * @file fastcgi.c * @purpose Runs the FCGI request loop to handle web interface requests. - * - * 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 -#include -#include +#include +#include "fastcgi.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; + } -/* - 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(); -enum {STATUS_OK = 200, STATUS_BADREQUEST = 400, - STATUS_UNAUTHORIZED = 401}; + SHA1_Init(&sha1ctx); + SHA1_Update(&sha1ctx, &now, sizeof(now)); + SHA1_Update(&sha1ctx, &i, sizeof(i)); + SHA1_Final(sha1, &sha1ctx); -typedef void (*ModuleHandler) (void *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(×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. @@ -59,7 +100,12 @@ char *FCGI_KeyPair(char *in, const char **key, const char **value) return ptr; } -void FCGI_BeginJSON(int status_code, const char *module) +/** + * 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: @@ -75,29 +121,29 @@ void FCGI_BeginJSON(int status_code, const char *module) printf("\t\"module\" : \"%s\"", module); } +/** + * 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 + * ¶m value The value associated with the key. + */ void FCGI_BuildJSON(const char *key, const char *value) { printf(",\r\n\t\"%s\" : \"%s\"", key, value); } +/** + * Ends a JSON response that was initiated by FCGI_BeginJSON. + */ 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(); -} - +/** + * 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; @@ -116,9 +162,11 @@ void FCGI_RequestLoop (void *data) 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) { @@ -132,13 +180,11 @@ 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(); } count++; } } - -int main(int argc, char *argv[]) { - FCGI_RequestLoop(NULL); -}