refactor: tr_webRun() now takes std::string_view args (#2162)

This commit is contained in:
Charles Kerr 2021-11-14 19:17:03 -06:00 committed by GitHub
parent f46c4c4df9
commit 8980a33f4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 56 deletions

View File

@ -1755,8 +1755,8 @@ static char const* torrentAdd(tr_session* session, tr_variant* args_in, tr_varia
/* set the optional arguments */
char const* cookies = nullptr;
(void)tr_variantDictFindStr(args_in, TR_KEY_cookies, &cookies, nullptr);
auto cookies = std::string_view{};
(void)tr_variantDictFindStrView(args_in, TR_KEY_cookies, &cookies);
if (download_dir != nullptr)
{

View File

@ -34,17 +34,15 @@
#include "version.h" /* User-Agent */
#include "web.h"
using namespace std::literals;
#if LIBCURL_VERSION_NUM >= 0x070F06 /* CURLOPT_SOCKOPT* was added in 7.15.6 */
#define USE_LIBCURL_SOCKOPT
#endif
static auto constexpr ThreadfuncMaxSleepMsec = int{ 200 };
#if 0
#define dbgmsg(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__)
#else
#define dbgmsg(...) tr_logAddDeepNamed("web", __VA_ARGS__)
#endif
/***
****
@ -52,21 +50,25 @@ static auto constexpr ThreadfuncMaxSleepMsec = int{ 200 };
struct tr_web_task
{
int torrentId;
long code;
long timeout_secs;
bool did_connect;
bool did_timeout;
struct evbuffer* response;
struct evbuffer* freebuf;
char* url;
char* range;
char* cookies;
tr_session* session;
tr_web_done_func done_func;
void* done_func_user_data;
CURL* curl_easy;
struct tr_web_task* next;
std::string cookies;
std::string range;
std::string url;
CURL* curl_easy = nullptr;
evbuffer* freebuf = nullptr;
evbuffer* response = nullptr;
tr_session* session = nullptr;
tr_web_done_func done_func = nullptr;
tr_web_task* next = nullptr;
void* done_func_user_data = nullptr;
long code = 0;
long timeout_secs = 0;
int torrentId = 0;
bool did_connect = false;
bool did_timeout = false;
};
static void task_free(struct tr_web_task* task)
@ -76,10 +78,7 @@ static void task_free(struct tr_web_task* task)
evbuffer_free(task->freebuf);
}
tr_free(task->cookies);
tr_free(task->range);
tr_free(task->url);
tr_free(task);
delete task;
}
/***
@ -129,8 +128,8 @@ static size_t writeFunc(void* ptr, size_t size, size_t nmemb, void* vtask)
static int sockoptfunction(void* vtask, curl_socket_t fd, curlsocktype /*purpose*/)
{
auto* task = static_cast<struct tr_web_task*>(vtask);
bool const isScrape = strstr(task->url, "scrape") != nullptr;
bool const isAnnounce = strstr(task->url, "announce") != nullptr;
auto const isScrape = tr_strvContains(task->url, "scrape"sv);
auto const isAnnounce = tr_strvContains(task->url, "announce"sv);
/* announce and scrape requests have tiny payloads. */
if (isScrape || isAnnounce)
@ -215,12 +214,12 @@ static long getTimeoutFromURL(struct tr_web_task const* task)
return 20L;
}
if (strstr(task->url, "scrape") != nullptr)
if (tr_strvContains(task->url, "scrape"sv))
{
return 30L;
}
if (strstr(task->url, "announce") != nullptr)
if (tr_strvContains(task->url, "announce"sv))
{
return 90L;
}
@ -265,7 +264,7 @@ static CURL* createEasy(tr_session* s, struct tr_web* web, struct tr_web_task* t
}
curl_easy_setopt(e, CURLOPT_TIMEOUT, task->timeout_secs);
curl_easy_setopt(e, CURLOPT_URL, task->url);
curl_easy_setopt(e, CURLOPT_URL, task->url.c_str());
curl_easy_setopt(e, CURLOPT_USERAGENT, TR_NAME "/" SHORT_VERSION_STRING);
curl_easy_setopt(e, CURLOPT_VERBOSE, (long)(web->curl_verbose ? 1 : 0));
curl_easy_setopt(e, CURLOPT_WRITEDATA, task);
@ -284,9 +283,9 @@ static CURL* createEasy(tr_session* s, struct tr_web* web, struct tr_web_task* t
curl_easy_setopt(e, CURLOPT_INTERFACE, tr_address_to_string(addr));
}
if (task->cookies != nullptr)
if (!std::empty(task->cookies))
{
curl_easy_setopt(e, CURLOPT_COOKIE, task->cookies);
curl_easy_setopt(e, CURLOPT_COOKIE, task->cookies.c_str());
}
if (web->cookie_filename != nullptr)
@ -294,9 +293,9 @@ static CURL* createEasy(tr_session* s, struct tr_web* web, struct tr_web_task* t
curl_easy_setopt(e, CURLOPT_COOKIEFILE, web->cookie_filename);
}
if (task->range != nullptr)
if (!std::empty(task->range))
{
curl_easy_setopt(e, CURLOPT_RANGE, task->range);
curl_easy_setopt(e, CURLOPT_RANGE, task->range.c_str());
/* don't bother asking the server to compress webseed fragments */
curl_easy_setopt(e, CURLOPT_ENCODING, "identity");
}
@ -337,9 +336,9 @@ static void tr_webThreadFunc(void* vsession);
static struct tr_web_task* tr_webRunImpl(
tr_session* session,
int torrentId,
char const* url,
char const* range,
char const* cookies,
std::string_view url,
std::string_view range,
std::string_view cookies,
tr_web_done_func done_func,
void* done_func_user_data,
struct evbuffer* buffer)
@ -358,12 +357,12 @@ static struct tr_web_task* tr_webRunImpl(
}
}
task = tr_new0(struct tr_web_task, 1);
task = new tr_web_task{};
task->session = session;
task->torrentId = torrentId;
task->url = tr_strdup(url);
task->range = tr_strdup(range);
task->cookies = tr_strdup(cookies);
task->url = url;
task->range = range;
task->cookies = cookies;
task->done_func = done_func;
task->done_func_user_data = done_func_user_data;
task->response = buffer != nullptr ? buffer : evbuffer_new();
@ -380,28 +379,28 @@ static struct tr_web_task* tr_webRunImpl(
struct tr_web_task* tr_webRunWithCookies(
tr_session* session,
char const* url,
char const* cookies,
std::string_view url,
std::string_view cookies,
tr_web_done_func done_func,
void* done_func_user_data)
{
return tr_webRunImpl(session, -1, url, nullptr, cookies, done_func, done_func_user_data, nullptr);
return tr_webRunImpl(session, -1, url, {}, cookies, done_func, done_func_user_data, nullptr);
}
struct tr_web_task* tr_webRun(tr_session* session, char const* url, tr_web_done_func done_func, void* done_func_user_data)
struct tr_web_task* tr_webRun(tr_session* session, std::string_view url, tr_web_done_func done_func, void* done_func_user_data)
{
return tr_webRunWithCookies(session, url, nullptr, done_func, done_func_user_data);
return tr_webRunWithCookies(session, url, {}, done_func, done_func_user_data);
}
struct tr_web_task* tr_webRunWebseed(
tr_torrent* tor,
char const* url,
char const* range,
std::string_view url,
std::string_view range,
tr_web_done_func done_func,
void* done_func_user_data,
struct evbuffer* buffer)
{
return tr_webRunImpl(tor->session, tr_torrentId(tor), url, range, nullptr, done_func, done_func_user_data, buffer);
return tr_webRunImpl(tor->session, tr_torrentId(tor), url, range, {}, done_func, done_func_user_data, buffer);
}
static void tr_webThreadFunc(void* vsession)
@ -465,7 +464,7 @@ static void tr_webThreadFunc(void* vsession)
web->tasks = task->next;
task->next = nullptr;
dbgmsg("adding task to curl: [%s]", task->url);
dbgmsg("adding task to curl: [%s]", task->url.c_str());
curl_multi_add_handle(multi, createEasy(session, web, task));
}
@ -559,7 +558,7 @@ static void tr_webThreadFunc(void* vsession)
{
struct tr_web_task* task = web->tasks;
web->tasks = task->next;
dbgmsg("Discarding task \"%s\"", task->url);
dbgmsg("Discarding task \"%s\"", task->url.c_str());
task_free(task);
}

View File

@ -9,6 +9,7 @@
#pragma once
#include <cstdint>
#include <string_view>
#include "transmission.h"
@ -33,19 +34,19 @@ using tr_web_done_func = void (*)(
size_t response_byte_count,
void* user_data);
struct tr_web_task* tr_webRun(tr_session* session, char const* url, tr_web_done_func done_func, void* done_func_user_data);
struct tr_web_task* tr_webRun(tr_session* session, std::string_view url, tr_web_done_func done_func, void* done_func_user_data);
struct tr_web_task* tr_webRunWithCookies(
tr_session* session,
char const* url,
char const* cookies,
std::string_view url,
std::string_view cookies,
tr_web_done_func done_func,
void* done_func_user_data);
struct tr_web_task* tr_webRunWebseed(
tr_torrent* tor,
char const* url,
char const* range,
std::string_view url,
std::string_view range,
tr_web_done_func done_func,
void* done_func_user_data,
struct evbuffer* buffer);