Update to use Libraries and fix include order

This commit is contained in:
2020-03-07 14:46:26 +01:00
parent 3b7491f57d
commit 157b1ceb51
32 changed files with 114 additions and 1405 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
#include <algorithm>
#include "Device.h"
#include <algorithm>
namespace PresenceDetection {
-36
View File
@@ -1,36 +0,0 @@
#ifndef UTIL_HELPERS_H
#define UTIL_HELPERS_H
#include <functional>
namespace PresenceDetection {
namespace Util {
namespace Helpers {
template <int... Is>
struct index {};
template <int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <int... Is>
struct gen_seq<0, Is...> : index<Is...> {};
template <typename ...FunctionArguments, typename ...Arguments, int... Is>
void execute(std::function<void(FunctionArguments...)> const & function, std::tuple<Arguments...>& tuple, index<Is...>)
{
function(std::get<Is>(tuple)...);
}
template <typename ...FunctionArguments, typename ...Arguments>
void execute(std::function<void(FunctionArguments...)> const & function, std::tuple<Arguments...>& tuple)
{
execute(function, tuple, gen_seq<sizeof...(Arguments)>{});
}
} // namespace Helpers
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_HELPERS_H
-630
View File
@@ -1,630 +0,0 @@
#include <fstream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <curl/curl.h>
#include <openssl/crypto.h>
#include "HttpClientHelpers.h"
#include "HttpClient.h"
namespace PresenceDetection {
namespace Util {
HttpClient::HttpClient()
{
curl_global_init(CURL_GLOBAL_ALL);
m_userAgents.push_back("Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.00");
InitializeLocks();
}
HttpClient::~HttpClient()
{
FreeLocks();
curl_global_cleanup();
}
std::string HttpClient::GetUrlContents(const std::string& url) const
{
std::string buffer;
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
error << "Data: " << buffer;
throw std::runtime_error(error.str());
}
return buffer;
}
void HttpClient::GetUrlSilent(const std::string& url) const
{
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
throw std::runtime_error(error.str());
}
}
std::string HttpClient::GetUrlContents(const std::string& url, const std::string& cookieFile) const
{
std::string buffer;
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
error << "Data: " << buffer;
throw std::runtime_error(error.str());
}
return buffer;
}
void HttpClient::GetUrlSilent(const std::string& url, const std::string& cookieFile) const
{
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
throw std::runtime_error(error.str());
}
}
std::string HttpClient::GetUrlPostContents(const std::string& url, const std::string& postData, const std::string& contentType) const
{
std::string buffer;
CURL* curl = curl_easy_init();
std::stringstream contentTypeString;
contentTypeString << "Content-Type: " << contentType;
struct curl_slist *list = NULL;
list = curl_slist_append(list, contentTypeString.str().c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
error << "Data: " << buffer;
throw std::runtime_error(error.str());
}
return buffer;
}
void HttpClient::GetUrlPostSilent(const std::string& url, const std::string& postData, const std::string& contentType) const
{
CURL* curl = curl_easy_init();
std::stringstream contentTypeString;
contentTypeString << "Content-Type: " << contentType;
struct curl_slist *list = NULL;
list = curl_slist_append(list, contentTypeString.str().c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
throw std::runtime_error(error.str());
}
}
std::string HttpClient::GetUrlPostContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const
{
std::string buffer;
CURL* curl = curl_easy_init();
std::stringstream contentTypeString;
contentTypeString << "Content-Type: " << contentType;
struct curl_slist *list = NULL;
list = curl_slist_append(list, contentTypeString.str().c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
error << "Data: " << buffer;
throw std::runtime_error(error.str());
}
return buffer;
}
void HttpClient::GetUrlPostSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const
{
CURL* curl = curl_easy_init();
std::stringstream contentTypeString;
contentTypeString << "Content-Type: " << contentType;
struct curl_slist *list = NULL;
list = curl_slist_append(list, contentTypeString.str().c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size());
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
throw std::runtime_error(error.str());
}
}
std::string HttpClient::GetUrlPostAttachmentContents(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const
{
std::string buffer;
std::string contents;
std::ifstream fileStream(filename, std::ios::in | std::ios::binary);
if (fileStream)
{
fileStream.seekg(0, std::ios::end);
contents.resize(fileStream.tellg());
fileStream.seekg(0, std::ios::beg);
fileStream.read(&contents[0], contents.size());
fileStream.close();
}
CURL* curl = curl_easy_init();
CURLcode result;
struct curl_httppost *formpost = nullptr;
struct curl_httppost *lastptr = nullptr;
struct curl_slist *headerlist = nullptr;
static const char headerBuffer[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "cache-control:",
CURLFORM_COPYCONTENTS, "no-cache",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "content-type:",
CURLFORM_COPYCONTENTS, "multipart/form-data",
CURLFORM_END);
std::vector<std::string> postTokens;
boost::split(postTokens, postData, boost::is_any_of("&"));
for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
{
std::vector<std::string> tokens;
boost::split(tokens, *it, boost::is_any_of("="));
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, tokens[0].c_str(),
CURLFORM_COPYCONTENTS, tokens[1].c_str(),
CURLFORM_END);
}
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, fileFieldname.c_str(),
CURLFORM_BUFFER, "data",
CURLFORM_BUFFERPTR, contents.data(),
CURLFORM_BUFFERLENGTH, contents.size(),
CURLFORM_END);
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all(headerlist);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
error << "Data: " << buffer;
throw std::runtime_error(error.str());
}
return buffer;
}
void HttpClient::GetUrlPostAttachmentSilent(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const
{
std::string contents;
std::ifstream fileStream(filename, std::ios::in | std::ios::binary);
if (fileStream)
{
fileStream.seekg(0, std::ios::end);
contents.resize(fileStream.tellg());
fileStream.seekg(0, std::ios::beg);
fileStream.read(&contents[0], contents.size());
fileStream.close();
}
CURL* curl = curl_easy_init();
CURLcode result;
struct curl_httppost *formpost = nullptr;
struct curl_httppost *lastptr = nullptr;
struct curl_slist *headerlist = nullptr;
static const char headerBuffer[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "cache-control:",
CURLFORM_COPYCONTENTS, "no-cache",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "content-type:",
CURLFORM_COPYCONTENTS, "multipart/form-data",
CURLFORM_END);
std::vector<std::string> postTokens;
boost::split(postTokens, postData, boost::is_any_of("&"));
for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
{
std::vector<std::string> tokens;
boost::split(tokens, *it, boost::is_any_of("="));
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, tokens[0].c_str(),
CURLFORM_COPYCONTENTS, tokens[1].c_str(),
CURLFORM_END);
}
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, fileFieldname.c_str(),
CURLFORM_BUFFER, "data",
CURLFORM_BUFFERPTR, contents.data(),
CURLFORM_BUFFERLENGTH, contents.size(),
CURLFORM_END);
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all(headerlist);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
throw std::runtime_error(error.str());
}
}
std::string HttpClient::GetUrlPostAttachmentContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const
{
std::string buffer;
std::string contents;
std::ifstream fileStream(filename, std::ios::in | std::ios::binary);
if (fileStream)
{
fileStream.seekg(0, std::ios::end);
contents.resize(fileStream.tellg());
fileStream.seekg(0, std::ios::beg);
fileStream.read(&contents[0], contents.size());
fileStream.close();
}
CURL* curl = curl_easy_init();
CURLcode result;
struct curl_httppost *formpost = nullptr;
struct curl_httppost *lastptr = nullptr;
struct curl_slist *headerlist = nullptr;
static const char headerBuffer[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "cache-control:",
CURLFORM_COPYCONTENTS, "no-cache",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "content-type:",
CURLFORM_COPYCONTENTS, "multipart/form-data",
CURLFORM_END);
std::vector<std::string> postTokens;
boost::split(postTokens, postData, boost::is_any_of("&"));
for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
{
std::vector<std::string> tokens;
boost::split(tokens, *it, boost::is_any_of("="));
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, tokens[0].c_str(),
CURLFORM_COPYCONTENTS, tokens[1].c_str(),
CURLFORM_END);
}
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, fileFieldname.c_str(),
CURLFORM_BUFFER, "data",
CURLFORM_BUFFERPTR, contents.data(),
CURLFORM_BUFFERLENGTH, contents.size(),
CURLFORM_END);
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all(headerlist);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
error << "Data: " << buffer;
throw std::runtime_error(error.str());
}
return buffer;
}
void HttpClient::GetUrlPostAttachmentSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const
{
std::string contents;
std::ifstream fileStream(filename, std::ios::in | std::ios::binary);
if (fileStream)
{
fileStream.seekg(0, std::ios::end);
contents.resize(fileStream.tellg());
fileStream.seekg(0, std::ios::beg);
fileStream.read(&contents[0], contents.size());
fileStream.close();
}
CURL* curl = curl_easy_init();
CURLcode result;
struct curl_httppost *formpost = nullptr;
struct curl_httppost *lastptr = nullptr;
struct curl_slist *headerlist = nullptr;
static const char headerBuffer[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "cache-control:",
CURLFORM_COPYCONTENTS, "no-cache",
CURLFORM_END);
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, "content-type:",
CURLFORM_COPYCONTENTS, "multipart/form-data",
CURLFORM_END);
std::vector<std::string> postTokens;
boost::split(postTokens, postData, boost::is_any_of("&"));
for (std::vector<std::string>::iterator it = postTokens.begin(); it != postTokens.end(); ++it)
{
std::vector<std::string> tokens;
boost::split(tokens, *it, boost::is_any_of("="));
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, tokens[0].c_str(),
CURLFORM_COPYCONTENTS, tokens[1].c_str(),
CURLFORM_END);
}
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, fileFieldname.c_str(),
CURLFORM_BUFFER, "data",
CURLFORM_BUFFERPTR, contents.data(),
CURLFORM_BUFFERLENGTH, contents.size(),
CURLFORM_END);
headerlist = curl_slist_append(headerlist, headerBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str());
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str());
int code = 0;
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all(headerlist);
if (code != 200)
{
std::stringstream error;
error << "Error (" << code << ") encountered while retrieving " << url << "\n";
throw std::runtime_error(error.str());
}
}
size_t HttpClient::WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData)
{
if (writerData == nullptr)
return size * nmemb;
writerData->append(data, size * nmemb);
return size * nmemb;
}
} // namespace Util
} // namespace PresenceDetection
-42
View File
@@ -1,42 +0,0 @@
#ifndef UTIL_HTTPCLIENT_H
#define UTIL_HTTPCLIENT_H
#include <string>
#include <vector>
namespace PresenceDetection {
namespace Util {
class HttpClient
{
public:
HttpClient();
~HttpClient();
HttpClient(const HttpClient&) = delete;
std::string GetUrlContents(const std::string& url) const;
void GetUrlSilent(const std::string& url) const;
std::string GetUrlContents(const std::string& url, const std::string& cookieFile) const;
void GetUrlSilent(const std::string& url, const std::string& cookieFile) const;
std::string GetUrlPostContents(const std::string& url, const std::string& postData, const std::string& contentType) const;
void GetUrlPostSilent(const std::string& url, const std::string& postData, const std::string& contentType) const;
std::string GetUrlPostContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const;
void GetUrlPostSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const;
std::string GetUrlPostAttachmentContents(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const;
void GetUrlPostAttachmentSilent(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const;
std::string GetUrlPostAttachmentContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const;
void GetUrlPostAttachmentSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const;
private:
static size_t WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData);
private:
std::vector<std::string> m_userAgents;
};
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_HTTPCLIENT_H
-46
View File
@@ -1,46 +0,0 @@
#ifndef UTIL_HTTPCLIENTHELPERS_H
#define UTIL_HTTPCLIENTHELPERS_H
#include <memory>
#include <mutex>
#include <vector>
#include <openssl/crypto.h>
namespace PresenceDetection {
namespace Util {
static std::vector<std::shared_ptr<std::mutex> > g_mutexes;
static void LockCallback(int mode, int type, const char* /*file*/, int /*line*/)
{
if (mode & CRYPTO_LOCK)
g_mutexes[type]->lock();
else
g_mutexes[type]->unlock();
}
static unsigned long ThreadId(void)
{
return (unsigned long)pthread_self();
}
static void InitializeLocks(void)
{
g_mutexes.resize(CRYPTO_num_locks());
for (int i = 0; i < CRYPTO_num_locks(); ++i)
g_mutexes[i] = std::shared_ptr<std::mutex>(new std::mutex());
CRYPTO_set_id_callback(ThreadId);
CRYPTO_set_locking_callback(LockCallback);
}
static void FreeLocks(void)
{
CRYPTO_set_locking_callback(NULL);
}
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_HTTPCLIENTHELPERS_H
-20
View File
@@ -1,20 +0,0 @@
#include <iostream>
#include <syslog.h>
#include "Logger.h"
namespace PresenceDetection {
namespace Util {
void Logger::Log(Severity::type severity, const std::string& message)
{
if (severity > Severity::Error)
std::cout << message << std::endl;
else
std::cerr << message << std::endl;
syslog(static_cast<int>(severity), "%s", message.c_str());
}
} // namespace Util
} // namespace PresenceDetection
-37
View File
@@ -1,37 +0,0 @@
#ifndef UTIL_LOGGER_H
#define UTIL_LOGGER_H
#include <string>
namespace PresenceDetection {
namespace Util {
class Logger
{
public:
class Severity
{
public:
enum type
{
Emergency = 0,
Alert = 1,
Critical = 2,
Error = 3,
Warning = 4,
Notice = 5,
Info = 6,
Debug = 7
};
};
public:
static void Log(Severity::type severity, const std::string& message);
};
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_LOGGER_H
-59
View File
@@ -1,59 +0,0 @@
#include <chrono>
#include "Util.h"
#include "Timer.h"
namespace PresenceDetection {
namespace Util {
Timer::Timer() :
m_run(false),
m_aborted(false),
m_identifier(CreateRandomString(10))
{
}
Timer::~Timer()
{
Abort();
Wait();
}
bool Timer::operator==(const Timer& other) const
{
return m_identifier == other.Identifier();
}
std::string Timer::Identifier() const
{
return m_identifier;
}
void Timer::Stop()
{
m_run.store(false, std::memory_order_release);
}
void Timer::Abort()
{
m_run.store(false, std::memory_order_release);
{
std::unique_lock<std::mutex> lock(m_mutex);
m_aborted = true;
}
m_conditionVariable.notify_all();
}
bool Timer::IsRunning() const noexcept
{
return (m_run.load(std::memory_order_acquire) && m_thread.joinable());
}
void Timer::Wait()
{
if (m_thread.joinable())
m_thread.join();
}
} // namespace Util
} // namespace PresenceDetection
-106
View File
@@ -1,106 +0,0 @@
#ifndef UTIL_TIMER_H
#define UTIL_TIMER_H
#include <atomic>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include "Helpers.h"
namespace PresenceDetection {
namespace Util {
class Timer
{
public:
Timer();
~Timer();
Timer(const Timer&) = delete;
Timer& operator= (const Timer&) = delete;
bool operator==(const Timer& other) const;
public:
std::string Identifier() const;
template <typename ...FunctionArguments, typename ...Arguments>
void StartSingle(int milliSeconds, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
{
Start(TimerType::Single, milliSeconds, function, std::forward<Arguments>(arguments)...);
}
template <typename ...FunctionArguments, typename ...Arguments>
void StartContinuous(int milliSeconds, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
{
Start(TimerType::Continuous, milliSeconds, function, std::forward<Arguments>(arguments)...);
}
void Stop();
void Abort();
bool IsRunning() const noexcept;
void Wait();
private:
class TimerType
{
public:
enum type
{
Unknown,
Single,
Continuous
};
};
private:
template <typename ...FunctionArguments, typename ...Arguments>
void Start(TimerType::type type, int milliSeconds, std::function<void(FunctionArguments...)> const & function, Arguments && ...arguments)
{
if (type == TimerType::Unknown)
return;
if (m_run.load(std::memory_order_acquire))
Stop();
m_thread = std::thread([this, type, milliSeconds, function, &arguments...]()
{
auto argumentsCopy = std::make_tuple(std::forward<Arguments>(arguments)...);
m_run.store(true, std::memory_order_release);
m_aborted = false;
while (m_run.load(std::memory_order_acquire))
{
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_conditionVariable.wait_for(lock, std::chrono::milliseconds(milliSeconds), [&]{return m_aborted;}))
{
Helpers::execute(function, argumentsCopy);
if (type == TimerType::Single)
m_run.store(false, std::memory_order_release);
}
}
});
while (!IsRunning())
{
}
}
private:
std::atomic<bool> m_run;
bool m_aborted;
std::mutex m_mutex;
std::condition_variable m_conditionVariable;
std::string m_identifier;
std::thread m_thread;
};
typedef std::unique_ptr<Timer> TimerPointer;
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_TIMER_H
-25
View File
@@ -1,25 +0,0 @@
#include <algorithm>
#include "Util.h"
namespace PresenceDetection {
namespace Util {
std::string CreateRandomString(size_t length)
{
auto randchar = []() -> char
{
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[ rand() % max_index ];
};
std::string str(length,0);
std::generate_n(str.begin(), length, randchar);
return str;
}
} // namespace Util
} // namespace PresenceDetection
-15
View File
@@ -1,15 +0,0 @@
#ifndef UTIL_UTIL_H
#define UTIL_UTIL_H
#include <string>
namespace PresenceDetection {
namespace Util {
std::string CreateRandomString(size_t length);
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_UTIL_H
-17
View File
@@ -1,17 +0,0 @@
#ifndef UTIL_CLIPP_H
#define UTIL_CLIPP_H
#include "Libraries/clipp/clipp.h"
namespace PresenceDetection {
namespace Util {
namespace clipp {
using namespace ::clipp;
} // namespace clipp
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_CLIPP_H