1aaeccd36d1c9d64119f7871cd23134ea2c71d7b
[dcpomatic.git] / src / lib / internet.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "scoped_temporary.h"
21 #include "compose.hpp"
22 #include "safe_stringstream.h"
23 #include "exceptions.h"
24 #include <curl/curl.h>
25 #include <zip.h>
26 #include <boost/function.hpp>
27 #include <boost/optional.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <string>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::list;
36 using boost::optional;
37 using boost::function;
38 using boost::algorithm::trim;
39
40 static size_t
41 get_from_zip_url_data (void* buffer, size_t size, size_t nmemb, void* stream)
42 {
43         FILE* f = reinterpret_cast<FILE*> (stream);
44         return fwrite (buffer, size, nmemb, f);
45 }
46
47 /** @param url URL of ZIP file.
48  *  @param file Filename within ZIP file.
49  *  @param load Function passed a (temporary) filesystem path of the unpacked file.
50  */
51 optional<string>
52 get_from_zip_url (string url, string file, bool pasv, function<void (boost::filesystem::path)> load)
53 {
54         /* Download the ZIP file to temp_zip */
55         CURL* curl = curl_easy_init ();
56         curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
57
58         ScopedTemporary temp_zip;
59         FILE* f = temp_zip.open ("wb");
60         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, get_from_zip_url_data);
61         curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
62         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
63         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPRT, 0);
64         if (!pasv) {
65                 curl_easy_setopt (curl, CURLOPT_FTPPORT, "-");
66         }
67
68         /* Maximum time is 20s */
69         curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
70
71         CURLcode const cr = curl_easy_perform (curl);
72
73         temp_zip.close ();
74         curl_easy_cleanup (curl);
75         if (cr != CURLE_OK) {
76                 return String::compose (_("Download failed (%1/%2 error %3)"), url, file, cr);
77         }
78
79         /* Open the ZIP file and read `file' out of it */
80
81         struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
82         if (!zip) {
83                 return optional<string> (_("Could not open downloaded ZIP file"));
84         }
85
86         struct zip_file* zip_file = zip_fopen (zip, file.c_str(), 0);
87         if (!zip_file) {
88                 return optional<string> (_("Unexpected ZIP file contents"));
89         }
90
91         ScopedTemporary temp_cert;
92         f = temp_cert.open ("wb");
93         char buffer[4096];
94         while (true) {
95                 int const N = zip_fread (zip_file, buffer, sizeof (buffer));
96                 fwrite (buffer, 1, N, f);
97                 if (N < int (sizeof (buffer))) {
98                         break;
99                 }
100         }
101         temp_cert.close ();
102
103         load (temp_cert.file ());
104         return optional<string> ();
105 }
106
107 static size_t
108 ftp_ls_data (void* buffer, size_t size, size_t nmemb, void* data)
109 {
110         string* s = reinterpret_cast<string *> (data);
111         uint8_t* b = reinterpret_cast<uint8_t *> (buffer);
112         for (size_t i = 0; i < (size * nmemb); ++i) {
113                 *s += b[i];
114         }
115         return size * nmemb;
116 }
117
118 list<string>
119 ftp_ls (string url, bool pasv)
120 {
121         CURL* curl = curl_easy_init ();
122         if (!curl) {
123                 throw NetworkError ("could not set up curl");
124         }
125
126         if (url.substr (url.length() - 1, 1) != "/") {
127                 url += "/";
128         }
129         curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
130         /* 20s timeout */
131         curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
132
133         string ls_raw;
134         struct curl_slist* commands = 0;
135         commands = curl_slist_append (commands, "NLST");
136         curl_easy_setopt (curl, CURLOPT_POSTQUOTE, commands);
137         curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls_raw);
138         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_ls_data);
139         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
140         curl_easy_setopt (curl, CURLOPT_FTP_USE_EPRT, 0);
141         curl_easy_setopt (curl, CURLOPT_VERBOSE, 1);
142         if (!pasv) {
143                 curl_easy_setopt (curl, CURLOPT_FTPPORT, "-");
144         }
145         CURLcode const r = curl_easy_perform (curl);
146         if (r != CURLE_OK) {
147                 curl_easy_cleanup (curl);
148                 throw NetworkError (curl_easy_strerror (r));
149         }
150
151         SafeStringStream s (ls_raw);
152         list<string> ls;
153         while (s.good ()) {
154                 string line = s.getline ();
155                 trim (line);
156                 if (line.length() > 55) {
157                         string const file = line.substr (55);
158                         if (file != "." && file != "..") {
159                                 ls.push_back (file);
160                         }
161                 }
162         }
163
164         curl_easy_cleanup (curl);
165
166         return ls;
167 }