* @license GPLv2
*/
-#include "httphelper.h"
+#include "httphelper.hpp"
+#include "version_info.h"
using namespace std;
: Log(new Logger)
, ProxyPort(0)
, CurlError(CURLE_OK)
+ , CurlInitError(CURLE_OK)
{
CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
}
/**
* Constructor. Use this constructor if HTTP AUTH should be used. Username and password will then be set as HTTP auth options.
- * @param _log Logger Object
+ * @param _log Logger Object
* @param _proxy Proxy to use
* @param _proxy_port Proxy Port
* @param _username Username
, Proxy(_proxy)
, ProxyPort(_proxy_port)
, CurlError(CURLE_OK)
+ , CurlInitError(CURLE_OK)
+ , Username(_username)
+ , Password(_password)
{
CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
if ( CurlEasyHandle != NULL )
{
- if ( set_curl_auth(_username,_password) != CURLE_OK )
+ if ( (CurlInitError = set_curl_auth(Username,Password)) != CURLE_OK )
{
curl_easy_cleanup(CurlEasyHandle);
CurlEasyHandle = NULL;
/**
* Constructor. Use this constructor if you have to encode the username and password into the url
- * @param _log Logger Object
+ * @param _log Logger Object
* @param _proxy Proxy to use
* @param _proxy_port Proxy Port
*/
, Proxy(_proxy)
, ProxyPort(_proxy_port)
, CurlError(CURLE_OK)
+ , CurlInitError(CURLE_OK)
{
CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
}
}
}
+/**
+ * Re-Init curl
+ */
+void HTTPHelper::re_initialize()
+{
+ CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
+ if ( (CurlEasyHandle != NULL) && !(Username.empty()) && !(Password.empty()) )
+ {
+ if ( (CurlInitError = set_curl_auth(Username,Password)) != CURLE_OK )
+ {
+ curl_easy_cleanup(CurlEasyHandle);
+ CurlEasyHandle = NULL;
+ }
+ }
+}
+
/**
* Perform a HTTP GET operation
long HTTPHelper::http_get(const string& url)
{
long curl_info;
+ CurlError = CURLE_OK;
if ( CurlEasyHandle != NULL )
{
- if ( (CurlError = set_curl_url(url)) != CURLE_OK )
+ if ( (CurlInitError = set_curl_url(url)) != CURLE_OK )
{
- Log->print_curl_error(url,CurlError,CurlErrBuff);
+ Log->print_curl_error(url,CurlInitError,CurlErrBuff);
CurlWritedataBuff.clear();
return -1;
}
*/
CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff)
{
- string user_agent = "Bullet Proof DYNDNS Daemon 0.1.1 - Intra2net AG 2009";
+ CurlInitError = CURLE_OK;
+ ostringstream user_agent_stream;
+ user_agent_stream << "Intra2net AG - Bullet Proof DYNDNS Daemon - " << MAJOR_VERSION << "." << MINOR_VERSION;
+ string user_agent = user_agent_stream.str();
CURL *curl_easy_handle = curl_easy_init();
if ( curl_easy_handle == NULL )
{
// something went wrong.
- CurlError = CURLE_FAILED_INIT;
+ CurlInitError = CURLE_FAILED_INIT;
Log->print_curl_error_init("Could not initialize CURL object.",CURLE_FAILED_INIT);
return NULL;
}
- if ( CurlError == CURLE_OK )
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
- if ( CurlError == CURLE_OK)
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
- if ( CurlError == CURLE_OK)
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
- if ( CurlError == CURLE_OK)
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
- if ( CurlError == CURLE_OK)
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
- if ( CurlError == CURLE_OK)
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
- if ( CurlError == CURLE_OK)
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
- if ( CurlError == CURLE_OK)
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_USERAGENT,&user_agent);
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1);
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5);
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10);
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024);
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff);
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive);
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff);
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_USERAGENT,user_agent.c_str());
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_SSL_VERIFYHOST,0);
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_SSL_VERIFYPEER,0);
if ( !Proxy.empty() )
{
- if ( CurlError == CURLE_OK)
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
- if ( CurlError == CURLE_OK)
- CurlError = curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str());
+ if ( CurlInitError == CURLE_OK)
+ CurlInitError = curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort);
}
- if ( CurlError != CURLE_OK )
+ if ( CurlInitError != CURLE_OK )
{
// Some options could not be set, so destroy the CURL handle.
- Log->print_curl_error_init("Could not set CURL options properly.",CurlError);
+ Log->print_curl_error_init("Could not set CURL options properly.",CurlInitError);
curl_easy_cleanup(curl_easy_handle);
curl_easy_handle = NULL;
}
* Test if the curl handle is initialized correctly.
* @return True if correctly initialized, false if something went wrong during initialization.
*/
-bool HTTPHelper::is_initialized()
+bool HTTPHelper::is_initialized() const
{
- if ( (CurlError == CURLE_OK) && (CurlEasyHandle != NULL) )
+ if ( (CurlInitError == CURLE_OK) && (CurlEasyHandle != NULL) )
{
return true;
}
*/
CURLcode HTTPHelper::set_curl_url(const string& url)
{
- if ( (CurlEasyHandle != NULL) && (CurlError == CURLE_OK) )
+ CURLcode curlError = CURLE_OK;
+ if ( (CurlEasyHandle != NULL) && (CurlInitError == CURLE_OK) )
{
- CurlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
- if ( CurlError != CURLE_OK )
+ curlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str());
+ if ( curlError != CURLE_OK )
{
// Some options could not be set, so destroy the CURL handle.
- Log->print_curl_error_init("Could not set CURL URL properly.",CurlError);
+ Log->print_curl_error_init("Could not set CURL URL properly.",curlError);
curl_easy_cleanup(CurlEasyHandle);
CurlEasyHandle = NULL;
}
}
- return CurlError;
+ else
+ {
+ return CURLE_FAILED_INIT;
+ }
+ return curlError;
}
*/
CURLcode HTTPHelper::set_curl_auth(const string& username, const string& password)
{
- if ( (CurlEasyHandle != NULL) && (CurlError == CURLE_OK) )
+ CURLcode curlError = CURLE_OK;
+ if ( (CurlEasyHandle != NULL) && (CurlInitError == CURLE_OK) )
{
- CurlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
- if ( CurlError != CURLE_OK )
- Log->print_curl_error_init("Could not set CURL username properly.",CurlError);
- CurlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
- if ( CurlError != CURLE_OK )
- Log->print_curl_error_init("Could not set CURL password properly.",CurlError);
+ curlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
+ if ( curlError != CURLE_OK )
+ Log->print_curl_error_init("Could not set CURL username properly.",curlError);
+ curlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
+ if ( curlError != CURLE_OK )
+ Log->print_curl_error_init("Could not set CURL password properly.",curlError);
}
- return CurlError;
+ else
+ {
+ return CURLE_FAILED_INIT;
+ }
+ return curlError;
}
* @param outBuffer Pointer to output stream.
* @return The size received.
*/
-size_t HTTPHelper::http_receive( const char *inBuffer, size_t size, size_t nmemb, string *outBuffer )
+size_t HTTPHelper::http_receive( void *inBuffer, size_t size, size_t nmemb, string *outBuffer )
{
- outBuffer->append(inBuffer);
+ outBuffer->append(static_cast<char *>(inBuffer), size*nmemb);
return (size*nmemb);
-}
-
+} //lint !e818