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) {
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.
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;
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')
{
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);
if (context->control_key[0] != '\0')
{
- FCGI_RejectJSON(context, "Already logged in.");
+ FCGI_RejectJSON(context, "Someone is already logged in.");
return;
}
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");
}
<link rel="stylesheet" type="text/css" href="static/style.css">
<link rel="stylesheet" type="text/css" href="static/nav-menu.css">
<script type="text/javascript">
- //runBeforeLoad();
+ runBeforeLoad();
$(document).ready(function () {
//$("#menu-container").populateNavbar();
$("#login").submit(function () {
*/
mctx = {};
-mctx.location = location.protocol + "//" + location.host + "/";
-mctx.api = mctx.location + "/api/";
+//Don't use this in the final version
+mctx.location = window.location.pathname;
+mctx.location = mctx.location.substring(0, mctx.location.lastIndexOf('/')) + "/";
+//mctx.location = location.protocol + "//" + location.host + "/";
+mctx.api = location.protocol + "//" + location.host + "/" + "api/";
mctx.expected_api_version = 0;
mctx.has_control = false;
+//mctx.debug = true;
-mctx.return_codes = {
+mctx.statusCodes = {
+ STATUS_OK : 1
+}
+
+mctx.statusCodesDescription = {
"1" : "Ok",
"-1" : "General error",
"-2" : "Unauthorized",
$.ajax({
url : mctx.api + "identify"
}).done(function (data) {
- if (data.logged_in && isLoginPage) {
+ if (mctx.debug) {
+ debugLog("Redirect disabled!");
+ } else if (data.logged_in && isLoginPage) {
window.location = mctx.location;
} else if (!data.logged_in && !isLoginPage) {
//Note: this only clears the nameless cookie
window.location = mctx.location + "login.html";
} else {
mctx.friendlyName = data.friendly_name;
+ $("#content").css("display", "block");
}
}).fail(function (jqHXR) {
if (!isLoginPage) {
url : mctx.api + "bind",
data : {user: username, pass : password}
}).done(function (data) {
- //todo: error check
- mctx.has_control = true;
- out.attr("class", "pass");
- out.text("Login ok!");
- setTimeout(redirect, 1000);
+ if (data.status < 0) {
+ mctx.has_control = false;
+ out.attr("class", "fail");
+ out.text("Login failed: " + data.description);
+ } else {
+ //todo: error check
+ mctx.has_control = true;
+ out.attr("class", "pass");
+ out.text("Login ok!");
+ setTimeout(redirect, 800);
+ }
}).fail(function (jqXHR) {
mctx.has_control = false;
out.attr("class", "fail");
#content {
padding: 1em 2em;
+ /* display: none; Enable in non-debug mode*/
}
#sidebar{