static int auth_handler(struct mg_connection *conn) { int result = 0; // Not authorized FILE *fp; // To populate passwords file, do // mongoose -A my_passwords.txt mydomain.com admin admin if ((fp = fopen("my_passwords.txt", "r")) != NULL) { result = mg_authorize_digest(conn, fp); fclose(fp); } return result; }
static int server_callback(struct mg_connection *c, enum mg_event ev) { int result = MG_FALSE; FILE *fp = NULL; switch(ev) { case MG_CLOSE: mpd_close_handler(c); return MG_TRUE; case MG_REQUEST: if (c->is_websocket) { c->content[c->content_len] = '\0'; if(c->content_len) return callback_mpd(c); else return MG_TRUE; } else #ifdef WITH_DYNAMIC_ASSETS return MG_FALSE; #else return callback_http(c); #endif case MG_AUTH: // no auth for websockets since mobile safari does not support it if ( (gpass == NULL) || (c->is_websocket) ) return MG_TRUE; else { if ( (fp = fopen(gpass, "r")) != NULL ) { result = mg_authorize_digest(c, fp); fclose(fp); } } return result; default: return MG_FALSE; } }
/** * Callback function which handles all HTTP requests. */ static int handle_http(struct mg_connection *conn) { char *url = (char *) &conn->uri[1]; // exclude the starting '/' Item *item; ACSIServer *acsiServer = (ACSIServer *) conn->server_param; #ifdef USE_SSL #if USE_HTTP_AUTH == 1 static const char *passwords_file = "htpasswd.txt"; FILE *fp = fopen(passwords_file, "r"); if (fp == NULL || !mg_authorize_digest(conn, fp)) { mg_send_digest_auth_request(conn); return 1; } if (fp != NULL) { fclose(fp); } #endif #endif // check for method names in url if (strcmp(conn->request_method, "GET") == 0) { int len = 0; char printBuf[ACSI_RESPONSE_MAX_SIZE]; if (strncmp(url, ACSI_GET_DEFINITION, strlen(ACSI_GET_DEFINITION)) == 0) { item = getItemFromPath(acsiServer->iedName, (char *) &url[strlen(ACSI_GET_DEFINITION) + 1]); len = itemDescriptionTreeToJSON(printBuf, item, FALSE); } else if (strncmp(url, ACSI_GET_DIRECTORY, strlen(ACSI_GET_DIRECTORY)) == 0) { item = getItemFromPath(acsiServer->iedName, (char *) &url[strlen(ACSI_GET_DIRECTORY) + 1]); len = itemDescriptionTreeToJSON(printBuf, item, TRUE); } // else if (strncmp(url, "scd", strlen("scd")) == 0) { // mg_send_header(conn, "Content-Type", "application/xml"); // mg_send_data(conn, scd_file, strlen(scd_file)); // } else if (strncmp(url, ACSI_ASSOCIATE, strlen(ACSI_ASSOCIATE)) == 0) { acsiServer->clients = addClient(acsiServer->clients, conn->remote_ip, conn->remote_port); mg_send_data(conn, ACSI_OK, strlen(ACSI_OK)); return 1; } else if (strncmp(url, ACSI_RELEASE, strlen(ACSI_RELEASE)) == 0) { // TODO raise flag here, rather than remove? acsiServer->clients = removeClientByConnection(acsiServer->clients, conn->remote_ip, conn->remote_port); mg_send_data(conn, ACSI_OK, strlen(ACSI_OK)); return 1; } else if (strncmp(url, ACSI_ABORT, strlen(ACSI_ABORT)) == 0) { acsiServer->clients = removeClientByConnection(acsiServer->clients, conn->remote_ip, conn->remote_port); mg_send_data(conn, ACSI_OK, strlen(ACSI_OK)); return 1; } else { item = getItemFromPath(acsiServer->iedName, (char *) url); len = itemTreeToJSON(printBuf, item); } if (item != NULL && len == -1) { mg_send_status(conn, 500); mg_send_data(conn, ACSI_BUFFER_OVERRUN, strlen(ACSI_BUFFER_OVERRUN)); return 1; } else if (item != NULL && len > 0) { #if ACSI_AUTO_ASSOCIATE == 1 acsiServer->clients = addClient(acsiServer->clients, conn->remote_ip, conn->remote_port); #endif mg_send_header(conn, "Content-Type", "application/json"); mg_send_header(conn, "Cache-Control", "no-cache"); mg_send_header(conn, "Access-Control-Allow-Origin", "*"); mg_send_data(conn, printBuf, len); // printf("len: %d\n", len); // fflush(stdout); return 1; } } else if (strcmp(conn->request_method, "POST") == 0) { item = getItemFromPath(acsiServer->iedName, (char *) url); int setReturn = setItem(item, conn->content, conn->content_len); // printf("content: %.*s\n", conn->content_len, conn->content); // fflush(stdout); if (setReturn > 0) { mg_send_header(conn, "Cache-Control", "no-cache"); mg_send_header(conn, "Access-Control-Allow-Origin", "*"); mg_send_data(conn, ACSI_OK, strlen(ACSI_OK)); } else { mg_send_data(conn, ACSI_NOT_POSSIBLE, strlen(ACSI_NOT_POSSIBLE)); } return 1; } // by default, return 404 mg_send_status(conn, 404); mg_send_data(conn, ACSI_NOT_FOUND, strlen(ACSI_NOT_FOUND)); return 1; }