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.
|
2021-11-09 03:30:03 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-04-16 18:13:42 +00:00
|
|
|
#include <cstdint> // uint16_t
|
2021-11-09 03:30:03 +00:00
|
|
|
#include <optional>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-11-09 03:30:03 +00:00
|
|
|
#include <string_view>
|
2022-02-28 22:26:26 +00:00
|
|
|
#include <utility>
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-08-06 01:37:21 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/tr-macros.h" // tr_sha1_digest_t
|
2021-11-09 03:30:03 +00:00
|
|
|
|
|
|
|
/** @brief convenience function to determine if an address is an IP address (IPv4 or IPv6) */
|
|
|
|
bool tr_addressIsIP(char const* address);
|
|
|
|
|
|
|
|
/** @brief return true if the url is a http or https or UDP url that Transmission understands */
|
|
|
|
bool tr_urlIsValidTracker(std::string_view url);
|
|
|
|
|
|
|
|
/** @brief return true if the url is a [ http, https, ftp, sftp ] url that Transmission understands */
|
|
|
|
bool tr_urlIsValid(std::string_view url);
|
|
|
|
|
|
|
|
struct tr_url_parsed_t
|
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
// http://example.com:80/over/there?name=ferret#nose
|
|
|
|
|
|
|
|
std::string_view scheme; // "http"
|
|
|
|
std::string_view authority; // "example.com:80"
|
|
|
|
std::string_view host; // "example.com"
|
|
|
|
std::string_view sitename; // "example"
|
|
|
|
std::string_view path; // /"over/there"
|
|
|
|
std::string_view query; // "name=ferret"
|
|
|
|
std::string_view fragment; // "nose"
|
|
|
|
std::string_view full; // "http://example.com:80/over/there?name=ferret#nose"
|
2022-04-16 18:13:42 +00:00
|
|
|
uint16_t port = 0;
|
2021-11-09 03:30:03 +00:00
|
|
|
};
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::optional<tr_url_parsed_t> tr_urlParse(std::string_view url);
|
2021-11-09 03:30:03 +00:00
|
|
|
|
|
|
|
// like tr_urlParse(), but with the added constraint that 'scheme'
|
|
|
|
// must be one we that Transmission supports for announce and scrape
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] std::optional<tr_url_parsed_t> tr_urlParseTracker(std::string_view url);
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2023-01-17 07:46:43 +00:00
|
|
|
// Convenience function to get a log-safe version of a tracker URL.
|
|
|
|
// This is to avoid logging sensitive info, e.g. a personal announcer id in the URL.
|
|
|
|
[[nodiscard]] std::string tr_urlTrackerLogName(std::string_view url);
|
|
|
|
|
2021-11-09 03:30:03 +00:00
|
|
|
// example use: `for (auto const [key, val] : tr_url_query_view{ querystr })`
|
|
|
|
struct tr_url_query_view
|
|
|
|
{
|
|
|
|
std::string_view const query;
|
|
|
|
|
|
|
|
explicit tr_url_query_view(std::string_view query_in)
|
|
|
|
: query{ query_in }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
struct iterator
|
|
|
|
{
|
|
|
|
std::pair<std::string_view, std::string_view> keyval = std::make_pair(std::string_view{ "" }, std::string_view{ "" });
|
2023-01-28 23:58:20 +00:00
|
|
|
std::string_view remain = "";
|
2021-11-09 03:30:03 +00:00
|
|
|
|
|
|
|
iterator& operator++();
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr auto const& operator*() const
|
2021-11-09 03:30:03 +00:00
|
|
|
{
|
|
|
|
return keyval;
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr auto const* operator->() const
|
2021-11-09 03:30:03 +00:00
|
|
|
{
|
|
|
|
return &keyval;
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool operator==(iterator const& that) const
|
2021-11-09 03:30:03 +00:00
|
|
|
{
|
|
|
|
return this->remain == that.remain && this->keyval == that.keyval;
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr bool operator!=(iterator const& that) const
|
2021-11-09 03:30:03 +00:00
|
|
|
{
|
|
|
|
return !(*this == that);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] iterator begin() const;
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] constexpr iterator end() const
|
2021-11-09 03:30:03 +00:00
|
|
|
{
|
|
|
|
return iterator{};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-21 13:43:09 +00:00
|
|
|
template<typename BackInsertIter>
|
|
|
|
constexpr void tr_urlPercentEncode(BackInsertIter out, std::string_view input, bool escape_reserved = true)
|
2022-03-28 22:13:32 +00:00
|
|
|
{
|
2023-01-28 23:58:20 +00:00
|
|
|
auto constexpr IsUnreserved = [](unsigned char ch)
|
2022-08-21 13:43:09 +00:00
|
|
|
{
|
|
|
|
return ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == '-' || ch == '_' ||
|
|
|
|
ch == '.' || ch == '~';
|
|
|
|
};
|
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
auto constexpr IsReserved = [](unsigned char ch)
|
2022-08-21 13:43:09 +00:00
|
|
|
{
|
|
|
|
return ch == '!' || ch == '*' || ch == '(' || ch == ')' || ch == ';' || ch == ':' || ch == '@' || ch == '&' ||
|
|
|
|
ch == '=' || ch == '+' || ch == '$' || ch == ',' || ch == '/' || ch == '?' || ch == '%' || ch == '#' || ch == '[' ||
|
|
|
|
ch == ']' || ch == '\'';
|
|
|
|
};
|
2021-11-10 02:42:18 +00:00
|
|
|
|
2022-08-21 13:43:09 +00:00
|
|
|
for (unsigned char ch : input)
|
2022-03-28 22:13:32 +00:00
|
|
|
{
|
2023-01-28 23:58:20 +00:00
|
|
|
if (IsUnreserved(ch) || (!escape_reserved && IsReserved(ch)))
|
2022-03-28 22:13:32 +00:00
|
|
|
{
|
|
|
|
out = ch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-21 13:43:09 +00:00
|
|
|
fmt::format_to(out, "%{:02X}", ch);
|
2022-03-28 22:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2022-08-21 13:43:09 +00:00
|
|
|
template<typename BackInsertIter>
|
|
|
|
constexpr void tr_urlPercentEncode(BackInsertIter out, tr_sha1_digest_t const& digest)
|
2022-08-19 19:33:06 +00:00
|
|
|
{
|
2022-08-21 13:43:09 +00:00
|
|
|
tr_urlPercentEncode(out, std::string_view{ reinterpret_cast<char const*>(digest.data()), std::size(digest) });
|
2022-08-19 19:33:06 +00:00
|
|
|
}
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2022-09-09 02:49:51 +00:00
|
|
|
[[nodiscard]] char const* tr_webGetResponseStr(long response_code);
|
2021-11-09 03:30:03 +00:00
|
|
|
|
2023-01-28 23:58:20 +00:00
|
|
|
[[nodiscard]] std::string tr_urlPercentDecode(std::string_view /*url*/);
|