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);
57 * Perform a HTTP GET operation
58 * @param url URL for HTTP GET operation
59 * @return The status code from the http operation or -1 if an curl error occurs
61 long HTTPHelper::http_get(const string& url)
68 if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != 0 )
70 Log->print_curl_error(url,curl_err_code,CurlErrBuff);
73 if ( (curl_err_code = curl_easy_getinfo(CurlEasyHandle,CURLINFO_RESPONSE_CODE,&curl_info)) != 0 )
75 Log->print_curl_error(url,curl_err_code);
79 Log->print_curl_data(CurlWritedataBuff);
81 // Operation performed without any problems so we can return the curl_info
87 * Getter for member CurlWritedataBuff
88 * @return CurlWritedataBuff
90 string HTTPHelper::get_curl_data() const
92 return CurlWritedataBuff;
97 * Initialized curl easy handle with a few options.
98 * @param curl_writedata_buff Reference to a string wich will be filled with the curl result
99 * @param curl_err_buff A pointer to an char array which will be filled with error message.
100 * @return A pointer to the easy curl handle.
102 CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const
104 string user_agent = "Bullet Proof DYNDNS Daemon - Intra2net AG 2009";
106 CURL *curl_easy_handle = curl_easy_init();
108 curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
109 curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
110 curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
111 curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
112 curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
113 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
114 curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
115 curl_easy_setopt(curl_easy_handle,CURLOPT_USERAGENT,&user_agent);
117 if ( !Proxy.empty() )
119 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
120 curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
123 return curl_easy_handle;
128 * Sets a url to the easy curl handle
129 * @param url The url to set.
131 void HTTPHelper::set_curl_url(const string& url)
133 curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
138 * Sets HTTP AUTH parameters
139 * @param username The username for HTTP AUTH
140 * @param password The password for HTTP AUTH
142 void HTTPHelper::set_curl_auth(const string& username, const string& password)
144 curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
145 curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
150 * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer
151 * @param inBuffer Pointer to input.
152 * @param size How many mem blocks are received
153 * @param nmemb size of each memblock
154 * @param outBuffer Pointer to output stream.
155 * @return The size received.
157 int HTTPHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, string *outBuffer )
159 outBuffer->append(inBuffer);