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