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