Disable ArdourHTTP debug
[ardour.git] / gtk2_ardour / ardour_http.cc
index 3ffea61b33013647fa8097663677b8794fef8b69..23edbc62e8170b0fb54581d850515c05331d2220 100644 (file)
@@ -16,6 +16,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 
+//#define ARDOURCURLDEBUG
+
 #include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #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";
@@ -105,21 +111,53 @@ WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data) {
        return realsize;
 }
 
+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<const char*>(ptr), realsize);
+       std::string::size_type index = header.find (':', 0);
+       if (index != std::string::npos) {
+               std::string k = header.substr (0, index);
+               std::string v = header.substr (index + 2);
+               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;
+}
 
 HttpGet::HttpGet (bool p, bool ssl)
        : persist (p)
        , _status (-1)
        , _result (-1)
 {
-       error_buffer[0] = '\0';
+       memset (error_buffer, 0, sizeof (*error_buffer));
        _curl = curl_easy_init ();
 
-       curl_easy_setopt (_curl, CURLOPT_WRITEDATA, (void *)&mem);
-       curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
-       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) {
@@ -132,7 +170,9 @@ HttpGet::HttpGet (bool p, bool ssl)
 
 HttpGet::~HttpGet ()
 {
-       curl_easy_cleanup (_curl);
+       if (_curl) {
+               curl_easy_cleanup (_curl);
+       }
        if (!persist) {
                free (mem.data);
        }
@@ -141,12 +181,23 @@ HttpGet::~HttpGet ()
 char*
 HttpGet::get (const char* url)
 {
+#ifdef ARDOURCURLDEBUG
+               std::cerr << "HttpGet::get() ---- new request ---"<< std::endl;
+#endif
        _status = _result = -1;
        if (!_curl || !url) {
+               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)) {
+               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;
        }
 
@@ -154,19 +205,30 @@ 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));
        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);
+               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) {
-               PBD::error << string_compose (_("HTTP request status: %1"), _status);
+               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;
        }