-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestapi.cpp
More file actions
101 lines (83 loc) · 3.81 KB
/
Copy pathrestapi.cpp
File metadata and controls
101 lines (83 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <string.h>
#include <boost/foreach.hpp>
#include "restapi.hpp"
#include "strutil.hpp"
using namespace ourapi;
struct validate_data
{
string api;
set <string>* params;
};
api::api()
{
set<string> params;
string sysinfoparams[] = {"cpus", "memory", "os"};
string processinfoparams[] = {"percentmemory", "percentcpu" };
string diskinfoparamas[] = {"totalparts", "spaceinfo" };
_apiparams["/sysinfo"] = set<string>(sysinfoparams, sysinfoparams + 3);
_apiparams["/procinfo"] = set<string>(processinfoparams, processinfoparams + 2);
_apiparams["/diskinfo"] = set<string>(diskinfoparamas, diskinfoparamas + 2);
}
bool api::executeAPI(const string& url, const map<string, string>& argvals, string& response)
{
// Ignore all the args except the "fields" param
validate_data vdata ;
vdata.api = url;
Executor::outputType type = Executor::TYPE_JSON;
vector<string> params;
set<string> uniqueparams;
map<string,string>::const_iterator it1 = argvals.find("fields");
if (it1 != argvals.end()) {
string prms = it1->second;
StrUtil::eraseWhiteSpace(prms);
StrUtil::splitString(prms, ",", params);
}
BOOST_FOREACH( string pr, params ) {
uniqueparams.insert(pr);
}
vdata.params = &uniqueparams;
if ( !_validate(&vdata)) {
_getInvalidResponse(response);
return false;
}
it1 = argvals.find("type");
if (it1 != argvals.end()){
const string outputtype = it1->second;
if (strcasecmp(outputtype.c_str(), "xml") == 0 ) {
type = Executor::TYPE_XML;
}
}
return _executeAPI(url, uniqueparams, type, response);
}
bool api::_executeAPI(const string& url, const set<string>& argvals,
Executor::outputType type, string& response)
{
bool ret = false;
if (url == "/sysinfo")
ret = _executor.sysinfo(argvals, type, response);
if (url == "/diskinfo")
ret = _executor.diskinfo(argvals, type, response);
if (url == "/procinfo")
ret = _executor.procinfo(argvals, type, response);
return ret;
}
bool api::_validate(const void *data)
{
const validate_data *vdata = static_cast<const validate_data *>(data );
map<string, set<string> > ::iterator it = _apiparams.find(vdata->api);
it = _apiparams.find(vdata->api);
if ( it == _apiparams.end()){
return false;
}
set<string>::iterator it2 = vdata->params->begin();
while (it2 != vdata->params->end()) {
if (it->second.find(*it2) == it->second.end())
return false;
++it2;
}
return true;
}
void api::_getInvalidResponse(string& response)
{
response = "Some error in your data ";
}