2 * @brief HTTPHelper class implementation. This class represents a Helper to perform HTTP operations easily.
6 * @copyright Intra2net AG
10 #include "httphelper.h"
15 HTTPHelper::HTTPHelper()
24 * Constructor. Use this constructor if HTTP AUTH should be used. Username and password will then be set as HTTP auth options.
25 * @param _log Logger Object
26 * @param _proxy Proxy to use
27 * @param _proxy_port Proxy Port
28 * @param _username Username
29 * @param _password Password
31 HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_port, const string& _username, const string& _password)
35 ProxyPort = _proxy_port;
36 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
38 set_curl_auth(_username,_password);
43 * Constructor. Use this constructor if you have to encode the username and password into the url
44 * @param _log Logger Object
45 * @param _proxy Proxy to use
46 * @param _proxy_port Proxy Port
48 HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_port)
52 ProxyPort = _proxy_port;
53 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
60 HTTPHelper::~HTTPHelper()
66 * Perform a HTTP GET operation
67 * @param url URL for HTTP GET operation
68 * @return The status code from the http operation or -1 if an curl error occurs
70 long HTTPHelper::http_get(const string& url)
77 if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != 0 )
79 Log->print_curl_error(url,curl_err_code,CurlErrBuff);
82 if ( (curl_err_code = curl_easy_getinfo(CurlEasyHandle,CURLINFO_RESPONSE_CODE,&curl_info)) != 0 )
84 Log->print_curl_error(url,curl_err_code);
88 Log->print_curl_data(CurlWritedataBuff);
90 // Operation performed without any problems so we can return the curl_info
96 * Getter for member CurlWritedataBuff
97 * @return CurlWritedataBuff
99 string HTTPHelper::get_curl_data() const
101 return CurlWritedataBuff;
106 * Initialized curl easy handle with a few options.
107 * @param curl_writedata_buff Reference to a string wich will be filled with the curl result
108 * @param curl_err_buff A pointer to an char array which will be filled with error message.
109 * @return A pointer to the easy curl handle.
111 CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const
113 string user_agent = "bpdyndnsd Intra2net AG 2009";
115 CURL *curl_easy_handle = curl_easy_init();
117 curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
118 curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
119 curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
120 curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
121 curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
122 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
123 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
124 curl_easy_setopt(curl_easy_handle,CURLOPT_USERAGENT,&user_agent);
126 if ( !Proxy.empty() )
128 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
129 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
132 return curl_easy_handle;
137 * Sets a url to the easy curl handle
138 * @param url The url to set.
140 void HTTPHelper::set_curl_url(const string& url)
142 curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
147 * Sets HTTP AUTH parameters
148 * @param username The username for HTTP AUTH
149 * @param password The password for HTTP AUTH
151 void HTTPHelper::set_curl_auth(const string& username, const string& password)
153 curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
154 curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
159 * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer
160 * @param inBuffer Pointer to input.
161 * @param size How many mem blocks are received
162 * @param nmemb size of each memblock
163 * @param outBuffer Pointer to output stream.
164 * @return The size received.
166 int HTTPHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, string *outBuffer )
168 outBuffer->append(inBuffer);