]> granicus.if.org Git - transmission/commitdiff
(trunk) #2938 "crash when adding a torrent by URL from an ftp source over RPC" -...
authorCharles Kerr <charles@transmissionbt.com>
Sat, 20 Feb 2010 15:57:05 +0000 (15:57 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Sat, 20 Feb 2010 15:57:05 +0000 (15:57 +0000)
libtransmission/announcer.c
libtransmission/makemeta.c
libtransmission/metainfo.c
libtransmission/torrent.c
libtransmission/utils-test.c
libtransmission/utils.c
libtransmission/utils.h
libtransmission/web.c

index 763ddcb315c28fa8a9d175632d7a402b3ce3bac9..f51d60425914d68cefefb0e2706e734cbc11ddf3 100644 (file)
@@ -133,7 +133,7 @@ getHostName( const char * url )
     int port = 0;
     char * host = NULL;
     char * ret;
-    tr_httpParseURL( url, strlen( url ), &host, &port, NULL );
+    tr_urlParse( url, strlen( url ), NULL, &host, &port, NULL );
     ret = tr_strdup_printf( "%s:%d", ( host ? host : "invalid" ), port );
     tr_free( host );
     return ret;
index a7b188efef50bbeba1bb07b50e21dfdc7291d916..603fb971cca9619703356d8deb31f38321f0e10e 100644 (file)
@@ -380,7 +380,7 @@ tr_realMakeMetaInfo( tr_metainfo_builder * builder )
 
     /* allow an empty set, but if URLs *are* listed, verify them. #814, #971 */
     for( i = 0; i < builder->trackerCount && !builder->result; ++i ) {
-        if( !tr_httpIsValidURL( builder->trackers[i].announce ) ) {
+        if( !tr_urlIsValidTracker( builder->trackers[i].announce ) ) {
             tr_strlcpy( builder->errfile, builder->trackers[i].announce,
                        sizeof( builder->errfile ) );
             builder->result = TR_MAKEMETA_URL;
index c3cac5d1c75e4be1ba8dc4be3d0d83f1b480ea52..3dc9aea7812fcf16f22dce35108a69437f3e4344 100644 (file)
@@ -288,7 +288,7 @@ getannounce( tr_info * inf, tr_benc * meta )
                 if( tr_bencGetStr( tr_bencListChild( tier, j ), &str ) )
                 {
                     char * url = tr_strstrip( tr_strdup( str ) );
-                    if( tr_httpIsValidURL( url ) )
+                    if( tr_urlIsValidTracker( url ) )
                     {
                         tr_tracker_info * t = trackers + trackerCount;
                         t->tier = validTiers;
@@ -320,7 +320,7 @@ getannounce( tr_info * inf, tr_benc * meta )
       && tr_bencDictFindStr( meta, "announce", &str ) )
     {
         char * url = tr_strstrip( tr_strdup( str ) );
-        if( tr_httpIsValidURL( url ) )
+        if( tr_urlIsValidTracker( url ) )
         {
             trackers = tr_new0( tr_tracker_info, 1 );
             trackers[trackerCount].tier = 0;
index ed2dc47117b962627adbb98dbb237a5f9d6da7f6..856df7adbf3253036def783cc8ab3138b3d9a7ad 100644 (file)
@@ -2086,7 +2086,7 @@ tr_torrentSetAnnounceList( tr_torrent             * tor,
 
     /* look for bad URLs */
     for( i=0; ok && i<trackerCount; ++i )
-        if( !tr_httpIsValidURL( trackers[i].announce ) )
+        if( !tr_urlIsValidTracker( trackers[i].announce ) )
             ok = FALSE;
 
     /* save to the .torrent file */
index 7ef74d08ddabdd3b6a902fdf648a189920ffec45..a2fe748acee275b2ce38c3e7bbb374927a073028 100644 (file)
@@ -324,24 +324,29 @@ static int
 test_url( void )
 {
     int port;
+    char * scheme;
     char * host;
     char * path;
     char * str;
     const char * url;
 
     url = "http://www.some-tracker.org/some/path";
-    check( !tr_httpParseURL( url, -1, &host, &port, &path ) )
+    check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) )
+    check( !strcmp( scheme, "http" ) )
     check( !strcmp( host, "www.some-tracker.org" ) )
     check( !strcmp( path, "/some/path" ) )
     check( port == 80 )
+    tr_free( scheme );
     tr_free( path );
     tr_free( host );
 
     url = "http://www.some-tracker.org:80/some/path";
-    check( !tr_httpParseURL( url, -1, &host, &port, &path ) )
+    check( !tr_urlParse( url, -1, &scheme, &host, &port, &path ) )
+    check( !strcmp( scheme, "http" ) )
     check( !strcmp( host, "www.some-tracker.org" ) )
     check( !strcmp( path, "/some/path" ) )
     check( port == 80 )
+    tr_free( scheme );
     tr_free( path );
     tr_free( host );
 
index c7b4b52e7f8dd01b338aa6a867d080ce1757d816..65cc87d3efaf7b5ea313eba0b5d65d5a728b3872 100644 (file)
@@ -913,8 +913,8 @@ tr_hex_to_sha1( uint8_t * out, const char * in )
 ****
 ***/
 
-tr_bool
-tr_httpIsValidURL( const char * url )
+static tr_bool
+isValidURLChars( const char * url )
 {
     const char * c;
     static const char * rfc2396_valid_chars =
@@ -933,21 +933,55 @@ tr_httpIsValidURL( const char * url )
         if( !strchr( rfc2396_valid_chars, *c ) )
             return FALSE;
 
-    return tr_httpParseURL( url, -1, NULL, NULL, NULL ) == 0;
+    return TRUE;
+}
+
+/** @brief return TRUE if the url is a http or https url that Transmission understands */
+tr_bool
+tr_urlIsValidTracker( const char * url )
+{
+    tr_bool valid;
+    char * scheme = NULL;
+
+    valid = isValidURLChars( url ) 
+         && !tr_urlParse( url, -1, &scheme, NULL, NULL, NULL )
+         && ( scheme != NULL )
+         && ( !strcmp(scheme,"http") || !strcmp(scheme,"https") );
+
+    tr_free( scheme );
+    return valid;
 }
 
-tr_bool tr_addressIsIP( const char * address )
+/** @brief return TRUE if the url is a http or https or ftp or sftp url that Transmission understands */
+tr_bool
+tr_urlIsValid( const char * url )
+{
+    tr_bool valid;
+    char * scheme = NULL;
+
+    valid = isValidURLChars( url ) 
+         && !tr_urlParse( url, -1, &scheme, NULL, NULL, NULL )
+         && ( scheme != NULL )
+         && ( !strcmp(scheme,"http") || !strcmp(scheme,"https") || !strcmp(scheme,"ftp") || !strcmp(scheme,"sftp") );
+
+    tr_free( scheme );
+    return valid;
+}
+
+tr_bool
+tr_addressIsIP( const char * address )
 {
     tr_address tempAddr;
     return tr_pton(address, &tempAddr) != NULL;
 }
 
 int
-tr_httpParseURL( const char * url_in,
-                 int          len,
-                 char **      setme_host,
-                 int *        setme_port,
-                 char **      setme_path )
+tr_urlParse( const char * url_in,
+             int          len,
+             char **      setme_protocol,
+             char **      setme_host,
+             int *        setme_port,
+             char **      setme_path )
 {
     int          err;
     int          port = 0;
@@ -984,17 +1018,20 @@ tr_httpParseURL( const char * url_in,
         }
     }
 
-    err = !host || !path || !protocol
-          || ( strcmp( protocol, "http" ) && strcmp( protocol, "https" ) );
+    err = !host || !path || !protocol;
 
     if( !err && !port )
     {
+        if( !strcmp( protocol, "ftp" ) ) port = 21;
+        if( !strcmp( protocol, "sftp" ) ) port = 22;
         if( !strcmp( protocol, "http" ) ) port = 80;
         if( !strcmp( protocol, "https" ) ) port = 443;
     }
 
     if( !err )
     {
+        if( setme_protocol ) *setme_protocol = tr_strdup( protocol );
+
         if( setme_host ){ ( (char*)host )[-3] = ':'; *setme_host =
                               tr_strdup( host ); }
         if( setme_path ){ if( path[0] == '/' ) *setme_path = tr_strdup( path );
index 9e8710fc56de53d985a875de5683211b65741556..bf05ea64a86199d05827df6dea6626033ddb4147 100644 (file)
@@ -436,20 +436,23 @@ void tr_sha1_to_hex( char * out, const uint8_t * sha1 ) TR_GNUC_NONNULL(1,2);
 
 void tr_hex_to_sha1( uint8_t * out, const char * hex ) TR_GNUC_NONNULL(1,2);
 
-
-/** @brief return TRUE if the url is a http, https, or ftp url that Transmission understands */
-tr_bool tr_httpIsValidURL( const char * url ) TR_GNUC_NONNULL(1);
-
 /** @brief convenience function to determine if an address is an IP address (IPv4 or IPv6) */
 tr_bool tr_addressIsIP( const char * address );
 
+/** @brief return TRUE if the url is a http or https url that Transmission understands */
+tr_bool tr_urlIsValidTracker( const char * url ) TR_GNUC_NONNULL(1);
+
+/** @brief return TRUE if the url is a [ http, https, ftp, ftps ] url that Transmission understands */
+tr_bool tr_urlIsValid( const char * url ) TR_GNUC_NONNULL(1);
+
 /** @brief parse a URL into its component parts
     @return zero on success or an error number if an error occurred */
-int  tr_httpParseURL( const char * url,
-                      int          url_len,
-                      char      ** setme_host,
-                      int        * setme_port,
-                      char      ** setme_path ) TR_GNUC_NONNULL(1);
+int  tr_urlParse( const char * url,
+                  int          url_len,
+                  char      ** setme_scheme,
+                  char      ** setme_host,
+                  int        * setme_port,
+                  char      ** setme_path ) TR_GNUC_NONNULL(1);
 
 
 /** @brief return TR_RATIO_NA, TR_RATIO_INF, or a number in [0..1]
index 70d0e30002c687d7edde025d370cb7d6fdf59fb2..dacd48da5f9576771c7dcb489deec80e5f500edb 100644 (file)
@@ -207,7 +207,7 @@ dns_cache_lookup( struct tr_web_task * task, const char * host, const char ** re
 static void
 dns_cache_set_fail( struct tr_web_task * task, const char * host )
 {
-    if( task->session->web != NULL )
+    if( ( task->session->web != NULL ) && ( host != NULL ) )
     {
         struct dns_cache_item * item;
         tr_ptrArray * cache = &task->session->web->dns_cache;
@@ -463,7 +463,7 @@ doDNS( void * vtask )
 
     assert( task->resolved_host == NULL );
 
-    if( !tr_httpParseURL( task->url, -1, &host, &port, NULL ) )
+    if( !tr_urlParse( task->url, -1, NULL, &host, &port, NULL ) )
     {
         task->port = port;
         task->host = host;