pvs studio suggestions

This commit is contained in:
DarthArgus
2016-09-26 06:24:20 +00:00
parent 06925a9219
commit 2b8a8fdd03
2 changed files with 71 additions and 72 deletions
+39 -40
View File
@@ -1,17 +1,17 @@
/*
* Version: 1.3
*
*
* This code is just a simple wrapper around nlohmann's wonderful json lib
* (https://github.com/nlohmann/json) and libcurl. While originally included directly,
* we have come to realize that we may require web API functionality elsewhere in the future.
*
*
* As such, and in an effort to keep the code clean, we've broken it out into this simple little
* namespace/lib that is easy to include. Just make sure to link against curl when including, and
* make all the cmake modifications required to properly use it.
*
*
* (c) stellabellum/swgilluminati (combined crews), written by DA with help from DC
* based on the original prototype by parz1val
*
*
* License: what's a license? we're a bunch of dirty pirates!
*/
@@ -19,17 +19,17 @@
using namespace StellaBellum;
webAPI::webAPI(std::string endpoint, std::string userAgent) : uri(endpoint), userAgent(userAgent) {}
webAPI::webAPI(std::string endpoint, std::string userAgent) : uri(endpoint), userAgent(userAgent), statusCode(0) {}
webAPI::~webAPI()
{
this->requestData.clear();
this->responseData.clear();
}
bool webAPI::setEndpoint(std::string endpoint)
bool webAPI::setEndpoint(const std::string endpoint)
{
this->uri = endpoint;
return true;
}
@@ -40,26 +40,25 @@ std::string webAPI::getRaw()
bool webAPI::setData(std::string &data)
{
if (!data.empty())
if (!data.empty())
{
this->sRequest = data;
return true;
}
return false;
}
bool webAPI::submit(const int &reqType, const int &getPost, const int &respType)
{
{
if (reqType == 0) // json request
{
if (!this->requestData.is_null())
if (!this->requestData.is_null())
{
// serialize our data into sRequest
this->sRequest = this->requestData.dump();
// clear our the object for next time
this->requestData.clear();
}
@@ -68,48 +67,48 @@ bool webAPI::submit(const int &reqType, const int &getPost, const int &respType)
return false;
}
}
if (fetch(getPost, respType) && !(this->sResponse.empty()))
if (fetch(getPost, respType) && !(this->sResponse.empty()))
{
return true;
}
this->sResponse.clear();
return false;
}
bool webAPI::fetch(const int &getPost, const int &mimeType) // 0 for json 1 for string
{
bool fetchStatus = false;
if (!uri.empty()) //data is allowed to be an empty string if we're doing a normal GET
{
CURL *curl = curl_easy_init(); // start up curl
if (curl)
{
std::string readBuffer; // container for the remote response
struct curl_slist *slist = nullptr;
// set the content type
if (mimeType == 0)
if (mimeType == 0)
{
slist = curl_slist_append(slist, "Accept: application/json");
slist = curl_slist_append(slist, "Content-Type: application/json");
slist = curl_slist_append(slist, "Content-Type: application/json");
}
else
{
slist = curl_slist_append(slist, "Content-Type: application/x-www-form-urlencoded");
}
slist = curl_slist_append(slist, "charsets: utf-8");
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback); // place the data into readBuffer using writeCallback
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); // specify readBuffer as the container for data
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
switch (getPost)
{
case 0:
@@ -121,19 +120,19 @@ bool webAPI::fetch(const int &getPost, const int &mimeType) // 0 for json 1 for
break;
// want to do a put, or whatever other type? feel free to add here
}
CURLcode res = curl_easy_perform(curl); // make the request!
char * contentType;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &this->statusCode); //get status code
curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &contentType); // get response mime type
std::string conType(contentType);
if (res == CURLE_OK && this->statusCode == 200 && !(readBuffer.empty())) // check it all out and parse
{
this->sResponse = readBuffer;
if (conType == "application/json")
{
fetchStatus = this->processJSON();
@@ -144,18 +143,18 @@ bool webAPI::fetch(const int &getPost, const int &mimeType) // 0 for json 1 for
fetchStatus = true;
}
}
curl_slist_free_all(slist);
curl_easy_cleanup(curl); // always wipe our butt
}
}
if (!fetchStatus)
{
this->sResponse.clear();
this->responseData.clear();
}
return fetchStatus;
}
@@ -170,17 +169,17 @@ bool webAPI::processJSON()
{
if (!(this->sResponse.empty())) // check it all out and parse
{
try
try
{
this->responseData = nlohmann::json::parse(this->sResponse);
return true;
}
catch (std::string &e)
catch (std::string &e)
{
this->responseData["message"] = e;
this->responseData["status"] = "failure";
}
catch (...)
catch (...)
{
this->responseData["message"] = "JSON parse error for endpoint.";
this->responseData["status"] = "failure";
@@ -191,6 +190,6 @@ bool webAPI::processJSON()
this->responseData["message"] = "Error fetching data from remote.";
this->responseData["status"] = "failure";
}
return false;
}
}
+32 -32
View File
@@ -1,17 +1,17 @@
/*
* Version: 1.3
*
*
* This code is just a simple wrapper around nlohmann's wonderful json lib
* (https://github.com/nlohmann/json) and libcurl. While originally included directly,
* we have come to realize that we may require web API functionality elsewhere in the future.
*
*
* As such, and in an effort to keep the code clean, we've broken it out into this simple little
* namespace/lib that is easy to include. Just make sure to link against curl when including, and
* make all the cmake modifications required to properly use it.
*
*
* (c) stellabellum/swgilluminati (combined crews), written by DA with help from DC
* based on the original prototype by parz1val
*
*
* License: what's a license? we're a bunch of dirty pirates!
*/
@@ -30,76 +30,76 @@ namespace StellaBellum
{
class webAPI
{
public:
public:
// useragent
std::string userAgent;
// constructor - can setup with the endpoint from the start
webAPI(std::string endpoint, std::string userAgent = "StellaBellum webAPI");
~webAPI();
// submits the request - respType 0 is json, else raw, get = 0 post = 1
bool submit(const int &reqType = 0, const int &getPost = 1, const int &respType = 0);
// set the endpoint after object creation...or change the target if needed
bool setEndpoint(std::string endpoint);
bool setEndpoint(const std::string endpoint);
// get raw response
std::string getRaw();
// set a standard request string
bool setData(std::string &data); // all or nothing
// set json key and value for request
template<typename T> bool addJsonData(std::string key, T value)
{
template<typename T> bool addJsonData(std::string key, T value)
{
if (!key.empty())
{
this->requestData[key] = value;
this->requestData[key] = value;
return true;
}
return false;
}
// get json response slot
template<typename T> T getRespValue(std::string slot)
{
template<typename T> T getRespValue(std::string slot)
{
if (!this->responseData.is_null() && !slot.empty() && this->responseData.count(slot))
{
return this->responseData[slot].get<T>();
}
return T(); // empty value of T
}
private:
private:
// json request data - object is serialized before sending, used with above setter template
nlohmann::json requestData;
// json response, stored so we can use the getter template above
nlohmann::json responseData;
// raw response
std::string sResponse;
// raw request string
std::string sRequest;
// API endpoint
std::string uri;
// fetcher - returns raw response direct from remote - 0 for get, 1 for post, 0 for json mime, 1 for standard
bool fetch(const int &getPost = 1, const int &mimeType = 0);
// cURL writeback callback
static size_t writeCallback(void *contents, size_t size, size_t nmemb, void *userp);
// json processor - string to json
bool processJSON();
protected:
// http response code (200, 404, etc)
protected:
// http response code (200, 404, etc)
long statusCode;
};
}