Fix 'occurred' typo
[bpdyndnsd] / src / httphelper.cpp
index 428286a..eb35fd9 100644 (file)
@@ -7,7 +7,8 @@
  * @license GPLv2
 */
 
-#include "httphelper.h"
+#include "httphelper.hpp"
+#include "version_info.h"
 
 using namespace std;
 
@@ -19,6 +20,7 @@ HTTPHelper::HTTPHelper()
     : Log(new Logger)
     , ProxyPort(0)
     , CurlError(CURLE_OK)
+    , CurlInitError(CURLE_OK)
 {
     CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
 }
@@ -26,7 +28,7 @@ HTTPHelper::HTTPHelper()
 
 /**
  * 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
@@ -37,11 +39,14 @@ HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_
     , 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;
@@ -52,7 +57,7 @@ HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_
 
 /**
  * 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
  */
@@ -61,6 +66,7 @@ HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_
     , Proxy(_proxy)
     , ProxyPort(_proxy_port)
     , CurlError(CURLE_OK)
+    , CurlInitError(CURLE_OK)
 {
     CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
 }
@@ -79,6 +85,22 @@ HTTPHelper::~HTTPHelper()
     }
 }
 
+/**
+ * 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
@@ -88,12 +110,13 @@ HTTPHelper::~HTTPHelper()
 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;
         }
@@ -141,46 +164,52 @@ string HTTPHelper::get_curl_data() const
  */
 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;
     }
@@ -195,7 +224,7 @@ CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff)
  */
 bool HTTPHelper::is_initialized() const
 {
-    if ( (CurlError == CURLE_OK) && (CurlEasyHandle != NULL) )
+    if ( (CurlInitError == CURLE_OK) && (CurlEasyHandle != NULL) )
     {
         return true;
     }
@@ -210,18 +239,23 @@ bool HTTPHelper::is_initialized() const
  */
 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;
 }
 
 
@@ -233,16 +267,21 @@ CURLcode HTTPHelper::set_curl_url(const string& url)
  */
 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;
 }
 
 
@@ -254,9 +293,8 @@ CURLcode HTTPHelper::set_curl_auth(const string& username, const string& passwor
  * @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