2016-07-18 10:45:45 -04:00
|
|
|
/*
|
2019-08-02 17:26:43 -04:00
|
|
|
* Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
|
|
|
|
* Copyright (C) 2018 Paul Davis <paul@linuxaudiosystems.com>
|
2016-07-18 10:45:45 -04:00
|
|
|
*
|
2019-08-02 17:26:43 -04:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2016-07-18 10:45:45 -04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2019-08-02 17:26:43 -04:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2016-07-18 10:45:45 -04:00
|
|
|
*/
|
2017-08-31 18:36:19 -04:00
|
|
|
|
|
|
|
//#define ARDOURCURLDEBUG
|
2016-07-18 10:45:45 -04:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <glibmm.h>
|
|
|
|
|
|
|
|
#include "pbd/compose.h"
|
|
|
|
#include "pbd/error.h"
|
|
|
|
|
|
|
|
#include "ardour_http.h"
|
|
|
|
|
2019-09-26 00:11:30 -04:00
|
|
|
#include "pbd/i18n.h"
|
|
|
|
|
2016-07-18 10:45:45 -04:00
|
|
|
#ifdef WAF_BUILD
|
|
|
|
#include "gtk2ardour-version.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ARDOUR_CURL_TIMEOUT
|
|
|
|
#define ARDOUR_CURL_TIMEOUT (60)
|
|
|
|
#endif
|
|
|
|
|
2017-08-31 14:45:51 -04:00
|
|
|
#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
|
2022-06-21 19:36:26 -04:00
|
|
|
#define CCERR(msg) (void) cc;
|
2017-08-31 14:45:51 -04:00
|
|
|
#endif
|
|
|
|
|
2016-07-18 10:45:45 -04:00
|
|
|
using namespace ArdourCurl;
|
|
|
|
|
|
|
|
const char* HttpGet::ca_path = NULL;
|
|
|
|
const char* HttpGet::ca_info = NULL;
|
|
|
|
|
2022-05-20 15:36:24 -04:00
|
|
|
void
|
|
|
|
HttpGet::ca_setopt (CURL* c)
|
|
|
|
{
|
2022-10-18 13:37:06 -04:00
|
|
|
if (ca_info && strlen (ca_info) > 0) {
|
2022-05-20 15:36:24 -04:00
|
|
|
curl_easy_setopt (c, CURLOPT_CAINFO, ca_info);
|
|
|
|
}
|
|
|
|
if (ca_path) {
|
|
|
|
curl_easy_setopt (c, CURLOPT_CAPATH, ca_path);
|
|
|
|
}
|
2022-10-18 13:37:06 -04:00
|
|
|
|
|
|
|
if (ca_info && strlen (ca_info) == 0) {
|
|
|
|
/* not hat for you */
|
|
|
|
curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
|
|
|
|
curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
|
|
|
|
} else if (ca_info || ca_path) {
|
2022-05-20 15:36:24 -04:00
|
|
|
curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 10:45:45 -04:00
|
|
|
void
|
|
|
|
HttpGet::setup_certificate_paths ()
|
|
|
|
{
|
|
|
|
/* this is only needed for Linux Bundles.
|
|
|
|
* (on OSX, Windows, we use system-wide ssl (darwinssl, winssl)
|
|
|
|
* Gnu/Linux distro will link against system-wide libcurl.
|
|
|
|
*
|
|
|
|
* but for linux-bundles we get to enjoy:
|
|
|
|
* https://www.happyassassin.net/2015/01/12/a-note-about-ssltls-trusted-certificate-stores-and-platforms/
|
|
|
|
*
|
|
|
|
* (we do ship curl + nss + nss-pem)
|
|
|
|
*
|
|
|
|
* Short of this mess: we could simply bundle a .crt of
|
|
|
|
* COMODO (ardour) and ghandi (freesound) and be done with it.
|
|
|
|
*/
|
|
|
|
assert (!ca_path && !ca_info); // call once
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
else if (Glib::file_test ("/etc/ssl/certs/ca-certificates.crt", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
|
|
|
|
// Debian and derivatives
|
|
|
|
ca_info = "/etc/ssl/certs/ca-certificates.crt";
|
|
|
|
}
|
|
|
|
else if (Glib::file_test ("/etc/pki/tls/cert.pem", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
|
|
|
|
// GNU/TLS can keep extra stuff here
|
|
|
|
ca_info = "/etc/pki/tls/cert.pem";
|
2022-10-18 13:37:06 -04:00
|
|
|
} else {
|
|
|
|
ca_info = ""; // disable cert check
|
2016-07-18 10:45:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Glib::file_test ("/etc/pki/tls/certs/ca-bundle.crt", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
|
|
|
|
// we're on RHEL // https://bugzilla.redhat.com/show_bug.cgi?id=1053882
|
|
|
|
ca_path = "/nonexistent_path"; // don't try "/etc/ssl/certs" in case it's curl's default
|
|
|
|
}
|
|
|
|
else if (Glib::file_test ("/etc/ssl/certs", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
|
|
|
|
// Debian and derivs + OpenSuSe
|
|
|
|
ca_path = "/etc/ssl/certs";
|
|
|
|
} else {
|
2022-10-18 13:37:06 -04:00
|
|
|
ca_path = "/nonexistent_path";
|
2016-07-18 10:45:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we don't set anything defaults are used. at the time of writing we compile bundled curl on debian
|
|
|
|
* and it'll default to /etc/ssl/certs and /etc/ssl/certs/ca-certificates.crt
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data) {
|
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
struct HttpGet::MemStruct *mem = (struct HttpGet::MemStruct*)data;
|
|
|
|
|
|
|
|
mem->data = (char *)realloc (mem->data, mem->size + realsize + 1);
|
|
|
|
if (mem->data) {
|
|
|
|
memcpy (&(mem->data[mem->size]), ptr, realsize);
|
|
|
|
mem->size += realsize;
|
|
|
|
mem->data[mem->size] = 0;
|
|
|
|
}
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:39:04 -04:00
|
|
|
static size_t headerCallback (char* ptr, size_t size, size_t nmemb, void* data)
|
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
2017-08-31 14:45:51 -04:00
|
|
|
#ifdef ARDOURCURLDEBUG
|
|
|
|
std::cerr << string_compose ("ArdourCurl HTTP-header recv %1 bytes", realsize) << std::endl;
|
|
|
|
#endif
|
2016-09-15 19:39:04 -04:00
|
|
|
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;
|
2017-08-31 14:45:51 -04:00
|
|
|
#ifdef ARDOURCURLDEBUG
|
|
|
|
std::cerr << string_compose ("ArdourCurl HTTP-header '%1' = '%2'", k, v) << std::endl;
|
|
|
|
#endif
|
2016-09-15 19:39:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return realsize;
|
|
|
|
}
|
2016-07-18 10:45:45 -04:00
|
|
|
|
|
|
|
HttpGet::HttpGet (bool p, bool ssl)
|
|
|
|
: persist (p)
|
|
|
|
, _status (-1)
|
|
|
|
, _result (-1)
|
|
|
|
{
|
2017-11-23 04:10:32 -05:00
|
|
|
error_buffer[0] = 0;
|
2016-07-18 10:45:45 -04:00
|
|
|
_curl = curl_easy_init ();
|
|
|
|
|
2017-08-31 14:45:51 -04:00
|
|
|
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");
|
2022-06-21 19:32:09 -04:00
|
|
|
// cc = curl_easy_setopt (_curl, CURLOPT_FOLLOWLOCATION, 1); CCERR ("CURLOPT_FOLLOWLOCATION");
|
2016-07-18 10:45:45 -04:00
|
|
|
|
|
|
|
// by default use curl's default.
|
2022-05-20 15:36:24 -04:00
|
|
|
if (ssl) {
|
|
|
|
ca_setopt (_curl);
|
2020-10-30 10:58:04 -04:00
|
|
|
}
|
2016-07-18 10:45:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
HttpGet::~HttpGet ()
|
|
|
|
{
|
2017-08-31 14:45:51 -04:00
|
|
|
if (_curl) {
|
|
|
|
curl_easy_cleanup (_curl);
|
|
|
|
}
|
2016-07-18 10:45:45 -04:00
|
|
|
if (!persist) {
|
2021-02-28 09:17:30 -05:00
|
|
|
::free (mem.data);
|
2016-07-18 10:45:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2018-06-02 13:24:17 -04:00
|
|
|
HttpGet::get (const char* url, bool with_error_logging)
|
2016-07-18 10:45:45 -04:00
|
|
|
{
|
2017-08-31 14:45:51 -04:00
|
|
|
#ifdef ARDOURCURLDEBUG
|
2018-06-02 13:24:17 -04:00
|
|
|
std::cerr << "HttpGet::get() ---- new request ---"<< std::endl;
|
2017-08-31 14:45:51 -04:00
|
|
|
#endif
|
2016-07-18 13:19:35 -04:00
|
|
|
_status = _result = -1;
|
|
|
|
if (!_curl || !url) {
|
2018-06-02 13:24:17 -04:00
|
|
|
if (with_error_logging) {
|
|
|
|
PBD::error << "HttpGet::get() not initialized (or NULL url)"<< endmsg;
|
|
|
|
}
|
2017-08-31 14:45:51 -04:00
|
|
|
#ifdef ARDOURCURLDEBUG
|
|
|
|
std::cerr << "HttpGet::get() not initialized (or NULL url)"<< std::endl;
|
|
|
|
#endif
|
2016-07-18 10:45:45 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp ("http://", url, 7) && strncmp ("https://", url, 8)) {
|
2018-06-02 13:24:17 -04:00
|
|
|
if (with_error_logging) {
|
|
|
|
PBD::error << "HttpGet::get() not a http[s] URL"<< endmsg;
|
|
|
|
}
|
2017-08-31 14:45:51 -04:00
|
|
|
#ifdef ARDOURCURLDEBUG
|
|
|
|
std::cerr << "HttpGet::get() not a http[s] URL"<< std::endl;
|
|
|
|
#endif
|
2016-07-18 10:45:45 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!persist) {
|
2021-02-28 09:17:30 -05:00
|
|
|
::free (mem.data);
|
2016-07-18 13:19:35 -04:00
|
|
|
} // otherwise caller is expected to have free()d or re-used it.
|
|
|
|
|
2017-11-23 04:10:32 -05:00
|
|
|
error_buffer[0] = 0;
|
2016-07-18 13:19:35 -04:00
|
|
|
mem.data = NULL;
|
2016-07-18 10:45:45 -04:00
|
|
|
mem.size = 0;
|
|
|
|
|
2017-08-31 14:45:51 -04:00
|
|
|
CURLcode cc;
|
|
|
|
|
|
|
|
cc = curl_easy_setopt (_curl, CURLOPT_URL, url);
|
|
|
|
CCERR ("CURLOPT_URL");
|
2016-07-18 10:45:45 -04:00
|
|
|
_result = curl_easy_perform (_curl);
|
2017-08-31 14:45:51 -04:00
|
|
|
cc = curl_easy_getinfo (_curl, CURLINFO_RESPONSE_CODE, &_status);
|
|
|
|
CCERR ("CURLINFO_RESPONSE_CODE,");
|
2016-07-18 10:45:45 -04:00
|
|
|
|
|
|
|
if (_result) {
|
2018-06-02 13:24:17 -04:00
|
|
|
if (with_error_logging) {
|
|
|
|
PBD::error << string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer) << endmsg;
|
|
|
|
}
|
2017-08-31 14:45:51 -04:00
|
|
|
#ifdef ARDOURCURLDEBUG
|
|
|
|
std::cerr << string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer) << std::endl;
|
|
|
|
#endif
|
2016-07-18 10:45:45 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (_status != 200) {
|
2018-06-02 13:24:17 -04:00
|
|
|
if (with_error_logging) {
|
|
|
|
PBD::error << string_compose (_("HTTP request status: %1"), _status) << endmsg;
|
|
|
|
}
|
2017-08-31 14:45:51 -04:00
|
|
|
#ifdef ARDOURCURLDEBUG
|
|
|
|
std::cerr << string_compose (_("HTTP request status: %1"), _status) << std::endl;
|
|
|
|
#endif
|
2016-07-18 10:45:45 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mem.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
HttpGet::error () const {
|
|
|
|
if (_result != 0) {
|
|
|
|
return string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer);
|
|
|
|
}
|
|
|
|
if (_status != 200) {
|
|
|
|
return string_compose (_("HTTP request status: %1"), _status);
|
|
|
|
}
|
|
|
|
return "No Error";
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2018-06-02 13:24:17 -04:00
|
|
|
ArdourCurl::http_get (const char* url, int* status, bool with_error_logging) {
|
2016-07-18 10:45:45 -04:00
|
|
|
HttpGet h (true);
|
2018-06-02 13:24:17 -04:00
|
|
|
char* rv = h.get (url, with_error_logging);
|
2016-07-18 10:45:45 -04:00
|
|
|
if (status) {
|
|
|
|
*status = h.status ();
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2018-06-02 13:24:17 -04:00
|
|
|
ArdourCurl::http_get (const std::string& url, bool with_error_logging) {
|
|
|
|
return HttpGet (false).get (url, with_error_logging);
|
2016-07-18 10:45:45 -04:00
|
|
|
}
|