2 Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "compose.hpp"
24 #include "exceptions.h"
25 #include "scoped_temporary.h"
28 #include <curl/curl.h>
30 #include <boost/optional.hpp>
31 #include <boost/filesystem.hpp>
32 #include <boost/algorithm/string.hpp>
41 using boost::algorithm::trim;
42 using boost::optional;
46 ls_url_data (void* buffer, size_t size, size_t nmemb, void* output)
48 string* s = reinterpret_cast<string*>(output);
49 char* c = reinterpret_cast<char*>(buffer);
50 for (size_t i = 0; i < (size * nmemb); ++i) {
60 auto curl = curl_easy_init ();
61 curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
62 curl_easy_setopt (curl, CURLOPT_DIRLISTONLY, 1);
65 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ls_url_data);
66 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls);
67 auto const cr = curl_easy_perform (curl);
75 for (size_t i = 0; i < ls.size(); ++i) {
78 } else if (ls[i] != '\r') {
79 result.back() += ls[i];
89 get_from_url_data (void* buffer, size_t size, size_t nmemb, void* stream)
91 auto f = reinterpret_cast<FILE*> (stream);
92 return fwrite (buffer, size, nmemb, f);
97 get_from_url (string url, bool pasv, bool skip_pasv_ip, ScopedTemporary& temp)
99 auto curl = curl_easy_init ();
100 curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
102 auto& f = temp.open ("wb");
103 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, get_from_url_data);
104 curl_easy_setopt (curl, CURLOPT_WRITEDATA, f.get());
105 curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
106 curl_easy_setopt (curl, CURLOPT_FTP_USE_EPRT, 0);
108 curl_easy_setopt (curl, CURLOPT_FTP_SKIP_PASV_IP, 1);
111 curl_easy_setopt (curl, CURLOPT_FTPPORT, "-");
114 /* Maximum time is 20s */
115 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
117 auto const cr = curl_easy_perform (curl);
120 curl_easy_cleanup (curl);
121 if (cr != CURLE_OK) {
122 return String::compose (_("Download failed (%1 error %2)"), url, (int) cr);
130 get_from_url (string url, bool pasv, bool skip_pasv_ip, function<optional<string> (boost::filesystem::path, string)> load)
132 ScopedTemporary temp;
133 auto e = get_from_url (url, pasv, skip_pasv_ip, temp);
137 return load (temp.path(), url);
141 /** @param url URL of ZIP file.
142 * @param file Filename within ZIP file.
143 * @param load Function passed a (temporary) filesystem path of the unpacked file.
146 get_from_zip_url (string url, string file, bool pasv, bool skip_pasv_ip, function<optional<string> (boost::filesystem::path, string)> load)
148 /* Download the ZIP file to temp_zip */
149 ScopedTemporary temp_zip;
150 auto e = get_from_url (url, pasv, skip_pasv_ip, temp_zip);
155 /* Open the ZIP file and read `file' out of it */
157 #ifdef DCPOMATIC_HAVE_ZIP_SOURCE_T
158 /* This is the way to do it with newer versions of libzip, and is required on Windows.
159 The zip_source_t API is missing in the libzip versions shipped with Ubuntu 14.04,
160 Centos 6, Centos 7, Debian 7 and Debian 8.
163 auto& zip_file = temp_zip.open("rb");
165 return string(_("Could not open downloaded ZIP file"));
168 auto zip_source = zip_source_filep_create (zip_file.take(), 0, -1, 0);
170 return string(_("Could not open downloaded ZIP file"));
174 zip_error_init (&error);
175 auto zip = zip_open_from_source (zip_source, ZIP_RDONLY, &error);
177 return String::compose (_("Could not open downloaded ZIP file (%1:%2: %3)"), error.zip_err, error.sys_err, error.str ? error.str : "");
181 struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
184 struct zip_file* file_in_zip = zip_fopen (zip, file.c_str(), 0);
186 return string(_("Unexpected ZIP file contents"));
189 ScopedTemporary temp_cert;
190 auto& f = temp_cert.open ("wb");
193 int const N = zip_fread (file_in_zip, buffer, sizeof (buffer));
194 f.checked_write(buffer, N);
195 if (N < int (sizeof (buffer))) {
199 zip_fclose (file_in_zip);
203 return load (temp_cert.path(), url);