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