2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2023-07-08 15:24:03 +00:00
|
|
|
#ifdef _WIN32
|
2022-08-19 02:31:53 +00:00
|
|
|
#include <array>
|
2023-07-08 15:24:03 +00:00
|
|
|
#endif
|
2022-11-30 19:00:34 +00:00
|
|
|
#include <atomic>
|
2022-02-25 20:45:00 +00:00
|
|
|
#include <condition_variable>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstdint> // uint64_t
|
2022-02-17 23:35:57 +00:00
|
|
|
#include <list>
|
2022-02-16 18:33:50 +00:00
|
|
|
#include <map>
|
2022-02-17 23:35:57 +00:00
|
|
|
#include <memory>
|
2022-02-16 18:33:50 +00:00
|
|
|
#include <mutex>
|
2022-11-30 19:00:34 +00:00
|
|
|
#include <stack>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2022-01-31 19:34:04 +00:00
|
|
|
#include <thread>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2011-03-16 18:04:23 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2019-06-22 13:02:17 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <wincrypt.h>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <sys/socket.h> // setsockopt, SOL_SOCKET, SO_RC...
|
2010-06-25 20:36:10 +00:00
|
|
|
#endif
|
2010-01-04 20:06:39 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
#include <curl/curl.h>
|
2010-12-20 02:07:51 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <event2/buffer.h>
|
2022-03-15 14:52:16 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <fmt/core.h>
|
2023-04-14 19:33:23 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#ifdef _WIN32
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/crypto-utils.h"
|
2023-07-08 15:24:03 +00:00
|
|
|
#endif
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/log.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
|
|
|
#include "libtransmission/utils-ev.h"
|
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
#include "libtransmission/web.h"
|
|
|
|
#include "libtransmission/web-utils.h"
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
#if LIBCURL_VERSION_NUM >= 0x070F06 // CURLOPT_SOCKOPT* was added in 7.15.6
|
2017-04-19 12:04:45 +00:00
|
|
|
#define USE_LIBCURL_SOCKOPT
|
2010-04-15 15:32:36 +00:00
|
|
|
#endif
|
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2008-10-15 16:43:51 +00:00
|
|
|
|
2023-01-27 20:25:08 +00:00
|
|
|
namespace
|
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
namespace curl_helpers
|
|
|
|
{
|
|
|
|
|
|
|
|
struct ShareDeleter
|
|
|
|
{
|
|
|
|
void operator()(CURLSH* shared)
|
|
|
|
{
|
|
|
|
if (shared == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[maybe_unused]] auto const status = curl_share_cleanup(shared);
|
|
|
|
TR_ASSERT(status == CURLSHE_OK);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using shared_unique_ptr = std::unique_ptr<CURLSH, ShareDeleter>;
|
|
|
|
|
|
|
|
struct MultiDeleter
|
|
|
|
{
|
2022-12-23 16:56:27 +00:00
|
|
|
void operator()(CURLM* multi) const
|
2022-11-30 19:00:34 +00:00
|
|
|
{
|
|
|
|
if (multi == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[maybe_unused]] auto const status = curl_multi_cleanup(multi);
|
|
|
|
TR_ASSERT(status == CURLM_OK);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using multi_unique_ptr = std::unique_ptr<CURLM, MultiDeleter>;
|
|
|
|
|
|
|
|
struct EasyDeleter
|
|
|
|
{
|
|
|
|
void operator()(CURL* val) const noexcept
|
|
|
|
{
|
|
|
|
if (val != nullptr)
|
|
|
|
{
|
|
|
|
curl_easy_cleanup(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using easy_unique_ptr = std::unique_ptr<CURL, EasyDeleter>;
|
|
|
|
|
|
|
|
} // namespace curl_helpers
|
2023-01-27 20:25:08 +00:00
|
|
|
} // namespace
|
2022-11-30 19:00:34 +00:00
|
|
|
|
2022-11-13 23:17:16 +00:00
|
|
|
#ifdef _WIN32
|
2021-10-24 16:41:54 +00:00
|
|
|
static CURLcode ssl_context_func(CURL* /*curl*/, void* ssl_ctx, void* /*user_data*/)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
2022-02-07 04:28:36 +00:00
|
|
|
auto const cert_store = tr_ssl_get_x509_store(ssl_ctx);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cert_store == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_version_info_data const* const curl_ver = curl_version_info(CURLVERSION_NOW);
|
|
|
|
if (curl_ver->age >= 0 && strncmp(curl_ver->ssl_version, "Schannel", 8) == 0)
|
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2022-08-19 02:31:53 +00:00
|
|
|
static auto constexpr SysStoreNames = std::array<LPCWSTR, 2>{
|
2019-06-22 13:02:17 +00:00
|
|
|
L"CA",
|
2021-08-15 09:41:48 +00:00
|
|
|
L"ROOT",
|
2019-06-22 13:02:17 +00:00
|
|
|
};
|
|
|
|
|
2022-08-19 02:31:53 +00:00
|
|
|
for (auto& sys_store_name : SysStoreNames)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
2022-08-19 02:31:53 +00:00
|
|
|
HCERTSTORE const sys_cert_store = CertOpenSystemStoreW(0, sys_store_name);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (sys_cert_store == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
PCCERT_CONTEXT sys_cert = nullptr;
|
2019-06-22 13:02:17 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
sys_cert = CertFindCertificateInStore(sys_cert_store, X509_ASN_ENCODING, 0, CERT_FIND_ANY, nullptr, sys_cert);
|
|
|
|
if (sys_cert == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_x509_cert_t const cert = tr_x509_cert_new(sys_cert->pbCertEncoded, sys_cert->cbCertEncoded);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cert == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_x509_store_add(cert_store, cert);
|
|
|
|
tr_x509_cert_free(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
CertCloseStore(sys_cert_store, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2022-11-13 23:17:16 +00:00
|
|
|
#endif
|
2019-06-22 13:02:17 +00:00
|
|
|
|
2023-01-22 19:21:30 +00:00
|
|
|
// ---
|
2022-02-16 18:33:50 +00:00
|
|
|
|
|
|
|
class tr_web::Impl
|
2009-12-13 17:54:01 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
public:
|
2022-02-22 15:09:24 +00:00
|
|
|
explicit Impl(Mediator& mediator_in)
|
|
|
|
: mediator{ mediator_in }
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-27 04:26:37 +00:00
|
|
|
if (auto bundle = tr_env_get_string("CURL_CA_BUNDLE"); !std::empty(bundle))
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-07-27 04:26:37 +00:00
|
|
|
curl_ca_bundle = std::move(bundle);
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2023-09-03 23:26:39 +00:00
|
|
|
share_setopt();
|
2022-03-24 03:02:18 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
if (curl_ssl_verify)
|
|
|
|
{
|
|
|
|
auto const* bundle = std::empty(curl_ca_bundle) ? "none" : curl_ca_bundle.c_str();
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddInfo(
|
2022-03-15 14:52:16 +00:00
|
|
|
fmt::format(_("Will verify tracker certs using envvar CURL_CA_BUNDLE: {bundle}"), fmt::arg("bundle", bundle)));
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddInfo(_("NB: this only works if you built against libcurl with openssl or gnutls, NOT nss"));
|
|
|
|
tr_logAddInfo(_("NB: Invalid certs will appear as 'Could not connect to tracker' like many other errors"));
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
if (auto const& file = mediator.cookieFile(); file)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
this->cookie_file = *file;
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
if (auto const& ua = mediator.userAgent(); ua)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
this->user_agent = *ua;
|
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
auto const lock = std::unique_lock{ tasks_mutex_ };
|
|
|
|
curl_thread = std::make_unique<std::thread>(&Impl::curlThreadFunc, this);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
|
2022-08-03 17:03:28 +00:00
|
|
|
Impl(Impl&&) = delete;
|
|
|
|
Impl(Impl const&) = delete;
|
|
|
|
Impl& operator=(Impl&&) = delete;
|
|
|
|
Impl& operator=(Impl const&) = delete;
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
~Impl()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-12-23 14:03:26 +00:00
|
|
|
deadline_ = mediator.now();
|
2022-11-30 19:00:34 +00:00
|
|
|
queued_tasks_cv_.notify_one();
|
2022-02-16 18:33:50 +00:00
|
|
|
curl_thread->join();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
void startShutdown(std::chrono::milliseconds deadline)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-12-23 14:03:26 +00:00
|
|
|
deadline_ = mediator.now() + std::chrono::duration_cast<std::chrono::seconds>(deadline).count();
|
2022-11-30 19:00:34 +00:00
|
|
|
queued_tasks_cv_.notify_one();
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2009-12-13 17:54:01 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
void fetch(FetchOptions&& options)
|
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
if (deadline_exists())
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
auto const lock = std::unique_lock{ tasks_mutex_ };
|
|
|
|
queued_tasks_.emplace_back(*this, std::move(options));
|
|
|
|
queued_tasks_cv_.notify_one();
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
class Task
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Task(tr_web::Impl& impl_in, tr_web::FetchOptions&& options_in)
|
2022-11-30 19:00:34 +00:00
|
|
|
: impl{ impl_in }
|
|
|
|
, options{ std::move(options_in) }
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2023-01-03 20:10:12 +00:00
|
|
|
auto const parsed = tr_urlParse(options.url);
|
|
|
|
easy_ = parsed ? impl.get_easy(parsed->host) : nullptr;
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
response.user_data = options.done_func_user_data;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
// Some of the curl_easy_setopt() args took a pointer to this task.
|
|
|
|
// Disable moving so that we don't accidentally invalidate those pointers.
|
|
|
|
Task(Task&&) = delete;
|
|
|
|
Task(Task const&) = delete;
|
|
|
|
Task& operator=(Task&&) = delete;
|
|
|
|
Task& operator=(Task const&) = delete;
|
|
|
|
|
|
|
|
~Task()
|
|
|
|
{
|
|
|
|
easy_dispose(easy_);
|
|
|
|
}
|
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto* easy() const
|
2018-04-25 08:21:50 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
return easy_;
|
2018-04-25 08:21:50 +00:00
|
|
|
}
|
2022-02-16 18:33:50 +00:00
|
|
|
|
|
|
|
[[nodiscard]] auto* body() const
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
return options.buffer != nullptr ? options.buffer : privbuf.get();
|
2019-06-22 13:02:17 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto const& speedLimitTag() const
|
2022-02-14 04:24:15 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
return options.speed_limit_tag;
|
2022-02-14 04:24:15 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto const& url() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return options.url;
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto const& range() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return options.range;
|
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto const& cookies() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return options.cookies;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto const& sndbuf() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return options.sndbuf;
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto const& rcvbuf() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return options.rcvbuf;
|
|
|
|
}
|
2013-05-22 19:02:07 +00:00
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto const& timeoutSecs() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return options.timeout_secs;
|
|
|
|
}
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2023-01-02 04:43:23 +00:00
|
|
|
[[nodiscard]] constexpr auto ipProtocol() const
|
2022-06-18 01:33:11 +00:00
|
|
|
{
|
|
|
|
switch (options.ip_proto)
|
|
|
|
{
|
|
|
|
case FetchOptions::IPProtocol::V4:
|
|
|
|
return CURL_IPRESOLVE_V4;
|
|
|
|
case FetchOptions::IPProtocol::V6:
|
|
|
|
return CURL_IPRESOLVE_V6;
|
|
|
|
default:
|
|
|
|
return CURL_IPRESOLVE_WHATEVER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-14 15:31:12 +00:00
|
|
|
[[nodiscard]] auto bind_address() const
|
2022-08-21 23:37:38 +00:00
|
|
|
{
|
|
|
|
switch (options.ip_proto)
|
|
|
|
{
|
|
|
|
case FetchOptions::IPProtocol::V4:
|
2023-08-14 15:31:12 +00:00
|
|
|
return impl.mediator.bind_address_V4();
|
2022-08-21 23:37:38 +00:00
|
|
|
case FetchOptions::IPProtocol::V6:
|
2023-08-14 15:31:12 +00:00
|
|
|
return impl.mediator.bind_address_V6();
|
2022-08-21 23:37:38 +00:00
|
|
|
default:
|
2023-08-14 15:31:12 +00:00
|
|
|
auto ip = impl.mediator.bind_address_V4();
|
2022-08-21 23:37:38 +00:00
|
|
|
if (ip == std::nullopt)
|
|
|
|
{
|
2023-08-14 15:31:12 +00:00
|
|
|
ip = impl.mediator.bind_address_V6();
|
2022-08-21 23:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
void done()
|
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
if (!options.done_func)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
response.body.assign(reinterpret_cast<char const*>(evbuffer_pullup(body(), -1)), evbuffer_get_length(body()));
|
2022-02-22 15:09:24 +00:00
|
|
|
impl.mediator.run(std::move(options.done_func), std::move(this->response));
|
2022-11-30 19:00:34 +00:00
|
|
|
options.done_func = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool operator==(Task const& that) const noexcept
|
|
|
|
{
|
|
|
|
return easy() == that.easy();
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
tr_web::Impl& impl;
|
|
|
|
tr_web::FetchResponse response;
|
2022-11-30 19:00:34 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void easy_dispose(CURL* easy)
|
|
|
|
{
|
|
|
|
if (easy == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-06 05:42:52 +00:00
|
|
|
impl.paused_easy_handles.erase(easy_);
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (auto const url = tr_urlParse(options.url); url)
|
|
|
|
{
|
|
|
|
curl_easy_reset(easy);
|
|
|
|
impl.easy_pool_[std::string{ url->host }].emplace(easy);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curl_easy_cleanup(easy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
libtransmission::evhelpers::evbuffer_unique_ptr privbuf{ evbuffer_new() };
|
|
|
|
|
|
|
|
tr_web::FetchOptions options;
|
|
|
|
|
2023-01-03 20:10:12 +00:00
|
|
|
CURL* easy_;
|
2022-02-16 18:33:50 +00:00
|
|
|
};
|
2008-10-15 16:43:51 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
static auto constexpr BandwidthPauseMsec = long{ 500 };
|
|
|
|
static auto constexpr DnsCacheTimeoutSecs = long{ 60 * 60 };
|
2022-02-23 13:38:18 +00:00
|
|
|
static auto constexpr MaxRedirects = long{ 10 };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
bool const curl_verbose = tr_env_key_exists("TR_CURL_VERBOSE");
|
|
|
|
bool const curl_ssl_verify = !tr_env_key_exists("TR_CURL_SSL_NO_VERIFY");
|
|
|
|
bool const curl_proxy_ssl_verify = !tr_env_key_exists("TR_CURL_PROXY_SSL_NO_VERIFY");
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
Mediator& mediator;
|
2022-02-16 18:33:50 +00:00
|
|
|
|
|
|
|
std::string curl_ca_bundle;
|
|
|
|
|
|
|
|
std::string cookie_file;
|
|
|
|
std::string user_agent;
|
|
|
|
|
|
|
|
std::unique_ptr<std::thread> curl_thread;
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
// if unset: steady-state, all is good
|
|
|
|
// if set: do not accept new tasks
|
|
|
|
// if set and deadline reached: kill all remaining tasks
|
|
|
|
std::atomic<time_t> deadline_ = {};
|
|
|
|
|
|
|
|
[[nodiscard]] auto deadline() const
|
2022-02-14 02:09:56 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
return deadline_.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool deadline_exists() const
|
|
|
|
{
|
|
|
|
return deadline() != time_t{};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool deadline_reached() const
|
|
|
|
{
|
2022-12-23 14:03:26 +00:00
|
|
|
return deadline_exists() && deadline() <= mediator.now();
|
2022-11-30 19:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] CURL* get_easy(std::string_view host)
|
|
|
|
{
|
|
|
|
CURL* easy = nullptr;
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (auto iter = easy_pool_.find(host); iter != std::end(easy_pool_) && !std::empty(iter->second))
|
|
|
|
{
|
|
|
|
easy = iter->second.top().release();
|
|
|
|
iter->second.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (easy == nullptr)
|
|
|
|
{
|
|
|
|
easy = curl_easy_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return easy;
|
|
|
|
}
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
static size_t onDataReceived(void* data, size_t size, size_t nmemb, void* vtask)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
size_t const bytes_used = size * nmemb;
|
|
|
|
auto* task = static_cast<Task*>(vtask);
|
|
|
|
TR_ASSERT(std::this_thread::get_id() == task->impl.curl_thread->get_id());
|
|
|
|
|
2023-01-16 21:08:25 +00:00
|
|
|
if (auto const range = task->range(); range)
|
|
|
|
{
|
|
|
|
// https://curl.se/libcurl/c/CURLINFO_RESPONSE_CODE.html
|
|
|
|
// "The stored value will be zero if no server response code has been received"
|
|
|
|
static auto constexpr NoResponseCode = 0L;
|
|
|
|
static auto constexpr PartialContentResponseCode = 206L;
|
|
|
|
|
|
|
|
// Test for webservers that don't support partial-content, see GH #4595
|
|
|
|
auto code = long{};
|
|
|
|
(void)curl_easy_getinfo(task->easy(), CURLINFO_RESPONSE_CODE, &code);
|
|
|
|
if (code != NoResponseCode && code != PartialContentResponseCode)
|
|
|
|
{
|
|
|
|
tr_logAddWarn(fmt::format(
|
|
|
|
_("Couldn't fetch '{url}': expected HTTP response code {expected_code}, got {actual_code}"),
|
|
|
|
fmt::arg("url", task->url()),
|
|
|
|
fmt::arg("expected_code", PartialContentResponseCode),
|
|
|
|
fmt::arg("actual_code", code)));
|
|
|
|
|
|
|
|
// Tell curl to error out. Returning anything that's not
|
|
|
|
// `bytes_used` signals an error and causes the transfer
|
|
|
|
// to be aborted w/CURLE_WRITE_ERROR.
|
|
|
|
return bytes_used + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
if (auto const& tag = task->speedLimitTag(); tag)
|
2013-02-15 01:52:47 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
// If this is more bandwidth than is allocated for this tag,
|
|
|
|
// then pause the torrent for a tick. curl will deliver `data`
|
|
|
|
// again when the transfer is unpaused.
|
2022-02-22 15:09:24 +00:00
|
|
|
if (task->impl.mediator.clamp(*tag, bytes_used) < bytes_used)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2023-03-06 05:42:52 +00:00
|
|
|
task->impl.paused_easy_handles.emplace(task->easy(), tr_time_msec());
|
2022-02-16 18:33:50 +00:00
|
|
|
return CURL_WRITEFUNC_PAUSE;
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
task->impl.mediator.notifyBandwidthConsumed(*tag, bytes_used);
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2013-04-13 20:25:28 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
evbuffer_add(task->body(), data, bytes_used);
|
2022-03-17 22:39:06 +00:00
|
|
|
tr_logAddTrace(fmt::format("wrote {} bytes to task {}'s buffer", bytes_used, fmt::ptr(task)));
|
2022-02-16 18:33:50 +00:00
|
|
|
return bytes_used;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
#ifdef USE_LIBCURL_SOCKOPT
|
|
|
|
static int onSocketCreated(void* vtask, curl_socket_t fd, curlsocktype /*purpose*/)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
auto const* const task = static_cast<Task const*>(vtask);
|
|
|
|
TR_ASSERT(std::this_thread::get_id() == task->impl.curl_thread->get_id());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
// Ignore the sockopt() return values -- these are suggestions
|
|
|
|
// rather than hard requirements & it's OK for them to fail
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
if (auto const& buf = task->sndbuf(); buf)
|
|
|
|
{
|
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char const*>(&*buf), sizeof(*buf));
|
|
|
|
}
|
|
|
|
if (auto const& buf = task->rcvbuf(); buf)
|
|
|
|
{
|
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char const*>(&*buf), sizeof(*buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
// return nonzero if this function encountered an error
|
|
|
|
return 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-02-16 18:33:50 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
void initEasy(Task& task)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
TR_ASSERT(std::this_thread::get_id() == curl_thread->get_id());
|
|
|
|
auto* const e = task.easy();
|
2011-04-27 17:02:18 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_SHARE, shared());
|
2022-02-17 23:35:57 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_DNS_CACHE_TIMEOUT, DnsCacheTimeoutSecs);
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_AUTOREFERER, 1L);
|
2023-01-17 22:07:10 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_ACCEPT_ENCODING, "");
|
2022-02-17 23:35:57 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_MAXREDIRS, -1L);
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_NOSIGNAL, 1L);
|
2022-11-30 19:00:34 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_PRIVATE, &task);
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_IPRESOLVE, task.ipProtocol());
|
2009-12-11 15:41:34 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
#ifdef USE_LIBCURL_SOCKOPT
|
2022-02-17 23:35:57 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_SOCKOPTFUNCTION, onSocketCreated);
|
2022-11-30 19:00:34 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_SOCKOPTDATA, &task);
|
2022-02-16 18:33:50 +00:00
|
|
|
#endif
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (!curl_ssl_verify)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2023-01-26 18:52:32 +00:00
|
|
|
#if LIBCURL_VERSION_NUM >= 0x073400 /* 7.52.0 */
|
2022-02-17 23:35:57 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_SSL_VERIFYHOST, 0L);
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_SSL_VERIFYPEER, 0L);
|
2023-01-26 18:52:32 +00:00
|
|
|
#endif
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2022-11-30 19:00:34 +00:00
|
|
|
else if (!std::empty(curl_ca_bundle))
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_CAINFO, curl_ca_bundle.c_str());
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-13 23:17:16 +00:00
|
|
|
#ifdef _WIN32
|
2022-02-17 23:35:57 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_SSL_CTX_FUNCTION, ssl_context_func);
|
2022-11-13 23:17:16 +00:00
|
|
|
#endif
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (!curl_proxy_ssl_verify)
|
2013-04-13 20:56:24 +00:00
|
|
|
{
|
2022-11-13 23:17:16 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_CAINFO, NULL);
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_CAPATH, NULL);
|
2023-01-26 22:03:11 +00:00
|
|
|
#if LIBCURL_VERSION_NUM >= 0x073400 /* 7.52.0 */
|
2022-02-17 23:35:57 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_PROXY_SSL_VERIFYHOST, 0L);
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_PROXY_SSL_VERIFYPEER, 0L);
|
2023-01-26 22:03:11 +00:00
|
|
|
#endif
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2022-11-30 19:00:34 +00:00
|
|
|
else if (!std::empty(curl_ca_bundle))
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2023-01-26 18:52:32 +00:00
|
|
|
#if LIBCURL_VERSION_NUM >= 0x073400 /* 7.52.0 */
|
2022-11-30 19:00:34 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_PROXY_CAINFO, curl_ca_bundle.c_str());
|
2023-01-26 18:52:32 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-04-28 00:06:22 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (auto const& ua = user_agent; !std::empty(ua))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-17 23:35:57 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_USERAGENT, ua.c_str());
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_TIMEOUT, static_cast<long>(task.timeoutSecs().count()));
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_URL, task.url().c_str());
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_VERBOSE, curl_verbose ? 1L : 0L);
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_WRITEDATA, &task);
|
2023-01-16 21:08:25 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_WRITEFUNCTION, &tr_web::Impl::onDataReceived);
|
2022-02-23 13:38:18 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_MAXREDIRS, MaxRedirects);
|
2013-04-13 20:56:24 +00:00
|
|
|
|
2023-08-14 15:31:12 +00:00
|
|
|
if (auto const addrstr = task.bind_address(); addrstr)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_INTERFACE, addrstr->c_str());
|
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (auto const& cookies = task.cookies(); cookies)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_COOKIE, cookies->c_str());
|
2021-11-20 21:20:45 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (auto const& file = cookie_file; !std::empty(file))
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_COOKIEFILE, file.c_str());
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (auto const& range = task.range(); range)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
/* don't bother asking the server to compress webseed fragments */
|
2023-01-17 22:07:10 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_ACCEPT_ENCODING, "identity");
|
|
|
|
(void)curl_easy_setopt(e, CURLOPT_HTTP_CONTENT_DECODING, 0L);
|
2022-02-17 23:35:57 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_RANGE, range->c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
void resumePausedTasks()
|
|
|
|
{
|
|
|
|
TR_ASSERT(std::this_thread::get_id() == curl_thread->get_id());
|
|
|
|
|
|
|
|
auto& paused = paused_easy_handles;
|
|
|
|
if (std::empty(paused))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
return;
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2010-01-22 03:39:21 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
auto const now = tr_time_msec();
|
|
|
|
|
|
|
|
for (auto it = std::begin(paused); it != std::end(paused);)
|
|
|
|
{
|
2023-03-06 05:42:52 +00:00
|
|
|
if (it->second + BandwidthPauseMsec < now)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2023-03-06 05:42:52 +00:00
|
|
|
curl_easy_pause(it->first, CURLPAUSE_CONT);
|
2022-02-16 18:33:50 +00:00
|
|
|
it = paused.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
[[nodiscard]] bool is_idle() const noexcept
|
|
|
|
{
|
|
|
|
return std::empty(queued_tasks_) && std::empty(running_tasks_);
|
|
|
|
}
|
|
|
|
|
2022-12-23 16:56:27 +00:00
|
|
|
void remove_task(Task const& task)
|
2022-11-30 19:00:34 +00:00
|
|
|
{
|
|
|
|
auto const lock = std::unique_lock{ tasks_mutex_ };
|
|
|
|
|
|
|
|
auto const iter = std::find(std::begin(running_tasks_), std::end(running_tasks_), task);
|
|
|
|
TR_ASSERT(iter != std::end(running_tasks_));
|
|
|
|
if (iter == std::end(running_tasks_))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->done();
|
|
|
|
running_tasks_.erase(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void timeout_task(Task& task)
|
|
|
|
{
|
|
|
|
task.response.status = 408; // request timed out
|
|
|
|
task.response.did_timeout = true;
|
|
|
|
remove_task(task);
|
|
|
|
}
|
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
// the thread started by Impl.curl_thread runs this function
|
2022-11-30 19:00:34 +00:00
|
|
|
void curlThreadFunc()
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
auto const multi = curl_helpers::multi_unique_ptr{ curl_multi_init() };
|
2022-02-16 18:33:50 +00:00
|
|
|
|
|
|
|
auto repeats = unsigned{};
|
|
|
|
for (;;)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
if (deadline_reached())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
while (!std::empty(running_tasks_))
|
|
|
|
{
|
|
|
|
auto& task = running_tasks_.front();
|
|
|
|
curl_multi_remove_handle(multi.get(), task.easy());
|
|
|
|
timeout_task(task);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (deadline_exists() && is_idle())
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
if (auto lock = std::unique_lock{ tasks_mutex_ }; lock.owns_lock())
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-02-25 20:45:00 +00:00
|
|
|
// sleep until there's something to do
|
2022-11-30 19:00:34 +00:00
|
|
|
auto const stop_waiting = [this]()
|
2022-02-25 20:45:00 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
return !is_idle() || !deadline_exists();
|
2022-02-25 20:45:00 +00:00
|
|
|
};
|
2022-11-30 19:00:34 +00:00
|
|
|
if (!stop_waiting())
|
2022-02-25 20:45:00 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
queued_tasks_cv_.wait(lock, stop_waiting);
|
2022-02-25 20:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add queued tasks
|
2022-11-30 19:00:34 +00:00
|
|
|
if (!std::empty(queued_tasks_))
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
for (auto& task : queued_tasks_)
|
|
|
|
{
|
|
|
|
initEasy(task);
|
|
|
|
curl_multi_add_handle(multi.get(), task.easy());
|
|
|
|
}
|
|
|
|
|
|
|
|
running_tasks_.splice(std::end(running_tasks_), queued_tasks_);
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2022-02-23 17:00:40 +00:00
|
|
|
}
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
resumePausedTasks();
|
2022-06-11 04:56:43 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
// Adapted from https://curl.se/libcurl/c/curl_multi_wait.html docs.
|
|
|
|
// 'numfds' being zero means either a timeout or no file descriptors to
|
|
|
|
// wait for. Try timeout on first occurrence, then assume no file
|
|
|
|
// descriptors and no file descriptors to wait for means wait for 100
|
|
|
|
// milliseconds.
|
2021-10-15 17:27:12 +00:00
|
|
|
auto numfds = int{};
|
2022-02-16 18:33:50 +00:00
|
|
|
curl_multi_wait(multi.get(), nullptr, 0, 1000, &numfds);
|
2022-02-07 04:28:36 +00:00
|
|
|
if (numfds == 0)
|
2020-07-29 02:38:27 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
++repeats;
|
|
|
|
if (repeats > 1U)
|
2020-07-29 02:38:27 +00:00
|
|
|
{
|
2023-06-29 13:51:39 +00:00
|
|
|
std::this_thread::sleep_for(100ms);
|
2020-07-29 02:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
repeats = 0;
|
|
|
|
}
|
2010-01-22 03:39:21 +00:00
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
// nonblocking update of the tasks
|
2022-11-30 19:00:34 +00:00
|
|
|
auto n_running = int{};
|
|
|
|
curl_multi_perform(multi.get(), &n_running);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
// process any tasks that just finished
|
2022-02-16 18:33:50 +00:00
|
|
|
CURLMsg* msg = nullptr;
|
2022-02-17 23:35:57 +00:00
|
|
|
auto unused = int{};
|
2022-02-16 18:33:50 +00:00
|
|
|
while ((msg = curl_multi_info_read(multi.get(), &unused)) != nullptr)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
if (msg->msg == CURLMSG_DONE && msg->easy_handle != nullptr)
|
|
|
|
{
|
|
|
|
auto* const e = msg->easy_handle;
|
|
|
|
|
|
|
|
Task* task = nullptr;
|
|
|
|
curl_easy_getinfo(e, CURLINFO_PRIVATE, (void*)&task);
|
|
|
|
|
|
|
|
auto req_bytes_sent = long{};
|
|
|
|
auto total_time = double{};
|
|
|
|
curl_easy_getinfo(e, CURLINFO_REQUEST_SIZE, &req_bytes_sent);
|
|
|
|
curl_easy_getinfo(e, CURLINFO_TOTAL_TIME, &total_time);
|
|
|
|
curl_easy_getinfo(e, CURLINFO_RESPONSE_CODE, &task->response.status);
|
|
|
|
task->response.did_connect = task->response.status > 0 || req_bytes_sent > 0;
|
2022-11-30 00:09:32 +00:00
|
|
|
task->response.did_timeout = task->response.status == 0 &&
|
|
|
|
std::chrono::duration<double>(total_time) >= task->timeoutSecs();
|
2022-02-16 18:33:50 +00:00
|
|
|
curl_multi_remove_handle(multi.get(), e);
|
2022-11-30 19:00:34 +00:00
|
|
|
remove_task(*task);
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
curl_helpers::shared_unique_ptr const curlsh_{ curl_share_init() };
|
|
|
|
|
|
|
|
std::map<std::string /*host*/, std::stack<curl_helpers::easy_unique_ptr>, std::less<>> easy_pool_;
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
std::mutex tasks_mutex_;
|
|
|
|
std::condition_variable queued_tasks_cv_;
|
|
|
|
std::list<Task> queued_tasks_;
|
|
|
|
std::list<Task> running_tasks_;
|
2022-02-17 23:35:57 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
CURLSH* shared()
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
return curlsh_.get();
|
2011-02-21 01:13:28 +00:00
|
|
|
}
|
|
|
|
|
2023-09-03 23:26:39 +00:00
|
|
|
void share_setopt()
|
2022-03-24 03:02:18 +00:00
|
|
|
{
|
2023-09-03 23:26:39 +00:00
|
|
|
// Do *not* share HSTS cache
|
|
|
|
// https://github.com/transmission/transmission/issues/5199
|
2022-03-24 03:02:18 +00:00
|
|
|
|
|
|
|
auto* const sh = shared();
|
2023-09-03 23:26:39 +00:00
|
|
|
curl_share_setopt(sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
|
|
|
|
curl_share_setopt(sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
|
|
|
|
#if LIBCURL_VERSION_NUM >= 0x071700 /* 7.23.0 */
|
|
|
|
curl_share_setopt(sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
|
|
|
|
#endif
|
|
|
|
#if LIBCURL_VERSION_NUM >= 0x073900 /* 7.57.0 */
|
|
|
|
curl_share_setopt(sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
|
|
|
|
#endif
|
|
|
|
#if LIBCURL_VERSION_NUM >= 0x073D00 /* 7.61.0 */
|
|
|
|
curl_share_setopt(sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
|
|
|
|
#endif
|
2022-03-24 03:02:18 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 05:42:52 +00:00
|
|
|
std::map<CURL*, uint64_t /*tr_time_msec()*/> paused_easy_handles;
|
2022-02-16 18:33:50 +00:00
|
|
|
};
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
tr_web::tr_web(Mediator& mediator)
|
|
|
|
: impl_{ std::make_unique<Impl>(mediator) }
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-23 14:03:26 +00:00
|
|
|
tr_web::~tr_web()
|
|
|
|
{
|
|
|
|
impl_->startShutdown(0ms);
|
|
|
|
}
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
std::unique_ptr<tr_web> tr_web::create(Mediator& mediator)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
return std::unique_ptr<tr_web>(new tr_web(mediator));
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tr_web::fetch(FetchOptions&& options)
|
|
|
|
{
|
|
|
|
impl_->fetch(std::move(options));
|
|
|
|
}
|
|
|
|
|
2022-11-30 19:00:34 +00:00
|
|
|
void tr_web::startShutdown(std::chrono::milliseconds deadline)
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-11-30 19:00:34 +00:00
|
|
|
impl_->startShutdown(deadline);
|
2008-04-25 19:46:36 +00:00
|
|
|
}
|