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;
/* 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;
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;
&& 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;
/* 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 */
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 );
****
***/
-tr_bool
-tr_httpIsValidURL( const char * url )
+static tr_bool
+isValidURLChars( const char * url )
{
const char * c;
static const char * rfc2396_valid_chars =
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;
}
}
- 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 );
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]
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;
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;