1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#pragma once #include "curl/curl.h" #include "transport/Logging.h" #include "transport/ThreadPool.h" #include <iostream> #include <sstream> #include <string.h> #include "rapidjson/document.h" #include <Swiften/SwiftenCompat.h> namespace Transport { class HTTPRequest : public Thread { public: 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); virtual ~HTTPRequest(); void setProxy(std::string, std::string, std::string, std::string); bool execute(); bool execute(rapidjson::Document &json); std::string getError() {return std::string(curl_errorbuffer);} const std::string &getRawData() { return m_data; } void run(); void finalize(); const std::string &getURL() { return m_url; } SWIFTEN_SIGNAL_NAMESPACE::signal<void ()> onRequestFinished; static void globalInit() { curl_global_init(CURL_GLOBAL_ALL); } static void globalCleanup() { curl_global_cleanup(); } private: bool init(); bool GET(std::string url, std::string &output); bool GET(std::string url, rapidjson::Document &json); CURL *curlhandle; char curl_errorbuffer[1024]; std::string error; std::string callbackdata; ThreadPool *m_tp; std::string m_url; bool m_ok; rapidjson::Document m_json; std::string m_data; Callback m_callback; Type m_type; static int curlCallBack(char* data, size_t size, size_t nmemb, HTTPRequest *obj); }; }