Fix the build for older macOS.
[dcpomatic.git] / src / lib / internet.cc
1 /*
2     Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "scoped_temporary.h"
23 #include "compose.hpp"
24 #include "exceptions.h"
25 #include "cross.h"
26 #include "util.h"
27 #include <curl/curl.h>
28 #include <zip.h>
29 #include <boost/optional.hpp>
30 #include <boost/filesystem.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <string>
33
34 #include "i18n.h"
35
36
37 using std::function;
38 using std::list;
39 using std::string;
40 using boost::optional;
41 using boost::algorithm::trim;
42
43
44 static size_t
45 ls_url_data (void* buffer, size_t size, size_t nmemb, void* output)
46 {
47         string* s = reinterpret_cast<string*>(output);
48         char* c = reinterpret_cast<char*>(buffer);
49         for (size_t i = 0; i < (size * nmemb); ++i) {
50                 *s += c[i];
51         }
52         return nmemb;
53 }
54
55
56 list<string>
57 ls_url (string url)
58 {
59         auto curl = curl_easy_init ();
60         curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
61         curl_easy_setopt (curl, CURLOPT_DIRLISTONLY, 1);
62
63         string ls;
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);
67
68         if (cr != CURLE_OK) {
69                 return list<string>();
70         }
71
72         list<string> result;
73         result.push_back("");
74         for (size_t i = 0; i < ls.size(); ++i) {
75                 if (ls[i] == '\n') {
76                         result.push_back("");
77                 } else {
78                         result.back() += ls[i];
79                 }
80         }
81
82         result.pop_back ();
83         return result;
84 }
85
86
87 static size_t
88 get_from_url_data (void* buffer, size_t size, size_t nmemb, void* stream)
89 {
90         auto f = reinterpret_cast<FILE*> (stream);
91         return fwrite (buffer, size, nmemb, f);
92 }
93
94
95 optional<string>
96 get_from_url (string url, bool pasv, bool skip_pasv_ip, ScopedTemporary& temp)
97 {
98         auto curl = curl_easy_init ();
99         curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
100
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);
106         if (skip_pasv_ip) {
107                 curl_easy_setopt (curl, CURLOPT_FTP_SKIP_PASV_IP, 1);
108         }
109         if (!pasv) {
110                 curl_easy_setopt (curl, CURLOPT_FTPPORT, "-");
111         }
112
113         /* Maximum time is 20s */
114         curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
115
116         CURLcode const cr = curl_easy_perform (curl);
117
118         temp.close ();
119         curl_easy_cleanup (curl);
120         if (cr != CURLE_OK) {
121                 return String::compose (_("Download failed (%1 error %2)"), url, (int) cr);
122         }
123
124         return optional<string>();
125 }
126
127
128 optional<string>
129 get_from_url (string url, bool pasv, bool skip_pasv_ip, function<optional<string> (boost::filesystem::path)> load)
130 {
131         ScopedTemporary temp;
132         auto e = get_from_url (url, pasv, skip_pasv_ip, temp);
133         if (e) {
134                 return e;
135         }
136         return load (temp.file());
137 }
138
139
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.
143  */
144 optional<string>
145 get_from_zip_url (string url, string file, bool pasv, bool skip_pasv_ip, function<optional<string> (boost::filesystem::path)> load)
146 {
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);
150         if (e) {
151                 return e;
152         }
153
154         /* Open the ZIP file and read `file' out of it */
155
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.
160         */
161
162         auto zip_file = fopen_boost (temp_zip.file (), "rb");
163         if (!zip_file) {
164                 return optional<string> (_("Could not open downloaded ZIP file"));
165         }
166
167         auto zip_source = zip_source_filep_create (zip_file, 0, -1, 0);
168         if (!zip_source) {
169                 return optional<string> (_("Could not open downloaded ZIP file"));
170         }
171
172         zip_error_t error;
173         zip_error_init (&error);
174         auto zip = zip_open_from_source (zip_source, ZIP_RDONLY, &error);
175         if (!zip) {
176                 return String::compose (_("Could not open downloaded ZIP file (%1:%2: %3)"), error.zip_err, error.sys_err, error.str ? error.str : "");
177         }
178
179 #else
180         struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
181 #endif
182
183         struct zip_file* file_in_zip = zip_fopen (zip, file.c_str(), 0);
184         if (!file_in_zip) {
185                 return optional<string> (_("Unexpected ZIP file contents"));
186         }
187
188         ScopedTemporary temp_cert;
189         auto f = temp_cert.open ("wb");
190         char buffer[4096];
191         while (true) {
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))) {
195                         break;
196                 }
197         }
198         zip_fclose (file_in_zip);
199         zip_close (zip);
200         temp_cert.close ();
201
202         return load (temp_cert.file());
203 }