2 Copyright (C) 2014 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.
21 #include <boost/function.hpp>
22 #include <boost/optional.hpp>
23 #include <boost/filesystem.hpp>
24 #include <curl/curl.h>
31 using std::stringstream;
33 using boost::optional;
34 using boost::function;
37 get_from_zip_url_data (void* buffer, size_t size, size_t nmemb, void* stream)
39 FILE* f = reinterpret_cast<FILE*> (stream);
40 return fwrite (buffer, size, nmemb, f);
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.
48 get_from_zip_url (string url, string file, function<void (boost::filesystem::path)> load)
50 /* Download the ZIP file to temp_zip */
51 CURL* curl = curl_easy_init ();
52 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
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);
59 CURLcode const cr = curl_easy_perform (curl);
62 curl_easy_cleanup (curl);
64 return String::compose (_("Download failed (%1/%2 error %3)"), url, file, cr);
67 /* Open the ZIP file and read `file' out of it */
69 struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
71 return optional<string> (_("Could not open downloaded ZIP file"));
74 struct zip_file* zip_file = zip_fopen (zip, file.c_str(), 0);
76 return optional<string> (_("Unexpected ZIP file contents"));
79 ScopedTemporary temp_cert;
80 f = temp_cert.open ("wb");
83 int const N = zip_fread (zip_file, buffer, sizeof (buffer));
84 fwrite (buffer, 1, N, f);
85 if (N < int (sizeof (buffer))) {
91 load (temp_cert.file ());
92 return optional<string> ();
97 ftp_ls_data (void* buffer, size_t size, size_t nmemb, void* data)
99 string* s = reinterpret_cast<string *> (data);
100 uint8_t* b = reinterpret_cast<uint8_t *> (buffer);
101 for (size_t i = 0; i < (size * nmemb); ++i) {
110 CURL* curl = curl_easy_init ();
112 return list<string> ();
115 if (url.substr (url.length() - 1, 1) != "/") {
118 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
121 struct curl_slist* commands = 0;
122 commands = curl_slist_append (commands, "NLST");
123 curl_easy_setopt (curl, CURLOPT_POSTQUOTE, commands);
124 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls_raw);
125 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_ls_data);
126 curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
127 CURLcode const r = curl_easy_perform (curl);
129 return list<string> ();
132 stringstream s (ls_raw);
137 if (line.length() > 55) {
138 string const file = line.substr (55);
139 if (file != "." && file != "..") {
145 curl_easy_cleanup (curl);