2008-04-25 18:35:48 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2008-04-24 01:42:53 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-04-24 01:42:53 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring> /* strlen(), strstr() */
|
2021-09-27 16:27:02 +00:00
|
|
|
#include <set>
|
2011-03-16 18:04:23 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2019-06-22 13:02:17 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <wincrypt.h>
|
2010-06-25 20:36:10 +00:00
|
|
|
#endif
|
2010-01-04 20:06:39 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
#include <curl/curl.h>
|
2010-12-20 02:07:51 +00:00
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
2008-04-24 01:42:53 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2019-06-22 13:02:17 +00:00
|
|
|
#include "crypto-utils.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2010-03-06 15:05:05 +00:00
|
|
|
#include "net.h" /* tr_address */
|
2013-04-13 20:25:28 +00:00
|
|
|
#include "torrent.h"
|
2010-03-06 15:05:05 +00:00
|
|
|
#include "platform.h" /* mutex */
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "session.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2020-08-18 11:09:30 +00:00
|
|
|
#include "tr-macros.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread() */
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "utils.h"
|
2010-03-06 15:05:05 +00:00
|
|
|
#include "version.h" /* User-Agent */
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "web.h"
|
|
|
|
|
2010-04-15 15:32:36 +00:00
|
|
|
#if LIBCURL_VERSION_NUM >= 0x070F06 /* CURLOPT_SOCKOPT* was added in 7.15.6 */
|
2017-04-19 12:04:45 +00:00
|
|
|
#define USE_LIBCURL_SOCKOPT
|
2010-04-15 15:32:36 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr ThreadfuncMaxSleepMsec = int{ 200 };
|
2008-10-17 20:57:54 +00:00
|
|
|
|
2008-10-27 18:00:03 +00:00
|
|
|
#if 0
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__)
|
2008-10-27 18:00:03 +00:00
|
|
|
#else
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(...) tr_logAddDeepNamed("web", __VA_ARGS__)
|
2008-10-27 18:00:03 +00:00
|
|
|
#endif
|
2008-04-25 02:57:33 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
struct tr_web_task
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
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;
|
2008-04-24 01:42:53 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void task_free(struct tr_web_task* task)
|
2009-12-14 12:54:30 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (task->freebuf != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_free(task->freebuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_free(task->cookies);
|
|
|
|
tr_free(task->range);
|
|
|
|
tr_free(task->url);
|
|
|
|
tr_free(task);
|
2009-12-14 12:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-06-19 18:18:48 +00:00
|
|
|
struct tr_web
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool curl_verbose;
|
|
|
|
bool curl_ssl_verify;
|
|
|
|
char* curl_ca_bundle;
|
|
|
|
int close_mode;
|
|
|
|
struct tr_web_task* tasks;
|
|
|
|
tr_lock* taskLock;
|
|
|
|
char* cookie_filename;
|
2021-09-27 16:27:02 +00:00
|
|
|
std::set<CURL*> paused_easy_handles;
|
2011-06-19 18:18:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static size_t writeFunc(void* ptr, size_t size, size_t nmemb, void* vtask)
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const byteCount = size * nmemb;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* task = static_cast<struct tr_web_task*>(vtask);
|
2013-04-13 20:56:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* webseed downloads should be speed limited */
|
|
|
|
if (task->torrentId != -1)
|
2013-04-13 20:56:24 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_torrent const* const tor = tr_torrentFindFromId(task->session, task->torrentId);
|
2013-04-13 20:56:24 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (tor != nullptr && tor->bandwidth->clamp(TR_DOWN, nmemb) == 0)
|
2013-04-13 20:56:24 +00:00
|
|
|
{
|
2021-09-27 16:27:02 +00:00
|
|
|
task->session->web->paused_easy_handles.insert(task->curl_easy);
|
2017-04-19 12:04:45 +00:00
|
|
|
return CURL_WRITEFUNC_PAUSE;
|
2013-04-13 20:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(task->response, ptr, byteCount);
|
|
|
|
dbgmsg("wrote %zu bytes to task %p's buffer", byteCount, (void*)task);
|
|
|
|
return byteCount;
|
2008-04-25 18:35:48 +00:00
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2010-04-15 15:32:36 +00:00
|
|
|
#ifdef USE_LIBCURL_SOCKOPT
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static int sockoptfunction(void* vtask, curl_socket_t fd, curlsocktype /*purpose*/)
|
2009-12-02 05:30:46 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* task = static_cast<struct tr_web_task*>(vtask);
|
2021-09-15 00:18:09 +00:00
|
|
|
bool const isScrape = strstr(task->url, "scrape") != nullptr;
|
|
|
|
bool const isAnnounce = strstr(task->url, "announce") != nullptr;
|
2009-12-02 05:30:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* announce and scrape requests have tiny payloads. */
|
|
|
|
if (isScrape || isAnnounce)
|
2009-12-02 05:30:46 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const sndbuf = isScrape ? 4096 : 1024;
|
|
|
|
int const rcvbuf = isScrape ? 4096 : 3072;
|
2020-09-12 13:17:18 +00:00
|
|
|
/* ignore the sockopt() return values -- these are suggestions
|
|
|
|
rather than hard requirements & it's OK for them to fail */
|
2021-09-12 17:41:49 +00:00
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char const*>(&sndbuf), sizeof(sndbuf));
|
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char const*>(&rcvbuf), sizeof(rcvbuf));
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
2009-12-28 23:25:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* return nonzero if this function encountered an error */
|
|
|
|
return 0;
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-04-15 15:32:36 +00:00
|
|
|
#endif
|
2009-12-02 05:30:46 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static CURLcode ssl_context_func(CURL* /*curl*/, void* ssl_ctx, void* /*user_data*/)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
tr_x509_store_t const cert_store = tr_ssl_get_x509_store(ssl_ctx);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cert_store == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
curl_version_info_data const* const curl_ver = curl_version_info(CURLVERSION_NOW);
|
|
|
|
if (curl_ver->age >= 0 && strncmp(curl_ver->ssl_version, "Schannel", 8) == 0)
|
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static LPCWSTR const sys_store_names[] = {
|
2019-06-22 13:02:17 +00:00
|
|
|
L"CA",
|
2021-08-15 09:41:48 +00:00
|
|
|
L"ROOT",
|
2019-06-22 13:02:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < TR_N_ELEMENTS(sys_store_names); ++i)
|
|
|
|
{
|
|
|
|
HCERTSTORE const sys_cert_store = CertOpenSystemStoreW(0, sys_store_names[i]);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (sys_cert_store == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
PCCERT_CONTEXT sys_cert = nullptr;
|
2019-06-22 13:02:17 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
sys_cert = CertFindCertificateInStore(sys_cert_store, X509_ASN_ENCODING, 0, CERT_FIND_ANY, nullptr, sys_cert);
|
|
|
|
if (sys_cert == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_x509_cert_t const cert = tr_x509_cert_new(sys_cert->pbCertEncoded, sys_cert->cbCertEncoded);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cert == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_x509_store_add(cert_store, cert);
|
|
|
|
tr_x509_cert_free(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
CertCloseStore(sys_cert_store, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static long getTimeoutFromURL(struct tr_web_task const* task)
|
2009-12-13 17:54:01 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
tr_session const* const session = task->session;
|
2010-08-07 02:17:29 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (session == nullptr || session->isClosed)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
return 20L;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
|
|
|
|
if (strstr(task->url, "scrape") != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
return 30L;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
|
|
|
|
if (strstr(task->url, "announce") != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
return 90L;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-08-07 02:17:29 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
return 240L;
|
2009-12-13 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static CURL* createEasy(tr_session* s, struct tr_web* web, struct tr_web_task* task)
|
2008-04-25 18:35:48 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
CURL* const e = curl_easy_init();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2019-03-17 05:00:15 +00:00
|
|
|
task->curl_easy = e;
|
2017-04-19 12:04:45 +00:00
|
|
|
task->timeout_secs = getTimeoutFromURL(task);
|
|
|
|
|
|
|
|
curl_easy_setopt(e, CURLOPT_AUTOREFERER, 1L);
|
2018-01-30 21:56:24 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_ENCODING, "");
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
curl_easy_setopt(e, CURLOPT_MAXREDIRS, -1L);
|
|
|
|
curl_easy_setopt(e, CURLOPT_NOSIGNAL, 1L);
|
|
|
|
curl_easy_setopt(e, CURLOPT_PRIVATE, task);
|
|
|
|
|
2010-04-15 15:32:36 +00:00
|
|
|
#ifdef USE_LIBCURL_SOCKOPT
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_SOCKOPTFUNCTION, sockoptfunction);
|
|
|
|
curl_easy_setopt(e, CURLOPT_SOCKOPTDATA, task);
|
2010-04-15 15:32:36 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (web->curl_ssl_verify)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (web->curl_ca_bundle != nullptr)
|
2018-04-25 08:21:50 +00:00
|
|
|
{
|
|
|
|
curl_easy_setopt(e, CURLOPT_CAINFO, web->curl_ca_bundle);
|
|
|
|
}
|
2019-06-22 13:02:17 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
curl_easy_setopt(e, CURLOPT_SSL_CTX_FUNCTION, ssl_context_func);
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_SSL_VERIFYHOST, 0L);
|
|
|
|
curl_easy_setopt(e, CURLOPT_SSL_VERIFYPEER, 0L);
|
2012-03-04 13:21:42 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_TIMEOUT, task->timeout_secs);
|
|
|
|
curl_easy_setopt(e, CURLOPT_URL, task->url);
|
|
|
|
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);
|
|
|
|
curl_easy_setopt(e, CURLOPT_WRITEFUNCTION, writeFunc);
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto is_default_value = bool{};
|
|
|
|
tr_address const* addr = tr_sessionGetPublicAddress(s, TR_AF_INET, &is_default_value);
|
|
|
|
if (addr != nullptr && !is_default_value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
curl_easy_setopt(e, CURLOPT_INTERFACE, tr_address_to_string(addr));
|
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
|
|
|
|
addr = tr_sessionGetPublicAddress(s, TR_AF_INET6, &is_default_value);
|
|
|
|
if (addr != nullptr && !is_default_value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
curl_easy_setopt(e, CURLOPT_INTERFACE, tr_address_to_string(addr));
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (task->cookies != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
curl_easy_setopt(e, CURLOPT_COOKIE, task->cookies);
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (web->cookie_filename != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
curl_easy_setopt(e, CURLOPT_COOKIEFILE, web->cookie_filename);
|
|
|
|
}
|
2013-05-22 19:02:07 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (task->range != nullptr)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_RANGE, task->range);
|
|
|
|
/* don't bother asking the server to compress webseed fragments */
|
|
|
|
curl_easy_setopt(e, CURLOPT_ENCODING, "identity");
|
2011-12-14 05:58:23 +00:00
|
|
|
}
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return e;
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2008-04-27 18:27:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void task_finish_func(void* vtask)
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* task = static_cast<struct tr_web_task*>(vtask);
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg("finished web task %p; got %ld", (void*)task, task->code);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (task->done_func != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
(*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);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
task_free(task);
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_webThreadFunc(void* vsession);
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static struct tr_web_task* tr_webRunImpl(
|
|
|
|
tr_session* session,
|
|
|
|
int torrentId,
|
|
|
|
char const* url,
|
|
|
|
char const* range,
|
|
|
|
char const* cookies,
|
|
|
|
tr_web_done_func done_func,
|
|
|
|
void* done_func_user_data,
|
2019-02-10 11:05:16 +00:00
|
|
|
struct evbuffer* buffer)
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
struct tr_web_task* task = nullptr;
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!session->isClosing)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (session->web == nullptr)
|
2013-02-15 01:52:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_threadNew(tr_webThreadFunc, session);
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
while (session->web == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_wait_msec(20);
|
|
|
|
}
|
2013-02-15 01:52:47 +00:00
|
|
|
}
|
2015-01-02 11:15:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
task = tr_new0(struct tr_web_task, 1);
|
|
|
|
task->session = session;
|
|
|
|
task->torrentId = torrentId;
|
|
|
|
task->url = tr_strdup(url);
|
|
|
|
task->range = tr_strdup(range);
|
|
|
|
task->cookies = tr_strdup(cookies);
|
|
|
|
task->done_func = done_func;
|
|
|
|
task->done_func_user_data = done_func_user_data;
|
2021-09-15 00:18:09 +00:00
|
|
|
task->response = buffer != nullptr ? buffer : evbuffer_new();
|
|
|
|
task->freebuf = buffer != nullptr ? nullptr : task->response;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_lockLock(session->web->taskLock);
|
|
|
|
task->next = session->web->tasks;
|
|
|
|
session->web->tasks = task;
|
|
|
|
tr_lockUnlock(session->web->taskLock);
|
2008-08-06 23:33:29 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return task;
|
2008-05-06 15:52:57 +00:00
|
|
|
}
|
2008-04-24 19:38:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
struct tr_web_task* tr_webRunWithCookies(
|
|
|
|
tr_session* session,
|
|
|
|
char const* url,
|
|
|
|
char const* cookies,
|
|
|
|
tr_web_done_func done_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* done_func_user_data)
|
2013-04-13 20:25:28 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return tr_webRunImpl(session, -1, url, nullptr, cookies, done_func, done_func_user_data, nullptr);
|
2013-04-13 20:25:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
struct tr_web_task* tr_webRun(tr_session* session, char const* url, tr_web_done_func done_func, void* done_func_user_data)
|
2013-04-13 20:25:28 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return tr_webRunWithCookies(session, url, nullptr, done_func, done_func_user_data);
|
2013-04-13 20:25:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
struct tr_web_task* tr_webRunWebseed(
|
|
|
|
tr_torrent* tor,
|
|
|
|
char const* url,
|
|
|
|
char const* range,
|
|
|
|
tr_web_done_func done_func,
|
|
|
|
void* done_func_user_data,
|
|
|
|
struct evbuffer* buffer)
|
2013-04-13 20:25:28 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return tr_webRunImpl(tor->session, tr_torrentId(tor), url, range, nullptr, done_func, done_func_user_data, buffer);
|
2013-04-13 20:25:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_webThreadFunc(void* vsession)
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* try to enable ssl for https support; but if that fails,
|
|
|
|
* try a plain vanilla init */
|
2017-04-30 16:25:26 +00:00
|
|
|
if (curl_global_init(CURL_GLOBAL_SSL) != CURLE_OK)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
curl_global_init(0);
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:27:02 +00:00
|
|
|
auto* web = new tr_web{};
|
2017-04-19 12:04:45 +00:00
|
|
|
web->close_mode = ~0;
|
|
|
|
web->taskLock = tr_lockNew();
|
2021-09-15 00:18:09 +00:00
|
|
|
web->tasks = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
web->curl_verbose = tr_env_key_exists("TR_CURL_VERBOSE");
|
2017-07-16 17:28:13 +00:00
|
|
|
web->curl_ssl_verify = !tr_env_key_exists("TR_CURL_SSL_NO_VERIFY");
|
2021-09-15 00:18:09 +00:00
|
|
|
web->curl_ca_bundle = tr_env_get_string("CURL_CA_BUNDLE", nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (web->curl_ssl_verify)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddNamedInfo(
|
|
|
|
"web",
|
|
|
|
"will verify tracker certs using envvar CURL_CA_BUNDLE: %s",
|
2021-09-15 00:18:09 +00:00
|
|
|
web->curl_ca_bundle == nullptr ? "none" : web->curl_ca_bundle);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedInfo("web", "NB: this only works if you built against libcurl with openssl or gnutls, NOT nss");
|
|
|
|
tr_logAddNamedInfo("web", "NB: invalid certs will show up as 'Could not connect to tracker' like many other errors");
|
|
|
|
}
|
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
char* const str = tr_buildPath(session->configDir, "cookies.txt", nullptr);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (tr_sys_path_exists(str, nullptr))
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
web->cookie_filename = tr_strdup(str);
|
2012-03-04 13:21:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(str);
|
2011-04-27 17:02:18 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto* const multi = curl_multi_init();
|
2017-04-19 12:04:45 +00:00
|
|
|
session->web = web;
|
2009-12-11 15:41:34 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto repeats = uint32_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (web->close_mode == TR_WEB_CLOSE_NOW)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (web->close_mode == TR_WEB_CLOSE_WHEN_IDLE && web->tasks == nullptr)
|
2013-04-13 20:56:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-04-28 00:06:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* add tasks from the queue */
|
|
|
|
tr_lockLock(web->taskLock);
|
2013-04-28 00:06:22 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
while (web->tasks != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
/* pop the task */
|
2020-08-11 18:11:55 +00:00
|
|
|
struct tr_web_task* task = web->tasks;
|
2017-04-19 12:04:45 +00:00
|
|
|
web->tasks = task->next;
|
2021-09-15 00:18:09 +00:00
|
|
|
task->next = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
dbgmsg("adding task to curl: [%s]", task->url);
|
|
|
|
curl_multi_add_handle(multi, createEasy(session, web, task));
|
2013-04-13 20:56:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockUnlock(web->taskLock);
|
|
|
|
|
2021-09-27 16:27:02 +00:00
|
|
|
/* resume any paused curl handles.
|
|
|
|
swap paused_easy_handles to prevent oscillation
|
|
|
|
between writeFunc this while loop */
|
|
|
|
auto paused = decltype(web->paused_easy_handles){};
|
|
|
|
std::swap(paused, web->paused_easy_handles);
|
|
|
|
std::for_each(std::begin(paused), std::end(paused), [](auto* curl) { curl_easy_pause(curl, CURLPAUSE_CONT); });
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* maybe wait a little while before calling curl_multi_perform() */
|
2021-10-15 17:27:12 +00:00
|
|
|
auto msec = long{};
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_multi_timeout(multi, &msec);
|
|
|
|
|
|
|
|
if (msec < 0)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
msec = ThreadfuncMaxSleepMsec;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (session->isClosed)
|
|
|
|
{
|
2017-04-21 07:40:57 +00:00
|
|
|
msec = 100; /* on shutdown, call perform() more frequently */
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2010-01-22 03:39:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (msec > 0)
|
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
if (msec > ThreadfuncMaxSleepMsec)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
msec = ThreadfuncMaxSleepMsec;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto numfds = int{};
|
2021-09-15 00:18:09 +00:00
|
|
|
curl_multi_wait(multi, nullptr, 0, msec, &numfds);
|
2020-07-29 02:38:27 +00:00
|
|
|
if (!numfds)
|
|
|
|
{
|
|
|
|
repeats++;
|
|
|
|
if (repeats > 1)
|
|
|
|
{
|
|
|
|
/* curl_multi_wait() returns immediately if there are
|
|
|
|
* no fds to wait for, so we need an explicit wait here
|
|
|
|
* to emulate select() behavior */
|
2021-10-21 00:39:05 +00:00
|
|
|
tr_wait_msec(std::min(msec, ThreadfuncMaxSleepMsec / 2L));
|
2020-07-29 02:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
repeats = 0;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-01-22 03:39:21 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* call curl_multi_perform() */
|
2021-10-15 17:27:12 +00:00
|
|
|
auto mcode = CURLMcode{};
|
|
|
|
auto unused = int{};
|
2017-04-19 12:04:45 +00:00
|
|
|
do
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
mcode = curl_multi_perform(multi, &unused);
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (mcode == CURLM_CALL_MULTI_PERFORM);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* pump completed tasks from the multi */
|
2021-10-15 17:27:12 +00:00
|
|
|
CURLMsg* msg = nullptr;
|
2021-09-15 00:18:09 +00:00
|
|
|
while ((msg = curl_multi_info_read(multi, &unused)) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (msg->msg == CURLMSG_DONE && msg->easy_handle != nullptr)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
CURL* const e = msg->easy_handle;
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
struct tr_web_task* task = nullptr;
|
|
|
|
curl_easy_getinfo(e, CURLINFO_PRIVATE, (void*)&task);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(e == task->curl_easy);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto req_bytes_sent = long{};
|
|
|
|
auto total_time = double{};
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_getinfo(e, CURLINFO_RESPONSE_CODE, &task->code);
|
|
|
|
curl_easy_getinfo(e, CURLINFO_REQUEST_SIZE, &req_bytes_sent);
|
|
|
|
curl_easy_getinfo(e, CURLINFO_TOTAL_TIME, &total_time);
|
|
|
|
task->did_connect = task->code > 0 || req_bytes_sent > 0;
|
2017-04-30 16:25:26 +00:00
|
|
|
task->did_timeout = task->code == 0 && total_time >= task->timeout_secs;
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_multi_remove_handle(multi, e);
|
2021-09-27 16:27:02 +00:00
|
|
|
web->paused_easy_handles.erase(e);
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_cleanup(e);
|
|
|
|
tr_runInEventThread(task->session, task_finish_func, task);
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Discard any remaining tasks.
|
|
|
|
* This is rare, but can happen on shutdown with unresponsive trackers. */
|
2021-09-15 00:18:09 +00:00
|
|
|
while (web->tasks != nullptr)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
struct tr_web_task* task = web->tasks;
|
2017-04-19 12:04:45 +00:00
|
|
|
web->tasks = task->next;
|
|
|
|
dbgmsg("Discarding task \"%s\"", task->url);
|
|
|
|
task_free(task);
|
2011-02-21 01:13:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* cleanup */
|
|
|
|
curl_multi_cleanup(multi);
|
|
|
|
tr_lockFree(web->taskLock);
|
|
|
|
tr_free(web->curl_ca_bundle);
|
|
|
|
tr_free(web->cookie_filename);
|
2021-09-27 16:27:02 +00:00
|
|
|
delete web;
|
2021-09-15 00:18:09 +00:00
|
|
|
session->web = nullptr;
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_webClose(tr_session* session, tr_web_close_mode close_mode)
|
2008-04-25 19:46:36 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (session->web != nullptr)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
session->web->close_mode = close_mode;
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (close_mode == TR_WEB_CLOSE_NOW)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
while (session->web != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_wait_msec(100);
|
|
|
|
}
|
|
|
|
}
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2008-04-25 19:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 23:41:26 +00:00
|
|
|
long tr_webGetTaskResponseCode(struct tr_web_task* task)
|
|
|
|
{
|
|
|
|
long code = 0;
|
|
|
|
curl_easy_getinfo(task->curl_easy, CURLINFO_RESPONSE_CODE, &code);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const* tr_webGetTaskRealUrl(struct tr_web_task* task)
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char* url = nullptr;
|
2020-08-27 23:41:26 +00:00
|
|
|
curl_easy_getinfo(task->curl_easy, CURLINFO_EFFECTIVE_URL, &url);
|
|
|
|
return url;
|
2011-07-10 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/*****
|
|
|
|
******
|
|
|
|
******
|
|
|
|
*****/
|
2008-04-25 04:26:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_webGetResponseStr(long code)
|
2008-04-25 04:26:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (code)
|
2009-12-14 12:54:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 0:
|
|
|
|
return "No Response";
|
|
|
|
|
|
|
|
case 101:
|
|
|
|
return "Switching Protocols";
|
|
|
|
|
|
|
|
case 200:
|
|
|
|
return "OK";
|
|
|
|
|
|
|
|
case 201:
|
|
|
|
return "Created";
|
|
|
|
|
|
|
|
case 202:
|
|
|
|
return "Accepted";
|
|
|
|
|
|
|
|
case 203:
|
|
|
|
return "Non-Authoritative Information";
|
|
|
|
|
|
|
|
case 204:
|
|
|
|
return "No Content";
|
|
|
|
|
|
|
|
case 205:
|
|
|
|
return "Reset Content";
|
|
|
|
|
|
|
|
case 206:
|
|
|
|
return "Partial Content";
|
|
|
|
|
|
|
|
case 300:
|
|
|
|
return "Multiple Choices";
|
|
|
|
|
|
|
|
case 301:
|
|
|
|
return "Moved Permanently";
|
|
|
|
|
|
|
|
case 302:
|
|
|
|
return "Found";
|
|
|
|
|
|
|
|
case 303:
|
|
|
|
return "See Other";
|
|
|
|
|
|
|
|
case 304:
|
|
|
|
return "Not Modified";
|
|
|
|
|
|
|
|
case 305:
|
|
|
|
return "Use Proxy";
|
|
|
|
|
|
|
|
case 306:
|
|
|
|
return " (Unused)";
|
|
|
|
|
|
|
|
case 307:
|
|
|
|
return "Temporary Redirect";
|
|
|
|
|
|
|
|
case 400:
|
|
|
|
return "Bad Request";
|
|
|
|
|
|
|
|
case 401:
|
|
|
|
return "Unauthorized";
|
|
|
|
|
|
|
|
case 402:
|
|
|
|
return "Payment Required";
|
|
|
|
|
|
|
|
case 403:
|
|
|
|
return "Forbidden";
|
|
|
|
|
|
|
|
case 404:
|
|
|
|
return "Not Found";
|
|
|
|
|
|
|
|
case 405:
|
|
|
|
return "Method Not Allowed";
|
|
|
|
|
|
|
|
case 406:
|
|
|
|
return "Not Acceptable";
|
|
|
|
|
|
|
|
case 407:
|
|
|
|
return "Proxy Authentication Required";
|
|
|
|
|
|
|
|
case 408:
|
|
|
|
return "Request Timeout";
|
|
|
|
|
|
|
|
case 409:
|
|
|
|
return "Conflict";
|
|
|
|
|
|
|
|
case 410:
|
|
|
|
return "Gone";
|
|
|
|
|
|
|
|
case 411:
|
|
|
|
return "Length Required";
|
|
|
|
|
|
|
|
case 412:
|
|
|
|
return "Precondition Failed";
|
|
|
|
|
|
|
|
case 413:
|
|
|
|
return "Request Entity Too Large";
|
|
|
|
|
|
|
|
case 414:
|
|
|
|
return "Request-URI Too Long";
|
|
|
|
|
|
|
|
case 415:
|
|
|
|
return "Unsupported Media Type";
|
|
|
|
|
|
|
|
case 416:
|
|
|
|
return "Requested Range Not Satisfiable";
|
|
|
|
|
|
|
|
case 417:
|
|
|
|
return "Expectation Failed";
|
|
|
|
|
2018-01-11 18:00:41 +00:00
|
|
|
case 421:
|
|
|
|
return "Misdirected Request";
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 500:
|
|
|
|
return "Internal Server Error";
|
|
|
|
|
|
|
|
case 501:
|
|
|
|
return "Not Implemented";
|
|
|
|
|
|
|
|
case 502:
|
|
|
|
return "Bad Gateway";
|
|
|
|
|
|
|
|
case 503:
|
|
|
|
return "Service Unavailable";
|
|
|
|
|
|
|
|
case 504:
|
|
|
|
return "Gateway Timeout";
|
|
|
|
|
|
|
|
case 505:
|
|
|
|
return "HTTP Version Not Supported";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "Unknown Error";
|
2009-12-14 12:54:30 +00:00
|
|
|
}
|
2008-04-25 04:26:04 +00:00
|
|
|
}
|
2009-11-10 17:03:23 +00:00
|
|
|
|
2021-10-16 13:20:04 +00:00
|
|
|
void tr_http_escape(struct evbuffer* out, std::string_view str, bool escape_reserved)
|
2009-11-10 17:03:23 +00:00
|
|
|
{
|
2021-10-16 13:20:04 +00:00
|
|
|
auto constexpr ReservedChars = std::string_view{ "!*'();:@&=+$,/?%#[]" };
|
|
|
|
auto constexpr UnescapedChars = std::string_view{ "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.~" };
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-10-16 13:20:04 +00:00
|
|
|
for (auto& ch : str)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-10-16 13:20:04 +00:00
|
|
|
if ((UnescapedChars.find(ch) != std::string_view::npos) || (ReservedChars.find(ch) && !escape_reserved))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-16 13:20:04 +00:00
|
|
|
evbuffer_add_printf(out, "%c", ch);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-16 13:20:04 +00:00
|
|
|
evbuffer_add_printf(out, "%%%02X", (unsigned)(ch & 0xFF));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-11-10 17:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_http_unescape(char const* str, size_t len)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tmp = curl_unescape(str, len);
|
|
|
|
char* ret = tr_strdup(tmp);
|
|
|
|
curl_free(tmp);
|
|
|
|
return ret;
|
2009-11-20 04:38:19 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static bool is_rfc2396_alnum(uint8_t ch)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '.' || ch == '-' ||
|
2019-02-10 11:05:16 +00:00
|
|
|
ch == '_' || ch == '~';
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_http_escape_sha1(char* out, uint8_t const* sha1_digest)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* in = sha1_digest;
|
|
|
|
uint8_t const* end = in + SHA_DIGEST_LENGTH;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (in != end)
|
|
|
|
{
|
|
|
|
if (is_rfc2396_alnum(*in))
|
|
|
|
{
|
|
|
|
*out++ = (char)*in++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out += tr_snprintf(out, 4, "%%%02x", (unsigned int)*in++);
|
|
|
|
}
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*out = '\0';
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|