Initial Commit

This commit is contained in:
2018-12-18 12:07:12 +01:00
commit 7ad040c6c1
31 changed files with 1974 additions and 0 deletions
+629
View File
@@ -0,0 +1,629 @@
#include <fstream>
#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
@@ -0,0 +1,42 @@
#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
+45
View File
@@ -0,0 +1,45 @@
#ifndef UTIL_HTTPCLIENTHELPERS_H
#define UTIL_HTTPCLIENTHELPERS_H
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <openssl/crypto.h>
namespace PresenceDetection {
namespace Util {
static std::vector<boost::shared_ptr<boost::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] = boost::shared_ptr<boost::mutex>(new boost::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
+1
View File
@@ -0,0 +1 @@
../Makefile
+1
View File
@@ -0,0 +1 @@
../Makefile.conf
+51
View File
@@ -0,0 +1,51 @@
#include <chrono>
#include "Timer.h"
namespace PresenceDetection {
namespace Util {
Timer::Timer() :
m_run(false)
{
}
Timer::~Timer()
{
Stop();
}
void Timer::Start(int interval, std::function<void(void)> function)
{
if (m_run.load(std::memory_order_acquire))
Stop();
m_run.store(true, std::memory_order_release);
m_thread = std::thread([this, interval, function]()
{
while (m_run.load(std::memory_order_acquire))
{
function();
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
});
}
void Timer::Stop()
{
m_run.store(false, std::memory_order_release);
}
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
+31
View File
@@ -0,0 +1,31 @@
#ifndef UTIL_TIMER_H
#define UTIL_TIMER_H
#include <atomic>
#include <functional>
#include <thread>
namespace PresenceDetection {
namespace Util {
class Timer
{
public:
Timer();
~Timer();
void Start(int interval, std::function<void(void)> func);
void Stop();
bool IsRunning() const noexcept;
void Wait();
private:
std::atomic<bool> m_run;
std::thread m_thread;
};
} // namespace Util
} // namespace PresenceDetection
#endif // UTIL_TIMER_H