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