2015-11-23 09:07:38 +01:00
|
|
|
#include "transport/HTTPRequest.h"
|
|
|
|
|
|
|
|
namespace Transport {
|
|
|
|
|
|
|
|
DEFINE_LOGGER(logger, "HTTPRequest")
|
|
|
|
|
2015-11-24 12:26:26 +01:00
|
|
|
HTTPRequest::HTTPRequest(ThreadPool *tp, Type type, const std::string &url, Callback callback) {
|
|
|
|
m_type = type;
|
|
|
|
m_url = url;
|
|
|
|
m_tp = tp;
|
|
|
|
m_callback = callback;
|
2016-02-24 08:35:10 +01:00
|
|
|
curlhandle = NULL;
|
2015-11-24 12:26:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTPRequest::HTTPRequest(Type type, const std::string &url) {
|
|
|
|
m_type = type;
|
|
|
|
m_url = url;
|
|
|
|
m_tp = NULL;
|
2016-02-24 08:35:10 +01:00
|
|
|
curlhandle = NULL;
|
2015-11-23 09:07:38 +01:00
|
|
|
}
|
|
|
|
|
2016-02-21 19:43:39 +01:00
|
|
|
HTTPRequest::~HTTPRequest() {
|
|
|
|
if (curlhandle) {
|
|
|
|
LOG4CXX_INFO(logger, "Cleaning up CURL handle");
|
|
|
|
curl_easy_cleanup(curlhandle);
|
|
|
|
curlhandle = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
bool HTTPRequest::init() {
|
2016-02-24 08:35:10 +01:00
|
|
|
if (curlhandle) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
curlhandle = curl_easy_init();
|
|
|
|
if (curlhandle) {
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_PROXY, NULL);
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_PROXYUSERPWD, NULL);
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-12 00:41:35 +02:00
|
|
|
LOG4CXX_ERROR(logger, "Couldn't Initialize curl!");
|
2015-11-23 09:07:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPRequest::setProxy(std::string IP, std::string port, std::string username, std::string password) {
|
|
|
|
if (curlhandle) {
|
|
|
|
std::string proxyIpPort = IP + ":" + port;
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_PROXY, proxyIpPort.c_str());
|
|
|
|
if(username.length() && password.length()) {
|
|
|
|
std::string proxyUserPass = username + ":" + password;
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_PROXYUSERPWD, proxyUserPass.c_str());
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-12 00:41:35 +02:00
|
|
|
LOG4CXX_ERROR(logger, "Trying to set proxy while CURL isn't initialized");
|
2015-11-23 09:07:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int HTTPRequest::curlCallBack(char* data, size_t size, size_t nmemb, HTTPRequest* obj) {
|
|
|
|
int writtenSize = 0;
|
|
|
|
if (obj && data) {
|
|
|
|
obj->callbackdata.append(data, size*nmemb);
|
|
|
|
writtenSize = (int)(size*nmemb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return writtenSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPRequest::GET(std::string url, std::string &data) {
|
|
|
|
if (curlhandle) {
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_CUSTOMREQUEST, NULL);
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_ENCODING, "");
|
2017-06-12 00:41:35 +02:00
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
data = "";
|
|
|
|
callbackdata = "";
|
|
|
|
memset(curl_errorbuffer, 0, 1024);
|
2017-06-12 00:41:35 +02:00
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_ERRORBUFFER, curl_errorbuffer);
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, curlCallBack);
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, this);
|
2017-06-12 00:41:35 +02:00
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
/* Set http request and url */
|
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_HTTPGET, 1);
|
2015-11-24 12:26:26 +01:00
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 0);
|
2015-11-23 09:07:38 +01:00
|
|
|
curl_easy_setopt(curlhandle, CURLOPT_URL, url.c_str());
|
2017-06-12 00:41:35 +02:00
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
/* Send http request and return status*/
|
|
|
|
if(CURLE_OK == curl_easy_perform(curlhandle)) {
|
|
|
|
data = callbackdata;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-12 00:41:35 +02:00
|
|
|
LOG4CXX_ERROR(logger, "CURL not initialized!");
|
2015-11-23 09:07:38 +01:00
|
|
|
strcpy(curl_errorbuffer, "CURL not initialized!");
|
|
|
|
}
|
2016-02-21 19:43:39 +01:00
|
|
|
LOG4CXX_ERROR(logger, "Error fetching " << url);
|
2015-11-23 09:07:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPRequest::GET(std::string url, rapidjson::Document &json) {
|
2015-11-24 12:26:26 +01:00
|
|
|
if (!GET(url, m_data)) {
|
2015-11-23 09:07:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:26:26 +01:00
|
|
|
if(json.Parse<0>(m_data.c_str()).HasParseError()) {
|
|
|
|
LOG4CXX_ERROR(logger, "Error while parsing JSON");
|
|
|
|
LOG4CXX_ERROR(logger, m_data);
|
|
|
|
strcpy(curl_errorbuffer, "Error while parsing JSON");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPRequest::run() {
|
2016-02-24 08:35:10 +01:00
|
|
|
if (!init()) {
|
|
|
|
m_ok = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:26:26 +01:00
|
|
|
switch (m_type) {
|
|
|
|
case Get:
|
|
|
|
m_ok = GET(m_url, m_json);
|
|
|
|
break;
|
|
|
|
}
|
2016-02-24 08:35:10 +01:00
|
|
|
|
|
|
|
curl_easy_cleanup(curlhandle);
|
|
|
|
curlhandle = NULL;
|
2015-11-24 12:26:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPRequest::finalize() {
|
|
|
|
m_callback(this, m_ok, m_json, m_data);
|
|
|
|
onRequestFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPRequest::execute() {
|
|
|
|
if (!m_tp) {
|
2015-11-23 09:07:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:26:26 +01:00
|
|
|
m_tp->runAsThread(this);
|
2015-11-23 09:07:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:26:26 +01:00
|
|
|
bool HTTPRequest::execute(rapidjson::Document &json) {
|
2016-02-24 08:35:10 +01:00
|
|
|
init();
|
2015-11-24 12:26:26 +01:00
|
|
|
switch (m_type) {
|
|
|
|
case Get:
|
|
|
|
m_ok = GET(m_url, json);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_ok;
|
|
|
|
}
|
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
}
|