]> git.lizzy.rs Git - dragonfireclient.git/blob - src/httpfetch.cpp
1883cf263c9c1ec18a9eec9e5045abb9efadde1a
[dragonfireclient.git] / src / httpfetch.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "httpfetch.h"
21 #include "porting.h" // for sleep_ms(), get_sysinfo(), secure_rand_fill_buf()
22 #include <iostream>
23 #include <sstream>
24 #include <list>
25 #include <map>
26 #include <cerrno>
27 #include <mutex>
28 #include "network/socket.h" // for select()
29 #include "threading/event.h"
30 #include "config.h"
31 #include "exceptions.h"
32 #include "debug.h"
33 #include "log.h"
34 #include "util/container.h"
35 #include "util/thread.h"
36 #include "version.h"
37 #include "settings.h"
38 #include "noise.h"
39
40 std::mutex g_httpfetch_mutex;
41 std::map<unsigned long, std::queue<HTTPFetchResult>> g_httpfetch_results;
42 PcgRandom g_callerid_randomness;
43
44 HTTPFetchRequest::HTTPFetchRequest() :
45                 timeout(g_settings->getS32("curl_timeout")), connect_timeout(timeout),
46                 useragent(std::string(PROJECT_NAME_C "/") + g_version_hash + " (" +
47                                 porting::get_sysinfo() + ")")
48 {
49 }
50
51 static void httpfetch_deliver_result(const HTTPFetchResult &fetch_result)
52 {
53         unsigned long caller = fetch_result.caller;
54         if (caller != HTTPFETCH_DISCARD) {
55                 MutexAutoLock lock(g_httpfetch_mutex);
56                 g_httpfetch_results[caller].push(fetch_result);
57         }
58 }
59
60 static void httpfetch_request_clear(unsigned long caller);
61
62 unsigned long httpfetch_caller_alloc()
63 {
64         MutexAutoLock lock(g_httpfetch_mutex);
65
66         // Check each caller ID except HTTPFETCH_DISCARD
67         const unsigned long discard = HTTPFETCH_DISCARD;
68         for (unsigned long caller = discard + 1; caller != discard; ++caller) {
69                 std::map<unsigned long, std::queue<HTTPFetchResult>>::iterator it =
70                                 g_httpfetch_results.find(caller);
71                 if (it == g_httpfetch_results.end()) {
72                         verbosestream << "httpfetch_caller_alloc: allocating " << caller
73                                       << std::endl;
74                         // Access element to create it
75                         g_httpfetch_results[caller];
76                         return caller;
77                 }
78         }
79
80         FATAL_ERROR("httpfetch_caller_alloc: ran out of caller IDs");
81         return discard;
82 }
83
84 unsigned long httpfetch_caller_alloc_secure()
85 {
86         MutexAutoLock lock(g_httpfetch_mutex);
87
88         // Generate random caller IDs and make sure they're not
89         // already used or equal to HTTPFETCH_DISCARD
90         // Give up after 100 tries to prevent infinite loop
91         u8 tries = 100;
92         unsigned long caller;
93
94         do {
95                 caller = (((u64)g_callerid_randomness.next()) << 32) |
96                          g_callerid_randomness.next();
97
98                 if (--tries < 1) {
99                         FATAL_ERROR("httpfetch_caller_alloc_secure: ran out of caller "
100                                     "IDs");
101                         return HTTPFETCH_DISCARD;
102                 }
103         } while (g_httpfetch_results.find(caller) != g_httpfetch_results.end());
104
105         verbosestream << "httpfetch_caller_alloc_secure: allocating " << caller
106                       << std::endl;
107
108         // Access element to create it
109         g_httpfetch_results[caller];
110         return caller;
111 }
112
113 void httpfetch_caller_free(unsigned long caller)
114 {
115         verbosestream << "httpfetch_caller_free: freeing " << caller << std::endl;
116
117         httpfetch_request_clear(caller);
118         if (caller != HTTPFETCH_DISCARD) {
119                 MutexAutoLock lock(g_httpfetch_mutex);
120                 g_httpfetch_results.erase(caller);
121         }
122 }
123
124 bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetch_result)
125 {
126         MutexAutoLock lock(g_httpfetch_mutex);
127
128         // Check that caller exists
129         std::map<unsigned long, std::queue<HTTPFetchResult>>::iterator it =
130                         g_httpfetch_results.find(caller);
131         if (it == g_httpfetch_results.end())
132                 return false;
133
134         // Check that result queue is nonempty
135         std::queue<HTTPFetchResult> &caller_results = it->second;
136         if (caller_results.empty())
137                 return false;
138
139         // Pop first result
140         fetch_result = caller_results.front();
141         caller_results.pop();
142         return true;
143 }
144
145 #if USE_CURL
146 #include <curl/curl.h>
147
148 /*
149         USE_CURL is on: use cURL based httpfetch implementation
150 */
151
152 static size_t httpfetch_writefunction(
153                 char *ptr, size_t size, size_t nmemb, void *userdata)
154 {
155         std::ostringstream *stream = (std::ostringstream *)userdata;
156         size_t count = size * nmemb;
157         stream->write(ptr, count);
158         return count;
159 }
160
161 static size_t httpfetch_discardfunction(
162                 char *ptr, size_t size, size_t nmemb, void *userdata)
163 {
164         return size * nmemb;
165 }
166
167 class CurlHandlePool
168 {
169         std::list<CURL *> handles;
170
171 public:
172         CurlHandlePool() = default;
173
174         ~CurlHandlePool()
175         {
176                 for (std::list<CURL *>::iterator it = handles.begin();
177                                 it != handles.end(); ++it) {
178                         curl_easy_cleanup(*it);
179                 }
180         }
181         CURL *alloc()
182         {
183                 CURL *curl;
184                 if (handles.empty()) {
185                         curl = curl_easy_init();
186                         if (curl == NULL) {
187                                 errorstream << "curl_easy_init returned NULL"
188                                             << std::endl;
189                         }
190                 } else {
191                         curl = handles.front();
192                         handles.pop_front();
193                 }
194                 return curl;
195         }
196         void free(CURL *handle)
197         {
198                 if (handle)
199                         handles.push_back(handle);
200         }
201 };
202
203 class HTTPFetchOngoing
204 {
205 public:
206         HTTPFetchOngoing(const HTTPFetchRequest &request, CurlHandlePool *pool);
207         ~HTTPFetchOngoing();
208
209         CURLcode start(CURLM *multi);
210         const HTTPFetchResult *complete(CURLcode res);
211
212         const HTTPFetchRequest &getRequest() const { return request; };
213         const CURL *getEasyHandle() const { return curl; };
214
215 private:
216         CurlHandlePool *pool;
217         CURL *curl;
218         CURLM *multi;
219         HTTPFetchRequest request;
220         HTTPFetchResult result;
221         std::ostringstream oss;
222         struct curl_slist *http_header;
223         curl_httppost *post;
224 };
225
226 HTTPFetchOngoing::HTTPFetchOngoing(
227                 const HTTPFetchRequest &request_, CurlHandlePool *pool_) :
228                 pool(pool_),
229                 curl(NULL), multi(NULL), request(request_), result(request_),
230                 oss(std::ios::binary), http_header(NULL), post(NULL)
231 {
232         curl = pool->alloc();
233         if (curl == NULL) {
234                 return;
235         }
236
237         // Set static cURL options
238         curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
239         curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
240         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
241         curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3);
242         curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
243
244         std::string bind_address = g_settings->get("bind_address");
245         if (!bind_address.empty()) {
246                 curl_easy_setopt(curl, CURLOPT_INTERFACE, bind_address.c_str());
247         }
248
249         if (!g_settings->getBool("enable_ipv6")) {
250                 curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
251         }
252
253 #if LIBCURL_VERSION_NUM >= 0x071304
254         // Restrict protocols so that curl vulnerabilities in
255         // other protocols don't affect us.
256         // These settings were introduced in curl 7.19.4.
257         long protocols =
258                         CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FTP | CURLPROTO_FTPS;
259         curl_easy_setopt(curl, CURLOPT_PROTOCOLS, protocols);
260         curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, protocols);
261 #endif
262
263         // Set cURL options based on HTTPFetchRequest
264         curl_easy_setopt(curl, CURLOPT_URL, request.url.c_str());
265         curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, request.timeout);
266         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, request.connect_timeout);
267
268         if (!request.useragent.empty())
269                 curl_easy_setopt(curl, CURLOPT_USERAGENT, request.useragent.c_str());
270
271         // Set up a write callback that writes to the
272         // ostringstream ongoing->oss, unless the data
273         // is to be discarded
274         if (request.caller == HTTPFETCH_DISCARD) {
275                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, httpfetch_discardfunction);
276                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
277         } else {
278                 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, httpfetch_writefunction);
279                 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &oss);
280         }
281
282         // Set POST (or GET) data
283         if (request.post_fields.empty() && request.post_data.empty()) {
284                 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
285         } else if (request.multipart) {
286                 curl_httppost *last = NULL;
287                 for (StringMap::iterator it = request.post_fields.begin();
288                                 it != request.post_fields.end(); ++it) {
289                         curl_formadd(&post, &last, CURLFORM_NAMELENGTH, it->first.size(),
290                                         CURLFORM_PTRNAME, it->first.c_str(),
291                                         CURLFORM_CONTENTSLENGTH, it->second.size(),
292                                         CURLFORM_PTRCONTENTS, it->second.c_str(),
293                                         CURLFORM_END);
294                 }
295                 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
296                 // request.post_fields must now *never* be
297                 // modified until CURLOPT_HTTPPOST is cleared
298         } else if (request.post_data.empty()) {
299                 curl_easy_setopt(curl, CURLOPT_POST, 1);
300                 std::string str;
301                 for (auto &post_field : request.post_fields) {
302                         if (!str.empty())
303                                 str += "&";
304                         str += urlencode(post_field.first);
305                         str += "=";
306                         str += urlencode(post_field.second);
307                 }
308                 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, str.size());
309                 curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, str.c_str());
310         } else {
311                 curl_easy_setopt(curl, CURLOPT_POST, 1);
312                 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, request.post_data.size());
313                 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.post_data.c_str());
314                 // request.post_data must now *never* be
315                 // modified until CURLOPT_POSTFIELDS is cleared
316         }
317         // Set additional HTTP headers
318         for (const std::string &extra_header : request.extra_headers) {
319                 http_header = curl_slist_append(http_header, extra_header.c_str());
320         }
321         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_header);
322
323         if (!g_settings->getBool("curl_verify_cert")) {
324                 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
325         }
326 }
327
328 CURLcode HTTPFetchOngoing::start(CURLM *multi_)
329 {
330         if (!curl)
331                 return CURLE_FAILED_INIT;
332
333         if (!multi_) {
334                 // Easy interface (sync)
335                 return curl_easy_perform(curl);
336         }
337
338         // Multi interface (async)
339         CURLMcode mres = curl_multi_add_handle(multi_, curl);
340         if (mres != CURLM_OK) {
341                 errorstream << "curl_multi_add_handle"
342                             << " returned error code " << mres << std::endl;
343                 return CURLE_FAILED_INIT;
344         }
345         multi = multi_; // store for curl_multi_remove_handle
346         return CURLE_OK;
347 }
348
349 const HTTPFetchResult *HTTPFetchOngoing::complete(CURLcode res)
350 {
351         result.succeeded = (res == CURLE_OK);
352         result.timeout = (res == CURLE_OPERATION_TIMEDOUT);
353         result.data = oss.str();
354
355         // Get HTTP/FTP response code
356         result.response_code = 0;
357         if (curl && (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
358                                      &result.response_code) != CURLE_OK)) {
359                 // We failed to get a return code, make sure it is still 0
360                 result.response_code = 0;
361         }
362
363         if (res != CURLE_OK) {
364                 errorstream << request.url << " not found (" << curl_easy_strerror(res)
365                             << ")"
366                             << " (response code " << result.response_code << ")"
367                             << std::endl;
368         }
369
370         return &result;
371 }
372
373 HTTPFetchOngoing::~HTTPFetchOngoing()
374 {
375         if (multi) {
376                 CURLMcode mres = curl_multi_remove_handle(multi, curl);
377                 if (mres != CURLM_OK) {
378                         errorstream << "curl_multi_remove_handle"
379                                     << " returned error code " << mres << std::endl;
380                 }
381         }
382
383         // Set safe options for the reusable cURL handle
384         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, httpfetch_discardfunction);
385         curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
386         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
387         if (http_header) {
388                 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
389                 curl_slist_free_all(http_header);
390         }
391         if (post) {
392                 curl_easy_setopt(curl, CURLOPT_HTTPPOST, NULL);
393                 curl_formfree(post);
394         }
395
396         // Store the cURL handle for reuse
397         pool->free(curl);
398 }
399
400 class CurlFetchThread : public Thread
401 {
402 protected:
403         enum RequestType
404         {
405                 RT_FETCH,
406                 RT_CLEAR,
407                 RT_WAKEUP,
408         };
409
410         struct Request
411         {
412                 RequestType type;
413                 HTTPFetchRequest fetch_request;
414                 Event *event;
415         };
416
417         CURLM *m_multi;
418         MutexedQueue<Request> m_requests;
419         size_t m_parallel_limit;
420
421         // Variables exclusively used within thread
422         std::vector<HTTPFetchOngoing *> m_all_ongoing;
423         std::list<HTTPFetchRequest> m_queued_fetches;
424
425 public:
426         CurlFetchThread(int parallel_limit) : Thread("CurlFetch")
427         {
428                 if (parallel_limit >= 1)
429                         m_parallel_limit = parallel_limit;
430                 else
431                         m_parallel_limit = 1;
432         }
433
434         void requestFetch(const HTTPFetchRequest &fetch_request)
435         {
436                 Request req;
437                 req.type = RT_FETCH;
438                 req.fetch_request = fetch_request;
439                 req.event = NULL;
440                 m_requests.push_back(req);
441         }
442
443         void requestClear(unsigned long caller, Event *event)
444         {
445                 Request req;
446                 req.type = RT_CLEAR;
447                 req.fetch_request.caller = caller;
448                 req.event = event;
449                 m_requests.push_back(req);
450         }
451
452         void requestWakeUp()
453         {
454                 Request req;
455                 req.type = RT_WAKEUP;
456                 req.event = NULL;
457                 m_requests.push_back(req);
458         }
459
460 protected:
461         // Handle a request from some other thread
462         // E.g. new fetch; clear fetches for one caller; wake up
463         void processRequest(const Request &req)
464         {
465                 if (req.type == RT_FETCH) {
466                         // New fetch, queue until there are less
467                         // than m_parallel_limit ongoing fetches
468                         m_queued_fetches.push_back(req.fetch_request);
469
470                         // see processQueued() for what happens next
471
472                 } else if (req.type == RT_CLEAR) {
473                         unsigned long caller = req.fetch_request.caller;
474
475                         // Abort all ongoing fetches for the caller
476                         for (std::vector<HTTPFetchOngoing *>::iterator it =
477                                                         m_all_ongoing.begin();
478                                         it != m_all_ongoing.end();) {
479                                 if ((*it)->getRequest().caller == caller) {
480                                         delete (*it);
481                                         it = m_all_ongoing.erase(it);
482                                 } else {
483                                         ++it;
484                                 }
485                         }
486
487                         // Also abort all queued fetches for the caller
488                         for (std::list<HTTPFetchRequest>::iterator it =
489                                                         m_queued_fetches.begin();
490                                         it != m_queued_fetches.end();) {
491                                 if ((*it).caller == caller)
492                                         it = m_queued_fetches.erase(it);
493                                 else
494                                         ++it;
495                         }
496                 } else if (req.type == RT_WAKEUP) {
497                         // Wakeup: Nothing to do, thread is awake at this point
498                 }
499
500                 if (req.event != NULL)
501                         req.event->signal();
502         }
503
504         // Start new ongoing fetches if m_parallel_limit allows
505         void processQueued(CurlHandlePool *pool)
506         {
507                 while (m_all_ongoing.size() < m_parallel_limit &&
508                                 !m_queued_fetches.empty()) {
509                         HTTPFetchRequest request = m_queued_fetches.front();
510                         m_queued_fetches.pop_front();
511
512                         // Create ongoing fetch data and make a cURL handle
513                         // Set cURL options based on HTTPFetchRequest
514                         HTTPFetchOngoing *ongoing = new HTTPFetchOngoing(request, pool);
515
516                         // Initiate the connection (curl_multi_add_handle)
517                         CURLcode res = ongoing->start(m_multi);
518                         if (res == CURLE_OK) {
519                                 m_all_ongoing.push_back(ongoing);
520                         } else {
521                                 httpfetch_deliver_result(*ongoing->complete(res));
522                                 delete ongoing;
523                         }
524                 }
525         }
526
527         // Process CURLMsg (indicates completion of a fetch)
528         void processCurlMessage(CURLMsg *msg)
529         {
530                 // Determine which ongoing fetch the message pertains to
531                 size_t i = 0;
532                 bool found = false;
533                 for (i = 0; i < m_all_ongoing.size(); ++i) {
534                         if (m_all_ongoing[i]->getEasyHandle() == msg->easy_handle) {
535                                 found = true;
536                                 break;
537                         }
538                 }
539                 if (msg->msg == CURLMSG_DONE && found) {
540                         // m_all_ongoing[i] succeeded or failed.
541                         HTTPFetchOngoing *ongoing = m_all_ongoing[i];
542                         httpfetch_deliver_result(*ongoing->complete(msg->data.result));
543                         delete ongoing;
544                         m_all_ongoing.erase(m_all_ongoing.begin() + i);
545                 }
546         }
547
548         // Wait for a request from another thread, or timeout elapses
549         void waitForRequest(long timeout)
550         {
551                 if (m_queued_fetches.empty()) {
552                         try {
553                                 Request req = m_requests.pop_front(timeout);
554                                 processRequest(req);
555                         } catch (ItemNotFoundException &e) {
556                         }
557                 }
558         }
559
560         // Wait until some IO happens, or timeout elapses
561         void waitForIO(long timeout)
562         {
563                 fd_set read_fd_set;
564                 fd_set write_fd_set;
565                 fd_set exc_fd_set;
566                 int max_fd;
567                 long select_timeout = -1;
568                 struct timeval select_tv;
569                 CURLMcode mres;
570
571                 FD_ZERO(&read_fd_set);
572                 FD_ZERO(&write_fd_set);
573                 FD_ZERO(&exc_fd_set);
574
575                 mres = curl_multi_fdset(m_multi, &read_fd_set, &write_fd_set, &exc_fd_set,
576                                 &max_fd);
577                 if (mres != CURLM_OK) {
578                         errorstream << "curl_multi_fdset"
579                                     << " returned error code " << mres << std::endl;
580                         select_timeout = 0;
581                 }
582
583                 mres = curl_multi_timeout(m_multi, &select_timeout);
584                 if (mres != CURLM_OK) {
585                         errorstream << "curl_multi_timeout"
586                                     << " returned error code " << mres << std::endl;
587                         select_timeout = 0;
588                 }
589
590                 // Limit timeout so new requests get through
591                 if (select_timeout < 0 || select_timeout > timeout)
592                         select_timeout = timeout;
593
594                 if (select_timeout > 0) {
595                         // in Winsock it is forbidden to pass three empty
596                         // fd_sets to select(), so in that case use sleep_ms
597                         if (max_fd != -1) {
598                                 select_tv.tv_sec = select_timeout / 1000;
599                                 select_tv.tv_usec = (select_timeout % 1000) * 1000;
600                                 int retval = select(max_fd + 1, &read_fd_set,
601                                                 &write_fd_set, &exc_fd_set, &select_tv);
602                                 if (retval == -1) {
603 #ifdef _WIN32
604                                         errorstream << "select returned error code "
605                                                     << WSAGetLastError() << std::endl;
606 #else
607                                         errorstream << "select returned error code "
608                                                     << errno << std::endl;
609 #endif
610                                 }
611                         } else {
612                                 sleep_ms(select_timeout);
613                         }
614                 }
615         }
616
617         void *run()
618         {
619                 CurlHandlePool pool;
620
621                 m_multi = curl_multi_init();
622                 if (m_multi == NULL) {
623                         errorstream << "curl_multi_init returned NULL\n";
624                         return NULL;
625                 }
626
627                 FATAL_ERROR_IF(!m_all_ongoing.empty(), "Expected empty");
628
629                 while (!stopRequested()) {
630                         BEGIN_DEBUG_EXCEPTION_HANDLER
631
632                         /*
633                                 Handle new async requests
634                         */
635
636                         while (!m_requests.empty()) {
637                                 Request req = m_requests.pop_frontNoEx();
638                                 processRequest(req);
639                         }
640                         processQueued(&pool);
641
642                         /*
643                                 Handle ongoing async requests
644                         */
645
646                         int still_ongoing = 0;
647                         while (curl_multi_perform(m_multi, &still_ongoing) ==
648                                         CURLM_CALL_MULTI_PERFORM)
649                                 /* noop */;
650
651                         /*
652                                 Handle completed async requests
653                         */
654                         if (still_ongoing < (int)m_all_ongoing.size()) {
655                                 CURLMsg *msg;
656                                 int msgs_in_queue;
657                                 msg = curl_multi_info_read(m_multi, &msgs_in_queue);
658                                 while (msg != NULL) {
659                                         processCurlMessage(msg);
660                                         msg = curl_multi_info_read(
661                                                         m_multi, &msgs_in_queue);
662                                 }
663                         }
664
665                         /*
666                                 If there are ongoing requests, wait for data
667                                 (with a timeout of 100ms so that new requests
668                                 can be processed).
669
670                                 If no ongoing requests, wait for a new request.
671                                 (Possibly an empty request that signals
672                                 that the thread should be stopped.)
673                         */
674                         if (m_all_ongoing.empty())
675                                 waitForRequest(100000000);
676                         else
677                                 waitForIO(100);
678
679                         END_DEBUG_EXCEPTION_HANDLER
680                 }
681
682                 // Call curl_multi_remove_handle and cleanup easy handles
683                 for (HTTPFetchOngoing *i : m_all_ongoing) {
684                         delete i;
685                 }
686                 m_all_ongoing.clear();
687
688                 m_queued_fetches.clear();
689
690                 CURLMcode mres = curl_multi_cleanup(m_multi);
691                 if (mres != CURLM_OK) {
692                         errorstream << "curl_multi_cleanup"
693                                     << " returned error code " << mres << std::endl;
694                 }
695
696                 return NULL;
697         }
698 };
699
700 CurlFetchThread *g_httpfetch_thread = NULL;
701
702 void httpfetch_init(int parallel_limit)
703 {
704         verbosestream << "httpfetch_init: parallel_limit=" << parallel_limit << std::endl;
705
706         CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT);
707         FATAL_ERROR_IF(res != CURLE_OK, "CURL init failed");
708
709         g_httpfetch_thread = new CurlFetchThread(parallel_limit);
710
711         // Initialize g_callerid_randomness for httpfetch_caller_alloc_secure
712         u64 randbuf[2];
713         porting::secure_rand_fill_buf(randbuf, sizeof(u64) * 2);
714         g_callerid_randomness = PcgRandom(randbuf[0], randbuf[1]);
715 }
716
717 void httpfetch_cleanup()
718 {
719         verbosestream << "httpfetch_cleanup: cleaning up" << std::endl;
720
721         g_httpfetch_thread->stop();
722         g_httpfetch_thread->requestWakeUp();
723         g_httpfetch_thread->wait();
724         delete g_httpfetch_thread;
725
726         curl_global_cleanup();
727 }
728
729 void httpfetch_async(const HTTPFetchRequest &fetch_request)
730 {
731         g_httpfetch_thread->requestFetch(fetch_request);
732         if (!g_httpfetch_thread->isRunning())
733                 g_httpfetch_thread->start();
734 }
735
736 static void httpfetch_request_clear(unsigned long caller)
737 {
738         if (g_httpfetch_thread->isRunning()) {
739                 Event event;
740                 g_httpfetch_thread->requestClear(caller, &event);
741                 event.wait();
742         } else {
743                 g_httpfetch_thread->requestClear(caller, NULL);
744         }
745 }
746
747 void httpfetch_sync(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result)
748 {
749         // Create ongoing fetch data and make a cURL handle
750         // Set cURL options based on HTTPFetchRequest
751         CurlHandlePool pool;
752         HTTPFetchOngoing ongoing(fetch_request, &pool);
753         // Do the fetch (curl_easy_perform)
754         CURLcode res = ongoing.start(NULL);
755         // Update fetch result
756         fetch_result = *ongoing.complete(res);
757 }
758
759 #else // USE_CURL
760
761 /*
762         USE_CURL is off:
763
764         Dummy httpfetch implementation that always returns an error.
765 */
766
767 void httpfetch_init(int parallel_limit)
768 {
769 }
770
771 void httpfetch_cleanup()
772 {
773 }
774
775 void httpfetch_async(const HTTPFetchRequest &fetch_request)
776 {
777         errorstream << "httpfetch_async: unable to fetch " << fetch_request.url
778                     << " because USE_CURL=0" << std::endl;
779
780         HTTPFetchResult fetch_result(fetch_request); // sets succeeded = false etc.
781         httpfetch_deliver_result(fetch_result);
782 }
783
784 static void httpfetch_request_clear(unsigned long caller)
785 {
786 }
787
788 void httpfetch_sync(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result)
789 {
790         errorstream << "httpfetch_sync: unable to fetch " << fetch_request.url
791                     << " because USE_CURL=0" << std::endl;
792
793         fetch_result = HTTPFetchResult(fetch_request); // sets succeeded = false etc.
794 }
795
796 #endif // USE_CURL