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