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"
27 #include <curl/curl.h>
29 #include <boost/optional.hpp>
30 #include <boost/filesystem.hpp>
31 #include <boost/algorithm/string.hpp>
40 using boost::algorithm::trim;
41 using boost::optional;
45 ls_url_data (void* buffer, size_t size, size_t nmemb, void* output)
47 string* s = reinterpret_cast<string*>(output);
48 char* c = reinterpret_cast<char*>(buffer);
49 for (size_t i = 0; i < (size * nmemb); ++i) {
59 auto curl = curl_easy_init ();
60 curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
61 curl_easy_setopt (curl, CURLOPT_DIRLISTONLY, 1);
64 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ls_url_data);
65 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls);
66 auto const cr = curl_easy_perform (curl);
74 for (size_t i = 0; i < ls.size(); ++i) {
78 result.back() += ls[i];
88 get_from_url_data (void* buffer, size_t size, size_t nmemb, void* stream)
90 auto f = reinterpret_cast<FILE*> (stream);
91 return fwrite (buffer, size, nmemb, f);
96 get_from_url (string url, bool pasv, bool skip_pasv_ip, ScopedTemporary& temp)
98 auto curl = curl_easy_init ();
99 curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
101 auto f = temp.open ("wb");
102 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, get_from_url_data);
103 curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
104 curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
105 curl_easy_setopt (curl, CURLOPT_FTP_USE_EPRT, 0);
107 curl_easy_setopt (curl, CURLOPT_FTP_SKIP_PASV_IP, 1);
110 curl_easy_setopt (curl, CURLOPT_FTPPORT, "-");
113 /* Maximum time is 20s */
114 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
116 auto const cr = curl_easy_perform (curl);
119 curl_easy_cleanup (curl);
120 if (cr != CURLE_OK) {
121 return String::compose (_("Download failed (%1 error %2)"), url, (int) cr);
129 get_from_url (string url, bool pasv, bool skip_pasv_ip, function<optional<string> (boost::filesystem::path, string)> load)
131 ScopedTemporary temp;
132 auto e = get_from_url (url, pasv, skip_pasv_ip, temp);
136 return load (temp.file(), url);
140 /** @param url URL of ZIP file.
141 * @param file Filename within ZIP file.
142 * @param load Function passed a (temporary) filesystem path of the unpacked file.
145 get_from_zip_url (string url, string file, bool pasv, bool skip_pasv_ip, function<optional<string> (boost::filesystem::path, string)> load)
147 /* Download the ZIP file to temp_zip */
148 ScopedTemporary temp_zip;
149 auto e = get_from_url (url, pasv, skip_pasv_ip, temp_zip);
154 /* Open the ZIP file and read `file' out of it */
156 #ifdef DCPOMATIC_HAVE_ZIP_SOURCE_T
157 /* This is the way to do it with newer versions of libzip, and is required on Windows.
158 The zip_source_t API is missing in the libzip versions shipped with Ubuntu 14.04,
159 Centos 6, Centos 7, Debian 7 and Debian 8.
162 auto zip_file = fopen_boost (temp_zip.file (), "rb");
164 return optional<string> (_("Could not open downloaded ZIP file"));
167 auto zip_source = zip_source_filep_create (zip_file, 0, -1, 0);
169 return optional<string> (_("Could not open downloaded ZIP file"));
173 zip_error_init (&error);
174 auto zip = zip_open_from_source (zip_source, ZIP_RDONLY, &error);
176 return String::compose (_("Could not open downloaded ZIP file (%1:%2: %3)"), error.zip_err, error.sys_err, error.str ? error.str : "");
180 struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
183 struct zip_file* file_in_zip = zip_fopen (zip, file.c_str(), 0);
185 return optional<string> (_("Unexpected ZIP file contents"));
188 ScopedTemporary temp_cert;
189 auto f = temp_cert.open ("wb");
192 int const N = zip_fread (file_in_zip, buffer, sizeof (buffer));
193 checked_fwrite (buffer, N, f, temp_cert.file());
194 if (N < int (sizeof (buffer))) {
198 zip_fclose (file_in_zip);
202 return load (temp_cert.file(), url);