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