From e417b0348edf53ff3e1f5a0c10c3a334a2a48fea Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Tue, 15 Jun 2010 11:46:12 +0200 Subject: [PATCH] Bugfix. Separated CurlError into CurlError and CurlInitError. Added ability to re-init HTTPHelper if not correctly initialized. --- src/httphelper.cpp | 112 +++++++++++++++++++++++++++++---------------- src/httphelper.hpp | 5 ++ src/service_dhs.cpp | 1 + src/service_dyndns.cpp | 1 + src/service_dyns.cpp | 1 + src/service_easydns.cpp | 1 + src/service_gnudip.cpp | 1 + src/service_tzo.cpp | 1 + src/service_zoneedit.cpp | 1 + 9 files changed, 84 insertions(+), 40 deletions(-) diff --git a/src/httphelper.cpp b/src/httphelper.cpp index 37eb0ee..a728d4e 100644 --- a/src/httphelper.cpp +++ b/src/httphelper.cpp @@ -19,6 +19,7 @@ HTTPHelper::HTTPHelper() : Log(new Logger) , ProxyPort(0) , CurlError(CURLE_OK) + , CurlInitError(CURLE_OK) { CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff); } @@ -37,11 +38,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; @@ -61,6 +65,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 +84,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 +109,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 +163,46 @@ string HTTPHelper::get_curl_data() const */ CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) { + CurlInitError = CURLE_OK; string user_agent = "Intra2net AG - Bullet Proof DYNDNS Daemon - 0.1.1"; 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); 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 +217,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 +232,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 +260,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); + } + else { - 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 CURLE_FAILED_INIT; } - return CurlError; + return curlError; } diff --git a/src/httphelper.hpp b/src/httphelper.hpp index bbf3daa..abc984a 100644 --- a/src/httphelper.hpp +++ b/src/httphelper.hpp @@ -25,10 +25,13 @@ private: std::string Proxy; int ProxyPort; CURLcode CurlError; + CURLcode CurlInitError; CURL* CurlEasyHandle; std::string CurlWritedataBuff; std::string ReceivedCurlData; char CurlErrBuff[CURL_ERROR_SIZE]; + std::string Username; + std::string Password; CURLcode set_curl_url(const std::string& url); CURLcode set_curl_auth(const std::string& username, const std::string& password); @@ -52,6 +55,8 @@ public: bool is_initialized() const; + void re_initialize(); + // libcurl is a C library, so we have to make the callback member function static :-( static size_t http_receive(const char *inBuffer, size_t size, size_t nmemb, std::string *outBuffer); }; diff --git a/src/service_dhs.cpp b/src/service_dhs.cpp index 314d987..6dadcc8 100644 --- a/src/service_dhs.cpp +++ b/src/service_dhs.cpp @@ -175,6 +175,7 @@ int ServiceDhs::perform_update(const std::string& ip) else { get_logger()->print_httphelper_not_initialized(); + HTTPHelp->re_initialize(); } return -1; } diff --git a/src/service_dyndns.cpp b/src/service_dyndns.cpp index 0d15974..a298a10 100644 --- a/src/service_dyndns.cpp +++ b/src/service_dyndns.cpp @@ -134,6 +134,7 @@ int ServiceDyndns::perform_update(const std::string& ip) else { get_logger()->print_httphelper_not_initialized(); + HTTPHelp->re_initialize(); } return -1; } diff --git a/src/service_dyns.cpp b/src/service_dyns.cpp index 3855271..065d216 100644 --- a/src/service_dyns.cpp +++ b/src/service_dyns.cpp @@ -141,6 +141,7 @@ int ServiceDyns::perform_update(const std::string& ip) else { get_logger()->print_httphelper_not_initialized(); + HTTPHelp->re_initialize(); } return -1; } diff --git a/src/service_easydns.cpp b/src/service_easydns.cpp index ad6bcfb..1660061 100644 --- a/src/service_easydns.cpp +++ b/src/service_easydns.cpp @@ -219,6 +219,7 @@ int ServiceEasydns::perform_update(const std::string& ip) else { get_logger()->print_httphelper_not_initialized(); + HTTPHelp->re_initialize(); } return -1; } diff --git a/src/service_gnudip.cpp b/src/service_gnudip.cpp index a8f9bd6..8c332c5 100644 --- a/src/service_gnudip.cpp +++ b/src/service_gnudip.cpp @@ -303,6 +303,7 @@ int ServiceGnudip::perform_update(const std::string& ip) else { get_logger()->print_httphelper_not_initialized(); + HTTPHelp->re_initialize(); } return -1; } diff --git a/src/service_tzo.cpp b/src/service_tzo.cpp index 7a1fa61..bdab145 100644 --- a/src/service_tzo.cpp +++ b/src/service_tzo.cpp @@ -141,6 +141,7 @@ int ServiceTzo::perform_update(const std::string& ip) else { get_logger()->print_httphelper_not_initialized(); + HTTPHelp->re_initialize(); } return -1; } diff --git a/src/service_zoneedit.cpp b/src/service_zoneedit.cpp index db1e941..9f392e0 100644 --- a/src/service_zoneedit.cpp +++ b/src/service_zoneedit.cpp @@ -135,6 +135,7 @@ int ServiceZoneedit::perform_update(const std::string& ip) else { get_logger()->print_httphelper_not_initialized(); + HTTPHelp->re_initialize(); } return -1; } -- 1.7.1