2 * @brief HTTPHelper class implementation. This class represents a Helper to perform HTTP operations easily.
6 * @copyright Intra2net AG
10 #include "httphelper.h"
14 * Constructor. Use this constructor if HTTP AUTH should be used. Username and password will then be set as HTTP auth options.
15 * @param _log Logger Object
16 * @param _proxy Proxy to use
17 * @param _proxy_port Proxy Port
18 * @param _username Username
19 * @param _password Password
21 HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_port, const string& _username, const string& _password)
24 , ProxyPort(_proxy_port)
26 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
27 set_curl_auth(_username,_password);
32 * Constructor. Use this constructor if you have to encode the username and password into the url
33 * @param _log Logger Object
34 * @param _proxy Proxy to use
35 * @param _proxy_port Proxy Port
37 HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_port)
40 , ProxyPort(_proxy_port)
42 CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
49 HTTPHelper::~HTTPHelper()
52 curl_easy_cleanup(CurlEasyHandle);
58 * Perform a HTTP GET operation
59 * @param url URL for HTTP GET operation
60 * @return The status code from the http operation or -1 if an curl error occurs
62 long HTTPHelper::http_get(const string& url)
69 if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != 0 )
71 Log->print_curl_error(url,curl_err_code,CurlErrBuff);
74 if ( (curl_err_code = curl_easy_getinfo(CurlEasyHandle,CURLINFO_RESPONSE_CODE,&curl_info)) != 0 )
76 Log->print_curl_error(url,curl_err_code);
80 Log->print_curl_data(CurlWritedataBuff);
82 // Operation performed without any problems so we can return the curl_info
88 * Getter for member CurlWritedataBuff
89 * @return CurlWritedataBuff
91 string HTTPHelper::get_curl_data() const
93 return CurlWritedataBuff;
98 * Initialized curl easy handle with a few options.
99 * @param curl_writedata_buff Reference to a string wich will be filled with the curl result
100 * @param curl_err_buff A pointer to an char array which will be filled with error message.
101 * @return A pointer to the easy curl handle.
103 CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const
105 string user_agent = "Bullet Proof DYNDNS Daemon - Intra2net AG 2009";
107 CURL *curl_easy_handle = curl_easy_init();
109 curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
110 curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
111 curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
112 curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
113 curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
114 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
115 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
116 curl_easy_setopt(curl_easy_handle,CURLOPT_USERAGENT,&user_agent);
118 if ( !Proxy.empty() )
120 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
121 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
124 return curl_easy_handle;
129 * Sets a url to the easy curl handle
130 * @param url The url to set.
132 void HTTPHelper::set_curl_url(const string& url)
134 curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
139 * Sets HTTP AUTH parameters
140 * @param username The username for HTTP AUTH
141 * @param password The password for HTTP AUTH
143 void HTTPHelper::set_curl_auth(const string& username, const string& password)
145 curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
146 curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
151 * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer
152 * @param inBuffer Pointer to input.
153 * @param size How many mem blocks are received
154 * @param nmemb size of each memblock
155 * @param outBuffer Pointer to output stream.
156 * @return The size received.
158 int HTTPHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, string *outBuffer )
160 outBuffer->append(inBuffer);