Changeset 5676


Ignore:
Timestamp:
Apr 24, 2008, 3:26:36 AM (14 years ago)
Author:
charles
Message:

code tweaks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/metainfo.c

    r5673 r5676  
    4646 * Local prototypes
    4747 **********************************************************************/
    48 static int getannounce( tr_info * inf, tr_benc * meta );
    49 static char * announceToScrape( const char * announce );
    5048static int parseFiles( tr_info * inf, tr_benc * name,
    5149                       tr_benc * files, tr_benc * length );
     
    190188        tr_free( content );
    191189    }
     190}
     191
     192static char *
     193announceToScrape( const char * announce )
     194{
     195    char * scrape = NULL;
     196    const char * s;
     197
     198    /* To derive the scrape URL use the following steps:
     199     * Begin with the announce URL. Find the last '/' in it.
     200     * If the text immediately following that '/' isn't 'announce'
     201     * it will be taken as a sign that that tracker doesn't support
     202     * the scrape convention. If it does, substitute 'scrape' for
     203     * 'announce' to find the scrape page.  */
     204    if((( s = strrchr( announce, '/' ))) && !strncmp( ++s, "announce", 8 ))
     205    {
     206        struct evbuffer * buf = evbuffer_new( );
     207        evbuffer_add( buf, announce, s-announce );
     208        evbuffer_add( buf, "scrape", 6 );
     209        evbuffer_add_printf( buf, "%s", s+8 );
     210        scrape = tr_strdup( ( char * ) EVBUFFER_DATA( buf ) );
     211        evbuffer_free( buf );
     212    }
     213
     214    return scrape;
     215}
     216
     217static int
     218getannounce( tr_info * inf, tr_benc * meta )
     219{
     220    const char * str;
     221    tr_tracker_info * trackers = NULL;
     222    int trackerCount = 0;
     223    tr_benc * tiers;
     224
     225    /* Announce-list */
     226    if( tr_bencDictFindList( meta, "announce-list", &tiers ) )
     227    {
     228        int n;
     229        int i, j;
     230
     231        n = 0;
     232        for( i=0; i<tiers->val.l.count; ++i )
     233            n += tiers->val.l.vals[i].val.l.count;
     234
     235        trackers = tr_new0( tr_tracker_info, n );
     236        trackerCount = 0;
     237
     238        for( i=0; i<tiers->val.l.count; ++i ) {
     239            const tr_benc * tier = &tiers->val.l.vals[i];
     240            for( j=0; tr_bencIsList(tier) && j<tier->val.l.count; ++j ) {
     241                const tr_benc * a = &tier->val.l.vals[j];
     242                if( tr_bencIsString( a ) && tr_httpIsValidURL( a->val.s.s ) ) {
     243                    tr_tracker_info * t = trackers + trackerCount++;
     244                    t->tier = i;
     245                    t->announce = tr_strndup( a->val.s.s, a->val.s.i );
     246                    t->scrape = announceToScrape( a->val.s.s );
     247                    /*fprintf( stderr, "tier %d: %s\n", i, a->val.s.s );*/
     248                }
     249            }
     250        }
     251
     252        /* did we use any of the tiers? */
     253        if( !trackerCount ) {
     254            tr_inf( _( "Invalid metadata entry \"%s\"" ), "announce-list" );
     255            tr_free( trackers );
     256            trackers = NULL;
     257        }
     258    }
     259
     260    /* Regular announce value */
     261    if( !trackerCount
     262        && tr_bencDictFindStr( meta, "announce", &str )
     263        && tr_httpIsValidURL( str ) )
     264    {
     265        trackers = tr_new0( tr_tracker_info, 1 );
     266        trackers[trackerCount].tier = 0;
     267        trackers[trackerCount].announce = tr_strdup( str );
     268        trackers[trackerCount++].scrape = announceToScrape( str );
     269        /*fprintf( stderr, "single announce: [%s]\n", str );*/
     270    }
     271
     272    inf->trackers = trackers;
     273    inf->trackerCount = trackerCount;
     274
     275    return inf->trackerCount ? TR_OK : TR_ERROR;
    192276}
    193277
     
    314398    /* get announce or announce-list */
    315399    if( getannounce( inf, meta ) )
    316     {
    317         goto fail;
    318     }
     400        goto fail;
    319401
    320402    /* filename of Transmission's copy */
     
    408490
    409491    return TR_OK;
    410 }
    411 
    412 static int getannounce( tr_info * inf, tr_benc * meta )
    413 {
    414     const char * str;
    415     tr_tracker_info * trackers = NULL;
    416     int trackerCount = 0;
    417     tr_benc * tiers;
    418 
    419     /* Announce-list */
    420     if( tr_bencDictFindList( meta, "announce-list", &tiers ) )
    421     {
    422         int n;
    423         int i, j;
    424 
    425         n = 0;
    426         for( i=0; i<tiers->val.l.count; ++i )
    427             n += tiers->val.l.vals[i].val.l.count;
    428 
    429         trackers = tr_new0( tr_tracker_info, n );
    430         trackerCount = 0;
    431 
    432         for( i=0; i<tiers->val.l.count; ++i ) {
    433             const tr_benc * tier = &tiers->val.l.vals[i];
    434             for( j=0; tr_bencIsList(tier) && j<tier->val.l.count; ++j ) {
    435                 const tr_benc * address = &tier->val.l.vals[j];
    436                 if( tr_bencIsString( address ) && tr_httpIsValidURL( address->val.s.s ) ) {
    437                     trackers[trackerCount].tier = i;
    438                     trackers[trackerCount].announce = tr_strndup( address->val.s.s, address->val.s.i );
    439                     trackers[trackerCount++].scrape = announceToScrape( address->val.s.s );
    440                     /*fprintf( stderr, "tier %d: %s\n", i, address->val.s.s );*/
    441                 }
    442             }
    443         }
    444 
    445         /* did we use any of the tiers? */
    446         if( !trackerCount ) {
    447             tr_inf( _( "Invalid metadata entry \"%s\"" ), "announce-list" );
    448             tr_free( trackers );
    449             trackers = NULL;
    450         }
    451     }
    452 
    453     /* Regular announce value */
    454     if( !trackerCount
    455         && tr_bencDictFindStr( meta, "announce", &str )
    456         && tr_httpIsValidURL( str ) )
    457     {
    458         trackers = tr_new0( tr_tracker_info, 1 );
    459         trackers[trackerCount].tier = 0;
    460         trackers[trackerCount].announce = tr_strdup( str );
    461         trackers[trackerCount++].scrape = announceToScrape( str );
    462         /*fprintf( stderr, "single announce: [%s]\n", str );*/
    463     }
    464 
    465     inf->trackers = trackers;
    466     inf->trackerCount = trackerCount;
    467     return TR_OK;
    468 }
    469 
    470 static char *
    471 announceToScrape( const char * announce )
    472 {
    473     char * scrape = NULL;
    474     const char * slash;
    475     struct evbuffer * buf;
    476 
    477     /* To derive the scrape URL use the following steps:
    478      * Begin with the announce URL. Find the last '/' in it.
    479      * If the text immediately following that '/' isn't 'announce'
    480      * it will be taken as a sign that that tracker doesn't support
    481      * the scrape convention. If it does, substitute 'scrape' for
    482      * 'announce' to find the scrape page.  */
    483 
    484     /* is the last slash followed by "announce"? */
    485     slash = strrchr( announce, '/' );
    486     if( !slash )
    487         return NULL;
    488     ++slash;
    489     if( strncmp( slash, "announce", 8 ) )
    490         return NULL;
    491 
    492     /* build the scrape url */
    493     buf = evbuffer_new( );
    494     evbuffer_add( buf, announce, slash-announce );
    495     evbuffer_add( buf, "scrape", 6 );
    496     evbuffer_add_printf( buf, "%s", slash+8 );
    497     scrape = tr_strdup( ( char * ) EVBUFFER_DATA( buf ) );
    498     evbuffer_free( buf );
    499 
    500     return scrape;
    501492}
    502493
Note: See TracChangeset for help on using the changeset viewer.