From f7835777d9a2e85ab310919a150a200ea55c7222 Mon Sep 17 00:00:00 2001 From: Jeremy Tan Date: Thu, 3 Oct 2013 22:33:51 +0800 Subject: [PATCH] Integrate gui login with server code --- server/fastcgi.c | 31 ++++++++++++---- server/fastcgi.h | 1 + server/login.c | 18 +++++----- testing/MCTXWeb/public_html/index.html | 2 +- .../MCTXWeb/public_html/static/mctx.gui.js | 35 ++++++++++++++----- testing/MCTXWeb/public_html/static/style.css | 1 + 6 files changed, 62 insertions(+), 26 deletions(-) diff --git a/server/fastcgi.c b/server/fastcgi.c index 08c413b..41def56 100644 --- a/server/fastcgi.c +++ b/server/fastcgi.c @@ -45,6 +45,8 @@ static void IdentifyHandler(FCGIContext *context, char *params) { FCGI_JSONPair("description", "MCTX3420 Server API (2013)"); FCGI_JSONPair("build_date", __DATE__ " " __TIME__); FCGI_JSONLong("api_version", API_VERSION); + FCGI_JSONBool("logged_in", FCGI_HasControl(context, getenv("COOKIE_STRING"))); + FCGI_JSONPair("friendly_name", ""); //Sensor and actuator information if (ident_sensors) { @@ -287,6 +289,25 @@ void FCGI_BeginJSON(FCGIContext *context, StatusCodes status_code) FCGI_JSONPair("control_state", Control_GetModeName()); } +/** + * Generic accept response in JSON format. + * @param context The context to work in + * @param description A short description. + * @param cookie Optional. If given, the cookie field is set to that value. + */ +void FCGI_AcceptJSON(FCGIContext *context, const char *description, const char *cookie) +{ + printf("Content-type: application/json; charset=utf-8\r\n"); + if (cookie) { + printf("Set-Cookie: %s\r\n", cookie); + } + printf("\r\n{\r\n"); + printf("\t\"module\" : \"%s\"", context->current_module); + FCGI_JSONLong("status", STATUS_OK); + FCGI_JSONPair("description", description); + FCGI_EndJSON(); +} + /** * Adds a key/value pair to a JSON response. The response must have already * been initiated by FCGI_BeginJSON. Special characters are not escaped. @@ -441,17 +462,17 @@ void * FCGI_RequestLoop (void *data) while (FCGI_Accept() >= 0) { ModuleHandler module_handler = NULL; - char module[BUFSIZ], params[BUFSIZ], cookie[BUFSIZ]; + char module[BUFSIZ], params[BUFSIZ]; + //Don't need to copy if we're not modifying string contents + const char *cookie = getenv("COOKIE_STRING"); //strncpy doesn't zero-truncate properly snprintf(module, BUFSIZ, "%s", getenv("DOCUMENT_URI_LOCAL")); snprintf(params, BUFSIZ, "%s", getenv("QUERY_STRING")); - snprintf(cookie, BUFSIZ, "%s", getenv("COOKIE_STRING")); Log(LOGDEBUG, "Got request #%d - Module %s, params %s", context.response_number, module, params); Log(LOGDEBUG, "Cookie: %s", cookie); - //Remove trailing slashes (if present) from module query size_t lastchar = strlen(module) - 1; @@ -486,11 +507,9 @@ void * FCGI_RequestLoop (void *data) context.current_module = module; context.response_number++; - - if (module_handler) { - if (module_handler != Login_Handler) + if (module_handler != Login_Handler && module_handler != IdentifyHandler) { if (cookie[0] == '\0') { diff --git a/server/fastcgi.h b/server/fastcgi.h index 74468ae..aad2421 100644 --- a/server/fastcgi.h +++ b/server/fastcgi.h @@ -60,6 +60,7 @@ extern bool FCGI_HasControl(FCGIContext *context, const char *key); extern char *FCGI_KeyPair(char *in, const char **key, const char **value); extern bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], size_t count); extern void FCGI_BeginJSON(FCGIContext *context, StatusCodes status_code); +extern void FCGI_AcceptJSON(FCGIContext *context, const char *description, const char *cookie); extern void FCGI_JSONPair(const char *key, const char *value); extern void FCGI_JSONLong(const char *key, long value); extern void FCGI_JSONDouble(const char *key, double value); diff --git a/server/login.c b/server/login.c index 2aa702d..ddacdd9 100644 --- a/server/login.c +++ b/server/login.c @@ -164,7 +164,7 @@ void Login_Handler(FCGIContext * context, char * params) if (context->control_key[0] != '\0') { - FCGI_RejectJSON(context, "Already logged in."); + FCGI_RejectJSON(context, "Someone is already logged in."); return; } @@ -247,14 +247,12 @@ void Login_Handler(FCGIContext * context, char * params) if (!authenticated) { - FCGI_RejectJSON(context, "Authentication failure."); - return; + FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED, "Authentication failure."); + } + else + { + FCGI_LockControl(context, false); + // Give the user a cookie + FCGI_AcceptJSON(context, "Logged in", context->control_key); } - - FCGI_LockControl(context, false); - - // Give the user a cookie - FCGI_PrintRaw("Content-type: text\r\n"); - FCGI_PrintRaw("Set-Cookie: %s\r\n\r\n", context->control_key); - FCGI_PrintRaw("Logged in"); } diff --git a/testing/MCTXWeb/public_html/index.html b/testing/MCTXWeb/public_html/index.html index 7953e06..7a62aa2 100644 --- a/testing/MCTXWeb/public_html/index.html +++ b/testing/MCTXWeb/public_html/index.html @@ -14,7 +14,7 @@