Changeset 9750


Ignore:
Timestamp:
Dec 14, 2009, 12:54:30 PM (12 years ago)
Author:
charles
Message:

(trunk libT) more web.c simplification + handle an unhandled case CURL_POLL_NONE in the socket callback.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/web.c

    r9749 r9750  
    5555};
    5656
     57static void
     58web_close( tr_web * g )
     59{
     60    curl_multi_cleanup( g->multi );
     61    evtimer_del( &g->timer_event );
     62    tr_free( g );
     63}
     64
    5765/***
    5866****
     
    7078};
    7179
     80static void
     81task_free( struct tr_web_task * task )
     82{
     83    evbuffer_free( task->response );
     84    tr_free( task->range );
     85    tr_free( task->url );
     86    tr_free( task );
     87}
     88
     89/***
     90****
     91***/
     92
    7293static size_t
    7394writeFunc( void * ptr, size_t size, size_t nmemb, void * vtask )
     
    123144    {
    124145        CURLMcode mcode;
    125         CURL * easy = curl_easy_init( );
     146        CURL * e = curl_easy_init( );
    126147        struct tr_web * web = session->web;
    127148        const long timeout = getTimeoutFromURL( task->url );
     
    132153
    133154        if( !task->range && session->isProxyEnabled ) {
    134             curl_easy_setopt( easy, CURLOPT_PROXY, session->proxy );
    135             curl_easy_setopt( easy, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
    136             curl_easy_setopt( easy, CURLOPT_PROXYPORT, session->proxyPort );
    137             curl_easy_setopt( easy, CURLOPT_PROXYTYPE,
     155            curl_easy_setopt( e, CURLOPT_PROXY, session->proxy );
     156            curl_easy_setopt( e, CURLOPT_PROXYAUTH, CURLAUTH_ANY );
     157            curl_easy_setopt( e, CURLOPT_PROXYPORT, session->proxyPort );
     158            curl_easy_setopt( e, CURLOPT_PROXYTYPE,
    138159                                      getCurlProxyType( session->proxyType ) );
    139160        }
     
    141162            char * str = tr_strdup_printf( "%s:%s", session->proxyUsername,
    142163                                                    session->proxyPassword );
    143             curl_easy_setopt( easy, CURLOPT_PROXYUSERPWD, str );
     164            curl_easy_setopt( e, CURLOPT_PROXYUSERPWD, str );
    144165            tr_free( str );
    145166        }
    146167
    147         curl_easy_setopt( easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    148         curl_easy_setopt( easy, CURLOPT_TIMEOUT, timeout );
    149         curl_easy_setopt( easy, CURLOPT_CONNECTTIMEOUT, timeout-5 );
    150         curl_easy_setopt( easy, CURLOPT_SOCKOPTFUNCTION, sockoptfunction );
    151         curl_easy_setopt( easy, CURLOPT_SOCKOPTDATA, task );
    152         curl_easy_setopt( easy, CURLOPT_WRITEDATA, task );
    153         curl_easy_setopt( easy, CURLOPT_WRITEFUNCTION, writeFunc );
    154         curl_easy_setopt( easy, CURLOPT_DNS_CACHE_TIMEOUT, 1800L );
    155         curl_easy_setopt( easy, CURLOPT_FOLLOWLOCATION, 1L );
    156         curl_easy_setopt( easy, CURLOPT_AUTOREFERER, 1L );
    157         curl_easy_setopt( easy, CURLOPT_FORBID_REUSE, 1L );
    158         curl_easy_setopt( easy, CURLOPT_MAXREDIRS, -1L );
    159         curl_easy_setopt( easy, CURLOPT_NOSIGNAL, 1L );
    160         curl_easy_setopt( easy, CURLOPT_PRIVATE, task );
    161         curl_easy_setopt( easy, CURLOPT_SSL_VERIFYHOST, 0L );
    162         curl_easy_setopt( easy, CURLOPT_SSL_VERIFYPEER, 0L );
    163         curl_easy_setopt( easy, CURLOPT_URL, task->url );
    164         curl_easy_setopt( easy, CURLOPT_USERAGENT, user_agent );
    165         curl_easy_setopt( easy, CURLOPT_VERBOSE, verbose );
     168        curl_easy_setopt( e, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
     169        curl_easy_setopt( e, CURLOPT_TIMEOUT, timeout );
     170        curl_easy_setopt( e, CURLOPT_CONNECTTIMEOUT, timeout-5 );
     171        curl_easy_setopt( e, CURLOPT_SOCKOPTFUNCTION, sockoptfunction );
     172        curl_easy_setopt( e, CURLOPT_SOCKOPTDATA, task );
     173        curl_easy_setopt( e, CURLOPT_WRITEDATA, task );
     174        curl_easy_setopt( e, CURLOPT_WRITEFUNCTION, writeFunc );
     175        curl_easy_setopt( e, CURLOPT_DNS_CACHE_TIMEOUT, 1800L );
     176        curl_easy_setopt( e, CURLOPT_FOLLOWLOCATION, 1L );
     177        curl_easy_setopt( e, CURLOPT_AUTOREFERER, 1L );
     178        curl_easy_setopt( e, CURLOPT_FORBID_REUSE, 1L );
     179        curl_easy_setopt( e, CURLOPT_MAXREDIRS, -1L );
     180        curl_easy_setopt( e, CURLOPT_NOSIGNAL, 1L );
     181        curl_easy_setopt( e, CURLOPT_PRIVATE, task );
     182        curl_easy_setopt( e, CURLOPT_SSL_VERIFYHOST, 0L );
     183        curl_easy_setopt( e, CURLOPT_SSL_VERIFYPEER, 0L );
     184        curl_easy_setopt( e, CURLOPT_URL, task->url );
     185        curl_easy_setopt( e, CURLOPT_USERAGENT, user_agent );
     186        curl_easy_setopt( e, CURLOPT_VERBOSE, verbose );
    166187        if( web->haveAddr )
    167             curl_easy_setopt( easy, CURLOPT_INTERFACE,
    168                                             tr_ntop_non_ts( &web->addr ) );
     188            curl_easy_setopt( e, CURLOPT_INTERFACE, tr_ntop_non_ts( &web->addr ) );
    169189        if( task->range )
    170             curl_easy_setopt( easy, CURLOPT_RANGE, task->range );
     190            curl_easy_setopt( e, CURLOPT_RANGE, task->range );
    171191        else /* don't set encoding on webseeds; it messes up binary data */
    172             curl_easy_setopt( easy, CURLOPT_ENCODING, "" );
    173 
    174         mcode = curl_multi_add_handle( web->multi, easy );
     192            curl_easy_setopt( e, CURLOPT_ENCODING, "" );
     193
     194        mcode = curl_multi_add_handle( web->multi, e );
    175195        ++web->taskCount;
    176196        /*tr_multi_perform( web, CURL_SOCKET_TIMEOUT );*/
     
    181201****
    182202***/
    183 
    184 static void
    185 task_free( struct tr_web_task * task )
    186 {
    187     evbuffer_free( task->response );
    188     tr_free( task->range );
    189     tr_free( task->url );
    190     tr_free( task );
    191 }
    192203
    193204static void
     
    215226            long code;
    216227            struct tr_web_task * task;
    217             CURL * easy = msg->easy_handle;
    218             curl_easy_getinfo( easy, CURLINFO_PRIVATE, (void*)&task );
    219             curl_easy_getinfo( easy, CURLINFO_RESPONSE_CODE, &code );
    220             curl_multi_remove_handle( g->multi, easy );
    221             curl_easy_cleanup( easy );
     228            CURL * e = msg->easy_handle;
     229            curl_easy_getinfo( e, CURLINFO_PRIVATE, (void*)&task );
     230            curl_easy_getinfo( e, CURLINFO_RESPONSE_CODE, &code );
     231            curl_multi_remove_handle( g->multi, e );
     232            curl_easy_cleanup( e );
    222233            task_finish( task, code );
    223234        }
     
    233244}
    234245
    235 static void
    236 web_close( tr_web * g )
    237 {
    238     curl_multi_cleanup( g->multi );
    239     evtimer_del( &g->timer_event );
    240     tr_free( g );
    241 }
    242 
    243 /* note: this function can free the tr_web if its 'closing' flag is set
    244    and no tasks remain.  callers must not reference their g pointer
    245    after calling this function */
    246246static tr_bool
    247247tr_multi_perform( tr_web * g, int fd )
    248248{
     249    CURLMcode mcode;
    249250    tr_bool closed = FALSE;
    250     CURLMcode mcode;
    251251
    252252    dbgmsg( "check_run_count: %d taskCount", g->taskCount );
     
    259259    remove_finished_tasks( g );
    260260
    261     if( g->closing && !g->taskCount ) {
     261    if(( closed = g->closing && !g->taskCount ))
    262262        web_close( g );
    263         closed = TRUE;
    264     }
    265 
    266     if( !closed )
     263    else
    267264        restart_timer( g );
    268265
     
    279276/* CURLMOPT_SOCKETFUNCTION */
    280277static int
    281 sock_cb( CURL * easy UNUSED, curl_socket_t fd, int action, void * vweb, void * vevent )
     278sock_cb( CURL * easy UNUSED, curl_socket_t fd, int action,
     279         void * vweb, void * vevent )
    282280{
    283281    /*static int num_events = 0;*/
    284282    struct tr_web * web = vweb;
    285283    struct event * io_event = vevent;
    286     dbgmsg( "sock_cb: action is %d, fd is %d, io_event is %p", action, (int)fd, io_event );
     284    dbgmsg( "sock_cb: action is %d, fd is %d, io_event is %p",
     285            action, (int)fd, io_event );
    287286
    288287    if( action == CURL_POLL_REMOVE )
     
    296295        }
    297296    }
    298     else
     297    else if( action & ( EV_READ | EV_WRITE ) )
    299298    {
    300299        const short events = EV_PERSIST
     
    360359           void               * done_func_user_data )
    361360{
    362     if( session->web )
     361    if( session->web != NULL )
    363362    {
    364363        static unsigned long tag = 0;
     
    387386{
    388387    tr_web * web;
    389     static int curlInited = FALSE;
    390 
    391     /* call curl_global_init if we haven't done it already.
    392      * try to enable ssl for https support; but if that fails,
     388
     389    /* try to enable ssl for https support; but if that fails,
    393390     * try a plain vanilla init */
    394     if( curlInited == FALSE ) {
    395         curlInited = TRUE;
    396         if( curl_global_init( CURL_GLOBAL_SSL ) )
    397             curl_global_init( 0 );
    398     }
     391    if( curl_global_init( CURL_GLOBAL_SSL ) )
     392        curl_global_init( 0 );
    399393
    400394    web = tr_new0( struct tr_web, 1 );
     
    428422*****/
    429423
    430 static const struct http_msg {
    431     long code;
    432     const char * text;
    433 } http_msg[] = {
    434     {   0, "No Response" },
    435     { 101, "Switching Protocols" },
    436     { 200, "OK" },
    437     { 201, "Created" },
    438     { 202, "Accepted" },
    439     { 203, "Non-Authoritative Information" },
    440     { 204, "No Content" },
    441     { 205, "Reset Content" },
    442     { 206, "Partial Content" },
    443     { 300, "Multiple Choices" },
    444     { 301, "Moved Permanently" },
    445     { 302, "Found" },
    446     { 303, "See Other" },
    447     { 304, "Not Modified" },
    448     { 305, "Use Proxy" },
    449     { 306, "(Unused)" },
    450     { 307, "Temporary Redirect" },
    451     { 400, "Bad Request" },
    452     { 401, "Unauthorized" },
    453     { 402, "Payment Required" },
    454     { 403, "Forbidden" },
    455     { 404, "Not Found" },
    456     { 405, "Method Not Allowed" },
    457     { 406, "Not Acceptable" },
    458     { 407, "Proxy Authentication Required" },
    459     { 408, "Request Timeout" },
    460     { 409, "Conflict" },
    461     { 410, "Gone" },
    462     { 411, "Length Required" },
    463     { 412, "Precondition Failed" },
    464     { 413, "Request Entity Too Large" },
    465     { 414, "Request-URI Too Long" },
    466     { 415, "Unsupported Media Type" },
    467     { 416, "Requested Range Not Satisfiable" },
    468     { 417, "Expectation Failed" },
    469     { 500, "Internal Server Error" },
    470     { 501, "Not Implemented" },
    471     { 502, "Bad Gateway" },
    472     { 503, "Service Unavailable" },
    473     { 504, "Gateway Timeout" },
    474     { 505, "HTTP Version Not Supported" }
    475 };
    476 
    477424const char *
    478425tr_webGetResponseStr( long code )
    479426{
    480     int i;
    481     static const int n = sizeof( http_msg ) / sizeof( http_msg[0] );
    482     for( i=0; i<n; ++i )
    483         if( http_msg[i].code == code )
    484             return http_msg[i].text;
    485     return "Unknown Error";
     427    switch( code )
     428    {
     429        case   0: return "No Response";
     430        case 101: return "Switching Protocols";
     431        case 200: return "OK";
     432        case 201: return "Created";
     433        case 202: return "Accepted";
     434        case 203: return "Non-Authoritative Information";
     435        case 204: return "No Content";
     436        case 205: return "Reset Content";
     437        case 206: return "Partial Content";
     438        case 300: return "Multiple Choices";
     439        case 301: return "Moved Permanently";
     440        case 302: return "Found";
     441        case 303: return "See Other";
     442        case 304: return "Not Modified";
     443        case 305: return "Use Proxy";
     444        case 306: return "(Unused)";
     445        case 307: return "Temporary Redirect";
     446        case 400: return "Bad Request";
     447        case 401: return "Unauthorized";
     448        case 402: return "Payment Required";
     449        case 403: return "Forbidden";
     450        case 404: return "Not Found";
     451        case 405: return "Method Not Allowed";
     452        case 406: return "Not Acceptable";
     453        case 407: return "Proxy Authentication Required";
     454        case 408: return "Request Timeout";
     455        case 409: return "Conflict";
     456        case 410: return "Gone";
     457        case 411: return "Length Required";
     458        case 412: return "Precondition Failed";
     459        case 413: return "Request Entity Too Large";
     460        case 414: return "Request-URI Too Long";
     461        case 415: return "Unsupported Media Type";
     462        case 416: return "Requested Range Not Satisfiable";
     463        case 417: return "Expectation Failed";
     464        case 500: return "Internal Server Error";
     465        case 501: return "Not Implemented";
     466        case 502: return "Bad Gateway";
     467        case 503: return "Service Unavailable";
     468        case 504: return "Gateway Timeout";
     469        case 505: return "HTTP Version Not Supported";
     470        default:  return "Unknown Error";
     471    }
    486472}
    487473
Note: See TracChangeset for help on using the changeset viewer.