3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "httpfetch.h"
21 #include "porting.h" // for sleep_ms(), get_sysinfo(), secure_rand_fill_buf()
25 #include <unordered_map>
28 #include "network/socket.h" // for select()
29 #include "threading/event.h"
31 #include "exceptions.h"
34 #include "util/container.h"
35 #include "util/thread.h"
40 static std::mutex g_httpfetch_mutex;
41 static std::unordered_map<u64, std::queue<HTTPFetchResult>>
43 static PcgRandom g_callerid_randomness;
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() + ")")
53 static void httpfetch_deliver_result(const HTTPFetchResult &fetch_result)
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);
62 static void httpfetch_request_clear(u64 caller);
64 u64 httpfetch_caller_alloc()
66 MutexAutoLock lock(g_httpfetch_mutex);
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];
80 FATAL_ERROR("httpfetch_caller_alloc: ran out of caller IDs");
83 u64 httpfetch_caller_alloc_secure()
85 MutexAutoLock lock(g_httpfetch_mutex);
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
94 caller = (((u64) g_callerid_randomness.next()) << 32) |
95 g_callerid_randomness.next();
98 FATAL_ERROR("httpfetch_caller_alloc_secure: ran out of caller IDs");
99 return HTTPFETCH_DISCARD;
101 } while (caller >= HTTPFETCH_CID_START &&
102 g_httpfetch_results.find(caller) != g_httpfetch_results.end());
104 verbosestream << "httpfetch_caller_alloc_secure: allocating "
105 << caller << std::endl;
107 // Access element to create it
108 g_httpfetch_results[caller];
112 void httpfetch_caller_free(u64 caller)
114 verbosestream<<"httpfetch_caller_free: freeing "
117 httpfetch_request_clear(caller);
118 if (caller != HTTPFETCH_DISCARD) {
119 MutexAutoLock lock(g_httpfetch_mutex);
120 g_httpfetch_results.erase(caller);
124 bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result)
126 MutexAutoLock lock(g_httpfetch_mutex);
128 // Check that caller exists
129 auto it = g_httpfetch_results.find(caller);
130 if (it == g_httpfetch_results.end())
133 // Check that result queue is nonempty
134 std::queue<HTTPFetchResult> &caller_results = it->second;
135 if (caller_results.empty())
139 fetch_result = std::move(caller_results.front());
140 caller_results.pop();
145 #include <curl/curl.h>
148 USE_CURL is on: use cURL based httpfetch implementation
151 static size_t httpfetch_writefunction(
152 char *ptr, size_t size, size_t nmemb, void *userdata)
154 std::ostringstream *stream = (std::ostringstream*)userdata;
155 size_t count = size * nmemb;
156 stream->write(ptr, count);
160 static size_t httpfetch_discardfunction(
161 char *ptr, size_t size, size_t nmemb, void *userdata)
168 std::list<CURL*> handles;
171 CurlHandlePool() = default;
175 for (std::list<CURL*>::iterator it = handles.begin();
176 it != handles.end(); ++it) {
177 curl_easy_cleanup(*it);
183 if (handles.empty()) {
184 curl = curl_easy_init();
186 errorstream<<"curl_easy_init returned NULL"<<std::endl;
190 curl = handles.front();
195 void free(CURL *handle)
198 handles.push_back(handle);
202 class HTTPFetchOngoing
205 HTTPFetchOngoing(const HTTPFetchRequest &request, CurlHandlePool *pool);
208 CURLcode start(CURLM *multi);
209 const HTTPFetchResult * complete(CURLcode res);
211 const HTTPFetchRequest &getRequest() const { return request; };
212 const CURL *getEasyHandle() const { return curl; };
215 CurlHandlePool *pool;
218 HTTPFetchRequest request;
219 HTTPFetchResult result;
220 std::ostringstream oss;
221 struct curl_slist *http_header;
226 HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
227 CurlHandlePool *pool_):
233 oss(std::ios::binary),
237 curl = pool->alloc();
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");
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());
253 if (!g_settings->getBool("enable_ipv6")) {
254 curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
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.
266 curl_easy_setopt(curl, CURLOPT_PROTOCOLS, protocols);
267 curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS, protocols);
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,
275 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
276 request.connect_timeout);
278 if (!request.useragent.empty())
279 curl_easy_setopt(curl, CURLOPT_USERAGENT, request.useragent.c_str());
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);
289 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
290 httpfetch_writefunction);
291 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &oss);
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(),
306 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
307 // request.post_fields must now *never* be
308 // modified until CURLOPT_HTTPPOST is cleared
310 switch (request.method) {
312 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
315 curl_easy_setopt(curl, CURLOPT_POST, 1);
318 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
321 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
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()) {
332 for (auto &field : request.fields) {
335 str += urlencode(field.first);
337 str += urlencode(field.second);
339 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
341 curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS,
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());
350 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_header);
352 if (!g_settings->getBool("curl_verify_cert")) {
353 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
357 CURLcode HTTPFetchOngoing::start(CURLM *multi_)
360 return CURLE_FAILED_INIT;
363 // Easy interface (sync)
364 return curl_easy_perform(curl);
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
373 return CURLE_FAILED_INIT;
375 multi = multi_; // store for curl_multi_remove_handle
379 const HTTPFetchResult * HTTPFetchOngoing::complete(CURLcode res)
381 result.succeeded = (res == CURLE_OK);
382 result.timeout = (res == CURLE_OPERATION_TIMEDOUT);
383 result.data = oss.str();
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;
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
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;
410 HTTPFetchOngoing::~HTTPFetchOngoing()
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
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);
427 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
428 curl_slist_free_all(http_header);
431 curl_easy_setopt(curl, CURLOPT_HTTPPOST, NULL);
435 // Store the cURL handle for reuse
440 class CurlFetchThread : public Thread
451 HTTPFetchRequest fetch_request;
456 MutexedQueue<Request> m_requests;
457 size_t m_parallel_limit;
459 // Variables exclusively used within thread
460 std::vector<HTTPFetchOngoing*> m_all_ongoing;
461 std::list<HTTPFetchRequest> m_queued_fetches;
464 CurlFetchThread(int parallel_limit) :
467 if (parallel_limit >= 1)
468 m_parallel_limit = parallel_limit;
470 m_parallel_limit = 1;
473 void requestFetch(const HTTPFetchRequest &fetch_request)
477 req.fetch_request = fetch_request;
479 m_requests.push_back(req);
482 void requestClear(u64 caller, Event *event)
486 req.fetch_request.caller = caller;
488 m_requests.push_back(req);
494 req.type = RT_WAKEUP;
496 m_requests.push_back(req);
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)
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);
509 // see processQueued() for what happens next
512 else if (req.type == RT_CLEAR) {
513 u64 caller = req.fetch_request.caller;
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) {
521 it = m_all_ongoing.erase(it);
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);
537 else if (req.type == RT_WAKEUP) {
538 // Wakeup: Nothing to do, thread is awake at this point
541 if (req.event != NULL)
545 // Start new ongoing fetches if m_parallel_limit allows
546 void processQueued(CurlHandlePool *pool)
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();
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);
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);
564 httpfetch_deliver_result(*ongoing->complete(res));
570 // Process CURLMsg (indicates completion of a fetch)
571 void processCurlMessage(CURLMsg *msg)
573 // Determine which ongoing fetch the message pertains to
576 for (i = 0; i < m_all_ongoing.size(); ++i) {
577 if (m_all_ongoing[i]->getEasyHandle() == msg->easy_handle) {
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));
587 m_all_ongoing.erase(m_all_ongoing.begin() + i);
591 // Wait for a request from another thread, or timeout elapses
592 void waitForRequest(long timeout)
594 if (m_queued_fetches.empty()) {
596 Request req = m_requests.pop_front(timeout);
599 catch (ItemNotFoundException &e) {}
603 // Wait until some IO happens, or timeout elapses
604 void waitForIO(long timeout)
610 long select_timeout = -1;
611 struct timeval select_tv;
614 FD_ZERO(&read_fd_set);
615 FD_ZERO(&write_fd_set);
616 FD_ZERO(&exc_fd_set);
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
627 mres = curl_multi_timeout(m_multi, &select_timeout);
628 if (mres != CURLM_OK) {
629 errorstream<<"curl_multi_timeout"
630 <<" returned error code "<<mres
635 // Limit timeout so new requests get through
636 if (select_timeout < 0 || select_timeout > timeout)
637 select_timeout = timeout;
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
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,
650 errorstream<<"select returned error code "
651 <<WSAGetLastError()<<std::endl;
653 errorstream<<"select returned error code "
659 sleep_ms(select_timeout);
668 m_multi = curl_multi_init();
669 if (m_multi == NULL) {
670 errorstream<<"curl_multi_init returned NULL\n";
674 FATAL_ERROR_IF(!m_all_ongoing.empty(), "Expected empty");
676 while (!stopRequested()) {
677 BEGIN_DEBUG_EXCEPTION_HANDLER
680 Handle new async requests
683 while (!m_requests.empty()) {
684 Request req = m_requests.pop_frontNoEx();
687 processQueued(&pool);
690 Handle ongoing async requests
693 int still_ongoing = 0;
694 while (curl_multi_perform(m_multi, &still_ongoing) ==
695 CURLM_CALL_MULTI_PERFORM)
699 Handle completed async requests
701 if (still_ongoing < (int) m_all_ongoing.size()) {
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);
712 If there are ongoing requests, wait for data
713 (with a timeout of 100ms so that new requests
716 If no ongoing requests, wait for a new request.
717 (Possibly an empty request that signals
718 that the thread should be stopped.)
720 if (m_all_ongoing.empty())
721 waitForRequest(100000000);
725 END_DEBUG_EXCEPTION_HANDLER
728 // Call curl_multi_remove_handle and cleanup easy handles
729 for (HTTPFetchOngoing *i : m_all_ongoing) {
732 m_all_ongoing.clear();
734 m_queued_fetches.clear();
736 CURLMcode mres = curl_multi_cleanup(m_multi);
737 if (mres != CURLM_OK) {
738 errorstream<<"curl_multi_cleanup"
739 <<" returned error code "<<mres
747 CurlFetchThread *g_httpfetch_thread = NULL;
749 void httpfetch_init(int parallel_limit)
751 verbosestream<<"httpfetch_init: parallel_limit="<<parallel_limit
754 CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT);
755 FATAL_ERROR_IF(res != CURLE_OK, "CURL init failed");
757 g_httpfetch_thread = new CurlFetchThread(parallel_limit);
759 // Initialize g_callerid_randomness for httpfetch_caller_alloc_secure
761 porting::secure_rand_fill_buf(randbuf, sizeof(u64) * 2);
762 g_callerid_randomness = PcgRandom(randbuf[0], randbuf[1]);
765 void httpfetch_cleanup()
767 verbosestream<<"httpfetch_cleanup: cleaning up"<<std::endl;
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;
776 curl_global_cleanup();
779 void httpfetch_async(const HTTPFetchRequest &fetch_request)
781 g_httpfetch_thread->requestFetch(fetch_request);
782 if (!g_httpfetch_thread->isRunning())
783 g_httpfetch_thread->start();
786 static void httpfetch_request_clear(u64 caller)
788 if (g_httpfetch_thread->isRunning()) {
790 g_httpfetch_thread->requestClear(caller, &event);
793 g_httpfetch_thread->requestClear(caller, NULL);
797 void httpfetch_sync(const HTTPFetchRequest &fetch_request,
798 HTTPFetchResult &fetch_result)
800 // Create ongoing fetch data and make a cURL handle
801 // Set cURL options based on HTTPFetchRequest
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);
815 Dummy httpfetch implementation that always returns an error.
818 void httpfetch_init(int parallel_limit)
822 void httpfetch_cleanup()
826 void httpfetch_async(const HTTPFetchRequest &fetch_request)
828 errorstream << "httpfetch_async: unable to fetch " << fetch_request.url
829 << " because USE_CURL=0" << std::endl;
831 HTTPFetchResult fetch_result(fetch_request); // sets succeeded = false etc.
832 httpfetch_deliver_result(fetch_result);
835 static void httpfetch_request_clear(u64 caller)
839 void httpfetch_sync(const HTTPFetchRequest &fetch_request,
840 HTTPFetchResult &fetch_result)
842 errorstream << "httpfetch_sync: unable to fetch " << fetch_request.url
843 << " because USE_CURL=0" << std::endl;
845 fetch_result = HTTPFetchResult(fetch_request); // sets succeeded = false etc.