2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "scoped_temporary.h"
21 #include "compose.hpp"
22 #include "safe_stringstream.h"
23 #include "exceptions.h"
24 #include <curl/curl.h>
26 #include <boost/function.hpp>
27 #include <boost/optional.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string.hpp>
36 using boost::optional;
37 using boost::function;
38 using boost::algorithm::trim;
41 get_from_zip_url_data (void* buffer, size_t size, size_t nmemb, void* stream)
43 FILE* f = reinterpret_cast<FILE*> (stream);
44 return fwrite (buffer, size, nmemb, f);
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.
52 get_from_zip_url (string url, string file, function<void (boost::filesystem::path)> load)
54 /* Download the ZIP file to temp_zip */
55 CURL* curl = curl_easy_init ();
56 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
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 /* Maximum time is 20s */
64 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
66 CURLcode const cr = curl_easy_perform (curl);
69 curl_easy_cleanup (curl);
71 return String::compose (_("Download failed (%1/%2 error %3)"), url, file, cr);
74 /* Open the ZIP file and read `file' out of it */
76 struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
78 return optional<string> (_("Could not open downloaded ZIP file"));
81 struct zip_file* zip_file = zip_fopen (zip, file.c_str(), 0);
83 return optional<string> (_("Unexpected ZIP file contents"));
86 ScopedTemporary temp_cert;
87 f = temp_cert.open ("wb");
90 int const N = zip_fread (zip_file, buffer, sizeof (buffer));
91 fwrite (buffer, 1, N, f);
92 if (N < int (sizeof (buffer))) {
98 load (temp_cert.file ());
99 return optional<string> ();
104 ftp_ls_data (void* buffer, size_t size, size_t nmemb, void* data)
106 string* s = reinterpret_cast<string *> (data);
107 uint8_t* b = reinterpret_cast<uint8_t *> (buffer);
108 for (size_t i = 0; i < (size * nmemb); ++i) {
115 ftp_ls (string url, bool pasv)
117 CURL* curl = curl_easy_init ();
119 throw NetworkError ("could not set up curl");
122 if (url.substr (url.length() - 1, 1) != "/") {
125 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
127 curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
130 struct curl_slist* commands = 0;
131 commands = curl_slist_append (commands, "NLST");
132 curl_easy_setopt (curl, CURLOPT_POSTQUOTE, commands);
133 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls_raw);
134 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_ls_data);
135 curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
137 curl_easy_setopt (curl, CURLOPT_FTPPORT, "");
139 CURLcode const r = curl_easy_perform (curl);
141 curl_easy_cleanup (curl);
142 throw NetworkError (curl_easy_strerror (r));
145 SafeStringStream s (ls_raw);
148 string line = s.getline ();
150 if (line.length() > 55) {
151 string const file = line.substr (55);
152 if (file != "." && file != "..") {
158 curl_easy_cleanup (curl);