2 // Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 // 2014 Ingo Ruhnke <grumbel@gmail.com>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "addon/downloader.hpp"
25 #include "util/log.hpp"
30 size_t my_curl_string_append(void* ptr, size_t size, size_t nmemb, void* userdata)
32 std::string& s = *static_cast<std::string*>(userdata);
33 std::string buf(static_cast<char*>(ptr), size * nmemb);
35 log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
39 size_t my_curl_physfs_write(void* ptr, size_t size, size_t nmemb, void* userdata)
41 PHYSFS_file* f = static_cast<PHYSFS_file*>(userdata);
42 PHYSFS_sint64 written = PHYSFS_write(f, ptr, size, nmemb);
43 log_debug << "read " << size * nmemb << " bytes of data..." << std::endl;
44 return size * written;
52 Downloader& m_downloader;
57 std::array<char, CURL_ERROR_SIZE> m_error_buffer;
59 TransferStatusPtr m_status;
62 Transfer(Downloader& downloader, TransferId id, const std::string& url) :
63 m_downloader(downloader),
66 m_handle(curl_easy_init()),
68 m_status(new TransferStatus(id))
72 throw std::runtime_error("curl_easy_init() failed");
76 curl_easy_setopt(m_handle, CURLOPT_URL, url.c_str());
77 curl_easy_setopt(m_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
79 curl_easy_setopt(m_handle, CURLOPT_WRITEDATA, this);
80 curl_easy_setopt(m_handle, CURLOPT_WRITEFUNCTION,
81 [](void* ptr, size_t size, size_t nmemb, void* userdata) -> size_t
83 return static_cast<Transfer*>(userdata)
84 ->on_data(ptr, size, nmemb);
87 curl_easy_setopt(m_handle, CURLOPT_ERRORBUFFER, m_error_buffer.data());
88 curl_easy_setopt(m_handle, CURLOPT_NOSIGNAL, 1);
89 curl_easy_setopt(m_handle, CURLOPT_FAILONERROR, 1);
90 curl_easy_setopt(m_handle, CURLOPT_FOLLOWLOCATION, 1);
92 curl_easy_setopt(m_handle, CURLOPT_NOPROGRESS, 0);
93 curl_easy_setopt(m_handle, CURLOPT_XFERINFODATA, this);
94 curl_easy_setopt(m_handle, CURLOPT_XFERINFOFUNCTION,
96 curl_off_t dltotal, curl_off_t dlnow,
97 curl_off_t ultotal, curl_off_t ulnow)
99 return static_cast<Transfer*>(userdata)
100 ->on_progress(dltotal, dlnow,
108 curl_easy_cleanup(m_handle);
111 TransferStatusPtr get_status() const
116 TransferId get_id() const
121 CURL* get_curl_handle() const
126 std::string get_url() const
131 size_t on_data(void* ptr, size_t size, size_t nmemb)
136 void on_progress(curl_off_t dltotal, curl_off_t dlnow,
137 curl_off_t ultotal, curl_off_t ulnow)
139 m_status->dltotal = dltotal;
140 m_status->dlnow = dlnow;
142 m_status->ultotal = ultotal;
143 m_status->ulnow = ulnow;
147 Transfer(const Transfer&) = delete;
148 Transfer& operator=(const Transfer&) = delete;
151 Downloader::Downloader() :
154 m_next_transfer_id(1)
156 curl_global_init(CURL_GLOBAL_ALL);
157 m_multi_handle = curl_multi_init();
160 throw std::runtime_error("curl_multi_init() failed");
164 Downloader::~Downloader()
166 for(auto& transfer : m_transfers)
168 curl_multi_remove_handle(m_multi_handle, transfer->get_curl_handle());
172 curl_multi_cleanup(m_multi_handle);
173 curl_global_cleanup();
177 Downloader::download(const std::string& url,
178 size_t (*write_func)(void* ptr, size_t size, size_t nmemb, void* userdata),
181 log_info << "Downloading " << url << std::endl;
183 char error_buffer[CURL_ERROR_SIZE+1];
185 CURL* curl_handle = curl_easy_init();
186 curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
187 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SuperTux/" PACKAGE_VERSION " libcURL");
188 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_func);
189 curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, userdata);
190 curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error_buffer);
191 curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
192 curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
193 curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
194 curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
195 CURLcode result = curl_easy_perform(curl_handle);
196 curl_easy_cleanup(curl_handle);
198 if (result != CURLE_OK)
200 std::string why = error_buffer[0] ? error_buffer : "unhandled error";
201 throw std::runtime_error(url + ": download failed: " + why);
206 Downloader::download(const std::string& url)
209 download(url, my_curl_string_append, &result);
214 Downloader::download(const std::string& url, const std::string& filename)
216 std::unique_ptr<PHYSFS_file, int(*)(PHYSFS_File*)> fout(PHYSFS_openWrite(filename.c_str()),
218 download(url, my_curl_physfs_write, fout.get());
222 Downloader::abort(TransferId id)
224 auto it = std::find_if(m_transfers.begin(), m_transfers.end(),
225 [&id](const std::unique_ptr<Transfer>& rhs)
227 return id == rhs->get_id();
229 if (it == m_transfers.end())
231 log_warning << "transfer not found: " << id << std::endl;
235 curl_multi_remove_handle(m_multi_handle, (*it)->get_curl_handle());
236 m_transfers.erase(it);
243 // read data from the network
246 while((ret = curl_multi_perform(m_multi_handle, &running_handles)) == CURLM_CALL_MULTI_PERFORM)
249 // check if any downloads got finished
252 while ((msg = curl_multi_info_read(m_multi_handle, &msgs_in_queue)))
257 curl_multi_remove_handle(m_multi_handle, msg->easy_handle);
258 //FIXME: finish_transfer(msg->easy_handle);
262 log_warning << "unhandled cURL message: " << msg->msg << std::endl;
269 Downloader::request_download(const std::string& url, const std::string& filename)
271 std::unique_ptr<Transfer> transfer(new Transfer(*this, m_next_transfer_id++, url));
272 curl_multi_add_handle(m_multi_handle, transfer->get_curl_handle());
273 m_transfers.push_back(std::move(transfer));
274 return m_transfers.back()->get_status();