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