Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1569 from foglamp/1.6.0RC
Browse files Browse the repository at this point in the history
1.6.0RC to master
  • Loading branch information
praveen-garg authored May 23, 2019
2 parents e55a6cb + 1dffabd commit bbe7409
Show file tree
Hide file tree
Showing 309 changed files with 5,024 additions and 3,148 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ data/etc/storage.json
data/etc/certs/*
data/var
data/tmp
data/scripts
data/plugins
data/snapshots

# SQLite3 default db location and after migration
data/*.db
Expand Down
83 changes: 19 additions & 64 deletions C/common/config_category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <time.h>
#include <stdlib.h>
#include <logger.h>
#include <stdexcept>


using namespace std;
using namespace rapidjson;
Expand Down Expand Up @@ -98,8 +100,8 @@ string ConfigCategoryDescription::toJSON() const
{
ostringstream convert;

convert << "{\"key\": \"" << m_name << "\", ";
convert << "\"description\" : \"" << m_description << "\"}";
convert << "{\"key\": \"" << JSONescape(m_name) << "\", ";
convert << "\"description\" : \"" << JSONescape(m_description) << "\"}";

return convert.str();
}
Expand Down Expand Up @@ -756,8 +758,8 @@ string ConfigCategory::toJSON(const bool full) const
{
ostringstream convert;

convert << "{ \"key\" : \"" << m_name << "\", ";
convert << "\"description\" : \"" << m_description << "\", \"value\" : ";
convert << "{ \"key\" : \"" << JSONescape(m_name) << "\", ";
convert << "\"description\" : \"" << JSONescape(m_description) << "\", \"value\" : ";
// Add items
convert << ConfigCategory::itemsToJSON(full);
convert << " }";
Expand Down Expand Up @@ -924,7 +926,7 @@ ConfigCategory::CategoryItem::CategoryItem(const string& name,
// use current string
strbuf.GetString() :
// Unescape the string
this->unescape(strbuf.GetString());
JSONunescape(strbuf.GetString());

// If it's not a real eject, check the string buffer it is:
if (!item["value"].IsObject())
Expand Down Expand Up @@ -1025,7 +1027,7 @@ ConfigCategory::CategoryItem::CategoryItem(const string& name,
// use current string
strbuf.GetString() :
// Unescape the string
this->unescape(strbuf.GetString());
JSONunescape(strbuf.GetString());

// If it's not a real eject, check the string buffer it is:
if (!item["default"].IsObject())
Expand Down Expand Up @@ -1185,8 +1187,8 @@ string ConfigCategory::CategoryItem::toJSON(const bool full) const
{
ostringstream convert;

convert << "\"" << m_name << "\" : { ";
convert << "\"description\" : \"" << m_description << "\", ";
convert << "\"" << JSONescape(m_name) << "\" : { ";
convert << "\"description\" : \"" << JSONescape(m_description) << "\", ";
if (! m_displayName.empty())
{
convert << "\"displayName\" : \"" << m_displayName << "\", ";
Expand All @@ -1208,8 +1210,8 @@ ostringstream convert;
m_itemType == BoolItem ||
m_itemType == EnumerationItem)
{
convert << "\"value\" : \"" << m_value << "\", ";
convert << "\"default\" : \"" << m_default << "\"";
convert << "\"value\" : \"" << JSONescape(m_value) << "\", ";
convert << "\"default\" : \"" << JSONescape(m_default) << "\"";
}
else if (m_itemType == JsonItem ||
m_itemType == NumberItem ||
Expand Down Expand Up @@ -1270,8 +1272,8 @@ string ConfigCategory::CategoryItem::defaultToJSON() const
{
ostringstream convert;

convert << "\"" << m_name << "\" : { ";
convert << "\"description\" : \"" << m_description << "\", ";
convert << "\"" << JSONescape(m_name) << "\" : { ";
convert << "\"description\" : \"" << JSONescape(m_description) << "\", ";
convert << "\"type\" : \"" << m_type << "\"";

if (!m_order.empty())
Expand Down Expand Up @@ -1319,7 +1321,7 @@ ostringstream convert;
m_itemType == EnumerationItem ||
m_itemType == BoolItem)
{
convert << ", \"default\" : \"" << m_default << "\" }";
convert << ", \"default\" : \"" << JSONescape(m_default) << "\" }";
}
/**
* NOTE:
Expand All @@ -1337,7 +1339,7 @@ ostringstream convert;
m_itemType == DoubleItem ||
m_itemType == ScriptItem)
{
convert << ", \"default\" : \"" << escape(m_default) << "\" }";
convert << ", \"default\" : \"" << JSONescape(m_default) << "\" }";
}
return convert.str();
}
Expand Down Expand Up @@ -1366,8 +1368,8 @@ string DefaultConfigCategory::toJSON() const
ostringstream convert;

convert << "{ ";
convert << "\"key\" : \"" << m_name << "\", ";
convert << "\"description\" : \"" << m_description << "\", \"value\" : ";
convert << "\"key\" : \"" << JSONescape(m_name) << "\", ";
convert << "\"description\" : \"" << JSONescape(m_description) << "\", \"value\" : ";
// Add items
convert << DefaultConfigCategory::itemsToJSON();
convert << " }";
Expand Down Expand Up @@ -1396,20 +1398,6 @@ ostringstream convert;
return convert.str();
}

std::string ConfigCategory::CategoryItem::escape(const std::string& subject) const
{
size_t pos = 0;
string replace("\\\"");
string escaped = subject;

while ((pos = escaped.find("\"", pos)) != std::string::npos)
{
escaped.replace(pos, 1, replace);
pos += replace.length();
}
return escaped;
}

/**
* Return JSON string of a category item
* @param itemName The given item within current category
Expand All @@ -1433,39 +1421,6 @@ string ConfigCategory::itemToJSON(const string& itemName) const
return convert.str();
}

/**
* Return unescaped version of a JSON string
*
* Routine removes \" inside the string
* and leading and trailing "
*
* @param subject Input string
* @return Unescaped string
*/
std::string ConfigCategory::CategoryItem::unescape(const std::string& subject) const
{
size_t pos = 0;
string replace("");
string json = subject;

// Replace '\"' with '"'
while ((pos = json.find("\\\"", pos)) != std::string::npos)
{
json.replace(pos, 1, "");
}
// Remove leading '"'
if (json[0] == '\"')
{
json.erase(0, 1);
}
// Remove trainling '"'
if (json[json.length() - 1] == '\"')
{
json.erase(json.length() - 1, 1);
}
return json;
}

/**
* Configuration Category constructor
*
Expand Down Expand Up @@ -1507,7 +1462,7 @@ ConfigCategoryChange::ConfigCategoryChange(const string& json)
catch (exception* e)
{
Logger::getLogger()->error("Configuration parse error in category %s item '%s', %s: %s",
m_name,
m_name.c_str(),
itr->name.GetString(),
json.c_str(),
e->what());
Expand Down
3 changes: 1 addition & 2 deletions C/common/include/config_category.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <string>
#include <vector>
#include <rapidjson/document.h>
#include <json_utils.h>

class ConfigCategoryDescription {
public:
Expand Down Expand Up @@ -119,8 +120,6 @@ class ConfigCategory {
std::string toJSON(const bool full=false) const;
// Return only "default" items
std::string defaultToJSON() const;
std::string escape(const std::string& str) const;
std::string unescape(const std::string& subject) const;

public:
std::string m_name;
Expand Down
3 changes: 3 additions & 0 deletions C/common/include/json_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ bool JSONStringToVectorString(std::vector<std::string>& vectorString,
const std::string& JSONString,
const std::string& Key);

std::string JSONescape(const std::string& subject);
std::string JSONunescape(const std::string& subject);

#endif
5 changes: 3 additions & 2 deletions C/common/include/management_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <map>
#include <rapidjson/document.h>
#include <asset_tracking.h>
#include <json_utils.h>
#include <thread>

using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;
Expand Down Expand Up @@ -71,8 +72,8 @@ class ManagementClient {

// Build the JSON payload
std::ostringstream payload;
payload << "{ \"key\" : \"" << t.getName();
payload << "\", \"description\" : \"" << t.getDescription();
payload << "{ \"key\" : \"" << JSONescape(t.getName());
payload << "\", \"description\" : \"" << JSONescape(t.getDescription());
payload << "\", \"value\" : " << t.itemsToJSON();

/**
Expand Down
47 changes: 47 additions & 0 deletions C/common/json_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,50 @@ bool JSONStringToVectorString(std::vector<std::string>& vectorString,

return success;
}

string JSONescape(const std::string& subject)
{
size_t pos = 0;
string replace("\\\"");
string escaped = subject;

while ((pos = escaped.find("\"", pos)) != std::string::npos)
{
escaped.replace(pos, 1, replace);
pos += replace.length();
}
return escaped;
}
/**
* Return unescaped version of a JSON string
*
* Routine removes \" inside the string
* and leading and trailing "
*
* @param subject Input string
* @return Unescaped string
*/
std::string JSONunescape(const std::string& subject)
{
size_t pos = 0;
string replace("");
string json = subject;

// Replace '\"' with '"'
while ((pos = json.find("\\\"", pos)) != std::string::npos)
{
json.replace(pos, 1, "");
}
// Remove leading '"'
if (json[0] == '\"')
{
json.erase(0, 1);
}
// Remove trainling '"'
if (json[json.length() - 1] == '\"')
{
json.erase(json.length() - 1, 1);
}
return json;
}

6 changes: 3 additions & 3 deletions C/common/management_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ ostringstream convert;
return true;
}
try {
convert << "{ \"category\" : \"" << category << "\", ";
convert << "{ \"category\" : \"" << JSONescape(category) << "\", ";
convert << "\"service\" : \"" << *m_uuid << "\" }";
auto res = this->getHttpClient()->request("POST", "/foglamp/interest", convert.str());
Document doc;
Expand Down Expand Up @@ -483,7 +483,7 @@ string ManagementClient::addChildCategories(const string& parentCategory,

for (auto it = children.begin(); it != children.end(); ++it)
{
payload += "\"" + (*it)+ "\"";
payload += "\"" + JSONescape((*it)) + "\"";
if ((it + 1) != children.end())
{
payload += ", ";
Expand Down Expand Up @@ -595,7 +595,7 @@ bool ManagementClient::addAssetTrackingTuple(const std::string& service,
ostringstream convert;

try {
convert << "{ \"service\" : \"" << service << "\", ";
convert << "{ \"service\" : \"" << JSONescape(service) << "\", ";
convert << " \"plugin\" : \"" << plugin << "\", ";
convert << " \"asset\" : \"" << asset << "\", ";
convert << " \"event\" : \"" << event << "\" }";
Expand Down
3 changes: 2 additions & 1 deletion C/common/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ FogLampProcess::FogLampProcess(int argc, char** argv) :
signal(SIGABRT, handler);

string myName = LOG_SERVICE_NAME;
m_logger = new Logger(myName);

try
{
Expand All @@ -101,6 +100,8 @@ FogLampProcess::FogLampProcess(int argc, char** argv) :
{
throw runtime_error(string("Error while parsing required options: ") + e.what());
}
myName = m_name;
m_logger = new Logger(myName);

if (m_core_mngt_host.empty())
{
Expand Down
Loading

0 comments on commit bbe7409

Please sign in to comment.