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
|
|
|
*
|
2008-04-25 19:46:36 +00:00
|
|
|
* $Id$
|
2008-04-24 01:42:53 +00:00
|
|
|
*/
|
|
|
|
|
2013-07-24 00:00:03 +00:00
|
|
|
#include <assert.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <string.h> /* strlen (), strstr () */
|
|
|
|
#include <stdlib.h> /* getenv () */
|
2011-03-16 18:04:23 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2010-06-25 20:36:10 +00:00
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#else
|
|
|
|
#include <sys/select.h>
|
|
|
|
#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"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-04-13 20:56:24 +00:00
|
|
|
#include "list.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"
|
2012-12-05 17:29:46 +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 */
|
|
|
|
#define USE_LIBCURL_SOCKOPT
|
|
|
|
#endif
|
|
|
|
|
2009-01-09 19:24:40 +00:00
|
|
|
enum
|
2008-12-26 20:14:47 +00:00
|
|
|
{
|
2013-04-13 20:56:24 +00:00
|
|
|
THREADFUNC_MAX_SLEEP_MSEC = 200,
|
2008-12-26 20:14:47 +00:00
|
|
|
};
|
2008-10-17 20:57:54 +00:00
|
|
|
|
2008-10-27 18:00:03 +00:00
|
|
|
#if 0
|
|
|
|
#define dbgmsg(...) \
|
2013-01-24 23:59:52 +00:00
|
|
|
do { \
|
|
|
|
fprintf (stderr, __VA_ARGS__); \
|
|
|
|
fprintf (stderr, "\n"); \
|
|
|
|
} while (0)
|
2008-10-27 18:00:03 +00:00
|
|
|
#else
|
2012-12-05 17:29:46 +00:00
|
|
|
#define dbgmsg(...) \
|
2013-01-24 23:59:52 +00:00
|
|
|
do { \
|
2013-01-25 23:34:20 +00:00
|
|
|
if (tr_logGetDeepEnabled ()) \
|
|
|
|
tr_logAddDeep (__FILE__, __LINE__, "web", __VA_ARGS__); \
|
2013-01-24 23:59:52 +00:00
|
|
|
} while (0)
|
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
|
|
|
|
{
|
2013-04-13 20:25:28 +00:00
|
|
|
int torrentId;
|
2013-01-24 23:59:52 +00:00
|
|
|
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;
|
2013-09-08 17:08:18 +00:00
|
|
|
tr_web_done_func done_func;
|
2013-01-24 23:59:52 +00:00
|
|
|
void * done_func_user_data;
|
|
|
|
CURL * curl_easy;
|
|
|
|
struct tr_web_task * next;
|
2008-04-24 01:42:53 +00:00
|
|
|
};
|
|
|
|
|
2009-12-14 12:54:30 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
task_free (struct tr_web_task * task)
|
2009-12-14 12:54:30 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
if (task->freebuf)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2013-04-13 20:56:24 +00:00
|
|
|
static tr_list * paused_easy_handles = NULL;
|
|
|
|
|
2011-06-19 18:18:48 +00:00
|
|
|
struct tr_web
|
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
bool curl_verbose;
|
|
|
|
bool curl_ssl_verify;
|
|
|
|
const char * curl_ca_bundle;
|
|
|
|
int close_mode;
|
|
|
|
struct tr_web_task * tasks;
|
|
|
|
tr_lock * taskLock;
|
|
|
|
char * cookie_filename;
|
2011-06-19 18:18:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-25 18:35:48 +00:00
|
|
|
static size_t
|
2012-12-05 17:29:46 +00:00
|
|
|
writeFunc (void * ptr, size_t size, size_t nmemb, void * vtask)
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
const size_t byteCount = size * nmemb;
|
|
|
|
struct tr_web_task * task = vtask;
|
2013-04-13 20:56:24 +00:00
|
|
|
|
|
|
|
/* webseed downloads should be speed limited */
|
|
|
|
if (task->torrentId != -1)
|
|
|
|
{
|
|
|
|
tr_torrent * tor = tr_torrentFindFromId (task->session, task->torrentId);
|
|
|
|
|
|
|
|
if (tor && !tr_bandwidthClamp (&tor->bandwidth, TR_DOWN, nmemb))
|
|
|
|
{
|
|
|
|
tr_list_append (&paused_easy_handles, task->curl_easy);
|
|
|
|
return CURL_WRITEFUNC_PAUSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
evbuffer_add (task->response, ptr, byteCount);
|
2013-09-09 01:32:09 +00:00
|
|
|
dbgmsg ("wrote %"TR_PRIuSIZE" bytes to task %p's buffer", byteCount, (void*)task);
|
2013-01-24 23:59:52 +00:00
|
|
|
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
|
2009-12-28 23:25:50 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
sockoptfunction (void * vtask, curl_socket_t fd, curlsocktype purpose UNUSED)
|
2009-12-02 05:30:46 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
struct tr_web_task * task = vtask;
|
|
|
|
const bool isScrape = strstr (task->url, "scrape") != NULL;
|
|
|
|
const bool isAnnounce = strstr (task->url, "announce") != NULL;
|
2009-12-02 05:30:46 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
/* announce and scrape requests have tiny payloads. */
|
|
|
|
if (isScrape || isAnnounce)
|
2009-12-02 05:30:46 +00:00
|
|
|
{
|
2013-02-10 23:21:58 +00:00
|
|
|
const int sndbuf = isScrape ? 4096 : 1024;
|
|
|
|
const int rcvbuf = isScrape ? 4096 : 3072;
|
2013-01-24 23:59:52 +00:00
|
|
|
setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof (sndbuf));
|
|
|
|
setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof (rcvbuf));
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
2009-12-28 23:25:50 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
/* return nonzero if this function encountered an error */
|
|
|
|
return 0;
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
2010-04-15 15:32:36 +00:00
|
|
|
#endif
|
2009-12-02 05:30:46 +00:00
|
|
|
|
2010-03-06 15:05:05 +00:00
|
|
|
static long
|
2012-12-05 17:29:46 +00:00
|
|
|
getTimeoutFromURL (const struct tr_web_task * task)
|
2009-12-13 17:54:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
long timeout;
|
|
|
|
const tr_session * session = task->session;
|
2010-08-07 02:17:29 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
if (!session || session->isClosed) timeout = 20L;
|
|
|
|
else if (strstr (task->url, "scrape") != NULL) timeout = 30L;
|
|
|
|
else if (strstr (task->url, "announce") != NULL) timeout = 90L;
|
|
|
|
else timeout = 240L;
|
2010-08-07 02:17:29 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
return timeout;
|
2009-12-13 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
2010-03-06 15:05:05 +00:00
|
|
|
static CURL *
|
2012-12-05 17:29:46 +00:00
|
|
|
createEasy (tr_session * s, struct tr_web * web, struct tr_web_task * task)
|
2008-04-25 18:35:48 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
bool is_default_value;
|
|
|
|
const tr_address * addr;
|
|
|
|
CURL * e = task->curl_easy = curl_easy_init ();
|
|
|
|
|
|
|
|
task->timeout_secs = getTimeoutFromURL (task);
|
|
|
|
|
|
|
|
curl_easy_setopt (e, CURLOPT_AUTOREFERER, 1L);
|
|
|
|
curl_easy_setopt (e, CURLOPT_ENCODING, "gzip;q=1.0, deflate, identity");
|
|
|
|
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
|
2013-01-24 23:59:52 +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
|
2013-01-24 23:59:52 +00:00
|
|
|
if (web->curl_ssl_verify)
|
|
|
|
{
|
|
|
|
curl_easy_setopt (e, CURLOPT_CAINFO, web->curl_ca_bundle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
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);
|
|
|
|
|
|
|
|
if (((addr = tr_sessionGetPublicAddress (s, TR_AF_INET, &is_default_value))) && !is_default_value)
|
|
|
|
curl_easy_setopt (e, CURLOPT_INTERFACE, tr_address_to_string (addr));
|
|
|
|
else if (((addr = tr_sessionGetPublicAddress (s, TR_AF_INET6, &is_default_value))) && !is_default_value)
|
|
|
|
curl_easy_setopt (e, CURLOPT_INTERFACE, tr_address_to_string (addr));
|
|
|
|
|
|
|
|
if (task->cookies != NULL)
|
|
|
|
curl_easy_setopt (e, CURLOPT_COOKIE, task->cookies);
|
|
|
|
|
2013-05-22 19:02:07 +00:00
|
|
|
if (web->cookie_filename != NULL)
|
|
|
|
curl_easy_setopt (e, CURLOPT_COOKIEFILE, web->cookie_filename);
|
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
if (task->range != NULL)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-01-24 23:59:52 +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
|
|
|
|
2008-10-18 15:45:12 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
task_finish_func (void * vtask)
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
struct tr_web_task * task = vtask;
|
2013-09-09 01:32:09 +00:00
|
|
|
dbgmsg ("finished web task %p; got %ld", (void*)task, task->code);
|
2013-01-24 23:59:52 +00:00
|
|
|
|
|
|
|
if (task->done_func != NULL)
|
|
|
|
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);
|
|
|
|
|
|
|
|
task_free (task);
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2013-02-15 01:52:47 +00:00
|
|
|
static void tr_webThreadFunc (void * vsession);
|
|
|
|
|
2013-04-13 20:25:28 +00:00
|
|
|
static struct tr_web_task *
|
|
|
|
tr_webRunImpl (tr_session * session,
|
|
|
|
int torrentId,
|
|
|
|
const char * url,
|
|
|
|
const char * range,
|
|
|
|
const char * cookies,
|
|
|
|
tr_web_done_func done_func,
|
|
|
|
void * done_func_user_data,
|
|
|
|
struct evbuffer * buffer)
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2013-02-15 01:52:47 +00:00
|
|
|
struct tr_web_task * task = NULL;
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2013-02-15 01:52:47 +00:00
|
|
|
if (!session->isClosing)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2013-02-15 01:52:47 +00:00
|
|
|
if (session->web == NULL)
|
|
|
|
{
|
|
|
|
tr_threadNew (tr_webThreadFunc, session);
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2013-02-15 01:52:47 +00:00
|
|
|
while (session->web == NULL)
|
|
|
|
tr_wait_msec (20);
|
|
|
|
}
|
|
|
|
|
|
|
|
task = tr_new0 (struct tr_web_task, 1);
|
2013-01-24 23:59:52 +00:00
|
|
|
task->session = session;
|
2013-04-13 20:25:28 +00:00
|
|
|
task->torrentId = torrentId;
|
2013-01-24 23:59:52 +00:00
|
|
|
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;
|
|
|
|
task->response = buffer ? buffer : evbuffer_new ();
|
|
|
|
task->freebuf = buffer ? NULL : task->response;
|
|
|
|
|
2013-02-15 01:52:47 +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
|
|
|
|
2013-02-15 01:52:47 +00:00
|
|
|
return task;
|
2008-05-06 15:52:57 +00:00
|
|
|
}
|
2008-04-24 19:38:59 +00:00
|
|
|
|
2013-04-13 20:25:28 +00:00
|
|
|
struct tr_web_task *
|
|
|
|
tr_webRunWithCookies (tr_session * session,
|
|
|
|
const char * url,
|
|
|
|
const char * cookies,
|
|
|
|
tr_web_done_func done_func,
|
|
|
|
void * done_func_user_data)
|
|
|
|
{
|
|
|
|
return tr_webRunImpl (session, -1, url,
|
|
|
|
NULL, cookies,
|
|
|
|
done_func, done_func_user_data,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tr_web_task *
|
|
|
|
tr_webRun (tr_session * session,
|
|
|
|
const char * url,
|
|
|
|
tr_web_done_func done_func,
|
|
|
|
void * done_func_user_data)
|
|
|
|
{
|
|
|
|
return tr_webRunWithCookies (session, url, NULL,
|
|
|
|
done_func, done_func_user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct tr_web_task *
|
|
|
|
tr_webRunWebseed (tr_torrent * tor,
|
|
|
|
const char * url,
|
|
|
|
const char * 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, NULL,
|
|
|
|
done_func, done_func_user_data,
|
|
|
|
buffer);
|
|
|
|
}
|
|
|
|
|
2010-06-30 21:24:36 +00:00
|
|
|
/**
|
2012-12-05 17:29:46 +00:00
|
|
|
* Portability wrapper for select ().
|
2010-06-30 21:24:36 +00:00
|
|
|
*
|
|
|
|
* http://msdn.microsoft.com/en-us/library/ms740141%28VS.85%29.aspx
|
|
|
|
* On win32, any two of the parameters, readfds, writefds, or exceptfds,
|
|
|
|
* can be given as null. At least one must be non-null, and any non-null
|
2010-11-11 15:31:11 +00:00
|
|
|
* descriptor set must contain at least one handle to a socket.
|
2010-06-30 21:24:36 +00:00
|
|
|
*/
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_select (int nfds,
|
2010-06-30 21:24:36 +00:00
|
|
|
fd_set * r_fd_set, fd_set * w_fd_set, fd_set * c_fd_set,
|
2012-12-05 17:29:46 +00:00
|
|
|
struct timeval * t)
|
2010-06-30 21:24:36 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2013-01-24 23:59:52 +00:00
|
|
|
if (!r_fd_set->fd_count && !w_fd_set->fd_count && !c_fd_set->fd_count)
|
2010-06-30 21:24:36 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
const long int msec = t->tv_sec*1000 + t->tv_usec/1000;
|
|
|
|
tr_wait_msec (msec);
|
2010-06-30 21:24:36 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
else if (select (0, r_fd_set->fd_count ? r_fd_set : NULL,
|
|
|
|
w_fd_set->fd_count ? w_fd_set : NULL,
|
|
|
|
c_fd_set->fd_count ? c_fd_set : NULL, t) < 0)
|
2010-06-30 21:24:36 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
char errstr[512];
|
|
|
|
const int e = EVUTIL_SOCKET_ERROR ();
|
|
|
|
tr_net_strerror (errstr, sizeof (errstr), e);
|
|
|
|
dbgmsg ("Error: select (%d) %s", e, errstr);
|
2010-06-30 21:24:36 +00:00
|
|
|
}
|
|
|
|
#else
|
2013-01-24 23:59:52 +00:00
|
|
|
select (nfds, r_fd_set, w_fd_set, c_fd_set, t);
|
2010-06-30 21:24:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-03-06 15:05:05 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_webThreadFunc (void * vsession)
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2013-05-22 19:02:07 +00:00
|
|
|
char * str;
|
2013-01-24 23:59:52 +00:00
|
|
|
CURLM * multi;
|
|
|
|
struct tr_web * web;
|
|
|
|
int taskCount = 0;
|
|
|
|
struct tr_web_task * task;
|
|
|
|
tr_session * session = vsession;
|
|
|
|
|
|
|
|
/* try to enable ssl for https support; but if that fails,
|
|
|
|
* try a plain vanilla init */
|
|
|
|
if (curl_global_init (CURL_GLOBAL_SSL))
|
|
|
|
curl_global_init (0);
|
|
|
|
|
|
|
|
web = tr_new0 (struct tr_web, 1);
|
|
|
|
web->close_mode = ~0;
|
|
|
|
web->taskLock = tr_lockNew ();
|
|
|
|
web->tasks = NULL;
|
|
|
|
web->curl_verbose = getenv ("TR_CURL_VERBOSE") != NULL;
|
|
|
|
web->curl_ssl_verify = getenv ("TR_CURL_SSL_VERIFY") != NULL;
|
|
|
|
web->curl_ca_bundle = getenv ("CURL_CA_BUNDLE");
|
|
|
|
if (web->curl_ssl_verify)
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddNamedInfo ("web", "will verify tracker certs using envvar CURL_CA_BUNDLE: %s",
|
2013-01-24 23:59:52 +00:00
|
|
|
web->curl_ca_bundle == NULL ? "none" : web->curl_ca_bundle);
|
2013-01-25 23:34:20 +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");
|
2012-03-04 13:21:42 +00:00
|
|
|
}
|
2013-05-22 19:02:07 +00:00
|
|
|
|
|
|
|
str = tr_buildPath (session->configDir, "cookies.txt", NULL);
|
2014-07-08 00:08:43 +00:00
|
|
|
if (tr_sys_path_exists (str, NULL))
|
2013-05-22 19:02:07 +00:00
|
|
|
web->cookie_filename = tr_strdup (str);
|
|
|
|
tr_free (str);
|
2011-04-27 17:02:18 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
multi = curl_multi_init ();
|
|
|
|
session->web = web;
|
2009-12-11 15:41:34 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
for (;;)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
long msec;
|
|
|
|
int unused;
|
|
|
|
CURLMsg * msg;
|
|
|
|
CURLMcode mcode;
|
|
|
|
|
|
|
|
if (web->close_mode == TR_WEB_CLOSE_NOW)
|
|
|
|
break;
|
|
|
|
if ((web->close_mode == TR_WEB_CLOSE_WHEN_IDLE) && (web->tasks == NULL))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* add tasks from the queue */
|
|
|
|
tr_lockLock (web->taskLock);
|
|
|
|
while (web->tasks != NULL)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
/* pop the task */
|
|
|
|
task = web->tasks;
|
|
|
|
web->tasks = task->next;
|
|
|
|
task->next = NULL;
|
|
|
|
|
|
|
|
dbgmsg ("adding task to curl: [%s]", task->url);
|
|
|
|
curl_multi_add_handle (multi, createEasy (session, web, task));
|
|
|
|
/*fprintf (stderr, "adding a task.. taskCount is now %d\n", taskCount);*/
|
|
|
|
++taskCount;
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
tr_lockUnlock (web->taskLock);
|
|
|
|
|
2013-04-13 20:56:24 +00:00
|
|
|
/* unpause any paused curl handles */
|
|
|
|
if (paused_easy_handles != NULL)
|
|
|
|
{
|
|
|
|
CURL * handle;
|
2013-04-28 00:06:22 +00:00
|
|
|
tr_list * tmp;
|
|
|
|
|
|
|
|
/* swap paused_easy_handles to prevent oscillation
|
|
|
|
between writeFunc this while loop */
|
|
|
|
tmp = paused_easy_handles;
|
|
|
|
paused_easy_handles = NULL;
|
|
|
|
|
|
|
|
while ((handle = tr_list_pop_front (&tmp)))
|
2013-04-13 20:56:24 +00:00
|
|
|
curl_easy_pause (handle, CURLPAUSE_CONT);
|
|
|
|
}
|
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
/* maybe wait a little while before calling curl_multi_perform () */
|
|
|
|
msec = 0;
|
|
|
|
curl_multi_timeout (multi, &msec);
|
|
|
|
if (msec < 0)
|
|
|
|
msec = THREADFUNC_MAX_SLEEP_MSEC;
|
|
|
|
if (session->isClosed)
|
|
|
|
msec = 100; /* on shutdown, call perform () more frequently */
|
|
|
|
if (msec > 0)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
int usec;
|
|
|
|
int max_fd;
|
|
|
|
struct timeval t;
|
|
|
|
fd_set r_fd_set, w_fd_set, c_fd_set;
|
|
|
|
|
|
|
|
max_fd = 0;
|
|
|
|
FD_ZERO (&r_fd_set);
|
|
|
|
FD_ZERO (&w_fd_set);
|
|
|
|
FD_ZERO (&c_fd_set);
|
|
|
|
curl_multi_fdset (multi, &r_fd_set, &w_fd_set, &c_fd_set, &max_fd);
|
|
|
|
|
|
|
|
if (msec > THREADFUNC_MAX_SLEEP_MSEC)
|
|
|
|
msec = THREADFUNC_MAX_SLEEP_MSEC;
|
|
|
|
|
|
|
|
usec = msec * 1000;
|
|
|
|
t.tv_sec = usec / 1000000;
|
|
|
|
t.tv_usec = usec % 1000000;
|
|
|
|
tr_select (max_fd+1, &r_fd_set, &w_fd_set, &c_fd_set, &t);
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2010-01-22 03:39:21 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
/* call curl_multi_perform () */
|
|
|
|
do
|
|
|
|
mcode = curl_multi_perform (multi, &unused);
|
|
|
|
while (mcode == CURLM_CALL_MULTI_PERFORM);
|
2010-01-22 03:39:21 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
/* pump completed tasks from the multi */
|
|
|
|
while ((msg = curl_multi_info_read (multi, &unused)))
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
if ((msg->msg == CURLMSG_DONE) && (msg->easy_handle != NULL))
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
double total_time;
|
|
|
|
struct tr_web_task * task;
|
|
|
|
long req_bytes_sent;
|
|
|
|
CURL * e = msg->easy_handle;
|
|
|
|
curl_easy_getinfo (e, CURLINFO_PRIVATE, (void*)&task);
|
2013-07-24 00:00:03 +00:00
|
|
|
assert (e == task->curl_easy);
|
2013-01-24 23:59:52 +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;
|
|
|
|
task->did_timeout = !task->code && (total_time >= task->timeout_secs);
|
|
|
|
curl_multi_remove_handle (multi, e);
|
2013-07-24 00:00:03 +00:00
|
|
|
tr_list_remove_data (&paused_easy_handles, e);
|
2013-01-24 23:59:52 +00:00
|
|
|
curl_easy_cleanup (e);
|
|
|
|
tr_runInEventThread (task->session, task_finish_func, task);
|
|
|
|
--taskCount;
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
/* Discard any remaining tasks.
|
|
|
|
* This is rare, but can happen on shutdown with unresponsive trackers. */
|
|
|
|
while (web->tasks != NULL)
|
|
|
|
{
|
|
|
|
task = web->tasks;
|
|
|
|
web->tasks = task->next;
|
|
|
|
dbgmsg ("Discarding task \"%s\"", task->url);
|
|
|
|
task_free (task);
|
2011-02-21 01:13:28 +00:00
|
|
|
}
|
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
/* cleanup */
|
2013-07-24 00:00:03 +00:00
|
|
|
tr_list_free (&paused_easy_handles, NULL);
|
2013-01-24 23:59:52 +00:00
|
|
|
curl_multi_cleanup (multi);
|
|
|
|
tr_lockFree (web->taskLock);
|
|
|
|
tr_free (web->cookie_filename);
|
|
|
|
tr_free (web);
|
|
|
|
session->web = NULL;
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
|
|
|
|
2008-04-25 04:26:04 +00:00
|
|
|
|
2008-04-25 19:46:36 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_webClose (tr_session * session, tr_web_close_mode close_mode)
|
2008-04-25 19:46:36 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
if (session->web != NULL)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
session->web->close_mode = close_mode;
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
if (close_mode == TR_WEB_CLOSE_NOW)
|
|
|
|
while (session->web != NULL)
|
|
|
|
tr_wait_msec (100);
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2008-04-25 19:46:36 +00:00
|
|
|
}
|
|
|
|
|
2011-07-10 15:24:51 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_webGetTaskInfo (struct tr_web_task * task, tr_web_task_info info, void * dst)
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
curl_easy_getinfo (task->curl_easy, (CURLINFO) info, dst);
|
2011-07-10 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/*****
|
|
|
|
******
|
|
|
|
******
|
|
|
|
*****/
|
2008-04-25 04:26:04 +00:00
|
|
|
|
|
|
|
const char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_webGetResponseStr (long code)
|
2008-04-25 04:26:04 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
switch (code)
|
2009-12-14 12:54:30 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +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";
|
|
|
|
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
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_http_escape (struct evbuffer * out,
|
2013-01-24 23:59:52 +00:00
|
|
|
const char * str,
|
|
|
|
int len,
|
|
|
|
bool escape_slashes)
|
2009-11-10 17:03:23 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
const char * end;
|
|
|
|
|
|
|
|
if ((len < 0) && (str != NULL))
|
|
|
|
len = strlen (str);
|
|
|
|
|
|
|
|
for (end=str+len; str && str!=end; ++str)
|
|
|
|
{
|
|
|
|
if ((*str == ',') || (*str == '-')
|
|
|
|
|| (*str == '.')
|
|
|
|
|| (('0' <= *str) && (*str <= '9'))
|
|
|
|
|| (('A' <= *str) && (*str <= 'Z'))
|
|
|
|
|| (('a' <= *str) && (*str <= 'z'))
|
|
|
|
|| ((*str == '/') && (!escape_slashes)))
|
|
|
|
evbuffer_add_printf (out, "%c", *str);
|
|
|
|
else
|
|
|
|
evbuffer_add_printf (out, "%%%02X", (unsigned)(*str&0xFF));
|
2009-11-10 17:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2009-12-14 05:11:33 +00:00
|
|
|
char *
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_http_unescape (const char * str, int len)
|
2009-11-20 04:38:19 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +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
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
is_rfc2396_alnum (uint8_t ch)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
return ('0' <= ch && ch <= '9')
|
|
|
|
|| ('A' <= ch && ch <= 'Z')
|
|
|
|
|| ('a' <= ch && ch <= 'z')
|
|
|
|
|| ch == '.'
|
|
|
|
|| ch == '-'
|
|
|
|
|| ch == '_'
|
|
|
|
|| ch == '~';
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_http_escape_sha1 (char * out, const uint8_t * sha1_digest)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2013-01-24 23:59:52 +00:00
|
|
|
const uint8_t * in = sha1_digest;
|
|
|
|
const uint8_t * end = in + SHA_DIGEST_LENGTH;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2013-01-24 23:59:52 +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
|
|
|
|
2013-01-24 23:59:52 +00:00
|
|
|
*out = '\0';
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|