2008-04-25 18:35:48 +00:00
|
|
|
/*
|
2009-01-10 23:09:07 +00:00
|
|
|
* This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com>
|
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
|
|
|
*/
|
|
|
|
|
2008-05-06 15:52:57 +00:00
|
|
|
#include <assert.h>
|
2008-04-25 04:26:04 +00:00
|
|
|
#include <stdlib.h> /* bsearch */
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
#include <event.h>
|
2009-01-24 00:25:11 +00:00
|
|
|
|
|
|
|
#define CURL_DISABLE_TYPECHECK /* otherwise -Wunreachable-code goes insane */
|
2008-04-24 01:42:53 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2008-10-15 16:43:51 +00:00
|
|
|
#include "list.h"
|
2008-10-20 19:34:19 +00:00
|
|
|
#include "net.h" /* socklen_t */
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "session.h"
|
2008-04-25 18:35:48 +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-02-18 16:16:24 +00:00
|
|
|
/* Use curl_multi_socket_action() instead of curl_multi_perform()
|
|
|
|
if libcurl >= 7.18.2. See http://trac.transmissionbt.com/ticket/1844 */
|
|
|
|
#if LIBCURL_VERSION_NUM >= 0x071202
|
|
|
|
#define USE_CURL_MULTI_SOCKET_ACTION
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-01-09 19:24:40 +00:00
|
|
|
enum
|
2008-12-26 20:14:47 +00:00
|
|
|
{
|
|
|
|
/* arbitrary number */
|
2009-01-09 19:24:40 +00:00
|
|
|
MAX_CONCURRENT_TASKS = 100,
|
2008-10-16 05:24:57 +00:00
|
|
|
|
2008-12-26 20:14:47 +00:00
|
|
|
/* arbitrary number */
|
2009-01-09 19:24:40 +00:00
|
|
|
DEFAULT_TIMER_MSEC = 2500
|
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
|
|
|
|
2009-02-05 22:00:21 +00:00
|
|
|
struct tr_web_sockinfo
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
tr_bool evset;
|
|
|
|
struct event ev;
|
|
|
|
};
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
struct tr_web
|
|
|
|
{
|
2009-01-09 19:24:40 +00:00
|
|
|
tr_bool closing;
|
2008-10-15 16:43:51 +00:00
|
|
|
int prev_running;
|
|
|
|
int still_running;
|
2008-10-17 20:57:54 +00:00
|
|
|
long timer_ms;
|
2008-10-15 16:43:51 +00:00
|
|
|
CURLM * multi;
|
|
|
|
tr_session * session;
|
2008-10-17 20:57:54 +00:00
|
|
|
struct event timer_event;
|
2009-04-05 13:00:17 +00:00
|
|
|
tr_list * easy_queue;
|
2009-02-05 22:00:21 +00:00
|
|
|
tr_list * fds;
|
2008-04-24 01:42:53 +00:00
|
|
|
};
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-02-05 22:00:21 +00:00
|
|
|
static struct tr_web_sockinfo *
|
|
|
|
getSockinfo( tr_web * web, int fd, tr_bool createIfMissing )
|
|
|
|
{
|
2009-05-13 19:04:14 +00:00
|
|
|
tr_list * l;
|
2009-02-05 22:00:21 +00:00
|
|
|
|
|
|
|
for( l=web->fds; l!=NULL; l=l->next ) {
|
|
|
|
struct tr_web_sockinfo * s = l->data;
|
|
|
|
if( s->fd == fd ) {
|
|
|
|
dbgmsg( "looked up sockinfo %p for fd %d", s, fd );
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( createIfMissing ) {
|
|
|
|
struct tr_web_sockinfo * s = tr_new0( struct tr_web_sockinfo, 1 );
|
|
|
|
s->fd = fd;
|
|
|
|
tr_list_prepend( &web->fds, s );
|
|
|
|
dbgmsg( "created sockinfo %p for fd %d... we now have %d sockinfos", s, fd, tr_list_size(web->fds) );
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clearSockinfoEvent( struct tr_web_sockinfo * s )
|
|
|
|
{
|
|
|
|
if( s && s->evset )
|
|
|
|
{
|
|
|
|
dbgmsg( "clearing libevent polling for sockinfo %p, fd %d", s, s->fd );
|
|
|
|
event_del( &s->ev );
|
|
|
|
s->evset = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
purgeSockinfo( tr_web * web, int fd )
|
|
|
|
{
|
|
|
|
struct tr_web_sockinfo * s = getSockinfo( web, fd, FALSE );
|
|
|
|
|
|
|
|
if( s != NULL )
|
|
|
|
{
|
|
|
|
tr_list_remove_data( &web->fds, s );
|
|
|
|
clearSockinfoEvent( s );
|
|
|
|
dbgmsg( "freeing sockinfo %p, fd %d", s, s->fd );
|
|
|
|
tr_free( s );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
struct tr_web_task
|
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
unsigned long tag;
|
|
|
|
struct evbuffer * response;
|
|
|
|
char * url;
|
|
|
|
char * range;
|
|
|
|
tr_session * session;
|
|
|
|
tr_web_done_func * done_func;
|
|
|
|
void * done_func_user_data;
|
2008-04-24 01:42:53 +00:00
|
|
|
};
|
|
|
|
|
2008-04-25 18:35:48 +00:00
|
|
|
static size_t
|
2008-10-15 16:43:51 +00:00
|
|
|
writeFunc( void * ptr, size_t size, size_t nmemb, void * task )
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2008-04-25 18:35:48 +00:00
|
|
|
const size_t byteCount = size * nmemb;
|
2008-10-15 16:43:51 +00:00
|
|
|
evbuffer_add( ((struct tr_web_task*)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
|
|
|
|
2008-06-11 20:45:53 +00:00
|
|
|
static int
|
|
|
|
getCurlProxyType( tr_proxy_type t )
|
|
|
|
{
|
|
|
|
switch( t )
|
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
case TR_PROXY_SOCKS4: return CURLPROXY_SOCKS4;
|
|
|
|
case TR_PROXY_SOCKS5: return CURLPROXY_SOCKS5;
|
|
|
|
default: return CURLPROXY_HTTP;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-11 20:45:53 +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
|
|
|
|
2008-06-10 16:16:31 +00:00
|
|
|
if( session && session->web )
|
2008-04-27 18:27:32 +00:00
|
|
|
{
|
2008-06-10 16:16:31 +00:00
|
|
|
struct tr_web * web = session->web;
|
2008-10-16 05:24:57 +00:00
|
|
|
CURL * easy;
|
2008-08-06 23:33:29 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
dbgmsg( "adding task #%lu [%s]", task->tag, task->url );
|
2008-04-27 18:27:32 +00:00
|
|
|
|
2008-10-16 05:24:57 +00:00
|
|
|
easy = curl_easy_init( );
|
2008-06-10 16:16:31 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
if( !task->range && session->isProxyEnabled ) {
|
2008-10-16 05:24:57 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_PROXY, session->proxy );
|
2008-10-18 15:45:12 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
|
2008-10-16 05:24:57 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_PROXYPORT, session->proxyPort );
|
2008-10-17 20:57:54 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_PROXYTYPE,
|
|
|
|
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 );
|
2008-10-16 05:24:57 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_PROXYUSERPWD, str );
|
2008-06-10 16:16:31 +00:00
|
|
|
tr_free( str );
|
|
|
|
}
|
|
|
|
|
2009-02-07 00:20:47 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
|
2009-01-17 22:49:15 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_DNS_CACHE_TIMEOUT, 360L );
|
|
|
|
curl_easy_setopt( easy, CURLOPT_CONNECTTIMEOUT, 60L );
|
2008-10-29 16:06:14 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_FOLLOWLOCATION, 1L );
|
2009-01-16 17:29:42 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_FORBID_REUSE, 1L );
|
2009-01-17 22:49:15 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_MAXREDIRS, 16L );
|
2008-10-29 16:06:14 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_NOSIGNAL, 1L );
|
2008-10-16 05:24:57 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_PRIVATE, task );
|
2008-10-29 16:06:14 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_SSL_VERIFYHOST, 0L );
|
|
|
|
curl_easy_setopt( easy, CURLOPT_SSL_VERIFYPEER, 0L );
|
2008-10-18 15:45:12 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_URL, task->url );
|
|
|
|
curl_easy_setopt( easy, CURLOPT_USERAGENT,
|
|
|
|
TR_NAME "/" LONG_VERSION_STRING );
|
2008-10-17 20:57:54 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_VERBOSE,
|
2008-10-18 15:45:12 +00:00
|
|
|
getenv( "TR_CURL_VERBOSE" ) != NULL );
|
|
|
|
curl_easy_setopt( easy, CURLOPT_WRITEDATA, task );
|
|
|
|
curl_easy_setopt( easy, CURLOPT_WRITEFUNCTION, writeFunc );
|
2008-06-07 21:26:41 +00:00
|
|
|
if( task->range )
|
2008-10-16 05:24:57 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_RANGE, task->range );
|
2008-10-17 20:57:54 +00:00
|
|
|
else /* don't set encoding on webseeds; it messes up binary data */
|
2008-10-16 05:24:57 +00:00
|
|
|
curl_easy_setopt( easy, CURLOPT_ENCODING, "" );
|
|
|
|
|
2009-04-05 13:00:17 +00:00
|
|
|
if( web->still_running >= MAX_CONCURRENT_TASKS ) {
|
|
|
|
tr_list_append( &web->easy_queue, easy );
|
|
|
|
dbgmsg( ">> enqueueing a task... size is now %d", tr_list_size( web->easy_queue ) );
|
|
|
|
} else {
|
2009-02-05 14:52:00 +00:00
|
|
|
const CURLMcode mcode = curl_multi_add_handle( web->multi, easy );
|
|
|
|
tr_assert( mcode == CURLM_OK, "curl_multi_add_handle() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );
|
|
|
|
if( mcode == CURLM_OK )
|
2008-10-16 05:24:57 +00:00
|
|
|
++web->still_running;
|
|
|
|
else
|
2009-02-05 14:52:00 +00:00
|
|
|
tr_err( "%s", curl_multi_strerror( mcode ) );
|
2008-10-16 05:24:57 +00:00
|
|
|
}
|
2008-04-27 18:27:32 +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-15 16:43:51 +00:00
|
|
|
static void
|
2008-10-18 15:45:12 +00:00
|
|
|
task_free( struct tr_web_task * task )
|
|
|
|
{
|
|
|
|
evbuffer_free( task->response );
|
|
|
|
tr_free( task->range );
|
|
|
|
tr_free( task->url );
|
|
|
|
tr_free( task );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
task_finish( struct tr_web_task * task, long response_code )
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
|
|
|
dbgmsg( "finished a web task... response code is %ld", response_code );
|
|
|
|
dbgmsg( "===================================================" );
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-01-17 22:49:15 +00:00
|
|
|
CURL * easy;
|
2008-10-29 16:06:14 +00:00
|
|
|
|
2009-01-17 22:49:15 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
CURLMsg * msg;
|
|
|
|
int msgs_left;
|
|
|
|
|
|
|
|
easy = NULL;
|
|
|
|
while(( msg = curl_multi_info_read( g->multi, &msgs_left ))) {
|
|
|
|
if( msg->msg == CURLMSG_DONE ) {
|
|
|
|
easy = msg->easy_handle;
|
|
|
|
break;
|
|
|
|
}
|
2008-10-18 00:20:37 +00:00
|
|
|
}
|
2009-01-17 22:49:15 +00:00
|
|
|
|
|
|
|
if( easy ) {
|
2008-10-29 16:06:14 +00:00
|
|
|
long code;
|
2009-02-06 21:14:05 +00:00
|
|
|
long fd;
|
2008-10-29 16:06:14 +00:00
|
|
|
struct tr_web_task * task;
|
2009-02-05 14:52:00 +00:00
|
|
|
CURLcode ecode;
|
|
|
|
CURLMcode mcode;
|
2009-02-06 21:14:05 +00:00
|
|
|
|
2009-02-05 14:52:00 +00:00
|
|
|
ecode = curl_easy_getinfo( easy, CURLINFO_PRIVATE, (void*)&task );
|
|
|
|
tr_assert( ecode == CURLE_OK, "curl_easy_getinfo() failed: %d (%s)", ecode, curl_easy_strerror( ecode ) );
|
2009-02-06 21:14:05 +00:00
|
|
|
|
2009-02-05 14:52:00 +00:00
|
|
|
ecode = curl_easy_getinfo( easy, CURLINFO_RESPONSE_CODE, &code );
|
|
|
|
tr_assert( ecode == CURLE_OK, "curl_easy_getinfo() failed: %d (%s)", ecode, curl_easy_strerror( ecode ) );
|
2009-02-06 21:14:05 +00:00
|
|
|
|
|
|
|
ecode = curl_easy_getinfo( easy, CURLINFO_LASTSOCKET, &fd );
|
|
|
|
tr_assert( ecode == CURLE_OK, "curl_easy_getinfo() failed: %d (%s)", ecode, curl_easy_strerror( ecode ) );
|
|
|
|
if( fd != -1L )
|
|
|
|
purgeSockinfo( g, fd );
|
|
|
|
|
2009-02-05 14:52:00 +00:00
|
|
|
mcode = curl_multi_remove_handle( g->multi, easy );
|
2009-02-10 18:03:55 +00:00
|
|
|
tr_assert( mcode == CURLM_OK, "curl_multi_remove_handle() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );
|
2009-02-06 21:14:05 +00:00
|
|
|
|
2008-10-29 16:06:14 +00:00
|
|
|
curl_easy_cleanup( easy );
|
2009-01-17 22:49:15 +00:00
|
|
|
task_finish( task, code );
|
2008-10-29 16:06:14 +00:00
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
2009-01-17 22:49:15 +00:00
|
|
|
while ( easy );
|
2008-10-29 16:06:14 +00:00
|
|
|
|
|
|
|
g->prev_running = g->still_running;
|
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
|
|
|
|
stop_timer( tr_web* g )
|
|
|
|
{
|
|
|
|
if( evtimer_pending( &g->timer_event, NULL ) )
|
|
|
|
{
|
|
|
|
dbgmsg( "deleting the pending global timer" );
|
|
|
|
evtimer_del( &g->timer_event );
|
|
|
|
}
|
|
|
|
}
|
2008-07-29 00:51:07 +00:00
|
|
|
|
2008-10-18 00:20:37 +00:00
|
|
|
static void
|
|
|
|
restart_timer( tr_web * g )
|
|
|
|
{
|
2008-10-18 15:45:12 +00:00
|
|
|
struct timeval interval;
|
2009-01-28 19:35:39 +00:00
|
|
|
|
|
|
|
assert( tr_amInEventThread( g->session ) );
|
|
|
|
assert( g->session != NULL );
|
|
|
|
assert( g->session->events != NULL );
|
|
|
|
|
2008-10-18 00:20:37 +00:00
|
|
|
stop_timer( g );
|
2008-10-18 15:45:12 +00:00
|
|
|
dbgmsg( "adding a timeout for %ld seconds from now", g->timer_ms/1000L );
|
|
|
|
tr_timevalMsec( g->timer_ms, &interval );
|
|
|
|
evtimer_add( &g->timer_event, &interval );
|
2008-10-18 00:20:37 +00:00
|
|
|
}
|
2008-10-16 05:24:57 +00:00
|
|
|
|
2009-04-05 13:00:17 +00:00
|
|
|
static void
|
|
|
|
add_tasks_from_queue( tr_web * g )
|
|
|
|
{
|
2009-08-10 20:04:08 +00:00
|
|
|
while( ( g->still_running < MAX_CONCURRENT_TASKS )
|
2009-04-05 13:00:17 +00:00
|
|
|
&& ( tr_list_size( g->easy_queue ) > 0 ) )
|
|
|
|
{
|
|
|
|
CURL * easy = tr_list_pop_front( &g->easy_queue );
|
|
|
|
if( easy )
|
|
|
|
{
|
|
|
|
const CURLMcode rc = curl_multi_add_handle( g->multi, easy );
|
|
|
|
if( rc != CURLM_OK )
|
|
|
|
tr_err( "%s", curl_multi_strerror( rc ) );
|
|
|
|
else {
|
|
|
|
dbgmsg( "pumped the task queue, %d remain",
|
|
|
|
tr_list_size( g->easy_queue ) );
|
|
|
|
++g->still_running;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-18 00:20:37 +00:00
|
|
|
static void
|
2008-10-18 15:45:12 +00:00
|
|
|
web_close( tr_web * g )
|
2008-10-18 00:20:37 +00:00
|
|
|
{
|
2009-02-05 14:52:00 +00:00
|
|
|
CURLMcode mcode;
|
|
|
|
|
2008-10-18 15:45:12 +00:00
|
|
|
stop_timer( g );
|
2009-02-05 14:52:00 +00:00
|
|
|
|
|
|
|
mcode = curl_multi_cleanup( g->multi );
|
|
|
|
tr_assert( mcode == CURLM_OK, "curl_multi_cleanup() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );
|
|
|
|
if( mcode != CURLM_OK )
|
|
|
|
tr_err( "%s", curl_multi_strerror( mcode ) );
|
|
|
|
|
2008-10-18 15:45:12 +00:00
|
|
|
tr_free( g );
|
2008-10-18 00:20:37 +00:00
|
|
|
}
|
2008-10-17 20:57:54 +00:00
|
|
|
|
2009-01-09 19:24:40 +00:00
|
|
|
/* note: this function can free the tr_web if its 'closing' flag is set
|
2008-10-18 15:45:12 +00:00
|
|
|
and no tasks remain. callers must not reference their g pointer
|
|
|
|
after calling this function */
|
2008-10-17 20:57:54 +00:00
|
|
|
static void
|
2009-02-18 16:16:24 +00:00
|
|
|
tr_multi_perform( tr_web * g, int fd )
|
2008-10-17 20:57:54 +00:00
|
|
|
{
|
2008-10-18 00:20:37 +00:00
|
|
|
int closed = FALSE;
|
2009-02-05 14:52:00 +00:00
|
|
|
CURLMcode mcode;
|
2008-10-17 20:57:54 +00:00
|
|
|
|
2008-10-18 00:20:37 +00:00
|
|
|
dbgmsg( "check_run_count: prev_running %d, still_running %d",
|
|
|
|
g->prev_running, g->still_running );
|
2008-10-17 20:57:54 +00:00
|
|
|
|
2008-10-18 00:20:37 +00:00
|
|
|
/* invoke libcurl's processing */
|
2009-02-18 16:16:24 +00:00
|
|
|
#ifdef USE_CURL_MULTI_SOCKET_ACTION
|
|
|
|
do {
|
|
|
|
dbgmsg( "calling curl_multi_socket_action..." );
|
|
|
|
mcode = curl_multi_socket_action( g->multi, fd, 0, &g->still_running );
|
|
|
|
fd = CURL_SOCKET_TIMEOUT;
|
|
|
|
dbgmsg( "done calling curl_multi_socket_action..." );
|
|
|
|
} while( mcode == CURLM_CALL_MULTI_SOCKET );
|
|
|
|
#else
|
2008-10-18 00:20:37 +00:00
|
|
|
do {
|
2009-02-10 18:03:55 +00:00
|
|
|
dbgmsg( "calling curl_multi_perform..." );
|
|
|
|
mcode = curl_multi_perform( g->multi, &g->still_running );
|
|
|
|
dbgmsg( "done calling curl_multi_perform..." );
|
2009-02-05 14:52:00 +00:00
|
|
|
} while( mcode == CURLM_CALL_MULTI_PERFORM );
|
2009-02-18 16:16:24 +00:00
|
|
|
#endif
|
2009-02-10 18:03:55 +00:00
|
|
|
tr_assert( mcode == CURLM_OK, "curl_multi_perform() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );
|
|
|
|
if( mcode != CURLM_OK )
|
|
|
|
tr_err( "%s", curl_multi_strerror( mcode ) );
|
2008-10-18 00:20:37 +00:00
|
|
|
|
|
|
|
remove_finished_tasks( g );
|
|
|
|
|
2009-04-05 13:00:17 +00:00
|
|
|
add_tasks_from_queue( g );
|
|
|
|
|
2008-10-18 00:20:37 +00:00
|
|
|
if( !g->still_running ) {
|
2009-02-05 22:00:21 +00:00
|
|
|
assert( tr_list_size( g->fds ) == 0 );
|
2008-10-18 00:20:37 +00:00
|
|
|
stop_timer( g );
|
2009-01-09 19:24:40 +00:00
|
|
|
if( g->closing ) {
|
2008-10-18 15:45:12 +00:00
|
|
|
web_close( g );
|
2008-10-18 00:20:37 +00:00
|
|
|
closed = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !closed )
|
|
|
|
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-02-18 16:16:24 +00:00
|
|
|
event_cb( int fd, short kind UNUSED, void * g )
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2009-02-18 16:16:24 +00:00
|
|
|
tr_multi_perform( g, fd );
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
|
|
|
|
2008-10-17 20:57:54 +00:00
|
|
|
/* libevent says that timer_ms have passed, so wake up libcurl */
|
2008-10-15 16:43:51 +00:00
|
|
|
static void
|
2008-10-18 00:20:37 +00:00
|
|
|
timer_cb( int socket UNUSED, short action UNUSED, void * g )
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
|
|
|
dbgmsg( "libevent timer is done" );
|
2009-02-18 16:16:24 +00:00
|
|
|
tr_multi_perform( g, CURL_SOCKET_TIMEOUT );
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CURLMOPT_SOCKETFUNCTION */
|
|
|
|
static int
|
|
|
|
sock_cb( CURL * e UNUSED,
|
2009-02-05 22:00:21 +00:00
|
|
|
curl_socket_t fd,
|
|
|
|
int action,
|
|
|
|
void * vweb,
|
|
|
|
void * unused UNUSED)
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2009-02-05 22:00:21 +00:00
|
|
|
struct tr_web * web = vweb;
|
|
|
|
dbgmsg( "sock_cb: action is %d, fd is %d", action, (int)fd );
|
|
|
|
|
|
|
|
if( action == CURL_POLL_REMOVE )
|
|
|
|
{
|
|
|
|
purgeSockinfo( web, fd );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
2009-02-05 22:00:21 +00:00
|
|
|
{
|
|
|
|
struct tr_web_sockinfo * sockinfo = getSockinfo( web, fd, TRUE );
|
|
|
|
const int kind = EV_PERSIST
|
|
|
|
| (( action & CURL_POLL_IN ) ? EV_READ : 0 )
|
|
|
|
| (( action & CURL_POLL_OUT ) ? EV_WRITE : 0 );
|
|
|
|
dbgmsg( "setsock: fd is %d, curl action is %d, libevent action is %d", fd, action, kind );
|
|
|
|
assert( tr_amInEventThread( web->session ) );
|
|
|
|
assert( kind != EV_PERSIST );
|
|
|
|
|
|
|
|
/* clear any old polling on this fd */
|
|
|
|
clearSockinfoEvent( sockinfo );
|
|
|
|
|
|
|
|
/* set the new polling on this fd */
|
|
|
|
dbgmsg( "enabling (libevent %d, libcurl %d) polling on sockinfo %p, fd %d", action, kind, sockinfo, fd );
|
|
|
|
event_set( &sockinfo->ev, fd, kind, event_cb, web );
|
|
|
|
event_add( &sockinfo->ev, NULL );
|
|
|
|
sockinfo->evset = TRUE;
|
|
|
|
}
|
2008-10-15 16:43:51 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2008-11-04 19:40:18 +00:00
|
|
|
multi_timer_cb( CURLM *multi UNUSED, long timer_ms, void * vg )
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2008-11-04 19:40:18 +00:00
|
|
|
tr_web * g = vg;
|
|
|
|
|
2008-10-17 20:57:54 +00:00
|
|
|
if( timer_ms < 1 ) {
|
|
|
|
if( timer_ms == 0 ) /* call it immediately */
|
|
|
|
timer_cb( 0, 0, g );
|
|
|
|
timer_ms = DEFAULT_TIMER_MSEC;
|
|
|
|
}
|
2008-11-04 19:40:18 +00:00
|
|
|
|
|
|
|
g->timer_ms = timer_ms;
|
2008-10-18 00:20:37 +00:00
|
|
|
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 )
|
|
|
|
{
|
|
|
|
if( session->web )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
static unsigned long tag = 0;
|
|
|
|
struct tr_web_task * task;
|
|
|
|
|
|
|
|
task = tr_new0( struct tr_web_task, 1 );
|
|
|
|
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( );
|
|
|
|
|
|
|
|
tr_runInEventThread( session, addTask, 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
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
tr_web*
|
|
|
|
tr_webInit( tr_session * session )
|
|
|
|
{
|
2009-02-05 14:52:00 +00:00
|
|
|
CURLMcode mcode;
|
2008-04-24 01:42:53 +00:00
|
|
|
static int curlInited = FALSE;
|
2008-10-15 16:43:51 +00:00
|
|
|
tr_web * web;
|
2008-04-24 01:42:53 +00:00
|
|
|
|
|
|
|
/* call curl_global_init if we haven't done it already.
|
|
|
|
* try to enable ssl for https support; but if that fails,
|
2009-08-10 20:04:08 +00:00
|
|
|
* try a plain vanilla init */
|
2008-10-15 16:43:51 +00:00
|
|
|
if( curlInited == FALSE ) {
|
2008-04-24 01:42:53 +00:00
|
|
|
curlInited = TRUE;
|
|
|
|
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 );
|
2008-10-15 16:43:51 +00:00
|
|
|
web->multi = curl_multi_init( );
|
2008-04-24 01:42:53 +00:00
|
|
|
web->session = session;
|
2008-10-18 00:20:37 +00:00
|
|
|
web->timer_ms = DEFAULT_TIMER_MSEC; /* overwritten by multi_timer_cb() */
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2008-10-18 15:45:12 +00:00
|
|
|
evtimer_set( &web->timer_event, timer_cb, web );
|
2009-02-05 14:52:00 +00:00
|
|
|
mcode = curl_multi_setopt( web->multi, CURLMOPT_SOCKETDATA, web );
|
|
|
|
tr_assert( mcode == CURLM_OK, "curl_mutli_setopt() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );
|
|
|
|
mcode = curl_multi_setopt( web->multi, CURLMOPT_SOCKETFUNCTION, sock_cb );
|
|
|
|
tr_assert( mcode == CURLM_OK, "curl_mutli_setopt() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );
|
|
|
|
mcode = curl_multi_setopt( web->multi, CURLMOPT_TIMERDATA, web );
|
|
|
|
tr_assert( mcode == CURLM_OK, "curl_mutli_setopt() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );
|
|
|
|
mcode = curl_multi_setopt( web->multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb );
|
|
|
|
tr_assert( mcode == CURLM_OK, "curl_mutli_setopt() failed: %d (%s)", mcode, curl_multi_strerror( mcode ) );
|
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;
|
2008-10-15 16:43:51 +00:00
|
|
|
if( web->still_running < 1 )
|
2008-10-18 15:45:12 +00:00
|
|
|
web_close( 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
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
static struct http_msg {
|
|
|
|
long code;
|
|
|
|
const char * text;
|
2008-04-25 04:26:04 +00:00
|
|
|
} http_msg[] = {
|
2008-10-31 19:01:27 +00:00
|
|
|
{ 0, "No Response" },
|
2008-10-15 16:43:51 +00:00
|
|
|
{ 101, "Switching Protocols" },
|
|
|
|
{ 200, "OK" },
|
|
|
|
{ 201, "Created" },
|
|
|
|
{ 202, "Accepted" },
|
|
|
|
{ 203, "Non-Authoritative Information" },
|
|
|
|
{ 204, "No Content" },
|
|
|
|
{ 205, "Reset Content" },
|
|
|
|
{ 206, "Partial Content" },
|
|
|
|
{ 300, "Multiple Choices" },
|
|
|
|
{ 301, "Moved Permanently" },
|
|
|
|
{ 302, "Found" },
|
|
|
|
{ 303, "See Other" },
|
|
|
|
{ 304, "Not Modified" },
|
|
|
|
{ 305, "Use Proxy" },
|
|
|
|
{ 306, "(Unused)" },
|
|
|
|
{ 307, "Temporary Redirect" },
|
|
|
|
{ 400, "Bad Request" },
|
|
|
|
{ 401, "Unauthorized" },
|
|
|
|
{ 402, "Payment Required" },
|
|
|
|
{ 403, "Forbidden" },
|
|
|
|
{ 404, "Not Found" },
|
|
|
|
{ 405, "Method Not Allowed" },
|
|
|
|
{ 406, "Not Acceptable" },
|
|
|
|
{ 407, "Proxy Authentication Required" },
|
|
|
|
{ 408, "Request Timeout" },
|
|
|
|
{ 409, "Conflict" },
|
|
|
|
{ 410, "Gone" },
|
|
|
|
{ 411, "Length Required" },
|
|
|
|
{ 412, "Precondition Failed" },
|
|
|
|
{ 413, "Request Entity Too Large" },
|
|
|
|
{ 414, "Request-URI Too Long" },
|
|
|
|
{ 415, "Unsupported Media Type" },
|
2008-04-25 04:26:04 +00:00
|
|
|
{ 416, "Requested Range Not Satisfiable" },
|
2008-10-15 16:43:51 +00:00
|
|
|
{ 417, "Expectation Failed" },
|
|
|
|
{ 500, "Internal Server Error" },
|
|
|
|
{ 501, "Not Implemented" },
|
|
|
|
{ 502, "Bad Gateway" },
|
|
|
|
{ 503, "Service Unavailable" },
|
|
|
|
{ 504, "Gateway Timeout" },
|
2008-10-31 19:01:27 +00:00
|
|
|
{ 505, "HTTP Version Not Supported" }
|
2008-04-25 04:26:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2008-10-15 16:43:51 +00:00
|
|
|
compareResponseCodes( const void * va, const void * vb )
|
2008-04-25 04:26:04 +00:00
|
|
|
{
|
2008-10-15 16:43:51 +00:00
|
|
|
const long a = *(const long*) va;
|
2008-04-25 04:26:04 +00:00
|
|
|
const struct http_msg * b = vb;
|
|
|
|
return a - b->code;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
tr_webGetResponseStr( long code )
|
|
|
|
{
|
|
|
|
struct http_msg * msg = bsearch( &code,
|
2009-08-10 20:04:08 +00:00
|
|
|
http_msg,
|
2008-10-15 16:43:51 +00:00
|
|
|
sizeof( http_msg ) / sizeof( http_msg[0] ),
|
2008-04-25 04:26:04 +00:00
|
|
|
sizeof( http_msg[0] ),
|
|
|
|
compareResponseCodes );
|
|
|
|
return msg ? msg->text : "Unknown Error";
|
|
|
|
}
|