Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / internet.cc
1 /*
2     Copyright (C) 2014-2019 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 #include "scoped_temporary.h"
22 #include "compose.hpp"
23 #include "exceptions.h"
24 #include "cross.h"
25 #include "util.h"
26 #include <curl/curl.h>
27 #include <zip.h>
28 #include <boost/function.hpp>
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 using std::string;
37 using std::list;
38 using boost::optional;
39 using boost::function;
40 using boost::algorithm::trim;
41
42 static size_t
43 ls_url_data (void* buffer, size_t size, size_t nmemb, void* output)
44 {
45         string* s = reinterpret_cast<string*>(output);
46         char* c = reinterpret_cast<char*>(buffer);
47         for (size_t i = 0; i < (size * nmemb); ++i) {
48                 *s += c[i];
49         }
50         return nmemb;
51 }
52
53 list<string>
54 ls_url (string url)
55 {
56         CURL* curl = curl_easy_init ();
57         curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
58         curl_easy_setopt (curl, CURLOPT_DIRLISTONLY, 1);
59
60         string ls;
61         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ls_url_data);
62         curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls);
63         CURLcode const cr = curl_easy_perform (curl);
64
65         if (cr != CURLE_OK) {
66                 return list<string>();
67         }
68
69         list<string> result;
70         result.push_back("");
71         for (size_t i = 0; i < ls.size(); ++i) {
72                 if (ls[i] == '\n') {
73                         result.push_back("");
74                 } else {
75                         result.back() += ls[i];
76                 }
77         }
78
79         result.pop_back ();
80         return result;
81 }
82
83 static size_t
84 get_from_url_data (void* buffer, size_t size, size_t nmemb, void* stream)
85 {
86         FILE* f = reinterpret_cast<FILE*> (stream);
87         return fwrite (buffer, size, nmemb, f);
88 }
89
90 optional<string>
91 get_from_url (string url, bool pasv, bool skip_pasv_ip, ScopedTemporary& temp)
92 {
93         CURL* curl = curl_easy_init ();
94         curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
95
96         FILE* f = temp.open ("wb");
97         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, get_from_url_data);
98         curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
99         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
100         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPRT, 0);
101         if (skip_pasv_ip) {
102                 curl_easy_setopt (curl, CURLOPT_FTP_SKIP_PASV_IP, 1);
103         }
104         if (!pasv) {
105                 curl_easy_setopt (curl, CURLOPT_FTPPORT, "-");
106         }
107
108         /* Maximum time is 20s */
109         curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
110
111         CURLcode const cr = curl_easy_perform (curl);
112
113         temp.close ();
114         curl_easy_cleanup (curl);
115         if (cr != CURLE_OK) {
116                 return String::compose (_("Download failed (%1 error %2)"), url, (int) cr);
117         }
118
119         return optional<string>();
120 }
121
122 optional<string>
123 get_from_url (string url, bool pasv, bool skip_pasv_ip, function<void (boost::filesystem::path)> load)
124 {
125         ScopedTemporary temp;
126         optional<string> e = get_from_url (url, pasv, skip_pasv_ip, temp);
127         if (e) {
128                 return e;
129         }
130         load (temp.file());
131         return optional<string>();
132 }
133
134 /** @param url URL of ZIP file.
135  *  @param file Filename within ZIP file.
136  *  @param load Function passed a (temporary) filesystem path of the unpacked file.
137  */
138 optional<string>
139 get_from_zip_url (string url, string file, bool pasv, bool skip_pasv_ip, function<void (boost::filesystem::path)> load)
140 {
141         /* Download the ZIP file to temp_zip */
142         ScopedTemporary temp_zip;
143         optional<string> e = get_from_url (url, pasv, skip_pasv_ip, temp_zip);
144         if (e) {
145                 return e;
146         }
147
148         /* Open the ZIP file and read `file' out of it */
149
150 #ifdef DCPOMATIC_HAVE_ZIP_SOURCE_T
151         /* This is the way to do it with newer versions of libzip, and is required on Windows.
152            The zip_source_t API is missing in the libzip versions shipped with Ubuntu 14.04,
153            Centos 6, Centos 7, Debian 7 and Debian 8.
154         */
155
156         FILE* zip_file = fopen_boost (temp_zip.file (), "rb");
157         if (!zip_file) {
158                 return optional<string> (_("Could not open downloaded ZIP file"));
159         }
160
161         zip_source_t* zip_source = zip_source_filep_create (zip_file, 0, -1, 0);
162         if (!zip_source) {
163                 return optional<string> (_("Could not open downloaded ZIP file"));
164         }
165
166         zip_error_t error;
167         zip_error_init (&error);
168         zip_t* zip = zip_open_from_source (zip_source, ZIP_RDONLY, &error);
169         if (!zip) {
170                 return String::compose (_("Could not open downloaded ZIP file (%1:%2: %3)"), error.zip_err, error.sys_err, error.str ? error.str : "");
171         }
172
173 #else
174         struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
175 #endif
176
177         struct zip_file* file_in_zip = zip_fopen (zip, file.c_str(), 0);
178         if (!file_in_zip) {
179                 return optional<string> (_("Unexpected ZIP file contents"));
180         }
181
182         ScopedTemporary temp_cert;
183         FILE* f = temp_cert.open ("wb");
184         char buffer[4096];
185         while (true) {
186                 int const N = zip_fread (file_in_zip, buffer, sizeof (buffer));
187                 checked_fwrite (buffer, N, f, temp_cert.file());
188                 if (N < int (sizeof (buffer))) {
189                         break;
190                 }
191         }
192         zip_fclose (file_in_zip);
193         zip_close (zip);
194         temp_cert.close ();
195
196         load (temp_cert.file ());
197         return optional<string> ();
198 }