refactor: tr_torrentInfoGetMagnetLink now takes a std::string_view (#1968)

* refactor: tr_torrentInfoGetMagnetLink now takes a std::string_view
This commit is contained in:
Charles Kerr 2021-10-16 08:20:04 -05:00 committed by GitHub
parent 7b423b7e0d
commit 43ad1346eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 24 deletions

View File

@ -110,7 +110,7 @@ static char* announce_url_new(tr_session const* session, tr_announce_request con
char ipv6_readable[INET6_ADDRSTRLEN];
evutil_inet_ntop(AF_INET6, ipv6, ipv6_readable, INET6_ADDRSTRLEN);
evbuffer_add_printf(buf, "&ipv6=");
tr_http_escape(buf, ipv6_readable, TR_BAD_SIZE, true);
tr_http_escape(buf, ipv6_readable, true);
}
return evbuffer_free_to_str(buf, nullptr);

View File

@ -427,19 +427,19 @@ char* tr_torrentInfoGetMagnetLink(tr_info const* inf)
if (!tr_str_is_empty(name))
{
evbuffer_add_printf(s, "%s", "&dn=");
tr_http_escape(s, name, TR_BAD_SIZE, true);
tr_http_escape(s, name, true);
}
for (unsigned int i = 0; i < inf->trackerCount; ++i)
{
evbuffer_add_printf(s, "%s", "&tr=");
tr_http_escape(s, inf->trackers[i].announce, TR_BAD_SIZE, true);
tr_http_escape(s, inf->trackers[i].announce, true);
}
for (unsigned int i = 0; i < inf->webseedCount; i++)
{
evbuffer_add_printf(s, "%s", "&ws=");
tr_http_escape(s, inf->webseeds[i], TR_BAD_SIZE, true);
tr_http_escape(s, inf->webseeds[i], true);
}
return evbuffer_free_to_str(s, nullptr);

View File

@ -746,28 +746,20 @@ char const* tr_webGetResponseStr(long code)
}
}
void tr_http_escape(struct evbuffer* out, char const* str, size_t len, bool escape_slashes)
void tr_http_escape(struct evbuffer* out, std::string_view str, bool escape_reserved)
{
if (str == nullptr)
{
return;
}
auto constexpr ReservedChars = std::string_view{ "!*'();:@&=+$,/?%#[]" };
auto constexpr UnescapedChars = std::string_view{ "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.~" };
if (len == TR_BAD_SIZE)
for (auto& ch : str)
{
len = strlen(str);
}
for (char const* end = str + len; str != end; ++str)
{
if (*str == ',' || *str == '-' || *str == '.' || ('0' <= *str && *str <= '9') || ('A' <= *str && *str <= 'Z') ||
('a' <= *str && *str <= 'z') || (*str == '/' && !escape_slashes))
if ((UnescapedChars.find(ch) != std::string_view::npos) || (ReservedChars.find(ch) && !escape_reserved))
{
evbuffer_add_printf(out, "%c", *str);
evbuffer_add_printf(out, "%c", ch);
}
else
{
evbuffer_add_printf(out, "%%%02X", (unsigned)(*str & 0xFF));
evbuffer_add_printf(out, "%%%02X", (unsigned)(ch & 0xFF));
}
}
}

View File

@ -8,6 +8,9 @@
#pragma once
#include <cstdint>
#include <string_view>
#include "tr-macros.h"
struct tr_address;
@ -55,7 +58,7 @@ long tr_webGetTaskResponseCode(struct tr_web_task* task);
char const* tr_webGetTaskRealUrl(struct tr_web_task* task);
void tr_http_escape(struct evbuffer* out, char const* str, size_t len, bool escape_slashes);
void tr_http_escape(struct evbuffer* out, std::string_view str, bool escape_reserved);
void tr_http_escape_sha1(char* out, uint8_t const* sha1_digest);

View File

@ -495,7 +495,7 @@ static std::string make_url(tr_webseed* w, tr_file const* file)
/* if url ends with a '/', add the torrent name */
if (*std::rbegin(w->base_url) == '/' && file->name != nullptr)
{
tr_http_escape(buf, file->name, strlen(file->name), false);
tr_http_escape(buf, file->name, false);
}
auto const url = std::string{ (char const*)evbuffer_pullup(buf, -1), evbuffer_get_length(buf) };

View File

@ -7,6 +7,7 @@
*/
#include "transmission.h"
#include "metainfo.h"
#include "utils.h"
@ -21,11 +22,11 @@ TEST(Metainfo, magnetLink)
// background info @ http://wiki.theory.org/BitTorrent_Magnet-URI_Webseeding
char const constexpr* const MagnetLink =
"magnet:?"
"xt=urn:btih:14FFE5DD23188FD5CB53A1D47F1289DB70ABF31E"
"&dn=ubuntu+12+04+1+desktop+32+bit"
"xt=urn:btih:14ffe5dd23188fd5cb53a1d47f1289db70abf31e"
"&dn=ubuntu_12_04_1_desktop_32_bit"
"&tr=http%3A%2F%2Ftracker.publicbt.com%2Fannounce"
"&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80"
"&ws=http://transmissionbt.com ";
"&ws=http%3A%2F%2Ftransmissionbt.com";
auto* ctor = tr_ctorNew(nullptr);
tr_ctorSetMetainfoFromMagnetLink(ctor, MagnetLink);
@ -39,6 +40,10 @@ TEST(Metainfo, magnetLink)
EXPECT_EQ(1, inf.webseedCount);
EXPECT_STREQ("http://transmissionbt.com", inf.webseeds[0]);
auto* const link = tr_torrentInfoGetMagnetLink(&inf);
EXPECT_STREQ(MagnetLink, link);
tr_free(link);
/* cleanup */
tr_metainfoFree(&inf);
tr_ctorFree(ctor);