diff --git a/libtransmission/announcer-udp.cc b/libtransmission/announcer-udp.cc
index 4caddca15..702e4f5fb 100644
--- a/libtransmission/announcer-udp.cc
+++ b/libtransmission/announcer-udp.cc
@@ -32,6 +32,8 @@
#define dbgmsg(key, ...) tr_logAddDeepNamed(tr_quark_get_string(key), __VA_ARGS__)
+using namespace std::literals;
+
/****
*****
****/
@@ -221,11 +223,15 @@ static void tau_scrape_request_finished(struct tau_scrape_request const* request
}
}
-static void tau_scrape_request_fail(struct tau_scrape_request* request, bool did_connect, bool did_timeout, char const* errmsg)
+static void tau_scrape_request_fail(
+ struct tau_scrape_request* request,
+ bool did_connect,
+ bool did_timeout,
+ std::string_view errmsg)
{
request->response.did_connect = did_connect;
request->response.did_timeout = did_timeout;
- request->response.errmsg = errmsg == nullptr ? "" : errmsg;
+ request->response.errmsg = errmsg;
tau_scrape_request_finished(request);
}
@@ -369,11 +375,11 @@ static void tau_announce_request_fail(
struct tau_announce_request* request,
bool did_connect,
bool did_timeout,
- char const* errmsg)
+ std::string_view errmsg)
{
request->response.did_connect = did_connect;
request->response.did_timeout = did_timeout;
- request->response.errmsg = tr_strdup(errmsg);
+ request->response.errmsg = tr_strvDup(errmsg);
tau_announce_request_finished(request);
}
@@ -400,10 +406,10 @@ static void on_announce_response(struct tau_announce_request* request, tau_actio
}
else
{
- char* const errmsg = action == TAU_ACTION_ERROR && buflen > 0 ? tr_strndup(evbuffer_pullup(buf, -1), buflen) :
- tr_strdup(_("Unknown error"));
+ auto const errmsg = action == TAU_ACTION_ERROR && buflen > 0 ?
+ std::string_view{ reinterpret_cast(evbuffer_pullup(buf, -1)), buflen } :
+ _("Unknown error");
tau_announce_request_fail(request, true, false, errmsg);
- tr_free(errmsg);
}
}
@@ -460,7 +466,7 @@ static void tau_tracker_free(struct tau_tracker* t)
delete t;
}
-static void tau_tracker_fail_all(struct tau_tracker* tracker, bool did_connect, bool did_timeout, char const* errmsg)
+static void tau_tracker_fail_all(struct tau_tracker* tracker, bool did_connect, bool did_timeout, std::string_view errmsg)
{
/* fail all the scrapes */
tr_ptrArray* reqs = &tracker->scrapes;
@@ -495,10 +501,9 @@ static void tau_tracker_on_dns(int errcode, struct evutil_addrinfo* addr, void*
if (errcode != 0)
{
- char* errmsg = tr_strdup_printf(_("DNS Lookup failed: %s"), evutil_gai_strerror(errcode));
- dbgmsg(tracker->key, "%s", errmsg);
- tau_tracker_fail_all(tracker, false, false, errmsg);
- tr_free(errmsg);
+ auto const errmsg = tr_strvJoin("DNS Lookup failed: "sv, evutil_gai_strerror(errcode));
+ dbgmsg(tracker->key, "%s", errmsg.c_str());
+ tau_tracker_fail_all(tracker, false, false, errmsg.c_str());
}
else
{
@@ -621,7 +626,7 @@ static void tau_tracker_timeout_reqs(struct tau_tracker* tracker)
if (cancel_all || req->created_at + TauRequestTtl < now)
{
dbgmsg(tracker->key, "timeout announce req %p", (void*)req);
- tau_announce_request_fail(req, false, true, nullptr);
+ tau_announce_request_fail(req, false, true, "");
tau_announce_request_free(req);
tr_ptrArrayRemove(reqs, i);
--i;
@@ -638,7 +643,7 @@ static void tau_tracker_timeout_reqs(struct tau_tracker* tracker)
if (cancel_all || req->created_at + TauRequestTtl < now)
{
dbgmsg(tracker->key, "timeout scrape req %p", (void*)req);
- tau_scrape_request_fail(req, false, true, nullptr);
+ tau_scrape_request_fail(req, false, true, "");
tau_scrape_request_free(req);
tr_ptrArrayRemove(reqs, i);
--i;
diff --git a/libtransmission/error.cc b/libtransmission/error.cc
index a00817666..dc1bcba47 100644
--- a/libtransmission/error.cc
+++ b/libtransmission/error.cc
@@ -109,53 +109,17 @@ void tr_error_clear(tr_error** error)
*error = nullptr;
}
-static void error_prefix_valist(tr_error** error, char const* prefix_format, va_list args) TR_GNUC_PRINTF(2, 0);
-
-static void error_prefix_valist(tr_error** error, char const* prefix_format, va_list args)
+void tr_error_prefix(tr_error** error, char const* prefix)
{
- TR_ASSERT(error != nullptr);
- TR_ASSERT(*error != nullptr);
- TR_ASSERT(prefix_format != nullptr);
-
- char* prefix = tr_strdup_vprintf(prefix_format, args);
-
- char* new_message = tr_strdup_printf("%s%s", prefix, (*error)->message);
- tr_free((*error)->message);
- (*error)->message = new_message;
-
- tr_free(prefix);
-}
-
-void tr_error_prefix(tr_error** error, char const* prefix_format, ...)
-{
- TR_ASSERT(prefix_format != nullptr);
+ TR_ASSERT(prefix != nullptr);
if (error == nullptr || *error == nullptr)
{
return;
}
- va_list args;
-
- va_start(args, prefix_format);
- error_prefix_valist(error, prefix_format, args);
- va_end(args);
-}
-
-void tr_error_propagate_prefixed(tr_error** new_error, tr_error** old_error, char const* prefix_format, ...)
-{
- TR_ASSERT(prefix_format != nullptr);
-
- tr_error_propagate(new_error, old_error);
-
- if (new_error == nullptr)
- {
- return;
- }
-
- va_list args;
-
- va_start(args, prefix_format);
- error_prefix_valist(new_error, prefix_format, args);
- va_end(args);
+ auto* err = *error;
+ auto* const new_message = tr_strvDup(tr_strvJoin(prefix, err->message));
+ tr_free(err->message);
+ err->message = new_message;
}
diff --git a/libtransmission/error.h b/libtransmission/error.h
index 05ed86d73..93ecbe2e6 100644
--- a/libtransmission/error.h
+++ b/libtransmission/error.h
@@ -96,21 +96,6 @@ void tr_error_clear(tr_error** error);
* @param[in] prefix_format Prefix format string.
* @param[in] ... Format arguments.
*/
-void tr_error_prefix(tr_error** error, char const* prefix_format, ...) TR_GNUC_PRINTF(2, 3);
-
-/**
- * @brief Prefix message and propagate existing error object upwards.
- *
- * If passed pointer to new error object is not `nullptr`, copy old error object
- * to new error object, prefix its message with `printf`-style formatted text,
- * and free old error object. Otherwise, just free old error object.
- *
- * @param[in,out] new_error Pointer to error object to be set.
- * @param[in,out] old_error Error object to be propagated. Cleared on return.
- * @param[in] prefix_format Prefix format string.
- * @param[in] ... Format arguments.
- */
-void tr_error_propagate_prefixed(tr_error** new_error, tr_error** old_error, char const* prefix_format, ...)
- TR_GNUC_PRINTF(3, 4);
+void tr_error_prefix(tr_error** error, char const* prefix);
/** @} */
diff --git a/libtransmission/magnet-metainfo.cc b/libtransmission/magnet-metainfo.cc
index 40203ffe3..36102aa0f 100644
--- a/libtransmission/magnet-metainfo.cc
+++ b/libtransmission/magnet-metainfo.cc
@@ -144,7 +144,7 @@ bool tr_magnet_metainfo::parseMagnet(std::string_view magnet_link, tr_error** er
auto const parsed = tr_urlParse(magnet_link);
if (!parsed || parsed->scheme != "magnet"sv)
{
- tr_error_set(error, TR_ERROR_EINVAL, "Error parsing URL");
+ tr_error_set_literal(error, TR_ERROR_EINVAL, "Error parsing URL");
return false;
}
diff --git a/libtransmission/makemeta.cc b/libtransmission/makemeta.cc
index 119c0a388..5c2b6c150 100644
--- a/libtransmission/makemeta.cc
+++ b/libtransmission/makemeta.cc
@@ -30,6 +30,8 @@
#include "version.h"
#include "web-utils.h"
+using namespace std::literals;
+
/****
*****
****/
@@ -590,7 +592,7 @@ void tr_makeMetaInfo(
}
else
{
- builder->outputFile = tr_strdup_printf("%s.torrent", builder->top);
+ builder->outputFile = tr_strvDup(tr_strvJoin(builder->top, ".torrent"sv));
}
/* enqueue the builder */
diff --git a/libtransmission/metainfo.cc b/libtransmission/metainfo.cc
index 2d04f57dd..9c412c8bf 100644
--- a/libtransmission/metainfo.cc
+++ b/libtransmission/metainfo.cc
@@ -300,7 +300,7 @@ static char* fix_webseed_url(tr_info const* inf, std::string_view url)
if (inf->fileCount > 1 && !std::empty(url) && url.back() != '/')
{
- return tr_strdup_printf("%" TR_PRIsv "/", TR_PRIsv_ARG(url));
+ return tr_strvDup(tr_strvJoin(url, "/"sv));
}
return tr_strvDup(url);
diff --git a/libtransmission/rpc-server.cc b/libtransmission/rpc-server.cc
index 09809d9b6..46882e695 100644
--- a/libtransmission/rpc-server.cc
+++ b/libtransmission/rpc-server.cc
@@ -364,9 +364,8 @@ static void serve_file(struct evhttp_request* req, tr_rpc_server* server, char c
if (file == nullptr)
{
- char* tmp = tr_strdup_printf("%s (%s)", filename, error->message);
- send_simple_response(req, HTTP_NOTFOUND, tmp);
- tr_free(tmp);
+ auto const tmp = tr_strvJoin(filename, " ("sv, error->message, ")"sv);
+ send_simple_response(req, HTTP_NOTFOUND, tmp.c_str());
tr_error_free(error);
}
else
@@ -423,13 +422,11 @@ static void handle_web_client(struct evhttp_request* req, tr_rpc_server* server)
}
else
{
- char* filename = tr_strdup_printf(
- "%s%s%s",
+ auto const filename = tr_strvJoin(
webClientDir,
TR_PATH_DELIMITER_STR,
tr_str_is_empty(subpath) ? "index.html" : subpath);
- serve_file(req, server, filename);
- tr_free(filename);
+ serve_file(req, server, filename.c_str());
}
tr_free(subpath);
@@ -493,7 +490,7 @@ static void handle_rpc(struct evhttp_request* req, tr_rpc_server* server)
struct rpc_response_data* data = tr_new0(struct rpc_response_data, 1);
data->req = req;
data->server = server;
- tr_rpc_request_exec_uri(server->session, q + 1, TR_BAD_SIZE, rpc_response_func, data);
+ tr_rpc_request_exec_uri(server->session, q + 1, rpc_response_func, data);
return;
}
}
@@ -644,11 +641,11 @@ static void handle_request(struct evhttp_request* req, void* arg)
++server->loginattempts;
}
- char* unauthuser = tr_strdup_printf(
- "Unauthorized User. %d unsuccessful login attempts.
",
- server->loginattempts);
- send_simple_response(req, 401, unauthuser);
- tr_free(unauthuser);
+ auto const unauthuser = tr_strvJoin(
+ "Unauthorized User. "sv,
+ std::to_string(server->loginattempts),
+ " unsuccessful login attempts.
"sv);
+ send_simple_response(req, 401, unauthuser.c_str());
return;
}
@@ -673,7 +670,7 @@ static void handle_request(struct evhttp_request* req, void* arg)
}
else if (!isHostnameAllowed(server, req))
{
- char* const tmp = tr_strdup_printf(
+ char const* const tmp =
"Transmission received your request, but the hostname was unrecognized.
"
"To fix this, choose one of the following options:"
"
"
@@ -683,15 +680,14 @@ static void handle_request(struct evhttp_request* req, void* arg)
"If you're editing settings.json, see the 'rpc-host-whitelist' and 'rpc-host-whitelist-enabled' entries.
"
"This requirement has been added to help prevent "
"DNS Rebinding "
- "attacks.
");
+ "attacks.
";
send_simple_response(req, 421, tmp);
- tr_free(tmp);
}
#ifdef REQUIRE_SESSION_ID
else if (!test_session_id(server, req))
{
char const* sessionId = get_current_session_id(server);
- char* tmp = tr_strdup_printf(
+ auto const tmp = tr_strvJoin(
"Your request had an invalid session-id header.
"
"To fix this, follow these steps:"
"
- When reading a response, get its X-Transmission-Session-Id header and remember it"
@@ -701,13 +697,13 @@ static void handle_request(struct evhttp_request* req, void* arg)
"
This requirement has been added to help prevent "
"CSRF "
"attacks.
"
- "%s: %s
",
- TR_RPC_SESSION_ID_HEADER,
- sessionId);
+ "" TR_RPC_SESSION_ID_HEADER,
+ ": "sv,
+ sessionId,
+ "
");
evhttp_add_header(req->output_headers, TR_RPC_SESSION_ID_HEADER, sessionId);
evhttp_add_header(req->output_headers, "Access-Control-Expose-Headers", TR_RPC_SESSION_ID_HEADER);
- send_simple_response(req, 409, tmp);
- tr_free(tmp);
+ send_simple_response(req, 409, tmp.c_str());
}
#endif
else if (tr_strvStartsWith(location, "rpc"sv))
diff --git a/libtransmission/rpcimpl.cc b/libtransmission/rpcimpl.cc
index 90a48353c..4804888c6 100644
--- a/libtransmission/rpcimpl.cc
+++ b/libtransmission/rpcimpl.cc
@@ -1376,9 +1376,8 @@ static char const* portTest(
struct tr_rpc_idle_data* idle_data)
{
int const port = tr_sessionGetPeerPort(session);
- char* url = tr_strdup_printf("https://portcheck.transmissionbt.com/%d", port);
+ auto const url = tr_strvJoin("https://portcheck.transmissionbt.com/"sv, std::to_string(port));
tr_webRun(session, url, portTested, idle_data);
- tr_free(url);
return nullptr;
}
@@ -2554,44 +2553,27 @@ void tr_rpc_parse_list_str(tr_variant* setme, std::string_view str)
void tr_rpc_request_exec_uri(
tr_session* session,
- void const* request_uri,
- size_t request_uri_len,
+ std::string_view request_uri,
tr_rpc_response_func callback,
void* callback_user_data)
{
- char* const request = tr_strndup(request_uri, request_uri_len);
-
auto top = tr_variant{};
tr_variantInitDict(&top, 3);
tr_variant* const args = tr_variantDictAddDict(&top, TR_KEY_arguments, 0);
- char const* pch = strchr(request, '?');
- if (pch == nullptr)
+ auto const parsed = tr_urlParse(request_uri);
+ if (parsed)
{
- pch = request;
- }
-
- while (pch != nullptr)
- {
- char const* delim = strchr(pch, '=');
- char const* next = strchr(pch, '&');
-
- if (delim != nullptr)
+ for (auto const& [key, val] : tr_url_query_view(parsed->query))
{
- auto const key = std::string_view{ pch, size_t(delim - pch) };
- bool isArg = key != "method" && key != "tag";
- tr_variant* parent = isArg ? args : ⊤
-
- auto const val = std::string_view{ delim + 1, next != nullptr ? (size_t)(next - (delim + 1)) : strlen(delim + 1) };
+ auto is_arg = key != "method"sv && key != "tag"sv;
+ auto* const parent = is_arg ? args : ⊤
tr_rpc_parse_list_str(tr_variantDictAdd(parent, tr_quark_new(key)), val);
}
-
- pch = next != nullptr ? next + 1 : nullptr;
}
tr_rpc_request_exec_json(session, &top, callback, callback_user_data);
- /* cleanup */
+ // cleanup
tr_variantFree(&top);
- tr_free(request);
}
diff --git a/libtransmission/rpcimpl.h b/libtransmission/rpcimpl.h
index 9817e34e2..33e6416f6 100644
--- a/libtransmission/rpcimpl.h
+++ b/libtransmission/rpcimpl.h
@@ -31,8 +31,7 @@ void tr_rpc_request_exec_json(
/* see the RPC spec's "Request URI Notation" section */
void tr_rpc_request_exec_uri(
tr_session* session,
- void const* request_uri,
- size_t request_uri_len,
+ std::string_view request_uri,
tr_rpc_response_func callback,
void* callback_user_data);
diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc
index 0aa651340..6ce9f48dc 100644
--- a/libtransmission/torrent.cc
+++ b/libtransmission/torrent.cc
@@ -2889,7 +2889,7 @@ static void refreshCurrentDir(tr_torrent* tor)
char* tr_torrentBuildPartial(tr_torrent const* tor, tr_file_index_t i)
{
- return tr_strdup_printf("%s.part", tor->file(i).name);
+ return tr_strvDup(tr_strvJoin(tor->file(i).name, ".part"sv));
}
/***
diff --git a/libtransmission/tr-macros.h b/libtransmission/tr-macros.h
index 4f291e35d..1c29e4237 100644
--- a/libtransmission/tr-macros.h
+++ b/libtransmission/tr-macros.h
@@ -132,8 +132,6 @@
#define TR_ADDRSTRLEN 64
-#define TR_BAD_SIZE ((size_t)-1)
-
// Mostly to enforce better formatting
#define TR_ARG_TUPLE(...) __VA_ARGS__
diff --git a/libtransmission/utils.cc b/libtransmission/utils.cc
index 8e0824a59..665651073 100644
--- a/libtransmission/utils.cc
+++ b/libtransmission/utils.cc
@@ -508,12 +508,12 @@ char* tr_strvDup(std::string_view in)
char* tr_strndup(void const* vin, size_t len)
{
auto const* const in = static_cast(vin);
- return in == nullptr ? nullptr : tr_strvDup({ in, len == TR_BAD_SIZE ? strlen(in) : len });
+ return in == nullptr ? nullptr : tr_strvDup({ in, len });
}
char* tr_strdup(void const* in)
{
- return tr_strndup(in, TR_BAD_SIZE);
+ return in == nullptr ? nullptr : tr_strvDup(static_cast(in));
}
char const* tr_memmem(char const* haystack, size_t haystacklen, char const* needle, size_t needlelen)
diff --git a/tests/libtransmission/error-test.cc b/tests/libtransmission/error-test.cc
index 01fe7155e..5066966ca 100644
--- a/tests/libtransmission/error-test.cc
+++ b/tests/libtransmission/error-test.cc
@@ -42,25 +42,18 @@ TEST(Error, propagate)
{
tr_error* err = nullptr;
tr_error* err2 = nullptr;
+ auto constexpr Code = int{ 1 };
- tr_error_set_literal(&err, 1, "oops");
+ tr_error_set_literal(&err, Code, "oops");
EXPECT_NE(nullptr, err);
- EXPECT_EQ(1, err->code);
+ EXPECT_EQ(Code, err->code);
EXPECT_STREQ("oops", err->message);
tr_error_propagate(&err2, &err);
EXPECT_NE(nullptr, err2);
- EXPECT_EQ(1, err2->code);
+ EXPECT_EQ(Code, err2->code);
EXPECT_STREQ("oops", err2->message);
EXPECT_EQ(nullptr, err);
- tr_error_propagate_prefixed(&err, &err2, "error: ");
- EXPECT_NE(nullptr, err);
- EXPECT_EQ(1, err->code);
- EXPECT_STREQ("error: oops", err->message);
- EXPECT_EQ(nullptr, err2);
-
- tr_error_propagate(nullptr, &err);
- EXPECT_EQ(nullptr, err);
- EXPECT_EQ(nullptr, err2);
+ tr_error_clear(&err2);
}
diff --git a/tests/libtransmission/file-test.cc b/tests/libtransmission/file-test.cc
index 59785bb3a..088559cce 100644
--- a/tests/libtransmission/file-test.cc
+++ b/tests/libtransmission/file-test.cc
@@ -124,7 +124,7 @@ protected:
slash_pos = p + strlen(p) - 1;
}
- auto const path_part = makeString(tr_strndup(path, size_t(slash_pos - path + 1)));
+ auto const path_part = std::string{ path, size_t(slash_pos - path + 1) };
if (!tr_sys_path_get_info(path_part.c_str(), TR_SYS_PATH_NO_FOLLOW, &info, nullptr) ||
(info.type != TR_SYS_PATH_IS_FILE && info.type != TR_SYS_PATH_IS_DIRECTORY))
diff --git a/tests/libtransmission/move-test.cc b/tests/libtransmission/move-test.cc
index e8d42c25b..8a511d622 100644
--- a/tests/libtransmission/move-test.cc
+++ b/tests/libtransmission/move-test.cc
@@ -52,9 +52,7 @@ TEST_P(IncompleteDirTest, incompleteDir)
// the test zero_torrent will be missing its first piece.
auto* tor = zeroTorrentInit();
zeroTorrentPopulate(tor, false);
- EXPECT_EQ(
- makeString(tr_strdup_printf("%s/%s.part", incomplete_dir, tr_torrentFile(tor, 0).name)),
- makeString(tr_torrentFindFile(tor, 0)));
+ EXPECT_EQ(tr_strvJoin(incomplete_dir, "/", tr_torrentFile(tor, 0).name, ".part"), makeString(tr_torrentFindFile(tor, 0)));
EXPECT_EQ(tr_strvPath(incomplete_dir, tr_torrentFile(tor, 1).name), makeString(tr_torrentFindFile(tor, 1)));
EXPECT_EQ(tor->pieceSize(), tr_torrentStat(tor)->leftUntilDone);
diff --git a/tests/libtransmission/test-fixtures.h b/tests/libtransmission/test-fixtures.h
index 0733e4156..5499810b5 100644
--- a/tests/libtransmission/test-fixtures.h
+++ b/tests/libtransmission/test-fixtures.h
@@ -399,9 +399,8 @@ protected:
{
auto const& file = tor->info.files[i];
- auto path = (!complete && i == 0) ?
- makeString(tr_strdup_printf("%s%c%s.part", tor->currentDir, TR_PATH_DELIMITER, file.name)) :
- makeString(tr_strdup_printf("%s%c%s", tor->currentDir, TR_PATH_DELIMITER, file.name));
+ auto path = (!complete && i == 0) ? tr_strvJoin(tor->currentDir, TR_PATH_DELIMITER_STR, file.name, ".part") :
+ tr_strvJoin(tor->currentDir, TR_PATH_DELIMITER_STR, file.name);
auto const dirname = makeString(tr_sys_path_dirname(path.c_str(), nullptr));
tr_sys_dir_create(dirname.data(), TR_SYS_DIR_CREATE_PARENTS, 0700, nullptr);
diff --git a/utils/create.cc b/utils/create.cc
index 2718ed378..b64371ef2 100644
--- a/utils/create.cc
+++ b/utils/create.cc
@@ -20,6 +20,8 @@
#include "units.h"
+using namespace std::literals;
+
#define MY_NAME "transmission-create"
#define MAX_TRACKERS 128
@@ -172,11 +174,10 @@ int tr_main(int argc, char* argv[])
return EXIT_FAILURE;
}
- char* end = tr_strdup_printf("%s.torrent", base);
+ auto const end = tr_strvJoin(base, ".torrent"sv);
char* cwd = tr_getcwd();
- outfile = out2 = tr_buildPath(cwd, end, nullptr);
+ outfile = out2 = tr_buildPath(cwd, end.c_str(), nullptr);
tr_free(cwd);
- tr_free(end);
tr_free(base);
}
diff --git a/utils/remote.cc b/utils/remote.cc
index d251ee21e..314a9b4fe 100644
--- a/utils/remote.cc
+++ b/utils/remote.cc
@@ -31,9 +31,12 @@
#include
#include
+using namespace std::literals;
+
+static auto constexpr DefaultHost = "localhost"sv;
+static auto constexpr DefaultPort = int{ TR_DEFAULT_RPC_PORT };
+
#define MY_NAME "transmission-remote"
-#define DEFAULT_HOST "localhost"
-#define DEFAULT_PORT TR_DEFAULT_RPC_PORT
#define DEFAULT_URL TR_DEFAULT_RPC_URL_STR "rpc/"
#define ARGUMENTS TR_KEY_arguments
@@ -56,8 +59,6 @@
#define SPEED_G_STR "GB/s"
#define SPEED_T_STR "TB/s"
-using namespace std::literals;
-
/***
****
**** Display Utilities
@@ -557,7 +558,7 @@ static int getOptMode(int val)
static bool debug = false;
static char* auth = nullptr;
static char* netrc = nullptr;
-static char* sessionId = nullptr;
+static std::string session_id;
static bool UseSSL = false;
static char* getEncodedMetainfo(char const* filename)
@@ -811,8 +812,7 @@ static size_t parseResponseHeader(void* ptr, size_t size, size_t nmemb, void* /*
++end;
}
- tr_free(sessionId);
- sessionId = tr_strndup(begin, end - begin);
+ session_id.assign(begin, end);
}
return line_len;
@@ -2168,11 +2168,10 @@ static CURL* tr_curl_easy_init(struct evbuffer* writebuf)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); /* since most certs will be self-signed, do not verify against CA */
}
- if (sessionId != nullptr)
+ if (!std::empty(session_id))
{
- char* h = tr_strdup_printf("%s: %s", TR_RPC_SESSION_ID_HEADER, sessionId);
- struct curl_slist* custom_headers = curl_slist_append(nullptr, h);
- tr_free(h);
+ auto const h = tr_strvJoin(TR_RPC_SESSION_ID_HEADER, ": "sv, session_id);
+ auto* const custom_headers = curl_slist_append(nullptr, h.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, custom_headers);
curl_easy_setopt(curl, CURLOPT_PRIVATE, custom_headers);
@@ -2201,10 +2200,10 @@ static int flush(char const* rpcurl, tr_variant** benc)
int status = EXIT_SUCCESS;
struct evbuffer* buf = evbuffer_new();
char* json = tr_variantToStr(*benc, TR_VARIANT_FMT_JSON_LEAN, nullptr);
- char* rpcurl_http = tr_strdup_printf(UseSSL ? "https://%s" : "http://%s", rpcurl);
+ auto const rpcurl_http = tr_strvJoin(UseSSL ? "https://" : "http://", rpcurl);
curl = tr_curl_easy_init(buf);
- curl_easy_setopt(curl, CURLOPT_URL, rpcurl_http);
+ curl_easy_setopt(curl, CURLOPT_URL, rpcurl_http.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, getTimeoutSecs(json));
@@ -2215,7 +2214,7 @@ static int flush(char const* rpcurl, tr_variant** benc)
if ((res = curl_easy_perform(curl)) != CURLE_OK)
{
- tr_logAddNamedError(MY_NAME, " (%s) %s", rpcurl_http, curl_easy_strerror(res));
+ tr_logAddNamedError(MY_NAME, " (%s) %s", rpcurl_http.c_str(), curl_easy_strerror(res));
status |= EXIT_FAILURE;
}
else
@@ -2250,7 +2249,6 @@ static int flush(char const* rpcurl, tr_variant** benc)
}
/* cleanup */
- tr_free(rpcurl_http);
tr_free(json);
evbuffer_free(buf);
@@ -3080,7 +3078,7 @@ static bool parsePortString(char const* s, int* port)
}
/* [host:port] or [host] or [port] or [http(s?)://host:port/transmission/] */
-static void getHostAndPortAndRpcUrl(int* argc, char** argv, char** host, int* port, char** rpcurl)
+static void getHostAndPortAndRpcUrl(int* argc, char** argv, std::string* host, int* port, std::string* rpcurl)
{
if (*argv[1] == '-')
{
@@ -3092,12 +3090,12 @@ static void getHostAndPortAndRpcUrl(int* argc, char** argv, char** host, int* po
if (strncmp(s, "http://", 7) == 0) /* user passed in http rpc url */
{
- *rpcurl = tr_strdup_printf("%s/rpc/", s + 7);
+ *rpcurl = tr_strvJoin(s + 7, "/rpc/"sv);
}
else if (strncmp(s, "https://", 8) == 0) /* user passed in https rpc url */
{
UseSSL = true;
- *rpcurl = tr_strdup_printf("%s/rpc/", s + 8);
+ *rpcurl = tr_strvJoin(s + 8, "/rpc/"sv);
}
else if (parsePortString(s, port))
{
@@ -3106,7 +3104,7 @@ static void getHostAndPortAndRpcUrl(int* argc, char** argv, char** host, int* po
else if (last_colon == nullptr)
{
// it was a non-ipv6 host with no port
- *host = tr_strdup(s);
+ *host = s;
}
else
{
@@ -3124,7 +3122,8 @@ static void getHostAndPortAndRpcUrl(int* argc, char** argv, char** host, int* po
bool const is_unbracketed_ipv6 = (*s != '[') && (memchr(s, ':', hend - s) != nullptr);
- *host = is_unbracketed_ipv6 ? tr_strdup_printf("[%*s]", TR_ARG_TUPLE((int)(hend - s), s)) : tr_strndup(s, hend - s);
+ auto const sv = std::string_view{ s, size_t(hend - s) };
+ *host = is_unbracketed_ipv6 ? tr_strvJoin("[", sv, "]") : sv;
}
*argc -= 1;
@@ -3137,10 +3136,9 @@ static void getHostAndPortAndRpcUrl(int* argc, char** argv, char** host, int* po
int tr_main(int argc, char* argv[])
{
- int port = DEFAULT_PORT;
- char* host = nullptr;
- char* rpcurl = nullptr;
- int exit_status = EXIT_SUCCESS;
+ auto port = DefaultPort;
+ auto host = std::string{};
+ auto rpcurl = std::string{};
if (argc < 2)
{
@@ -3154,19 +3152,15 @@ int tr_main(int argc, char* argv[])
getHostAndPortAndRpcUrl(&argc, argv, &host, &port, &rpcurl);
- if (host == nullptr)
+ if (std::empty(host))
{
- host = tr_strdup(DEFAULT_HOST);
+ host = DefaultHost;
}
- if (rpcurl == nullptr)
+ if (std::empty(rpcurl))
{
- rpcurl = tr_strdup_printf("%s:%d%s", host, port, DEFAULT_URL);
+ rpcurl = tr_strvJoin(host, ":", std::to_string(port), DEFAULT_URL);
}
- exit_status = processArgs(rpcurl, argc, (char const* const*)argv);
-
- tr_free(host);
- tr_free(rpcurl);
- return exit_status;
+ return processArgs(rpcurl.c_str(), argc, (char const* const*)argv);
}