Revert "refactor: use OutputIterator for url-escaped info hash strings (#3672)"

This reverts commit 479a16787e.
This commit is contained in:
Charles Kerr 2022-08-19 02:18:16 -05:00
parent dde626d5e5
commit 1cf19550ad
4 changed files with 40 additions and 15 deletions

View File

@ -56,10 +56,14 @@ static tr_urlbuf announce_url_new(tr_session const* session, tr_announce_request
auto url = tr_urlbuf{};
auto out = std::back_inserter(url);
auto escaped_info_hash = std::array<char, SHA_DIGEST_LENGTH * 3 + 1>{};
tr_http_escape_sha1(std::data(escaped_info_hash), req->info_hash);
fmt::format_to(
out,
"{url}"
"{sep}peer_id={peer_id}"
"{sep}info_hash={info_hash}"
"&peer_id={peer_id}"
"&port={port}"
"&uploaded={uploaded}"
"&downloaded={downloaded}"
@ -70,6 +74,7 @@ static tr_urlbuf announce_url_new(tr_session const* session, tr_announce_request
"&supportcrypto=1",
fmt::arg("url", req->announce_url),
fmt::arg("sep", tr_strvContains(req->announce_url.sv(), '?') ? '&' : '?'),
fmt::arg("info_hash", std::data(escaped_info_hash)),
fmt::arg("peer_id", std::string_view{ std::data(req->peer_id), std::size(req->peer_id) }),
fmt::arg("port", req->port.host()),
fmt::arg("uploaded", req->up),
@ -78,9 +83,6 @@ static tr_urlbuf announce_url_new(tr_session const* session, tr_announce_request
fmt::arg("numwant", req->numwant),
fmt::arg("key", req->key));
fmt::format_to(out, "&info_hash=");
tr_http_escape(out, req->info_hash);
if (session->encryptionMode() == TR_ENCRYPTION_REQUIRED)
{
fmt::format_to(out, "&requirecrypto=1");
@ -650,12 +652,12 @@ static auto scrape_url_new(tr_scrape_request const* req)
char delimiter = tr_strvContains(sv, '?') ? '&' : '?';
auto scrape_url = tr_pathbuf{ sv };
auto out = std::back_inserter(scrape_url);
for (int i = 0; i < req->info_hash_count; ++i)
{
fmt::format_to(out, "{}info_hash=", delimiter);
tr_http_escape(out, req->info_hash[i]);
char str[SHA_DIGEST_LENGTH * 3 + 1];
tr_http_escape_sha1(str, req->info_hash[i]);
scrape_url.append(delimiter, "info_hash=", str);
delimiter = '&';
}

View File

@ -172,6 +172,29 @@ char const* tr_webGetResponseStr(long code)
}
}
static bool is_rfc2396_alnum(uint8_t ch)
{
return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '.' || ch == '-' ||
ch == '_' || ch == '~';
}
void tr_http_escape_sha1(char* out, tr_sha1_digest_t const& digest)
{
for (auto const b : digest)
{
if (is_rfc2396_alnum(uint8_t(b)))
{
*out++ = (char)b;
}
else
{
out = fmt::format_to(out, FMT_STRING("%{:02x}"), unsigned(b));
}
}
*out = '\0';
}
//// URLs
namespace

View File

@ -111,11 +111,7 @@ void tr_http_escape(OutputIt out, std::string_view in, bool escape_reserved)
}
}
template<typename OutputIt>
void tr_http_escape(OutputIt out, tr_sha1_digest_t const& digest)
{
tr_http_escape(out, std::string_view{ reinterpret_cast<char const*>(digest.data()), std::size(digest) }, false);
}
void tr_http_escape_sha1(char* out, tr_sha1_digest_t const& digest);
char const* tr_webGetResponseStr(long response_code);

View File

@ -8,7 +8,6 @@
#include <cinttypes> // PRIu64
#include <cstdio>
#include <ctime>
#include <iterator>
#include <string>
#include <string_view>
@ -340,9 +339,14 @@ void doScrape(tr_torrent_metainfo const& metainfo)
}
// build the full scrape URL
auto escaped = std::array<char, TR_SHA1_DIGEST_LEN * 3 + 1>{};
tr_http_escape_sha1(std::data(escaped), metainfo.infoHash());
auto const scrape = tracker.scrape.sv();
auto url = tr_urlbuf{ scrape, tr_strvContains(scrape, '?') ? '&' : '?', "info_hash="sv };
tr_http_escape(std::back_inserter(url), metainfo.infoHash());
auto const url = tr_urlbuf{ scrape,
tr_strvContains(scrape, '?') ? '&' : '?',
"info_hash="sv,
std::string_view{ std::data(escaped) } };
printf("%" TR_PRIsv " ... ", TR_PRIsv_ARG(url));
fflush(stdout);