Changeset - 8780e3a1d0af
[Not reviewed]
0 1 0
Vitaly Takmazov - 7 years ago 2018-09-18 19:06:31
vitalyster@gmail.com
fix compatibility with gcc 4.9
1 file changed with 6 insertions and 6 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/APIServer.cpp
Show inline comments
 
@@ -66,49 +66,49 @@ void APIServer::send_json(struct mg_connection *conn, const Json::Value &d) {
 
			"\r\n"
 
			"%s",
 
			(int) json.size(), json.c_str());
 
}
 

	
 
void APIServer::send_ack(struct mg_connection *conn, bool error, const std::string &message) {
 
	Json::Value json;
 
	json["error"] = error;
 
	json["message"] = message;
 

	
 
	send_json(conn, json);
 
}
 

	
 
void APIServer::serve_instances(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	// rapidjson stores const char* pointer to status, so we have to keep
 
	// the std::string stored out of BOOST_FOREACH scope, otherwise the
 
	// const char * returned by c_str() would be invalid during send_json.
 
	std::vector<std::string> statuses;
 
	std::vector<std::string> usernames;
 
	std::vector<std::string> list = show_list(m_config, false);
 

	
 
	Json::Value json;
 
	json["error"] = 0;
 

	
 
	Json::Value instances(Json::ValueType::arrayValue);
 
	Json::Value instances(Json::arrayValue);
 
	BOOST_FOREACH(std::string &id, list) {
 
		Json::Value instance;
 
		instance["id"] = id;
 

	
 
		std::string name = get_config(m_config, id, "identity.name");
 
		if (name.empty() || name == "Spectrum 2 Transport") {
 
			instance["name"] = id;
 
		}
 
		else {
 
			statuses.push_back(name);
 
			instance["name"] = statuses.back();
 
		}
 

	
 
		std::string status = server->send_command(id, "status");
 
		if (status.empty()) {
 
			status = "Cannot get the instance status.";
 
		}
 

	
 
		statuses.push_back(status);
 
		instance["status"] = statuses.back();
 

	
 
		bool running = true;
 
		if (status.find("Running") == std::string::npos) {
 
			running = false;
 
@@ -269,49 +269,49 @@ void APIServer::serve_instances_register(Server *server, Server::session *sessio
 
// 	json.AddMember("password_label", fields.size() >= 3 ? StringRef(fields[2].c_str()) : "", json.GetAllocator());
 
// 	send_json(conn, json);
 
// }
 

	
 
void APIServer::serve_instances_commands(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	std::string uri(hm->uri.p, hm->uri.len);
 
	std::string instance = uri.substr(uri.rfind("/") + 1);
 

	
 
	UserInfo info;
 
	m_storage->getUser(session->user, info);
 

	
 
	std::string username = "";
 
	int type = (int) TYPE_STRING;
 
	m_storage->getUserSetting(info.id, instance, type, username);
 

	
 
	std::string response = server->send_command(instance, "commands");
 

	
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 

	
 
	Json::Value json;
 
	json["error"] = 0;
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Json::Value cmds(Json::ValueType::arrayValue);
 
	Json::Value cmds(Json::arrayValue);
 
	BOOST_FOREACH(const std::string &command, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(command, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 11) {
 
			continue;
 
		}
 

	
 
		if (!session->admin && tokens[6] == "Admin") {
 
			continue;
 
		}
 

	
 
		// Skip command which needs registered users.
 
		if (!session->admin && username.empty() && tokens[8] == "User") {
 
			continue;
 
		}
 

	
 

	
 
		// Skip 'register' command when user is registered.
 
@@ -333,49 +333,49 @@ void APIServer::serve_instances_commands(Server *server, Server::session *sessio
 
	json["commands"] = cmds;
 
	send_json(conn, json);
 
}
 

	
 
void APIServer::serve_instances_variables(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	std::string uri(hm->uri.p, hm->uri.len);
 
	std::string instance = uri.substr(uri.rfind("/") + 1);
 

	
 
	UserInfo info;
 
	m_storage->getUser(session->user, info);
 

	
 
	std::string username = "";
 
	int type = (int) TYPE_STRING;
 
	m_storage->getUserSetting(info.id, instance, type, username);
 

	
 
	std::string response = server->send_command(instance, "variables");
 

	
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 

	
 
	Json::Value json;
 
	json["error"] = 0;
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Json::Value cmds(Json::ValueType::arrayValue);
 
	Json::Value cmds(Json::arrayValue);
 
	BOOST_FOREACH(const std::string &command, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(command, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 13) {
 
			continue;
 
		}
 

	
 
		if (!session->admin && tokens[10] == "Admin") {
 
			continue;
 
		}
 

	
 
		tmp.push_back(tokens);
 

	
 
		Json::Value cmd;
 
		cmd["name"] = tokens[0];
 
		cmd["desc"] = tokens[2];
 
		cmd["value"] = tokens[4];
 
		cmd["read_only"] = tokens[6];
 
@@ -430,49 +430,49 @@ void APIServer::serve_instances_command_args(Server *server, Server::session *se
 

	
 
		found = true;
 
		break;
 
	}
 

	
 
	if (!found) {
 
		command = "unknown";
 
	}
 

	
 
	response = server->send_command(instance, "args " + command);
 
	if (response.find("Error:") == 0) {
 
		send_ack(conn, false, response);
 
		return;
 
	}
 

	
 
	
 

	
 
	std::vector<std::string> args;
 
	boost::split(args, response, boost::is_any_of("\n"));
 

	
 
	Json::Value json;
 
	json["error"] = 0;
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Json::Value argList(Json::ValueType::arrayValue);
 
	Json::Value argList(Json::arrayValue);
 

	
 
	if (userContext && session->admin) {
 
		Json::Value arg;
 
		arg["name"] = "username";
 
		arg["label"] = "Username";
 
		arg["example"] = "";
 
		argList.append(arg);
 
	}
 

	
 
	BOOST_FOREACH(const std::string &argument, args) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(argument, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 7) {
 
			continue;
 
		}
 

	
 
		tmp.push_back(tokens);
 

	
 
@@ -550,49 +550,49 @@ void APIServer::serve_instances_execute(Server *server, Server::session *session
 

	
 
		command += " " + username;
 
	}
 

	
 
	for (int i = 0; i < 10; ++i) {
 
		std::string var = get_http_var(hm, std::string(std::string("command_arg") + boost::lexical_cast<std::string>(i)).c_str());
 
		if (!var.empty()) {
 
			command += " " + var;
 
		}
 
	}
 

	
 
	response = server->send_command(instance, command);
 
	if (response.find("Error:") == 0) {
 
		send_ack(conn, false, response);
 
	}
 

	
 
	std::vector<std::string> fields;
 
	boost::split(fields, response, boost::is_any_of("\n"));
 
	if (!fields.empty() && /*fields[0].find(" - ") != std::string::npos &&*/ (fields[0].find(": ") != std::string::npos || fields[0].find(":\"") != std::string::npos)) {
 
		Json::Value json;
 
		json["error"] = 0;
 

	
 
		std::vector<std::string> tmp;
 
		std::vector<std::string> tmp2;
 
		Json::Value table(Json::ValueType::arrayValue);
 
		Json::Value table(Json::arrayValue);
 

	
 
		BOOST_FOREACH(const std::string &line, fields) {
 
			escaped_list_separator<char> els('\\', ' ', '\"');
 
			tokenizer<escaped_list_separator<char> > tok(line, els);
 

	
 
			Json::Value arg;
 

	
 
			std::string key;
 
			int i = 0;
 
			bool hasDesc = true;
 
			for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg, ++i) {
 
				if (i == 1 && *beg != "-") {
 
					hasDesc = false;
 
				}
 
				if (i == 0) {
 
					tmp.push_back(*beg);
 
					arg["Key"] = tmp.back();
 
				}
 
				else if (i == 2 && hasDesc) {
 
					tmp.push_back(*beg);
 
					arg["Description"] = tmp.back();
 
				}
 
				else if (i > 1 || (!hasDesc && i > 0)) {
 
					if (key.empty()) {
 
@@ -607,49 +607,49 @@ void APIServer::serve_instances_execute(Server *server, Server::session *session
 
				}
 
			}
 
			table.append(arg);
 
		}
 

	
 
		json["table"] = table;
 
		json["message"] = response;
 
		send_json(conn, json);
 
	}
 
	else {
 
		boost::replace_all(response, "\n", "<br/>");
 
		send_ack(conn, true, response);
 
	}
 
}
 

	
 
void APIServer::serve_users(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	ALLOW_ONLY_ADMIN();
 

	
 
	Json::Value json;
 
	json["error"] = 0;
 

	
 
	std::vector<std::string> list;
 
	m_storage->getUsers(list);
 

	
 
	Json::Value users(Json::ValueType::arrayValue);
 
	Json::Value users(Json::arrayValue);
 
	BOOST_FOREACH(std::string &id, list) {
 
		Json::Value user;
 
		user["username"] = id;
 
		users.append(user);
 
	}
 

	
 
	json["users"] = users;
 
	send_json(conn, json);
 
}
 

	
 
void APIServer::serve_users_add(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
 
	std::string user = get_http_var(hm, "username");
 
	std::string password = get_http_var(hm, "password");
 

	
 
	if (!user.empty() && !password.empty()) {
 
		if (m_storage) {
 
			UserInfo dummy;
 
			bool registered = m_storage->getUser(user, dummy);
 
			if (!registered) {
 
				UserInfo info;
 
				info.jid = user;
 
				info.password = password;
 
				m_storage->setUser(info);
 
			}
0 comments (0 inline, 0 general)