1e8c5d8e9e19dcdb2c4af155ca29ecdfec3c781c
[dcpomatic.git] / src / lib / internet.cc
1 /*
2     Copyright (C) 2014-2015 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 <curl/curl.h>
26 #include <zip.h>
27 #include <boost/function.hpp>
28 #include <boost/optional.hpp>
29 #include <boost/filesystem.hpp>
30 #include <boost/algorithm/string.hpp>
31 #include <string>
32
33 #include "i18n.h"
34
35 using std::string;
36 using std::list;
37 using boost::optional;
38 using boost::function;
39 using boost::algorithm::trim;
40
41 static size_t
42 get_from_zip_url_data (void* buffer, size_t size, size_t nmemb, void* stream)
43 {
44         FILE* f = reinterpret_cast<FILE*> (stream);
45         return fwrite (buffer, size, nmemb, f);
46 }
47
48 /** @param url URL of ZIP file.
49  *  @param file Filename within ZIP file.
50  *  @param load Function passed a (temporary) filesystem path of the unpacked file.
51  */
52 optional<string>
53 get_from_zip_url (string url, string file, bool pasv, function<void (boost::filesystem::path)> load)
54 {
55         /* Download the ZIP file to temp_zip */
56         CURL* curl = curl_easy_init ();
57         curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
58
59         ScopedTemporary temp_zip;
60         FILE* f = temp_zip.open ("wb");
61         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, get_from_zip_url_data);
62         curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
63         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
64         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPRT, 0);
65         if (!pasv) {
66                 curl_easy_setopt (curl, CURLOPT_FTPPORT, "-");
67         }
68
69         /* Maximum time is 20s */
70         curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
71
72         CURLcode const cr = curl_easy_perform (curl);
73
74         temp_zip.close ();
75         curl_easy_cleanup (curl);
76         if (cr != CURLE_OK) {
77                 return String::compose (_("Download failed (%1/%2 error %3)"), url, file, (int) cr);
78         }
79
80         /* Open the ZIP file and read `file' out of it */
81
82         FILE* zip_file = fopen_boost (temp_zip.file (), "rb");
83         if (!zip_file) {
84                 return optional<string> (_("Could not open downloaded ZIP file"));
85         }
86
87         zip_source_t* zip_source = zip_source_filep_create (zip_file, 0, -1, 0);
88         if (!zip_source) {
89                 return optional<string> (_("Could not open downloaded ZIP file"));
90         }
91
92         zip_t* zip = zip_open_from_source (zip_source, 0, 0);
93         if (!zip) {
94                 return optional<string> (_("Could not open downloaded ZIP file"));
95         }
96
97         struct zip_file* file_in_zip = zip_fopen (zip, file.c_str(), 0);
98         if (!file_in_zip) {
99                 return optional<string> (_("Unexpected ZIP file contents"));
100         }
101
102         ScopedTemporary temp_cert;
103         f = temp_cert.open ("wb");
104         char buffer[4096];
105         while (true) {
106                 int const N = zip_fread (file_in_zip, buffer, sizeof (buffer));
107                 fwrite (buffer, 1, N, f);
108                 if (N < int (sizeof (buffer))) {
109                         break;
110                 }
111         }
112         zip_fclose (file_in_zip);
113         zip_close (zip);
114         temp_cert.close ();
115
116         load (temp_cert.file ());
117         return optional<string> ();
118 }
119
120 static size_t
121 ftp_ls_data (void* buffer, size_t size, size_t nmemb, void* data)
122 {
123         string* s = reinterpret_cast<string *> (data);
124         uint8_t* b = reinterpret_cast<uint8_t *> (buffer);
125         for (size_t i = 0; i < (size * nmemb); ++i) {
126                 *s += b[i];
127         }
128         return size * nmemb;
129 }
130
131 list<string>
132 ftp_ls (string url, bool pasv)
133 {
134         CURL* curl = curl_easy_init ();
135         if (!curl) {
136                 throw NetworkError ("could not set up curl");
137         }
138
139         if (url.substr (url.length() - 1, 1) != "/") {
140                 url += "/";
141         }
142         curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
143         /* 20s timeout */
144         curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
145
146         string ls_raw;
147         struct curl_slist* commands = 0;
148         commands = curl_slist_append (commands, "NLST");
149         curl_easy_setopt (curl, CURLOPT_POSTQUOTE, commands);
150         curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls_raw);
151         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_ls_data);
152         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
153         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPRT, 0);
154         curl_easy_setopt (curl, CURLOPT_VERBOSE, 1);
155         if (!pasv) {
156                 curl_easy_setopt (curl, CURLOPT_FTPPORT, "-");
157         }
158         CURLcode const r = curl_easy_perform (curl);
159         if (r != CURLE_OK) {
160                 curl_easy_cleanup (curl);
161                 throw NetworkError (curl_easy_strerror (r));
162         }
163
164         list<string> ls;
165         string line;
166         for (size_t i = 0; i < ls_raw.length(); ++i) {
167                 line += ls_raw[i];
168                 if (ls_raw[i] == '\n') {
169                         trim (line);
170                         if (line.length() > 55) {
171                                 string const file = line.substr (55);
172                                 if (file != "." && file != "..") {
173                                         ls.push_back (file);
174                                 }
175                         }
176                         line = "";
177                 }
178         }
179
180         curl_easy_cleanup (curl);
181
182         return ls;
183 }