Optimize automation-event process splitting
[ardour.git] / gtk2_ardour / ardour_http.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 //#define ARDOURCURLDEBUG
20
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <cstring>
25
26 #include <glibmm.h>
27
28 #include "pbd/compose.h"
29 #include "pbd/i18n.h"
30 #include "pbd/error.h"
31
32 #include "ardour_http.h"
33
34 #ifdef WAF_BUILD
35 #include "gtk2ardour-version.h"
36 #endif
37
38 #ifndef ARDOUR_CURL_TIMEOUT
39 #define ARDOUR_CURL_TIMEOUT (60)
40 #endif
41
42 #ifdef ARDOURCURLDEBUG
43 #define CCERR(msg) do { if (cc != CURLE_OK) { std::cerr << string_compose ("curl_easy_setopt(%1) failed: %2", msg, cc) << std::endl; } } while (0)
44 #else
45 #define CCERR(msg)
46 #endif
47
48 using namespace ArdourCurl;
49
50 const char* HttpGet::ca_path = NULL;
51 const char* HttpGet::ca_info = NULL;
52
53 void
54 HttpGet::setup_certificate_paths ()
55 {
56         /* this is only needed for Linux Bundles.
57          * (on OSX, Windows, we use system-wide ssl (darwinssl, winssl)
58          * Gnu/Linux distro will link against system-wide libcurl.
59          *
60          * but for linux-bundles we get to enjoy:
61          * https://www.happyassassin.net/2015/01/12/a-note-about-ssltls-trusted-certificate-stores-and-platforms/
62          *
63          * (we do ship curl + nss + nss-pem)
64          *
65          * Short of this mess: we could simply bundle a .crt of
66          * COMODO (ardour) and ghandi (freesound) and be done with it.
67          */
68         assert (!ca_path && !ca_info); // call once
69
70         if (Glib::file_test ("/etc/pki/tls/certs/ca-bundle.crt", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
71                 // Fedora / RHEL, Arch
72                 ca_info = "/etc/pki/tls/certs/ca-bundle.crt";
73         }
74         else if (Glib::file_test ("/etc/ssl/certs/ca-certificates.crt", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
75                 // Debian and derivatives
76                 ca_info = "/etc/ssl/certs/ca-certificates.crt";
77         }
78         else if (Glib::file_test ("/etc/pki/tls/cert.pem", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
79                 // GNU/TLS can keep extra stuff here
80                 ca_info = "/etc/pki/tls/cert.pem";
81         }
82         // else NULL: use default (currently) "/etc/ssl/certs/ca-certificates.crt" if it exists
83
84         if (Glib::file_test ("/etc/pki/tls/certs/ca-bundle.crt", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
85                 // we're on RHEL // https://bugzilla.redhat.com/show_bug.cgi?id=1053882
86                 ca_path = "/nonexistent_path"; // don't try "/etc/ssl/certs" in case it's curl's default
87         }
88         else if (Glib::file_test ("/etc/ssl/certs", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
89                 // Debian and derivs + OpenSuSe
90                 ca_path = "/etc/ssl/certs";
91         } else {
92                 ca_path = "/nonexistent_path"; // don't try -- just in case:
93         }
94
95         /* If we don't set anything defaults are used. at the time of writing we compile bundled curl on debian
96          * and it'll default to  /etc/ssl/certs and /etc/ssl/certs/ca-certificates.crt
97          */
98 }
99
100 static size_t
101 WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data) {
102         size_t realsize = size * nmemb;
103         struct HttpGet::MemStruct *mem = (struct HttpGet::MemStruct*)data;
104
105         mem->data = (char *)realloc (mem->data, mem->size + realsize + 1);
106         if (mem->data) {
107                 memcpy (&(mem->data[mem->size]), ptr, realsize);
108                 mem->size += realsize;
109                 mem->data[mem->size] = 0;
110         }
111         return realsize;
112 }
113
114 static size_t headerCallback (char* ptr, size_t size, size_t nmemb, void* data)
115 {
116         size_t realsize = size * nmemb;
117 #ifdef ARDOURCURLDEBUG
118                 std::cerr << string_compose ("ArdourCurl HTTP-header recv %1 bytes", realsize) << std::endl;
119 #endif
120         struct HttpGet::HeaderInfo *nfo = (struct HttpGet::HeaderInfo*)data;
121         std::string header (static_cast<const char*>(ptr), realsize);
122         std::string::size_type index = header.find (':', 0);
123         if (index != std::string::npos) {
124                 std::string k = header.substr (0, index);
125                 std::string v = header.substr (index + 2);
126                 k.erase(k.find_last_not_of (" \n\r\t")+1);
127                 v.erase(v.find_last_not_of (" \n\r\t")+1);
128                 nfo->h[k] = v;
129 #ifdef ARDOURCURLDEBUG
130                 std::cerr << string_compose ("ArdourCurl HTTP-header  '%1' = '%2'", k, v) << std::endl;
131 #endif
132         }
133
134         return realsize;
135 }
136
137 HttpGet::HttpGet (bool p, bool ssl)
138         : persist (p)
139         , _status (-1)
140         , _result (-1)
141 {
142         error_buffer[0] = 0;
143         _curl = curl_easy_init ();
144
145         if (!_curl) {
146                 std::cerr << "HttpGet::HttpGet curl_easy_init() failed." << std::endl;
147                 return;
148         }
149
150         CURLcode cc;
151
152         cc = curl_easy_setopt (_curl, CURLOPT_WRITEDATA, (void *)&mem); CCERR ("CURLOPT_WRITEDATA");
153         cc = curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); CCERR ("CURLOPT_WRITEFUNCTION");
154         cc = curl_easy_setopt (_curl, CURLOPT_HEADERDATA, (void *)&nfo); CCERR ("CURLOPT_HEADERDATA");
155         cc = curl_easy_setopt (_curl, CURLOPT_HEADERFUNCTION, headerCallback); CCERR ("CURLOPT_HEADERFUNCTION");
156         cc = curl_easy_setopt (_curl, CURLOPT_USERAGENT, PROGRAM_NAME VERSIONSTRING); CCERR ("CURLOPT_USERAGENT");
157         cc = curl_easy_setopt (_curl, CURLOPT_TIMEOUT, ARDOUR_CURL_TIMEOUT); CCERR ("CURLOPT_TIMEOUT");
158         cc = curl_easy_setopt (_curl, CURLOPT_NOSIGNAL, 1); CCERR ("CURLOPT_NOSIGNAL");
159         cc = curl_easy_setopt (_curl, CURLOPT_ERRORBUFFER, error_buffer); CCERR ("CURLOPT_ERRORBUFFER");
160         // cc= curl_easy_setopt (_curl, CURLOPT_FOLLOWLOCATION, 1); CCERR ("CURLOPT_FOLLOWLOCATION");
161
162         // by default use curl's default.
163         if (ssl && ca_info) {
164                 curl_easy_setopt (_curl, CURLOPT_CAINFO, ca_info);
165         }
166         if (ssl && ca_path) {
167                 curl_easy_setopt (_curl, CURLOPT_CAPATH, ca_path);
168         }
169 }
170
171 HttpGet::~HttpGet ()
172 {
173         if (_curl) {
174                 curl_easy_cleanup (_curl);
175         }
176         if (!persist) {
177                 free (mem.data);
178         }
179 }
180
181 char*
182 HttpGet::get (const char* url, bool with_error_logging)
183 {
184 #ifdef ARDOURCURLDEBUG
185         std::cerr << "HttpGet::get() ---- new request ---"<< std::endl;
186 #endif
187         _status = _result = -1;
188         if (!_curl || !url) {
189                 if (with_error_logging) {
190                         PBD::error << "HttpGet::get() not initialized (or NULL url)"<< endmsg;
191                 }
192 #ifdef ARDOURCURLDEBUG
193                 std::cerr << "HttpGet::get() not initialized (or NULL url)"<< std::endl;
194 #endif
195                 return NULL;
196         }
197
198         if (strncmp ("http://", url, 7) && strncmp ("https://", url, 8)) {
199                 if (with_error_logging) {
200                         PBD::error << "HttpGet::get() not a http[s] URL"<< endmsg;
201                 }
202 #ifdef ARDOURCURLDEBUG
203                 std::cerr << "HttpGet::get() not a http[s] URL"<< std::endl;
204 #endif
205                 return NULL;
206         }
207
208         if (!persist) {
209                 free (mem.data);
210         } // otherwise caller is expected to have free()d or re-used it.
211
212         error_buffer[0] = 0;
213         mem.data = NULL;
214         mem.size = 0;
215
216         CURLcode cc;
217
218         cc = curl_easy_setopt (_curl, CURLOPT_URL, url);
219         CCERR ("CURLOPT_URL");
220         _result = curl_easy_perform (_curl);
221         cc = curl_easy_getinfo (_curl, CURLINFO_RESPONSE_CODE, &_status);
222         CCERR ("CURLINFO_RESPONSE_CODE,");
223
224         if (_result) {
225                 if (with_error_logging) {
226                         PBD::error << string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer) << endmsg;
227                 }
228 #ifdef ARDOURCURLDEBUG
229                 std::cerr << string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer) << std::endl;
230 #endif
231                 return NULL;
232         }
233         if (_status != 200) {
234                 if (with_error_logging) {
235                 PBD::error << string_compose (_("HTTP request status: %1"), _status) << endmsg;
236         }
237 #ifdef ARDOURCURLDEBUG
238                 std::cerr << string_compose (_("HTTP request status: %1"), _status) << std::endl;
239 #endif
240                 return NULL;
241         }
242
243         return mem.data;
244 }
245
246 std::string
247 HttpGet::error () const {
248         if (_result != 0) {
249                 return string_compose (_("HTTP request failed: (%1) %2"), _result, error_buffer);
250         }
251         if (_status != 200) {
252                 return string_compose (_("HTTP request status: %1"), _status);
253         }
254         return "No Error";
255 }
256
257 char*
258 ArdourCurl::http_get (const char* url, int* status, bool with_error_logging) {
259         HttpGet h (true);
260         char* rv = h.get (url, with_error_logging);
261         if (status) {
262                 *status = h.status ();
263         }
264         return rv;
265 }
266
267 std::string
268 ArdourCurl::http_get (const std::string& url, bool with_error_logging) {
269         return HttpGet (false).get (url, with_error_logging);
270 }