refactor: tr_web's callback now uses std::string_view (#2172)

* refactor: tr_web tasks now use a std::string_view argument
This commit is contained in:
Charles Kerr 2021-11-15 12:10:18 -06:00 committed by GitHub
parent 54cd1a2612
commit cef3f43088
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 37 deletions

View File

@ -142,12 +142,11 @@ static void onTorrentFileDownloaded(
bool /*did_connect*/,
bool /*did_timeout*/,
long /*response_code*/,
void const* response,
size_t response_byte_count,
std::string_view response,
void* vctor)
{
auto* ctor = static_cast<tr_ctor*>(vctor);
tr_ctorSetMetainfo(ctor, response, response_byte_count);
tr_ctorSetMetainfo(ctor, std::data(response), std::size(response));
waitingOnWeb = false;
}

View File

@ -73,7 +73,7 @@ Glib::RefPtr<Gdk::Pixbuf> favicon_load_from_cache(std::string const& host)
}
}
void favicon_web_done_cb(tr_session*, bool, bool, long, void const*, size_t, void*);
void favicon_web_done_cb(tr_session*, bool, bool, long, std::string_view, void*);
bool favicon_web_done_idle_cb(favicon_data* fav)
{
@ -114,12 +114,11 @@ void favicon_web_done_cb(
bool /*did_connect*/,
bool /*did_timeout*/,
long /*code*/,
void const* data,
size_t len,
std::string_view data,
void* vfav)
{
auto* fav = static_cast<favicon_data*>(vfav);
fav->contents.assign(static_cast<char const*>(data), len);
fav->contents.assign(std::data(data), std::size(data));
Glib::signal_idle().connect([fav]() { return favicon_web_done_idle_cb(fav); });
}

View File

@ -199,8 +199,7 @@ static void on_announce_done(
bool did_connect,
bool did_timeout,
long response_code,
void const* msg,
size_t msglen,
std::string_view msg,
void* vdata)
{
auto* data = static_cast<struct announce_data*>(vdata);
@ -219,7 +218,7 @@ static void on_announce_done(
else
{
tr_variant benc;
bool const variant_loaded = tr_variantFromBenc(&benc, { static_cast<char const*>(msg), msglen }) == 0;
bool const variant_loaded = tr_variantFromBenc(&benc, msg) == 0;
if (tr_env_key_exists("TR_CURL_VERBOSE"))
{
@ -368,8 +367,7 @@ static void on_scrape_done(
bool did_connect,
bool did_timeout,
long response_code,
void const* msg,
size_t msglen,
std::string_view msg,
void* vdata)
{
auto* data = static_cast<struct scrape_data*>(vdata);
@ -392,7 +390,7 @@ static void on_scrape_done(
else
{
auto top = tr_variant{};
auto const variant_loaded = tr_variantFromBenc(&top, { static_cast<char const*>(msg), msglen }) == 0;
auto const variant_loaded = tr_variantFromBenc(&top, msg) == 0;
if (tr_env_key_exists("TR_CURL_VERBOSE"))
{

View File

@ -1449,8 +1449,7 @@ static void portTested(
bool /*did_connect*/,
bool /*did_timeout*/,
long response_code,
void const* response,
size_t response_byte_count,
std::string_view response,
void* user_data)
{
char result[1024];
@ -1467,7 +1466,7 @@ static void portTested(
}
else /* success */
{
bool const isOpen = response_byte_count != 0 && *(char const*)response == '1';
bool const isOpen = tr_strvStartsWith(response, '1');
tr_variantDictAddBool(data->args_out, TR_KEY_port_is_open, isOpen);
tr_snprintf(result, sizeof(result), "success");
}
@ -1497,8 +1496,7 @@ static void gotNewBlocklist(
bool /*did_connect*/,
bool /*did_timeout*/,
long response_code,
void const* response,
size_t response_byte_count,
std::string_view response,
void* user_data)
{
char result[1024];
@ -1530,8 +1528,8 @@ static void gotNewBlocklist(
stream.zalloc = (alloc_func)Z_NULL;
stream.zfree = (free_func)Z_NULL;
stream.opaque = (voidpf)Z_NULL;
stream.next_in = static_cast<Bytef const*>(response);
stream.avail_in = response_byte_count;
stream.next_in = reinterpret_cast<Bytef const*>(std::data(response));
stream.avail_in = std::size(response);
inflateInit2(&stream, windowBits);
auto filename = tr_strvPath(configDir, "blocklist.tmp.XXXXXX");
@ -1571,7 +1569,7 @@ static void gotNewBlocklist(
inflateEnd(&stream);
if ((err == Z_DATA_ERROR) && // couldn't inflate it... it's probably already uncompressed
!tr_sys_file_write(fd, response, response_byte_count, nullptr, &error))
!tr_sys_file_write(fd, std::data(response), std::size(response), nullptr, &error))
{
tr_snprintf(result, sizeof(result), _("Couldn't save file \"%1$s\": %2$s"), filename.c_str(), error->message);
tr_error_clear(&error);
@ -1665,8 +1663,7 @@ static void gotMetadataFromURL(
bool /*did_connect*/,
bool /*did_timeout*/,
long response_code,
void const* response,
size_t response_byte_count,
std::string_view response,
void* user_data)
{
auto* data = static_cast<struct add_torrent_idle_data*>(user_data);
@ -1675,11 +1672,11 @@ static void gotMetadataFromURL(
"torrentAdd: HTTP response code was %ld (%s); response length was %zu bytes",
response_code,
tr_webGetResponseStr(response_code),
response_byte_count);
std::size(response));
if (response_code == 200 || response_code == 221) /* http or ftp success.. */
{
tr_ctorSetMetainfo(data->ctor, response, response_byte_count);
tr_ctorSetMetainfo(data->ctor, std::data(response), std::size(response));
addTorrentImpl(data->data, data->ctor);
}
else

View File

@ -314,14 +314,9 @@ static void task_finish_func(void* vtask)
if (task->done_func != nullptr)
{
(*task->done_func)(
task->session,
task->did_connect,
task->did_timeout,
task->code,
evbuffer_pullup(task->response, -1),
evbuffer_get_length(task->response),
task->done_func_user_data);
auto const sv = std::string_view{ reinterpret_cast<char const*>(evbuffer_pullup(task->response, -1)),
evbuffer_get_length(task->response) };
(*task->done_func)(task->session, task->did_connect, task->did_timeout, task->code, sv, task->done_func_user_data);
}
task_free(task);

View File

@ -30,8 +30,7 @@ using tr_web_done_func = void (*)(
bool did_connect_flag,
bool timeout_flag,
long response_code,
void const* response,
size_t response_byte_count,
std::string_view response,
void* 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);

View File

@ -406,8 +406,7 @@ static void web_response_func(
bool /*did_connect*/,
bool /*did_timeout*/,
long response_code,
void const* /*response*/,
size_t /*response_byte_count*/,
std::string_view /*response*/,
void* vtask)
{
auto* t = static_cast<struct tr_webseed_task*>(vtask);