X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=gtk2_ardour%2Fardour_http.cc;h=05e23b32d4fb25ccfc8f02de62080a199b85491b;hb=4423945643bf1a6d2196f1e5882e5fe4bcb6d8cf;hp=2b16ccc2c5e4709eaa1bc011304260a5cf20d61a;hpb=720292696a94c96d764e74b5e84fce09a1b3621c;p=ardour.git diff --git a/gtk2_ardour/ardour_http.cc b/gtk2_ardour/ardour_http.cc index 2b16ccc2c5..05e23b32d4 100644 --- a/gtk2_ardour/ardour_http.cc +++ b/gtk2_ardour/ardour_http.cc @@ -16,6 +16,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +//#define ARDOURCURLDEBUG + #include #include #include @@ -37,6 +39,12 @@ #define ARDOUR_CURL_TIMEOUT (60) #endif +#ifdef ARDOURCURLDEBUG +#define CCERR(msg) do { if (cc != CURLE_OK) { std::cerr << string_compose ("curl_easy_setopt(%1) failed: %2", msg, cc) << std::endl; } } while (0) +#else +#define CCERR(msg) +#endif + using namespace ArdourCurl; const char* HttpGet::ca_path = NULL; @@ -59,8 +67,6 @@ HttpGet::setup_certificate_paths () */ assert (!ca_path && !ca_info); // call once - curl_global_init (CURL_GLOBAL_DEFAULT); - if (Glib::file_test ("/etc/pki/tls/certs/ca-bundle.crt", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) { // Fedora / RHEL, Arch ca_info = "/etc/pki/tls/certs/ca-bundle.crt"; @@ -108,6 +114,9 @@ WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data) { static size_t headerCallback (char* ptr, size_t size, size_t nmemb, void* data) { size_t realsize = size * nmemb; +#ifdef ARDOURCURLDEBUG + std::cerr << string_compose ("ArdourCurl HTTP-header recv %1 bytes", realsize) << std::endl; +#endif struct HttpGet::HeaderInfo *nfo = (struct HttpGet::HeaderInfo*)data; std::string header (static_cast(ptr), realsize); std::string::size_type index = header.find (':', 0); @@ -117,6 +126,9 @@ static size_t headerCallback (char* ptr, size_t size, size_t nmemb, void* data) k.erase(k.find_last_not_of (" \n\r\t")+1); v.erase(v.find_last_not_of (" \n\r\t")+1); nfo->h[k] = v; +#ifdef ARDOURCURLDEBUG + std::cerr << string_compose ("ArdourCurl HTTP-header '%1' = '%2'", k, v) << std::endl; +#endif } return realsize; @@ -127,17 +139,25 @@ HttpGet::HttpGet (bool p, bool ssl) , _status (-1) , _result (-1) { - memset (error_buffer, 0, sizeof (*error_buffer)); + error_buffer[0] = 0; _curl = curl_easy_init (); - curl_easy_setopt (_curl, CURLOPT_WRITEDATA, (void *)&mem); - curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); - curl_easy_setopt (_curl, CURLOPT_HEADERDATA, (void *)&nfo); - curl_easy_setopt (_curl, CURLOPT_HEADERFUNCTION, headerCallback); - curl_easy_setopt (_curl, CURLOPT_USERAGENT, PROGRAM_NAME VERSIONSTRING); - curl_easy_setopt (_curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT); - curl_easy_setopt (_curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt (_curl, CURLOPT_ERRORBUFFER, error_buffer); + if (!_curl) { + std::cerr << "HttpGet::HttpGet curl_easy_init() failed." << std::endl; + return; + } + + CURLcode cc; + + cc = curl_easy_setopt (_curl, CURLOPT_WRITEDATA, (void *)&mem); CCERR ("CURLOPT_WRITEDATA"); + cc = curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); CCERR ("CURLOPT_WRITEFUNCTION"); + cc = curl_easy_setopt (_curl, CURLOPT_HEADERDATA, (void *)&nfo); CCERR ("CURLOPT_HEADERDATA"); + cc = curl_easy_setopt (_curl, CURLOPT_HEADERFUNCTION, headerCallback); CCERR ("CURLOPT_HEADERFUNCTION"); + cc = curl_easy_setopt (_curl, CURLOPT_USERAGENT, PROGRAM_NAME VERSIONSTRING); CCERR ("CURLOPT_USERAGENT"); + cc = curl_easy_setopt (_curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT); CCERR ("CURLOPT_TIMEOUT"); + cc = curl_easy_setopt (_curl, CURLOPT_NOSIGNAL, 1); CCERR ("CURLOPT_NOSIGNAL"); + cc = curl_easy_setopt (_curl, CURLOPT_ERRORBUFFER, error_buffer); CCERR ("CURLOPT_ERRORBUFFER"); + // cc= curl_easy_setopt (_curl, CURLOPT_FOLLOWLOCATION, 1); CCERR ("CURLOPT_FOLLOWLOCATION"); // by default use curl's default. if (ssl && ca_info) { @@ -150,21 +170,38 @@ HttpGet::HttpGet (bool p, bool ssl) HttpGet::~HttpGet () { - curl_easy_cleanup (_curl); + if (_curl) { + curl_easy_cleanup (_curl); + } if (!persist) { free (mem.data); } } char* -HttpGet::get (const char* url) +HttpGet::get (const char* url, bool with_error_logging) { +#ifdef ARDOURCURLDEBUG + std::cerr << "HttpGet::get() ---- new request ---"<< std::endl; +#endif _status = _result = -1; if (!_curl || !url) { + if (with_error_logging) { + PBD::error << "HttpGet::get() not initialized (or NULL url)"<< endmsg; + } +#ifdef ARDOURCURLDEBUG + std::cerr << "HttpGet::get() not initialized (or NULL url)"<< std::endl; +#endif return NULL; } if (strncmp ("http://", url, 7) && strncmp ("https://", url, 8)) { + if (with_error_logging) { + PBD::error << "HttpGet::get() not a http[s] URL"<< endmsg; + } +#ifdef ARDOURCURLDEBUG + std::cerr << "HttpGet::get() not a http[s] URL"<< std::endl; +#endif return NULL; } @@ -172,20 +209,34 @@ HttpGet::get (const char* url) free (mem.data); } // otherwise caller is expected to have free()d or re-used it. - memset (error_buffer, 0, sizeof (*error_buffer)); + error_buffer[0] = 0; mem.data = NULL; mem.size = 0; - curl_easy_setopt (_curl, CURLOPT_URL, url); + CURLcode cc; + + cc = curl_easy_setopt (_curl, CURLOPT_URL, url); + CCERR ("CURLOPT_URL"); _result = curl_easy_perform (_curl); - curl_easy_getinfo (_curl, CURLINFO_RESPONSE_CODE, &_status); + cc = curl_easy_getinfo (_curl, CURLINFO_RESPONSE_CODE, &_status); + CCERR ("CURLINFO_RESPONSE_CODE,"); if (_result) { - PBD::error << string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer) << endmsg; + if (with_error_logging) { + PBD::error << string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer) << endmsg; + } +#ifdef ARDOURCURLDEBUG + std::cerr << string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer) << std::endl; +#endif return NULL; } if (_status != 200) { + if (with_error_logging) { PBD::error << string_compose (_("HTTP request status: %1"), _status) << endmsg; + } +#ifdef ARDOURCURLDEBUG + std::cerr << string_compose (_("HTTP request status: %1"), _status) << std::endl; +#endif return NULL; } @@ -204,9 +255,9 @@ HttpGet::error () const { } char* -ArdourCurl::http_get (const char* url, int* status) { +ArdourCurl::http_get (const char* url, int* status, bool with_error_logging) { HttpGet h (true); - char* rv = h.get (url); + char* rv = h.get (url, with_error_logging); if (status) { *status = h.status (); } @@ -214,6 +265,6 @@ ArdourCurl::http_get (const char* url, int* status) { } std::string -ArdourCurl::http_get (const std::string& url) { - return HttpGet (false).get (url); +ArdourCurl::http_get (const std::string& url, bool with_error_logging) { + return HttpGet (false).get (url, with_error_logging); }