2015-11-24 12:26:26 +01:00
|
|
|
|
|
|
|
#pragma once
|
2015-11-23 09:07:38 +01:00
|
|
|
|
|
|
|
#include "curl/curl.h"
|
|
|
|
#include "transport/Logging.h"
|
2015-11-24 12:26:26 +01:00
|
|
|
#include "transport/ThreadPool.h"
|
2015-11-23 09:07:38 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string.h>
|
|
|
|
#include "rapidjson/document.h"
|
|
|
|
|
2015-11-24 12:26:26 +01:00
|
|
|
#include <boost/signal.hpp>
|
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
namespace Transport {
|
|
|
|
|
2015-11-24 12:26:26 +01:00
|
|
|
class HTTPRequest : public Thread {
|
2015-11-23 09:07:38 +01:00
|
|
|
public:
|
2015-11-24 12:26:26 +01:00
|
|
|
typedef enum { Get } Type;
|
|
|
|
typedef boost::function< void (HTTPRequest *, bool, rapidjson::Document &json, const std::string &data) > Callback;
|
|
|
|
|
|
|
|
HTTPRequest(ThreadPool *tp, Type type, const std::string &url, Callback callback);
|
|
|
|
HTTPRequest(Type type, const std::string &url);
|
2015-11-23 09:07:38 +01:00
|
|
|
|
2016-02-21 19:43:39 +01:00
|
|
|
virtual ~HTTPRequest();
|
2015-11-23 09:07:38 +01:00
|
|
|
|
|
|
|
void setProxy(std::string, std::string, std::string, std::string);
|
2015-11-24 12:26:26 +01:00
|
|
|
bool execute();
|
|
|
|
bool execute(rapidjson::Document &json);
|
|
|
|
std::string getError() {return std::string(curl_errorbuffer);}
|
2016-01-24 11:48:40 +01:00
|
|
|
const std::string &getRawData() {
|
|
|
|
return m_data;
|
|
|
|
}
|
2015-11-24 12:26:26 +01:00
|
|
|
|
|
|
|
void run();
|
|
|
|
void finalize();
|
|
|
|
|
2016-02-01 08:17:16 +01:00
|
|
|
const std::string &getURL() {
|
|
|
|
return m_url;
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:26:26 +01:00
|
|
|
boost::signal<void ()> onRequestFinished;
|
2015-11-23 09:07:38 +01:00
|
|
|
|
2016-02-23 12:37:10 +01:00
|
|
|
static void globalInit() {
|
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void globalCleanup() {
|
|
|
|
curl_global_cleanup();
|
|
|
|
}
|
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
private:
|
|
|
|
bool init();
|
2015-11-24 12:26:26 +01:00
|
|
|
bool GET(std::string url, std::string &output);
|
|
|
|
bool GET(std::string url, rapidjson::Document &json);
|
|
|
|
|
2015-11-23 09:07:38 +01:00
|
|
|
|
|
|
|
CURL *curlhandle;
|
|
|
|
char curl_errorbuffer[1024];
|
|
|
|
std::string error;
|
|
|
|
std::string callbackdata;
|
2015-11-24 12:26:26 +01:00
|
|
|
ThreadPool *m_tp;
|
|
|
|
std::string m_url;
|
|
|
|
bool m_ok;
|
|
|
|
rapidjson::Document m_json;
|
|
|
|
std::string m_data;
|
|
|
|
Callback m_callback;
|
|
|
|
Type m_type;
|
2015-11-23 09:07:38 +01:00
|
|
|
|
|
|
|
static int curlCallBack(char* data, size_t size, size_t nmemb, HTTPRequest *obj);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|