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