2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2021-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +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
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2022-11-30 00:09:32 +00:00
|
|
|
#include <chrono>
|
2023-06-30 04:51:55 +00:00
|
|
|
#include <cstddef> // size_t
|
|
|
|
#include <ctime> // time_t
|
2022-02-17 23:35:57 +00:00
|
|
|
#include <functional>
|
2022-02-16 18:33:50 +00:00
|
|
|
#include <memory>
|
2022-02-17 23:35:57 +00:00
|
|
|
#include <optional>
|
2022-02-14 02:09:56 +00:00
|
|
|
#include <string>
|
2021-11-15 01:17:03 +00:00
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2021-10-16 13:20:04 +00:00
|
|
|
|
2021-11-09 03:30:03 +00:00
|
|
|
struct evbuffer;
|
2011-07-10 15:24:51 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
class tr_web
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
public:
|
|
|
|
// The response struct passed to the user's FetchDoneFunc callback
|
|
|
|
// when a fetch() finishes.
|
|
|
|
struct FetchResponse
|
|
|
|
{
|
2022-11-30 00:09:32 +00:00
|
|
|
long status = 0; // http server response, e.g. 200
|
2022-02-16 18:33:50 +00:00
|
|
|
std::string body;
|
2022-11-30 00:09:32 +00:00
|
|
|
bool did_connect = false;
|
|
|
|
bool did_timeout = false;
|
|
|
|
void* user_data = nullptr;
|
2022-02-16 18:33:50 +00:00
|
|
|
};
|
2008-04-25 19:46:36 +00:00
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
// Callback to invoke when fetch() is done
|
|
|
|
using FetchDoneFunc = std::function<void(FetchResponse const&)>;
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
class FetchOptions
|
|
|
|
{
|
|
|
|
public:
|
2022-06-18 01:33:11 +00:00
|
|
|
enum class IPProtocol
|
|
|
|
{
|
|
|
|
ANY,
|
|
|
|
V4,
|
|
|
|
V6,
|
|
|
|
};
|
|
|
|
|
2022-11-30 00:09:32 +00:00
|
|
|
FetchOptions(
|
|
|
|
std::string_view url_in,
|
|
|
|
FetchDoneFunc&& done_func_in,
|
|
|
|
void* done_func_user_data_in,
|
|
|
|
std::chrono::seconds timeout_secs_in = DefaultTimeoutSecs)
|
2022-02-16 18:33:50 +00:00
|
|
|
: url{ url_in }
|
2022-02-17 23:35:57 +00:00
|
|
|
, done_func{ std::move(done_func_in) }
|
2022-02-16 18:33:50 +00:00
|
|
|
, done_func_user_data{ done_func_user_data_in }
|
2022-11-30 00:09:32 +00:00
|
|
|
, timeout_secs{ timeout_secs_in }
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
}
|
2008-04-25 04:26:04 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
// the URL to fetch
|
|
|
|
std::string url;
|
|
|
|
|
|
|
|
// Callback to invoke with a FetchResponse when done
|
|
|
|
FetchDoneFunc done_func = nullptr;
|
|
|
|
void* done_func_user_data = nullptr;
|
|
|
|
|
|
|
|
// If you need to set multiple cookies, set them all using a single
|
|
|
|
// option concatenated like this: "name1=content1; name2=content2;"
|
|
|
|
std::optional<std::string> cookies;
|
|
|
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests
|
|
|
|
std::optional<std::string> range;
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
// Tag used by tr_web::Mediator to limit some transfers' bandwidth
|
2022-02-16 18:33:50 +00:00
|
|
|
std::optional<int> speed_limit_tag;
|
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
// Optionally set the underlying sockets' send/receive buffers' size.
|
2022-02-17 23:35:57 +00:00
|
|
|
// Can be used to conserve resources for scrapes and announces, where
|
|
|
|
// the payload is known to be small.
|
2022-02-16 18:33:50 +00:00
|
|
|
std::optional<int> sndbuf;
|
|
|
|
std::optional<int> rcvbuf;
|
|
|
|
|
|
|
|
// Maximum time to wait before timeout
|
2022-11-30 00:09:32 +00:00
|
|
|
std::chrono::seconds timeout_secs = DefaultTimeoutSecs;
|
2022-02-16 18:33:50 +00:00
|
|
|
|
|
|
|
// If provided, this buffer will be used to hold the response body.
|
|
|
|
// Provided for webseeds, which need to set low-level callbacks on
|
|
|
|
// the buffer itself.
|
|
|
|
evbuffer* buffer = nullptr;
|
|
|
|
|
2022-06-18 01:33:11 +00:00
|
|
|
// IP protocol to use when making the request
|
|
|
|
IPProtocol ip_proto = IPProtocol::ANY;
|
|
|
|
|
2022-11-30 00:09:32 +00:00
|
|
|
static auto inline constexpr DefaultTimeoutSecs = std::chrono::seconds{ 120 };
|
2022-02-16 18:33:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void fetch(FetchOptions&& options);
|
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
// Notify tr_web that it's going to be destroyed soon.
|
2022-02-16 18:33:50 +00:00
|
|
|
// New fetch() tasks will be rejected, but already-running tasks
|
|
|
|
// are left alone so that they can finish.
|
2023-01-28 23:58:20 +00:00
|
|
|
void startShutdown(std::chrono::milliseconds /*deadline*/);
|
2022-02-16 18:33:50 +00:00
|
|
|
|
2022-02-17 23:35:57 +00:00
|
|
|
// If you want to give running tasks a chance to finish, call closeSoon()
|
|
|
|
// before destroying the tr_web object. Deleting the object will cancel
|
|
|
|
// all of its tasks.
|
2022-02-16 18:33:50 +00:00
|
|
|
~tr_web();
|
|
|
|
|
|
|
|
/**
|
2023-01-23 16:26:11 +00:00
|
|
|
* Mediates between `tr_web` and its clients.
|
2022-02-16 18:33:50 +00:00
|
|
|
*
|
2023-01-23 16:26:11 +00:00
|
|
|
* NB: Note that `tr_web` calls all these methods from its own thread.
|
2022-02-17 23:35:57 +00:00
|
|
|
* Overridden methods should take care to be threadsafe.
|
2022-02-16 18:33:50 +00:00
|
|
|
*/
|
2022-02-22 15:09:24 +00:00
|
|
|
class Mediator
|
2022-02-14 02:09:56 +00:00
|
|
|
{
|
2022-02-16 18:33:50 +00:00
|
|
|
public:
|
2022-02-22 15:09:24 +00:00
|
|
|
virtual ~Mediator() = default;
|
2022-02-16 18:33:50 +00:00
|
|
|
|
|
|
|
// Return the location of the cookie file, or nullopt to not use one
|
|
|
|
[[nodiscard]] virtual std::optional<std::string> cookieFile() const
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-08-21 23:37:38 +00:00
|
|
|
// Return IPv4 user public address string, or nullopt to not use one
|
2023-08-14 15:31:12 +00:00
|
|
|
[[nodiscard]] virtual std::optional<std::string> bind_address_V4() const
|
2022-08-21 23:37:38 +00:00
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return IPv6 user public address string, or nullopt to not use one
|
2023-08-14 15:31:12 +00:00
|
|
|
[[nodiscard]] virtual std::optional<std::string> bind_address_V6() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the preferred user aagent, or nullopt to not use one
|
2022-05-31 14:28:01 +00:00
|
|
|
[[nodiscard]] virtual std::optional<std::string_view> userAgent() const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the system that `byte_count` of download bandwidth was used
|
|
|
|
virtual void notifyBandwidthConsumed([[maybe_unused]] int bandwidth_tag, [[maybe_unused]] size_t byte_count)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-06-29 20:08:58 +00:00
|
|
|
// Return the number of bytes that should be allowed. See tr_bandwidth::clamp()
|
2022-11-21 16:19:45 +00:00
|
|
|
[[nodiscard]] virtual size_t clamp([[maybe_unused]] int bandwidth_tag, size_t byte_count) const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
|
|
|
return byte_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke the user-provided fetch callback
|
2022-02-17 23:35:57 +00:00
|
|
|
virtual void run(FetchDoneFunc&& func, FetchResponse&& response) const
|
2022-02-16 18:33:50 +00:00
|
|
|
{
|
2022-02-17 23:35:57 +00:00
|
|
|
func(response);
|
2022-02-16 18:33:50 +00:00
|
|
|
}
|
2022-12-23 14:03:26 +00:00
|
|
|
|
2023-04-23 02:11:16 +00:00
|
|
|
[[nodiscard]] virtual time_t now() const
|
|
|
|
{
|
|
|
|
return time(nullptr);
|
|
|
|
}
|
2022-02-16 18:33:50 +00:00
|
|
|
};
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
// Note that tr_web does no management of the `mediator` reference.
|
|
|
|
// The caller must ensure `mediator` is valid for tr_web's lifespan.
|
2023-01-02 17:34:36 +00:00
|
|
|
[[nodiscard]] static std::unique_ptr<tr_web> create(Mediator& mediator);
|
2022-02-16 18:33:50 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
class Impl;
|
|
|
|
std::unique_ptr<Impl> const impl_;
|
2022-02-22 15:09:24 +00:00
|
|
|
explicit tr_web(Mediator& mediator);
|
2022-02-14 02:09:56 +00:00
|
|
|
};
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2022-02-16 18:33:50 +00:00
|
|
|
void tr_sessionFetch(struct tr_session* session, tr_web::FetchOptions&& options);
|