From cd0de2ebe569c3c22c1a15a8212c599d5a5b1cd1 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 10 Dec 2009 09:13:08 +0000 Subject: [PATCH] (trunk libT) fix errors in tr_httpParseURL() --- libtransmission/utils-test.c | 26 ++++++++++++++++++++++++++ libtransmission/utils.c | 9 ++++----- 2 files changed, 30 insertions(+), 5 deletions(-) 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; }