2008-04-25 18:35:48 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2008-2010 Mnemosyne LLC
|
2008-04-24 01:42:53 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
2009-08-10 20:04:08 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-04-24 01:42:53 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2008-04-25 19:46:36 +00:00
|
|
|
* $Id$
|
2008-04-24 01:42:53 +00:00
|
|
|
*/
|
|
|
|
|
2010-01-04 20:06:39 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
#include <curl/curl.h>
|
2009-12-13 17:54:01 +00:00
|
|
|
#include <event.h>
|
2010-01-22 02:40:11 +00:00
|
|
|
#include <evdns.h>
|
2008-04-24 01:42:53 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2009-12-13 17:54:01 +00:00
|
|
|
#include "net.h"
|
2010-01-27 06:40:12 +00:00
|
|
|
#include "ptrarray.h"
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "session.h"
|
2009-12-13 17:54:01 +00:00
|
|
|
#include "trevent.h"
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "utils.h"
|
2009-04-13 19:04:21 +00:00
|
|
|
#include "version.h"
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "web.h"
|
|
|
|
|
2009-01-09 19:24:40 +00:00
|
|
|
enum
|
2008-12-26 20:14:47 +00:00
|
|
|
{
|
2009-12-18 17:32:16 +00:00
|
|
|
TR_MEMORY_TRASH = 0xCC,
|
|
|
|
|
2010-01-25 04:26:08 +00:00
|
|
|
DEFAULT_TIMER_MSEC = 1500, /* arbitrary */
|
2010-01-25 04:24:48 +00:00
|
|
|
|
|
|
|
MIN_DNS_CACHE_TIME = 60 * 60 * 24
|
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(...) \
|
|
|
|
do { \
|
|
|
|
fprintf( stderr, __VA_ARGS__ ); \
|
|
|
|
fprintf( stderr, "\n" ); \
|
|
|
|
} while( 0 )
|
|
|
|
#else
|
2008-10-26 15:39:04 +00:00
|
|
|
#define dbgmsg( ... ) \
|
|
|
|
do { \
|
|
|
|
if( tr_deepLoggingIsActive( ) ) \
|
|
|
|
tr_deepLog( __FILE__, __LINE__, "web", __VA_ARGS__ ); \
|
|
|
|
} while( 0 )
|
2008-10-27 18:00:03 +00:00
|
|
|
#endif
|
2008-04-25 02:57:33 +00:00
|
|
|
|
2010-01-04 20:06:39 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
struct tr_web
|
|
|
|
{
|
2009-01-09 19:24:40 +00:00
|
|
|
tr_bool closing;
|
2009-12-14 14:25:22 +00:00
|
|
|
tr_bool haveAddr;
|
2009-12-14 05:11:33 +00:00
|
|
|
int taskCount;
|
2009-12-10 19:05:21 +00:00
|
|
|
long timer_msec;
|
2008-10-15 16:43:51 +00:00
|
|
|
CURLM * multi;
|
|
|
|
tr_session * session;
|
2009-10-23 05:48:56 +00:00
|
|
|
tr_address addr;
|
2010-01-27 06:40:12 +00:00
|
|
|
tr_ptrArray dns_cache;
|
2008-10-17 20:57:54 +00:00
|
|
|
struct event timer_event;
|
2008-04-24 01:42:53 +00:00
|
|
|
};
|
|
|
|
|
2010-01-22 02:40:11 +00:00
|
|
|
struct dns_cache_item;
|
|
|
|
static void dns_cache_item_free( struct dns_cache_item * );
|
|
|
|
|
2009-12-14 12:54:30 +00:00
|
|
|
static void
|
2009-12-14 14:25:22 +00:00
|
|
|
web_free( tr_web * g )
|
2009-12-14 12:54:30 +00:00
|
|
|
{
|
2010-01-22 03:39:21 +00:00
|
|
|
evdns_shutdown( TRUE );
|
2009-12-14 12:54:30 +00:00
|
|
|
curl_multi_cleanup( g->multi );
|
|
|
|
evtimer_del( &g->timer_event );
|
2010-01-28 13:33:40 +00:00
|
|
|
tr_ptrArrayDestruct( &g->dns_cache, (PtrArrayForeachFunc)dns_cache_item_free );
|
2009-12-18 17:32:16 +00:00
|
|
|
memset( g, TR_MEMORY_TRASH, sizeof( struct tr_web ) );
|
2009-12-14 12:54:30 +00:00
|
|
|
tr_free( g );
|
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
struct tr_web_task
|
|
|
|
{
|
2010-01-23 07:51:51 +00:00
|
|
|
int port;
|
2008-10-15 16:43:51 +00:00
|
|
|
unsigned long tag;
|
2010-01-22 07:27:19 +00:00
|
|
|
struct curl_slist * slist;
|
2008-10-15 16:43:51 +00:00
|
|
|
struct evbuffer * response;
|
|
|
|
char * url;
|
2010-01-22 02:40:11 +00:00
|
|
|
char * host;
|
|
|
|
const char * resolved_host;
|
2008-10-15 16:43:51 +00:00
|
|
|
char * range;
|
|
|
|
tr_session * session;
|
|
|
|
tr_web_done_func * done_func;
|
|
|
|
void * done_func_user_data;
|
2010-01-08 21:45:56 +00:00
|
|
|
struct event timer_event;
|
|
|
|
CURL * easy;
|
2010-01-09 07:27:45 +00:00
|
|
|
CURLM * multi;
|
2010-01-26 07:22:50 +00:00
|
|
|
tr_bool timer_event_isSet;
|
2008-04-24 01:42:53 +00:00
|
|
|
};
|
|
|
|
|
2009-12-14 12:54:30 +00:00
|
|
|
static void
|
|
|
|
task_free( struct tr_web_task * task )
|
|
|
|
{
|
2010-01-22 07:27:19 +00:00
|
|
|
if( task->slist != NULL )
|
|
|
|
curl_slist_free_all( task->slist );
|
2010-01-26 07:22:50 +00:00
|
|
|
if( task->timer_event_isSet )
|
|
|
|
evtimer_del( &task->timer_event );
|
2009-12-14 12:54:30 +00:00
|
|
|
evbuffer_free( task->response );
|
2010-01-22 02:40:11 +00:00
|
|
|
tr_free( task->host );
|
2009-12-14 12:54:30 +00:00
|
|
|
tr_free( task->range );
|
|
|
|
tr_free( task->url );
|
2009-12-18 17:32:16 +00:00
|
|
|
memset( task, TR_MEMORY_TRASH, sizeof( struct tr_web_task ) );
|
2009-12-14 12:54:30 +00:00
|
|
|
tr_free( task );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-01-22 02:40:11 +00:00
|
|
|
struct dns_cache_item
|
|
|
|
{
|
|
|
|
char * host;
|
|
|
|
char * resolved_host;
|
|
|
|
time_t expiration;
|
2010-01-27 06:40:12 +00:00
|
|
|
tr_bool success;
|
2010-01-22 02:40:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
dns_cache_item_free( struct dns_cache_item * item )
|
|
|
|
{
|
2010-01-28 18:25:18 +00:00
|
|
|
if( item != NULL )
|
|
|
|
{
|
|
|
|
tr_free( item->host );
|
|
|
|
tr_free( item->resolved_host );
|
|
|
|
memset( item, TR_MEMORY_TRASH, sizeof( struct dns_cache_item ) );
|
|
|
|
tr_free( item );
|
|
|
|
}
|
2010-01-22 02:40:11 +00:00
|
|
|
}
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
static int
|
|
|
|
dns_cache_compare( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const struct dns_cache_item * a = va;
|
|
|
|
const struct dns_cache_item * b = vb;
|
|
|
|
return strcmp( a->host, b->host );
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
TR_DNS_OK,
|
|
|
|
TR_DNS_FAIL,
|
|
|
|
TR_DNS_UNTESTED
|
|
|
|
}
|
|
|
|
tr_dns_result;
|
|
|
|
|
2010-01-28 18:25:18 +00:00
|
|
|
static void
|
|
|
|
dns_cache_clear_entry( struct tr_ptrArray * cache, const char * host )
|
|
|
|
{
|
|
|
|
struct dns_cache_item key;
|
|
|
|
key.host = (char*) host;
|
|
|
|
dns_cache_item_free( tr_ptrArrayRemoveSorted( cache, &key, dns_cache_compare ) );
|
|
|
|
}
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
static tr_dns_result
|
|
|
|
dns_cache_lookup( struct tr_web_task * task, const char * host, const char ** resolved )
|
|
|
|
{
|
|
|
|
tr_dns_result result = TR_DNS_UNTESTED;
|
|
|
|
|
|
|
|
if( task->session->web != NULL )
|
2010-01-22 02:40:11 +00:00
|
|
|
{
|
2010-01-28 18:25:18 +00:00
|
|
|
struct dns_cache_item key;
|
|
|
|
struct dns_cache_item * item;
|
2010-01-27 06:40:12 +00:00
|
|
|
tr_ptrArray * cache = &task->session->web->dns_cache;
|
|
|
|
|
2010-01-28 18:25:18 +00:00
|
|
|
key.host = (char*) host;
|
|
|
|
item = tr_ptrArrayFindSorted( cache, &key, dns_cache_compare );
|
2010-01-22 02:40:11 +00:00
|
|
|
|
|
|
|
/* has the ttl expired? */
|
2010-01-27 06:40:12 +00:00
|
|
|
if( ( item != NULL ) && ( item->expiration <= tr_time( ) ) )
|
|
|
|
{
|
2010-01-28 18:25:18 +00:00
|
|
|
dns_cache_clear_entry( cache, host );
|
2010-01-22 02:40:11 +00:00
|
|
|
item = NULL;
|
|
|
|
}
|
2010-01-27 06:40:12 +00:00
|
|
|
|
|
|
|
if( item != NULL )
|
|
|
|
{
|
|
|
|
result = item->success ? TR_DNS_OK : TR_DNS_FAIL;
|
|
|
|
|
|
|
|
if( result == TR_DNS_OK )
|
|
|
|
{
|
|
|
|
*resolved = item->resolved_host;
|
|
|
|
|
|
|
|
dbgmsg( "found cached dns entry for \"%s\": %s", host, *resolved );
|
|
|
|
}
|
|
|
|
}
|
2010-01-22 02:40:11 +00:00
|
|
|
}
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
2010-01-22 02:40:11 +00:00
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
static void
|
|
|
|
dns_cache_set_fail( struct tr_web_task * task, const char * host )
|
|
|
|
{
|
|
|
|
if( task->session->web != NULL )
|
|
|
|
{
|
2010-01-28 18:25:18 +00:00
|
|
|
struct dns_cache_item * item;
|
|
|
|
tr_ptrArray * cache = &task->session->web->dns_cache;
|
|
|
|
|
|
|
|
dns_cache_clear_entry( cache, host );
|
|
|
|
|
|
|
|
item = tr_new( struct dns_cache_item, 1 );
|
2010-01-27 06:40:12 +00:00
|
|
|
item->host = tr_strdup( host );
|
|
|
|
item->resolved_host = NULL;
|
|
|
|
item->expiration = tr_time( ) + MIN_DNS_CACHE_TIME;
|
|
|
|
item->success = FALSE;
|
2010-01-28 18:25:18 +00:00
|
|
|
tr_ptrArrayInsertSorted( cache, item, dns_cache_compare );
|
2010-01-27 06:40:12 +00:00
|
|
|
}
|
2010-01-22 02:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char*
|
2010-01-27 06:40:12 +00:00
|
|
|
dns_cache_set_name( struct tr_web_task * task, const char * host,
|
|
|
|
const char * resolved, int ttl )
|
2010-01-22 02:40:11 +00:00
|
|
|
{
|
|
|
|
char * ret = NULL;
|
|
|
|
|
2010-01-25 04:24:48 +00:00
|
|
|
ttl = MAX( MIN_DNS_CACHE_TIME, ttl );
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
if( task->session->web != NULL )
|
2010-01-22 02:40:11 +00:00
|
|
|
{
|
2010-01-28 18:25:18 +00:00
|
|
|
struct dns_cache_item * item;
|
|
|
|
tr_ptrArray * cache = &task->session->web->dns_cache;
|
|
|
|
|
|
|
|
dns_cache_clear_entry( cache, host );
|
|
|
|
|
|
|
|
item = tr_new( struct dns_cache_item, 1 );
|
2010-01-22 02:40:11 +00:00
|
|
|
item->host = tr_strdup( host );
|
|
|
|
item->resolved_host = tr_strdup( resolved );
|
|
|
|
item->expiration = tr_time( ) + ttl;
|
2010-01-27 06:40:12 +00:00
|
|
|
item->success = TRUE;
|
2010-01-28 18:25:18 +00:00
|
|
|
tr_ptrArrayInsertSorted( cache, item, dns_cache_compare );
|
2010-01-22 02:40:11 +00:00
|
|
|
ret = item->resolved_host;
|
|
|
|
dbgmsg( "adding dns cache entry for \"%s\": %s", host, resolved );
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-25 18:35:48 +00:00
|
|
|
static size_t
|
2009-09-25 21:05:59 +00:00
|
|
|
writeFunc( void * ptr, size_t size, size_t nmemb, void * vtask )
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2008-04-25 18:35:48 +00:00
|
|
|
const size_t byteCount = size * nmemb;
|
2009-09-25 21:05:59 +00:00
|
|
|
struct tr_web_task * task = vtask;
|
|
|
|
evbuffer_add( task->response, ptr, byteCount );
|
2008-10-16 05:24:57 +00:00
|
|
|
dbgmsg( "wrote %zu bytes to task %p's buffer", byteCount, task );
|
2008-04-25 18:35:48 +00:00
|
|
|
return byteCount;
|
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2009-12-28 23:25:50 +00:00
|
|
|
static int
|
2009-12-02 05:30:46 +00:00
|
|
|
sockoptfunction( void * vtask, curl_socket_t fd, curlsocktype purpose UNUSED )
|
|
|
|
{
|
|
|
|
struct tr_web_task * task = vtask;
|
|
|
|
const tr_bool isScrape = strstr( task->url, "scrape" ) != NULL;
|
|
|
|
const tr_bool isAnnounce = strstr( task->url, "announce" ) != NULL;
|
|
|
|
|
2010-01-04 20:06:39 +00:00
|
|
|
/* announce and scrape requests have tiny payloads. */
|
2009-12-02 05:30:46 +00:00
|
|
|
if( isScrape || isAnnounce )
|
|
|
|
{
|
2009-12-13 19:33:02 +00:00
|
|
|
const int sndbuf = 1024;
|
|
|
|
const int rcvbuf = isScrape ? 2048 : 3072;
|
|
|
|
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
|
|
|
|
|
|
|
/* return nonzero if this function encountered an error */
|
|
|
|
return 0;
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
|
|
|
|
2009-12-14 05:11:33 +00:00
|
|
|
static int
|
|
|
|
getCurlProxyType( tr_proxy_type t )
|
|
|
|
{
|
|
|
|
if( t == TR_PROXY_SOCKS4 ) return CURLPROXY_SOCKS4;
|
|
|
|
if( t == TR_PROXY_SOCKS5 ) return CURLPROXY_SOCKS5;
|
|
|
|
return CURLPROXY_HTTP;
|
|
|
|
}
|
|
|
|
|
2009-12-13 17:54:01 +00:00
|
|
|
static int
|
|
|
|
getTimeoutFromURL( const char * url )
|
|
|
|
{
|
2009-12-14 05:11:33 +00:00
|
|
|
if( strstr( url, "scrape" ) != NULL ) return 20;
|
2010-01-24 23:32:49 +00:00
|
|
|
if( strstr( url, "announce" ) != NULL ) return 45;
|
2009-12-13 17:54:01 +00:00
|
|
|
return 240;
|
|
|
|
}
|
|
|
|
|
2010-01-08 21:45:56 +00:00
|
|
|
static void task_timeout_cb( int fd UNUSED, short what UNUSED, void * task );
|
2010-01-26 07:22:50 +00:00
|
|
|
static void task_finish( struct tr_web_task * task, long response_code );
|
2010-01-08 21:45:56 +00:00
|
|
|
|
2008-04-25 18:35:48 +00:00
|
|
|
static void
|
|
|
|
addTask( void * vtask )
|
|
|
|
{
|
|
|
|
struct tr_web_task * task = vtask;
|
2008-12-14 11:21:11 +00:00
|
|
|
const tr_session * session = task->session;
|
2008-04-27 18:27:32 +00:00
|
|
|
|
2010-01-26 07:22:50 +00:00
|
|
|
if( ( session == NULL ) || ( session->web == NULL ) )
|
|
|
|
return;
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
if( !task->resolved_host )
|
2010-01-26 07:22:50 +00:00
|
|
|
{
|
|
|
|
dbgmsg( "couldn't resolve host for \"%s\"... task failed", task->url );
|
|
|
|
task_finish( task, 0 );
|
|
|
|
}
|
|
|
|
else
|
2008-04-27 18:27:32 +00:00
|
|
|
{
|
2009-12-14 12:54:30 +00:00
|
|
|
CURL * e = curl_easy_init( );
|
2008-06-10 16:16:31 +00:00
|
|
|
struct tr_web * web = session->web;
|
2010-01-08 21:45:56 +00:00
|
|
|
const int timeout = getTimeoutFromURL( task->url );
|
2009-12-13 17:54:01 +00:00
|
|
|
const long verbose = getenv( "TR_CURL_VERBOSE" ) != NULL;
|
2009-12-13 19:33:02 +00:00
|
|
|
const char * user_agent = TR_NAME "/" LONG_VERSION_STRING;
|
2010-01-22 02:40:11 +00:00
|
|
|
char * url = NULL;
|
|
|
|
|
2010-01-26 07:22:50 +00:00
|
|
|
/* insert the resolved host into the URL s.t. curl's DNS won't block
|
|
|
|
* even if -- like on most OSes -- it wasn't built with C-Ares :(
|
|
|
|
* "http://www.craptrackular.org/announce?key=val&key2=..." becomes
|
|
|
|
* "http://127.0.0.1/announce?key=val&key2=..." */
|
2010-01-22 02:40:11 +00:00
|
|
|
{
|
2010-01-22 07:27:19 +00:00
|
|
|
char * host;
|
2010-01-22 02:40:11 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
|
|
|
char * pch = strstr( task->url, task->host );
|
|
|
|
char * tail = pch + strlen( task->host );
|
|
|
|
evbuffer_add( buf, task->url, pch - task->url );
|
|
|
|
evbuffer_add_printf( buf, "%s", task->resolved_host );
|
|
|
|
evbuffer_add_printf( buf, "%s", tail );
|
|
|
|
url = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
|
|
|
|
dbgmsg( "old url: \"%s\" -- new url: \"%s\"", task->url, url );
|
|
|
|
evbuffer_free( buf );
|
2010-01-22 07:27:19 +00:00
|
|
|
|
2010-01-26 07:22:50 +00:00
|
|
|
/* Manually add a Host: argument that refers to the true URL */
|
2010-01-24 19:22:22 +00:00
|
|
|
if( ( ( task->port <= 0 ) ) ||
|
|
|
|
( ( task->port == 80 ) && !strncmp( task->url, "http://", 7 ) ) ||
|
|
|
|
( ( task->port == 443 ) && !strncmp( task->url, "https://", 8 ) ) )
|
|
|
|
host = tr_strdup_printf( "Host: %s", task->host );
|
|
|
|
else
|
|
|
|
host = tr_strdup_printf( "Host: %s:%d", task->host, task->port );
|
|
|
|
|
2010-01-22 07:27:19 +00:00
|
|
|
task->slist = curl_slist_append( NULL, host );
|
|
|
|
curl_easy_setopt( e, CURLOPT_HTTPHEADER, task->slist );
|
|
|
|
tr_free( host );
|
2010-01-22 02:40:11 +00:00
|
|
|
}
|
2008-08-06 23:33:29 +00:00
|
|
|
|
2010-01-22 02:40:11 +00:00
|
|
|
dbgmsg( "adding task #%lu [%s]", task->tag, url ? url : task->url );
|
2008-04-27 18:27:32 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
if( !task->range && session->isProxyEnabled ) {
|
2009-12-14 12:54:30 +00:00
|
|
|
curl_easy_setopt( e, CURLOPT_PROXY, session->proxy );
|
|
|
|
curl_easy_setopt( e, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
|
|
|
|
curl_easy_setopt( e, CURLOPT_PROXYPORT, session->proxyPort );
|
|
|
|
curl_easy_setopt( e, CURLOPT_PROXYTYPE,
|
2008-10-17 20:57:54 +00:00
|
|
|
getCurlProxyType( session->proxyType ) );
|
2008-06-10 16:16:31 +00:00
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
if( !task->range && session->isProxyAuthEnabled ) {
|
2008-10-17 20:57:54 +00:00
|
|
|
char * str = tr_strdup_printf( "%s:%s", session->proxyUsername,
|
|
|
|
session->proxyPassword );
|
2009-12-14 12:54:30 +00:00
|
|
|
curl_easy_setopt( e, CURLOPT_PROXYUSERPWD, str );
|
2008-06-10 16:16:31 +00:00
|
|
|
tr_free( str );
|
|
|
|
}
|
2010-01-08 21:45:56 +00:00
|
|
|
|
|
|
|
task->easy = e;
|
2010-01-09 07:27:45 +00:00
|
|
|
task->multi = web->multi;
|
2010-01-08 21:45:56 +00:00
|
|
|
|
|
|
|
/* use our own timeout instead of CURLOPT_TIMEOUT because the latter
|
|
|
|
* doesn't play nicely with curl_multi. See curl bug #2501457 */
|
2010-01-26 07:22:50 +00:00
|
|
|
task->timer_event_isSet = TRUE;
|
2010-01-08 21:45:56 +00:00
|
|
|
evtimer_set( &task->timer_event, task_timeout_cb, task );
|
|
|
|
tr_timerAdd( &task->timer_event, timeout, 0 );
|
2008-06-10 16:16:31 +00:00
|
|
|
|
2009-12-14 12:54:30 +00:00
|
|
|
curl_easy_setopt( e, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
|
|
|
|
curl_easy_setopt( e, CURLOPT_SOCKOPTFUNCTION, sockoptfunction );
|
|
|
|
curl_easy_setopt( e, CURLOPT_SOCKOPTDATA, task );
|
|
|
|
curl_easy_setopt( e, CURLOPT_WRITEDATA, task );
|
|
|
|
curl_easy_setopt( e, CURLOPT_WRITEFUNCTION, writeFunc );
|
2010-01-25 04:24:48 +00:00
|
|
|
curl_easy_setopt( e, CURLOPT_DNS_CACHE_TIMEOUT, MIN_DNS_CACHE_TIME );
|
2009-12-14 12:54:30 +00:00
|
|
|
curl_easy_setopt( e, CURLOPT_FOLLOWLOCATION, 1L );
|
|
|
|
curl_easy_setopt( e, CURLOPT_AUTOREFERER, 1L );
|
|
|
|
curl_easy_setopt( e, CURLOPT_FORBID_REUSE, 1L );
|
|
|
|
curl_easy_setopt( e, CURLOPT_MAXREDIRS, -1L );
|
|
|
|
curl_easy_setopt( e, CURLOPT_NOSIGNAL, 1L );
|
|
|
|
curl_easy_setopt( e, CURLOPT_PRIVATE, task );
|
|
|
|
curl_easy_setopt( e, CURLOPT_SSL_VERIFYHOST, 0L );
|
|
|
|
curl_easy_setopt( e, CURLOPT_SSL_VERIFYPEER, 0L );
|
2010-01-22 02:40:11 +00:00
|
|
|
curl_easy_setopt( e, CURLOPT_URL, url ? url : task->url );
|
2009-12-14 12:54:30 +00:00
|
|
|
curl_easy_setopt( e, CURLOPT_USERAGENT, user_agent );
|
|
|
|
curl_easy_setopt( e, CURLOPT_VERBOSE, verbose );
|
2009-12-13 17:54:01 +00:00
|
|
|
if( web->haveAddr )
|
2009-12-14 12:54:30 +00:00
|
|
|
curl_easy_setopt( e, CURLOPT_INTERFACE, tr_ntop_non_ts( &web->addr ) );
|
2008-06-07 21:26:41 +00:00
|
|
|
if( task->range )
|
2009-12-14 12:54:30 +00:00
|
|
|
curl_easy_setopt( e, CURLOPT_RANGE, task->range );
|
2008-10-16 05:24:57 +00:00
|
|
|
|
2009-12-14 17:17:05 +00:00
|
|
|
if( curl_multi_add_handle( web->multi, e ) == CURLM_OK )
|
|
|
|
++web->taskCount;
|
2010-01-22 02:40:11 +00:00
|
|
|
|
|
|
|
tr_free( url );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dns_ipv6_done_cb( int err, char type, int count, int ttl, void * addresses, void * vtask )
|
|
|
|
{
|
|
|
|
struct tr_web_task * task = vtask;
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
if( !err && task->host && ( count>0 ) && ( ttl>=0 ) && ( type==DNS_IPv6_AAAA ) )
|
2010-01-22 02:40:11 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char buf[INET6_ADDRSTRLEN+1];
|
|
|
|
struct in6_addr *in6_addrs = addresses;
|
|
|
|
|
|
|
|
for( i=0; i<count; ++i ) {
|
|
|
|
const char * b = inet_ntop(AF_INET6, &in6_addrs[i], buf,sizeof(buf));
|
|
|
|
if( b != NULL ) {
|
|
|
|
/* FIXME: is there a better way to tell which one to use if count > 1? */
|
2010-01-27 06:40:12 +00:00
|
|
|
task->resolved_host = dns_cache_set_name( task, task->host, b, ttl );
|
2010-01-22 02:40:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
if( task->resolved_host == NULL )
|
|
|
|
dns_cache_set_fail( task, task->host );
|
|
|
|
|
2010-01-22 02:40:11 +00:00
|
|
|
addTask( task );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dns_ipv4_done_cb( int err, char type, int count, int ttl, void * addresses, void * vtask )
|
|
|
|
{
|
|
|
|
struct tr_web_task * task = vtask;
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
if( !err && task->host && ( count>0 ) && ( ttl>=0 ) && ( type==DNS_IPv4_A ) )
|
2010-01-22 02:40:11 +00:00
|
|
|
{
|
|
|
|
struct in_addr * in_addrs = addresses;
|
|
|
|
const char * resolved = inet_ntoa( in_addrs[0] );
|
2010-01-27 06:40:12 +00:00
|
|
|
task->resolved_host = dns_cache_set_name( task, task->host, resolved, ttl );
|
2010-01-22 02:40:11 +00:00
|
|
|
/* FIXME: if count > 1, is there a way to decide which is best to use? */
|
2008-04-27 18:27:32 +00:00
|
|
|
}
|
2010-01-22 02:40:11 +00:00
|
|
|
|
2010-01-25 04:00:08 +00:00
|
|
|
if( ( task->resolved_host != NULL )
|
|
|
|
|| ( task->host == NULL )
|
|
|
|
|| evdns_resolve_ipv6( task->host, 0, dns_ipv6_done_cb, task ) )
|
2010-01-22 02:40:11 +00:00
|
|
|
dns_ipv6_done_cb( DNS_ERR_UNKNOWN, DNS_IPv6_AAAA, 0, 0, NULL, task );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
doDNS( void * vtask )
|
|
|
|
{
|
2010-01-28 17:24:23 +00:00
|
|
|
tr_address addr;
|
2010-01-23 07:51:51 +00:00
|
|
|
int port = -1;
|
2010-01-22 02:40:11 +00:00
|
|
|
char * host = NULL;
|
|
|
|
struct tr_web_task * task = vtask;
|
2010-01-27 06:40:12 +00:00
|
|
|
tr_dns_result lookup_result = TR_DNS_UNTESTED;
|
2010-01-22 02:40:11 +00:00
|
|
|
|
|
|
|
assert( task->resolved_host == NULL );
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
if( !tr_httpParseURL( task->url, -1, &host, &port, NULL ) )
|
|
|
|
{
|
2010-01-23 07:51:51 +00:00
|
|
|
task->port = port;
|
2010-01-22 02:40:11 +00:00
|
|
|
task->host = host;
|
2010-01-28 17:24:23 +00:00
|
|
|
|
|
|
|
/* If 'host' is an IPv4 or IPv6 address in text form, use it as-is.
|
|
|
|
* Otherwise, see if its resolved name is in our DNS cache */
|
|
|
|
if( tr_pton( task->host, &addr ) != NULL )
|
|
|
|
{
|
|
|
|
task->resolved_host = task->host;
|
|
|
|
lookup_result = TR_DNS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lookup_result = dns_cache_lookup( task, host, &task->resolved_host );
|
|
|
|
}
|
2010-01-22 02:40:11 +00:00
|
|
|
}
|
|
|
|
|
2010-01-27 06:40:12 +00:00
|
|
|
if( lookup_result != TR_DNS_UNTESTED )
|
|
|
|
{
|
|
|
|
addTask( task );
|
|
|
|
}
|
|
|
|
else if( !host || evdns_resolve_ipv4( host, 0, dns_ipv4_done_cb, task ) )
|
|
|
|
{
|
2010-01-22 02:40:11 +00:00
|
|
|
dns_ipv4_done_cb( DNS_ERR_UNKNOWN, DNS_IPv4_A, 0, 0, NULL, task );
|
2010-01-27 06:40:12 +00:00
|
|
|
}
|
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
|
|
|
|
task_finish( struct tr_web_task * task, long response_code )
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2009-12-13 19:33:02 +00:00
|
|
|
dbgmsg( "finished web task %lu; got %ld", task->tag, response_code );
|
2009-09-25 21:05:59 +00:00
|
|
|
|
|
|
|
if( task->done_func != NULL )
|
|
|
|
task->done_func( task->session,
|
|
|
|
response_code,
|
|
|
|
EVBUFFER_DATA( task->response ),
|
|
|
|
EVBUFFER_LENGTH( task->response ),
|
|
|
|
task->done_func_user_data );
|
2008-10-18 15:45:12 +00:00
|
|
|
task_free( task );
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
|
|
|
|
2010-01-08 21:45:56 +00:00
|
|
|
static void
|
|
|
|
remove_task( struct tr_web_task * task )
|
|
|
|
{
|
|
|
|
long code;
|
|
|
|
|
|
|
|
curl_easy_getinfo( task->easy, CURLINFO_RESPONSE_CODE, &code );
|
2010-01-09 07:27:45 +00:00
|
|
|
curl_multi_remove_handle( task->multi, task->easy );
|
2010-01-08 21:45:56 +00:00
|
|
|
curl_easy_cleanup( task->easy );
|
|
|
|
task_finish( task, code );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
task_timeout_cb( int fd UNUSED, short what UNUSED, void * task )
|
|
|
|
{
|
|
|
|
remove_task( task );
|
|
|
|
}
|
|
|
|
|
2008-04-24 19:38:59 +00:00
|
|
|
static void
|
2008-10-18 00:20:37 +00:00
|
|
|
remove_finished_tasks( tr_web * g )
|
2008-07-29 00:51:07 +00:00
|
|
|
{
|
2009-12-13 17:54:01 +00:00
|
|
|
CURLMsg * msg;
|
|
|
|
int msgs_left;
|
2009-01-17 22:49:15 +00:00
|
|
|
|
2009-12-13 17:54:01 +00:00
|
|
|
while(( msg = curl_multi_info_read( g->multi, &msgs_left ))) {
|
|
|
|
if(( msg->msg == CURLMSG_DONE ) && ( msg->easy_handle != NULL )) {
|
2008-10-29 16:06:14 +00:00
|
|
|
struct tr_web_task * task;
|
2009-12-14 12:54:30 +00:00
|
|
|
CURL * e = msg->easy_handle;
|
|
|
|
curl_easy_getinfo( e, CURLINFO_PRIVATE, (void*)&task );
|
2010-01-08 21:45:56 +00:00
|
|
|
assert( e == task->easy );
|
|
|
|
remove_task( task );
|
2008-10-29 16:06:14 +00:00
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
2008-10-18 00:20:37 +00:00
|
|
|
}
|
2008-07-29 00:51:07 +00:00
|
|
|
|
2008-10-18 00:20:37 +00:00
|
|
|
static void
|
|
|
|
restart_timer( tr_web * g )
|
|
|
|
{
|
2009-12-10 19:05:21 +00:00
|
|
|
dbgmsg( "adding a timeout for %.1f seconds from now", g->timer_msec/1000.0 );
|
2009-12-14 05:11:33 +00:00
|
|
|
evtimer_del( &g->timer_event );
|
2009-12-10 19:05:21 +00:00
|
|
|
tr_timerAddMsec( &g->timer_event, g->timer_msec );
|
2008-10-18 00:20:37 +00:00
|
|
|
}
|
2008-10-16 05:24:57 +00:00
|
|
|
|
2009-12-14 14:25:22 +00:00
|
|
|
static void
|
2009-12-14 18:24:26 +00:00
|
|
|
tr_multi_perform( tr_web * g, int fd, int curl_what )
|
2008-10-17 20:57:54 +00:00
|
|
|
{
|
2010-01-04 20:06:39 +00:00
|
|
|
CURLMcode m;
|
2008-10-17 20:57:54 +00:00
|
|
|
|
2009-12-14 05:11:33 +00:00
|
|
|
dbgmsg( "check_run_count: %d taskCount", g->taskCount );
|
2008-10-17 20:57:54 +00:00
|
|
|
|
2008-10-18 00:20:37 +00:00
|
|
|
/* invoke libcurl's processing */
|
2009-12-14 14:25:22 +00:00
|
|
|
do
|
2010-01-04 20:06:39 +00:00
|
|
|
m = curl_multi_socket_action( g->multi, fd, curl_what, &g->taskCount );
|
|
|
|
while( m == CURLM_CALL_MULTI_SOCKET );
|
2008-10-18 00:20:37 +00:00
|
|
|
|
|
|
|
remove_finished_tasks( g );
|
|
|
|
|
2009-12-14 14:25:22 +00:00
|
|
|
if( g->closing && !g->taskCount )
|
|
|
|
web_free( g );
|
2009-12-14 12:54:30 +00:00
|
|
|
else
|
2008-10-18 00:20:37 +00:00
|
|
|
restart_timer( g );
|
2008-10-17 20:57:54 +00:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/* libevent says that sock is ready to be processed, so wake up libcurl */
|
|
|
|
static void
|
2009-12-14 18:24:26 +00:00
|
|
|
event_cb( int fd, short ev_what, void * g )
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2009-12-14 18:24:26 +00:00
|
|
|
int curl_what = 0;
|
|
|
|
if( ev_what & EV_READ ) curl_what |= CURL_POLL_IN;
|
|
|
|
if( ev_what & EV_WRITE ) curl_what |= CURL_POLL_OUT;
|
|
|
|
tr_multi_perform( g, fd, curl_what );
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CURLMOPT_SOCKETFUNCTION */
|
|
|
|
static int
|
2010-01-04 20:06:39 +00:00
|
|
|
sock_cb( CURL * e UNUSED, curl_socket_t fd, int curl_what,
|
2009-12-14 12:54:30 +00:00
|
|
|
void * vweb, void * vevent )
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2009-12-11 15:41:34 +00:00
|
|
|
/*static int num_events = 0;*/
|
2009-02-05 22:00:21 +00:00
|
|
|
struct tr_web * web = vweb;
|
2009-12-11 15:41:34 +00:00
|
|
|
struct event * io_event = vevent;
|
2010-01-04 20:06:39 +00:00
|
|
|
dbgmsg( "sock_cb: curl_what %d, fd %d, io_event %p",
|
|
|
|
curl_what, (int)fd, io_event );
|
2009-02-05 22:00:21 +00:00
|
|
|
|
2010-01-08 21:45:56 +00:00
|
|
|
if( io_event != NULL )
|
|
|
|
event_del( io_event );
|
|
|
|
|
|
|
|
if( curl_what & ( CURL_POLL_IN | CURL_POLL_OUT ) )
|
2009-02-05 22:00:21 +00:00
|
|
|
{
|
2010-01-04 20:06:39 +00:00
|
|
|
const short ev_what = EV_PERSIST
|
|
|
|
| (( curl_what & CURL_POLL_IN ) ? EV_READ : 0 )
|
|
|
|
| (( curl_what & CURL_POLL_OUT ) ? EV_WRITE : 0 );
|
2009-12-11 15:41:34 +00:00
|
|
|
|
2010-01-08 21:45:56 +00:00
|
|
|
if( io_event == NULL ) {
|
2009-12-29 00:00:33 +00:00
|
|
|
io_event = tr_new0( struct event, 1 );
|
2009-12-11 15:41:34 +00:00
|
|
|
curl_multi_assign( web->multi, fd, io_event );
|
|
|
|
/*fprintf( stderr, "+1 io_events to %d\n", ++num_events );*/
|
|
|
|
}
|
|
|
|
|
2010-01-04 20:06:39 +00:00
|
|
|
dbgmsg( "enabling (libevent %hd, libcurl %d) on io_event %p, fd %d",
|
|
|
|
ev_what, curl_what, io_event, fd );
|
|
|
|
event_set( io_event, fd, ev_what, event_cb, web );
|
2010-01-09 07:27:45 +00:00
|
|
|
assert( io_event->ev_base != NULL );
|
2009-12-11 15:41:34 +00:00
|
|
|
event_add( io_event, NULL );
|
2009-02-05 22:00:21 +00:00
|
|
|
}
|
2010-01-08 21:45:56 +00:00
|
|
|
|
|
|
|
if( ( io_event != NULL ) && ( curl_what & CURL_POLL_REMOVE ) )
|
|
|
|
{
|
2010-01-09 07:27:45 +00:00
|
|
|
memset( io_event, TR_MEMORY_TRASH, sizeof( struct event ) );
|
2010-01-08 21:45:56 +00:00
|
|
|
tr_free( io_event );
|
|
|
|
/*fprintf( stderr, "-1 io_events to %d\n", --num_events );*/
|
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
|
2009-12-13 17:54:01 +00:00
|
|
|
return 0; /* libcurl documentation: "The callback MUST return 0." */
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
|
|
|
|
2009-12-10 19:05:21 +00:00
|
|
|
/* libevent says that timer_msec have passed, so wake up libcurl */
|
|
|
|
static void
|
2010-01-04 20:06:39 +00:00
|
|
|
libevent_timer_cb( int fd UNUSED, short what UNUSED, void * g )
|
2009-12-10 19:05:21 +00:00
|
|
|
{
|
|
|
|
dbgmsg( "libevent timer is done" );
|
2009-12-14 18:24:26 +00:00
|
|
|
tr_multi_perform( g, CURL_SOCKET_TIMEOUT, 0 );
|
2009-12-10 19:05:21 +00:00
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
|
2008-10-18 15:45:12 +00:00
|
|
|
/* libcurl documentation: "If 0, it means you should proceed immediately
|
2008-10-18 00:20:37 +00:00
|
|
|
* without waiting for anything. If it returns -1, there's no timeout at all
|
|
|
|
* set ... (but) you must not wait too long (more than a few seconds perhaps)
|
|
|
|
* before you call curl_multi_perform() again." */
|
2008-10-15 16:43:51 +00:00
|
|
|
static void
|
2009-12-10 19:05:21 +00:00
|
|
|
multi_timer_cb( CURLM * multi UNUSED, long timer_msec, void * vg )
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2008-11-04 19:40:18 +00:00
|
|
|
tr_web * g = vg;
|
|
|
|
|
2009-12-14 14:25:22 +00:00
|
|
|
g->timer_msec = timer_msec > 0 ? timer_msec : DEFAULT_TIMER_MSEC;
|
2008-11-04 19:40:18 +00:00
|
|
|
|
2009-12-14 14:25:22 +00:00
|
|
|
if( timer_msec < 1 )
|
2009-12-14 18:24:26 +00:00
|
|
|
tr_multi_perform( g, CURL_SOCKET_TIMEOUT, 0 );
|
2010-01-04 20:06:39 +00:00
|
|
|
else
|
|
|
|
restart_timer( g );
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_webRun( tr_session * session,
|
|
|
|
const char * url,
|
|
|
|
const char * range,
|
|
|
|
tr_web_done_func done_func,
|
|
|
|
void * done_func_user_data )
|
|
|
|
{
|
2009-12-14 12:54:30 +00:00
|
|
|
if( session->web != NULL )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2009-12-10 19:05:21 +00:00
|
|
|
static unsigned long tag = 0;
|
2009-12-14 05:11:33 +00:00
|
|
|
struct tr_web_task * task = tr_new0( struct tr_web_task, 1 );
|
2008-10-15 16:43:51 +00:00
|
|
|
task->session = session;
|
|
|
|
task->url = tr_strdup( url );
|
|
|
|
task->range = tr_strdup( range );
|
|
|
|
task->done_func = done_func;
|
|
|
|
task->done_func_user_data = done_func_user_data;
|
|
|
|
task->tag = ++tag;
|
|
|
|
task->response = evbuffer_new( );
|
2010-01-22 02:40:11 +00:00
|
|
|
tr_runInEventThread( session, doDNS, task );
|
2008-08-06 23:33:29 +00:00
|
|
|
}
|
2008-05-06 15:52:57 +00:00
|
|
|
}
|
2008-04-24 19:38:59 +00:00
|
|
|
|
2009-10-23 05:48:56 +00:00
|
|
|
void
|
|
|
|
tr_webSetInterface( tr_web * web, const tr_address * addr )
|
|
|
|
{
|
|
|
|
if(( web->haveAddr = ( addr != NULL )))
|
|
|
|
web->addr = *addr;
|
|
|
|
}
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
tr_web*
|
2009-10-23 05:48:56 +00:00
|
|
|
tr_webInit( tr_session * session )
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
tr_web * web;
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2009-12-14 12:54:30 +00:00
|
|
|
/* try to enable ssl for https support; but if that fails,
|
2009-08-10 20:04:08 +00:00
|
|
|
* try a plain vanilla init */
|
2009-12-14 12:54:30 +00:00
|
|
|
if( curl_global_init( CURL_GLOBAL_SSL ) )
|
|
|
|
curl_global_init( 0 );
|
2009-08-10 20:04:08 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
web = tr_new0( struct tr_web, 1 );
|
2010-01-27 06:40:12 +00:00
|
|
|
web->dns_cache = TR_PTR_ARRAY_INIT;
|
2008-04-24 01:42:53 +00:00
|
|
|
web->session = session;
|
2009-12-11 15:41:34 +00:00
|
|
|
web->timer_msec = DEFAULT_TIMER_MSEC; /* overwritten by multi_timer_cb() */
|
2009-12-10 19:05:21 +00:00
|
|
|
evtimer_set( &web->timer_event, libevent_timer_cb, web );
|
2009-12-11 15:41:34 +00:00
|
|
|
|
|
|
|
web->multi = curl_multi_init( );
|
2010-01-22 03:39:21 +00:00
|
|
|
|
|
|
|
evdns_init( );
|
|
|
|
|
2009-12-10 19:05:21 +00:00
|
|
|
curl_multi_setopt( web->multi, CURLMOPT_SOCKETDATA, web );
|
|
|
|
curl_multi_setopt( web->multi, CURLMOPT_SOCKETFUNCTION, sock_cb );
|
|
|
|
curl_multi_setopt( web->multi, CURLMOPT_TIMERDATA, web );
|
|
|
|
curl_multi_setopt( web->multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb );
|
2008-04-24 01:42:53 +00:00
|
|
|
|
|
|
|
return web;
|
|
|
|
}
|
2008-04-25 04:26:04 +00:00
|
|
|
|
2008-04-25 19:46:36 +00:00
|
|
|
void
|
2008-05-06 15:52:57 +00:00
|
|
|
tr_webClose( tr_web ** web_in )
|
2008-04-25 19:46:36 +00:00
|
|
|
{
|
2008-05-06 15:52:57 +00:00
|
|
|
tr_web * web = *web_in;
|
|
|
|
*web_in = NULL;
|
2009-12-14 05:11:33 +00:00
|
|
|
if( web->taskCount < 1 )
|
2009-12-14 14:25:22 +00:00
|
|
|
web_free( web );
|
2008-08-06 23:33:29 +00:00
|
|
|
else
|
2009-01-09 19:24:40 +00:00
|
|
|
web->closing = 1;
|
2008-04-25 19:46:36 +00:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/*****
|
|
|
|
******
|
|
|
|
******
|
|
|
|
*****/
|
2008-04-25 04:26:04 +00:00
|
|
|
|
|
|
|
const char *
|
|
|
|
tr_webGetResponseStr( long code )
|
|
|
|
{
|
2009-12-14 12:54:30 +00:00
|
|
|
switch( code )
|
|
|
|
{
|
|
|
|
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";
|
|
|
|
}
|
2008-04-25 04:26:04 +00:00
|
|
|
}
|
2009-11-10 17:03:23 +00:00
|
|
|
|
|
|
|
void
|
2009-12-14 14:25:22 +00:00
|
|
|
tr_http_escape( struct evbuffer * out,
|
|
|
|
const char * str, int len, tr_bool escape_slashes )
|
2009-11-10 17:03:23 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2009-11-29 08:05:47 +00:00
|
|
|
if( ( len < 0 ) && ( str != NULL ) )
|
|
|
|
len = strlen( str );
|
|
|
|
|
2009-11-10 17:03:23 +00:00
|
|
|
for( i = 0; i < len; i++ ) {
|
2010-01-10 14:56:04 +00:00
|
|
|
if( str[i] == ',' || str[i] == '-' || str[i] == '.'
|
|
|
|
|| ( '0' <= str[i] && str[i] <= '9' )
|
|
|
|
|| ( 'A' <= str[i] && str[i] <= 'Z' )
|
2010-01-10 16:26:30 +00:00
|
|
|
|| ( 'a' <= str[i] && str[i] <= 'z' )
|
2010-01-10 14:56:04 +00:00
|
|
|
|| ( str[i] == '/' && !escape_slashes ) )
|
2009-11-10 17:03:23 +00:00
|
|
|
evbuffer_add( out, &str[i], 1 );
|
2010-01-10 14:56:04 +00:00
|
|
|
else
|
2009-11-10 17:03:23 +00:00
|
|
|
evbuffer_add_printf( out, "%%%02X", (unsigned)(str[i]&0xFF) );
|
|
|
|
}
|
|
|
|
}
|
2009-11-20 04:38:19 +00:00
|
|
|
|
2009-12-14 05:11:33 +00:00
|
|
|
char *
|
2009-11-20 04:38:19 +00:00
|
|
|
tr_http_unescape( const char * str, int len )
|
|
|
|
{
|
|
|
|
char * tmp = curl_unescape( str, len );
|
|
|
|
char * ret = tr_strdup( tmp );
|
|
|
|
curl_free( tmp );
|
|
|
|
return ret;
|
|
|
|
}
|