2 * @brief HTTPHelper class implementation. This class represents a Helper to perform HTTP operations easily.
6 * @copyright Intra2net AG
10 #include "httphelper.h"
12 HTTPHelper::HTTPHelper()
19 HTTPHelper::HTTPHelper(Logger::Ptr _log, string _proxy, int _proxy_port)
23 ProxyPort = _proxy_port;
24 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
28 HTTPHelper::~HTTPHelper()
35 * Perform a HTTP GET operation
36 * @param url URL for HTTP GET operation
37 * @return The data GET from the URL
39 string HTTPHelper::http_get(const string& url)
45 if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != 0 )
47 // TODO: Logging errors
50 // Operation performed without any problems so we can return the CurlWritedataBuff
51 return CurlWritedataBuff;
56 * Initialized curl easy handle with a few options.
57 * @param curl_writedata_buff Reference to a string wich will be filled with the curl result
58 * @param curl_err_buff A pointer to an char array which will be filled with error message.
59 * @return A pointer to the easy curl handle.
61 CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const
63 CURL *curl_easy_handle = curl_easy_init();
65 curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
66 curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
67 curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
68 curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
69 curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
70 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
71 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
75 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
76 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
79 return curl_easy_handle;
84 * Sets a url to the easy curl handle
85 * @param url The url to set.
87 void HTTPHelper::set_curl_url(const string& url)
89 curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
94 * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer
95 * @param inBuffer Pointer to input.
96 * @param size How many mem blocks are received
97 * @param nmemb size of each memblock
98 * @param outBuffer Pointer to output stream.
99 * @return The size received.
101 int HTTPHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, string *outBuffer )
103 outBuffer->append(inBuffer);