(trunk libT) #2631 "Add webseed support to magnet links"

This commit is contained in:
Charles Kerr 2009-11-29 17:49:58 +00:00
parent da7ab27ae2
commit 5c3fd1b5cc
4 changed files with 54 additions and 21 deletions

View File

@ -44,12 +44,15 @@ test1( void )
"d2354010a3ca4ade5b7427bb093a62a3899ff381"
"&dn=Display%20Name"
"&tr=http%3A%2F%2Ftracker.openbittorrent.com%2Fannounce"
"&tr=http%3A%2F%2Ftracker.opentracker.org%2Fannounce";
"&tr=http%3A%2F%2Ftracker.opentracker.org%2Fannounce"
"&ws=http%3A%2F%2Fserver.webseed.org%2Fpath%2Fto%2Ffile";
info = tr_magnetParse( uri );
check( info != NULL )
check( info->announceCount == 2 );
check( !strcmp( info->announceURLs[0], "http://tracker.openbittorrent.com/announce" ) )
check( !strcmp( info->announceURLs[1], "http://tracker.opentracker.org/announce" ) )
check( info->trackerCount == 2 );
check( !strcmp( info->trackers[0], "http://tracker.openbittorrent.com/announce" ) )
check( !strcmp( info->trackers[1], "http://tracker.opentracker.org/announce" ) )
check( info->webseedCount == 1 );
check( !strcmp( info->webseeds[0], "http://server.webseed.org/path/to/file" ) )
check( !strcmp( info->displayName, "Display Name" ) )
for( i=0; i<20; ++i )
check( info->hash[i] == dec[i] );
@ -61,12 +64,15 @@ test1( void )
"2I2UAEFDZJFN4W3UE65QSOTCUOEZ744B"
"&dn=Display%20Name"
"&tr=http%3A%2F%2Ftracker.openbittorrent.com%2Fannounce"
"&ws=http%3A%2F%2Fserver.webseed.org%2Fpath%2Fto%2Ffile"
"&tr=http%3A%2F%2Ftracker.opentracker.org%2Fannounce";
info = tr_magnetParse( uri );
check( info != NULL )
check( info->announceCount == 2 );
check( !strcmp( info->announceURLs[0], "http://tracker.openbittorrent.com/announce" ) )
check( !strcmp( info->announceURLs[1], "http://tracker.opentracker.org/announce" ) )
check( info->trackerCount == 2 );
check( !strcmp( info->trackers[0], "http://tracker.openbittorrent.com/announce" ) )
check( !strcmp( info->trackers[1], "http://tracker.opentracker.org/announce" ) )
check( info->webseedCount == 1 );
check( !strcmp( info->webseeds[0], "http://server.webseed.org/path/to/file" ) )
check( !strcmp( info->displayName, "Display Name" ) )
for( i=0; i<20; ++i )
check( info->hash[i] == dec[i] );

View File

@ -93,13 +93,16 @@ base32_to_sha1( uint8_t * out, const char * in, const int inlen )
***/
#define MAX_TRACKERS 64
#define MAX_WEBSEEDS 64
tr_magnet_info *
tr_magnetParse( const char * uri )
{
tr_bool got_checksum = FALSE;
int announceCount = 0;
char * announceURLs[MAX_TRACKERS];
int trCount = 0;
int wsCount = 0;
char * tr[MAX_TRACKERS];
char * ws[MAX_WEBSEEDS];
char * displayName = NULL;
uint8_t sha1[SHA_DIGEST_LENGTH];
tr_magnet_info * info = NULL;
@ -149,7 +152,10 @@ tr_magnetParse( const char * uri )
displayName = tr_http_unescape( val, vallen );
if( ( keylen==2 ) && !memcmp( key, "tr", 2 ) )
announceURLs[announceCount++] = tr_http_unescape( val, vallen );
tr[trCount++] = tr_http_unescape( val, vallen );
if( ( keylen==2 ) && !memcmp( key, "ws", 2 ) )
ws[wsCount++] = tr_http_unescape( val, vallen );
walk = next != NULL ? next + 1 : NULL;
}
@ -159,8 +165,10 @@ tr_magnetParse( const char * uri )
{
info = tr_new0( tr_magnet_info, 1 );
info->displayName = displayName;
info->announceCount = announceCount;
info->announceURLs = tr_memdup( announceURLs, sizeof(char*) * announceCount );
info->trackerCount = trCount;
info->trackers = tr_memdup( tr, sizeof(char*) * trCount );
info->webseedCount = wsCount;
info->webseeds = tr_memdup( ws, sizeof(char*) * wsCount );
memcpy( info->hash, sha1, sizeof(uint8_t) * SHA_DIGEST_LENGTH );
}
@ -174,10 +182,14 @@ tr_magnetFree( tr_magnet_info * info )
{
int i;
for( i=0; i<info->announceCount; ++i )
tr_free( info->announceURLs[i] );
for( i=0; i<info->trackerCount; ++i )
tr_free( info->trackers[i] );
tr_free( info->trackers );
for( i=0; i<info->webseedCount; ++i )
tr_free( info->webseeds[i] );
tr_free( info->webseeds );
tr_free( info->announceURLs );
tr_free( info->displayName );
tr_free( info );
}

View File

@ -18,9 +18,14 @@
typedef struct tr_magnet_info
{
uint8_t hash[20];
char * displayName;
char ** announceURLs;
int announceCount;
int trackerCount;
char ** trackers;
int webseedCount;
char ** webseeds;
}
tr_magnet_info;

View File

@ -585,18 +585,28 @@ tr_metainfoSetFromMagnet( tr_info * inf, const tr_magnet_info * m )
inf->name = tr_strdup( inf->hashString );
/* trackers */
if( m->announceCount > 0 )
if(( inf->trackerCount = m->trackerCount ))
{
int i;
const int n = m->announceCount;
const int n = m->trackerCount;
inf->trackerCount = n;
inf->trackers = tr_new0( tr_tracker_info, n );
for( i=0; i<n; ++i ) {
const char * url = m->announceURLs[i];
const char * url = m->trackers[i];
inf->trackers[i].tier = i;
inf->trackers[i].announce = tr_strdup( url );
inf->trackers[i].scrape = tr_convertAnnounceToScrape( url );
}
}
/* webseeds */
if(( inf->webseedCount = m->webseedCount ))
{
int i;
const int n = m->webseedCount;
inf->webseeds = tr_new0( char*, n );
for( i=0; i<n; ++i )
inf->webseeds[i] = tr_strdup( m->webseeds[i] );
}
}