2 * @brief HTTPHelper class implementation. This class represents a Helper to perform HTTP operations easily.
6 * @copyright Intra2net AG
10 #include "httphelper.hpp"
11 #include "version_info.h"
17 * Default Constructor.
19 HTTPHelper::HTTPHelper()
23 , CurlInitError(CURLE_OK)
25 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
30 * Constructor. Use this constructor if HTTP AUTH should be used. Username and password will then be set as HTTP auth options.
31 * @param _log Logger Object
32 * @param _proxy Proxy to use
33 * @param _proxy_port Proxy Port
34 * @param _username Username
35 * @param _password Password
37 HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_port, const string& _username, const string& _password)
40 , ProxyPort(_proxy_port)
42 , CurlInitError(CURLE_OK)
46 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
47 if ( CurlEasyHandle != NULL )
49 if ( (CurlInitError = set_curl_auth(Username,Password)) != CURLE_OK )
51 curl_easy_cleanup(CurlEasyHandle);
52 CurlEasyHandle = NULL;
59 * Constructor. Use this constructor if you have to encode the username and password into the url
60 * @param _log Logger Object
61 * @param _proxy Proxy to use
62 * @param _proxy_port Proxy Port
64 HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_port)
67 , ProxyPort(_proxy_port)
69 , CurlInitError(CURLE_OK)
71 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
78 HTTPHelper::~HTTPHelper()
81 if ( CurlEasyHandle != NULL )
83 curl_easy_cleanup(CurlEasyHandle);
84 CurlEasyHandle = NULL;
91 void HTTPHelper::re_initialize()
93 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
94 if ( (CurlEasyHandle != NULL) && !(Username.empty()) && !(Password.empty()) )
96 if ( (CurlInitError = set_curl_auth(Username,Password)) != CURLE_OK )
98 curl_easy_cleanup(CurlEasyHandle);
99 CurlEasyHandle = NULL;
106 * Perform a HTTP GET operation
107 * @param url URL for HTTP GET operation
108 * @return The status code from the http operation or -1 if an curl error occurs
110 long HTTPHelper::http_get(const string& url)
113 CurlError = CURLE_OK;
115 if ( CurlEasyHandle != NULL )
117 if ( (CurlInitError = set_curl_url(url)) != CURLE_OK )
119 Log->print_curl_error(url,CurlInitError,CurlErrBuff);
120 CurlWritedataBuff.clear();
123 if ( (CurlError = curl_easy_perform(CurlEasyHandle) ) != CURLE_OK )
125 Log->print_curl_error(url,CurlError,CurlErrBuff);
126 CurlWritedataBuff.clear();
129 if ( (CurlError = curl_easy_getinfo(CurlEasyHandle,CURLINFO_RESPONSE_CODE,&curl_info)) != CURLE_OK )
131 Log->print_curl_error(url,CurlError);
132 CurlWritedataBuff.clear();
136 Log->print_curl_data(CurlWritedataBuff);
138 // Copy the received data received via curl from the curl data buffer member to the received data member. This is needed because curl appends data to the buffer rather than overrites it.
139 ReceivedCurlData = CurlWritedataBuff;
140 CurlWritedataBuff.clear();
142 // Operation performed without any problems so we can return the curl_info
150 * Getter for member CurlWritedataBuff
151 * @return CurlWritedataBuff
153 string HTTPHelper::get_curl_data() const
155 return ReceivedCurlData;
160 * Initialized curl easy handle with a few options.
161 * @param curl_writedata_buff Reference to a string wich will be filled with the curl result
162 * @param curl_err_buff A pointer to an char array which will be filled with error message.
163 * @return A pointer to the easy curl handle or NULL if something went wrong.
165 CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff)
167 CurlInitError = CURLE_OK;
168 ostringstream user_agent_stream;
169 user_agent_stream << "Intra2net AG - Bullet Proof DYNDNS Daemon - " << MAJOR_VERSION << "." << MINOR_VERSION << endl;
170 string user_agent = user_agent_stream.str();
172 CURL *curl_easy_handle = curl_easy_init();
173 if ( curl_easy_handle == NULL )
175 // something went wrong.
176 CurlInitError = CURLE_FAILED_INIT;
177 Log->print_curl_error_init("Could not initialize CURL object.",CURLE_FAILED_INIT);
181 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
182 if ( CurlInitError == CURLE_OK)
183 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
184 if ( CurlInitError == CURLE_OK)
185 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
186 if ( CurlInitError == CURLE_OK)
187 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
188 if ( CurlInitError == CURLE_OK)
189 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
190 if ( CurlInitError == CURLE_OK)
191 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
192 if ( CurlInitError == CURLE_OK)
193 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
194 if ( CurlInitError == CURLE_OK)
195 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_USERAGENT,&user_agent);
197 if ( !Proxy.empty() )
199 if ( CurlInitError == CURLE_OK)
200 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
201 if ( CurlInitError == CURLE_OK)
202 CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
205 if ( CurlInitError != CURLE_OK )
207 // Some options could not be set, so destroy the CURL handle.
208 Log->print_curl_error_init("Could not set CURL options properly.",CurlInitError);
209 curl_easy_cleanup(curl_easy_handle);
210 curl_easy_handle = NULL;
213 return curl_easy_handle;
218 * Test if the curl handle is initialized correctly.
219 * @return True if correctly initialized, false if something went wrong during initialization.
221 bool HTTPHelper::is_initialized() const
223 if ( (CurlInitError == CURLE_OK) && (CurlEasyHandle != NULL) )
232 * Sets a url to the easy curl handle
233 * @param url The url to set.
234 * @return CURLcode CURLE_OK if everything is right.
236 CURLcode HTTPHelper::set_curl_url(const string& url)
238 CURLcode curlError = CURLE_OK;
239 if ( (CurlEasyHandle != NULL) && (CurlInitError == CURLE_OK) )
241 curlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
242 if ( curlError != CURLE_OK )
244 // Some options could not be set, so destroy the CURL handle.
245 Log->print_curl_error_init("Could not set CURL URL properly.",curlError);
246 curl_easy_cleanup(CurlEasyHandle);
247 CurlEasyHandle = NULL;
252 return CURLE_FAILED_INIT;
259 * Sets HTTP AUTH parameters
260 * @param username The username for HTTP AUTH
261 * @param password The password for HTTP AUTH
262 * @return CURLcode CURLE_OK if everything is right.
264 CURLcode HTTPHelper::set_curl_auth(const string& username, const string& password)
266 CURLcode curlError = CURLE_OK;
267 if ( (CurlEasyHandle != NULL) && (CurlInitError == CURLE_OK) )
269 curlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
270 if ( curlError != CURLE_OK )
271 Log->print_curl_error_init("Could not set CURL username properly.",curlError);
272 curlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
273 if ( curlError != CURLE_OK )
274 Log->print_curl_error_init("Could not set CURL password properly.",curlError);
278 return CURLE_FAILED_INIT;
285 * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer
286 * @param inBuffer Pointer to input.
287 * @param size How many mem blocks are received
288 * @param nmemb size of each memblock
289 * @param outBuffer Pointer to output stream.
290 * @return The size received.
292 size_t HTTPHelper::http_receive( const char *inBuffer, size_t size, size_t nmemb, string *outBuffer )
294 outBuffer->append(inBuffer);