diff --git a/libtransmission/utils-test.c b/libtransmission/utils-test.c index 95ba3abc9..7dde56f27 100644 --- a/libtransmission/utils-test.c +++ b/libtransmission/utils-test.c @@ -319,6 +319,30 @@ test_array( void ) return 0; } +static int +test_url( void ) +{ + int port; + char * host; + char * path; + const char * url; + + url = "http://www.some-tracker.org/some/path"; + check( !tr_httpParseURL( url, -1, &host, &port, &path ) ) + check( !strcmp( host, "www.some-tracker.org" ) ) + check( !strcmp( path, "/some/path" ) ) + check( port == 80 ) + + url = "http://www.some-tracker.org:80/some/path"; + check( !tr_httpParseURL( url, -1, &host, &port, &path ) ) + check( !strcmp( host, "www.some-tracker.org" ) ) + check( !strcmp( path, "/some/path" ) ) + check( port == 80 ) + + + return 0; +} + int main( void ) { @@ -365,6 +389,8 @@ main( void ) return i; if( ( i = test_array( ) ) ) return i; + if( ( i = test_url( ) ) ) + return i; /* test that tr_cryptoRandInt() stays in-bounds */ for( i = 0; i < 100000; ++i ) diff --git a/libtransmission/utils.c b/libtransmission/utils.c index a20c1adfb..49a8ba818 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -984,8 +984,7 @@ tr_httpParseURL( const char * url_in, *pch = '\0'; protocol = tmp; pch += 3; -/*fprintf( stderr, "protocol is [%s]... what's left is [%s]\n", protocol, pch - );*/ +/*fprintf( stderr, "protocol is [%s]... what's left is [%s]\n", protocol, pch);*/ if( ( n = strcspn( pch, ":/" ) ) ) { const int havePort = pch[n] == ':'; @@ -1017,9 +1016,9 @@ tr_httpParseURL( const char * url_in, if( !err ) { if( setme_host ){ ( (char*)host )[-3] = ':'; *setme_host = - tr_strdup( protocol ); } - if( setme_path ){ ( (char*)path )[-1] = '/'; *setme_path = - tr_strdup( path - 1 ); } + tr_strdup( host ); } + if( setme_path ){ if( path[0] == '/' ) *setme_path = tr_strdup( path ); + else { ( (char*)path )[-1] = '/'; *setme_path = tr_strdup( path - 1 ); } } if( setme_port ) *setme_port = port; }