(trunk) #1538 "Make Web UI URL configurable" -- added to trunk. Patch by wereHamster

This commit is contained in:
Charles Kerr
2010-12-12 18:22:11 +00:00
parent 4ce7896402
commit 3a5d17cfde
7 changed files with 136 additions and 61 deletions
+49 -39
View File
@@ -40,6 +40,7 @@
#define MY_NAME "transmission-remote"
#define DEFAULT_HOST "localhost"
#define DEFAULT_PORT atoi(TR_DEFAULT_RPC_PORT_STR)
#define DEFAULT_URL TR_DEFAULT_RPC_URL_STR "rpc/"
#define ARGUMENTS "arguments"
@@ -207,6 +208,8 @@ getUsage( void )
MY_NAME " [port] [options]\n"
" "
MY_NAME " [host:port] [options]\n"
" "
MY_NAME " [http://host:port/transmission/] [options]\n"
"\n"
"See the man page for detailed explanations and many examples.";
}
@@ -1591,7 +1594,7 @@ printSessionStats( tr_benc * top )
static char id[4096];
static int
processResponse( const char * host, int port, const void * response, size_t len )
processResponse( const char * rpcurl, const void * response, size_t len )
{
tr_benc top;
int status = EXIT_SUCCESS;
@@ -1654,7 +1657,7 @@ processResponse( const char * host, int port, const void * response, size_t len
if( !tr_bencDictFindStr( &top, "result", &str ) )
status |= EXIT_FAILURE;
else {
printf( "%s:%d responded: \"%s\"\n", host, port, str );
printf( "%s responded: \"%s\"\n", rpcurl, str );
if( strcmp( str, "success") )
status |= EXIT_FAILURE;
}
@@ -1693,17 +1696,16 @@ tr_curl_easy_init( struct evbuffer * writebuf )
}
static int
flush( const char * host, int port, tr_benc ** benc )
flush( const char * rpcurl, tr_benc ** benc )
{
CURLcode res;
CURL * curl;
int status = EXIT_SUCCESS;
struct evbuffer * buf = evbuffer_new( );
char * json = tr_bencToStr( *benc, TR_FMT_JSON_LEAN, NULL );
char * url = tr_strdup_printf( "http://%s:%d/transmission/rpc", host, port );
curl = tr_curl_easy_init( buf );
curl_easy_setopt( curl, CURLOPT_URL, url );
curl_easy_setopt( curl, CURLOPT_URL, rpcurl );
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, json );
curl_easy_setopt( curl, CURLOPT_TIMEOUT, getTimeoutSecs( json ) );
@@ -1712,7 +1714,7 @@ flush( const char * host, int port, tr_benc ** benc )
if(( res = curl_easy_perform( curl )))
{
tr_nerr( MY_NAME, "(%s) %s", url, curl_easy_strerror( res ) );
tr_nerr( MY_NAME, "(%s) %s", rpcurl, curl_easy_strerror( res ) );
status |= EXIT_FAILURE;
}
else
@@ -1721,7 +1723,7 @@ flush( const char * host, int port, tr_benc ** benc )
curl_easy_getinfo( curl, CURLINFO_RESPONSE_CODE, &response );
switch( response ) {
case 200:
status |= processResponse( host, port, EVBUFFER_DATA(buf), EVBUFFER_LENGTH(buf) );
status |= processResponse( rpcurl, EVBUFFER_DATA(buf), EVBUFFER_LENGTH(buf) );
break;
case 409:
/* session id failed. our curl header func has already
@@ -1729,7 +1731,7 @@ flush( const char * host, int port, tr_benc ** benc )
* build a new CURL* and try again */
curl_easy_cleanup( curl );
curl = NULL;
flush( host, port, benc );
flush( rpcurl, benc );
benc = NULL;
break;
default:
@@ -1740,7 +1742,6 @@ flush( const char * host, int port, tr_benc ** benc )
}
/* cleanup */
tr_free( url );
tr_free( json );
evbuffer_free( buf );
if( curl != 0 )
@@ -1787,7 +1788,7 @@ ensure_tset( tr_benc ** tset )
}
static int
processArgs( const char * host, int port, int argc, const char ** argv )
processArgs( const char * rpcurl, int argc, const char ** argv )
{
int c;
int status = EXIT_SUCCESS;
@@ -1807,9 +1808,9 @@ processArgs( const char * host, int port, int argc, const char ** argv )
switch( c )
{
case 'a': /* add torrent */
if( sset != 0 ) status |= flush( host, port, &sset );
if( tadd != 0 ) status |= flush( host, port, &tadd );
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( host, port, &tset ); }
if( sset != 0 ) status |= flush( rpcurl, &sset );
if( tadd != 0 ) status |= flush( rpcurl, &tadd );
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( rpcurl, &tset ); }
tadd = tr_new0( tr_benc, 1 );
tr_bencInitDict( tadd, 3 );
tr_bencDictAddStr( tadd, "method", "torrent-add" );
@@ -1841,8 +1842,8 @@ processArgs( const char * host, int port, int argc, const char ** argv )
break;
case 't': /* set current torrent */
if( tadd != 0 ) status |= flush( host, port, &tadd );
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( host, port, &tset ); }
if( tadd != 0 ) status |= flush( rpcurl, &tadd );
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( rpcurl, &tset ); }
tr_strlcpy( id, optarg, sizeof( id ) );
break;
@@ -1884,8 +1885,8 @@ processArgs( const char * host, int port, int argc, const char ** argv )
args = tr_bencDictAddDict( top, ARGUMENTS, 0 );
fields = tr_bencDictAddList( args, "fields", 0 );
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( host, port, &tset ); }
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( rpcurl, &tset ); }
switch( c )
{
case 'i': tr_bencDictAddInt( top, "tag", TAG_DETAILS );
@@ -1918,7 +1919,7 @@ processArgs( const char * host, int port, int argc, const char ** argv )
default: assert( "unhandled value" && 0 );
}
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
}
else if( stepMode == MODE_SESSION_SET )
{
@@ -2119,7 +2120,7 @@ processArgs( const char * host, int port, int argc, const char ** argv )
tr_bencDictAddStr( args, "location", optarg );
tr_bencDictAddBool( args, "move", FALSE );
addIdArg( args, id );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
break;
}
}
@@ -2131,7 +2132,7 @@ processArgs( const char * host, int port, int argc, const char ** argv )
tr_bencInitDict( top, 2 );
tr_bencDictAddStr( top, "method", "session-get" );
tr_bencDictAddInt( top, "tag", TAG_SESSION );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
break;
}
case 's': /* start */
@@ -2143,7 +2144,7 @@ processArgs( const char * host, int port, int argc, const char ** argv )
tr_bencInitDict( top, 2 );
tr_bencDictAddStr( top, "method", "torrent-start" );
addIdArg( tr_bencDictAddDict( top, ARGUMENTS, 1 ), id );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
}
break;
}
@@ -2156,7 +2157,7 @@ processArgs( const char * host, int port, int argc, const char ** argv )
tr_bencInitDict( top, 2 );
tr_bencDictAddStr( top, "method", "torrent-stop" );
addIdArg( tr_bencDictAddDict( top, ARGUMENTS, 1 ), id );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
}
break;
}
@@ -2177,7 +2178,7 @@ processArgs( const char * host, int port, int argc, const char ** argv )
tr_benc * top = tr_new0( tr_benc, 1 );
tr_bencInitDict( top, 1 );
tr_bencDictAddStr( top, "method", "blocklist-update" );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
break;
}
case 921:
@@ -2186,7 +2187,7 @@ processArgs( const char * host, int port, int argc, const char ** argv )
tr_bencInitDict( top, 2 );
tr_bencDictAddStr( top, "method", "session-stats" );
tr_bencDictAddInt( top, "tag", TAG_STATS );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
break;
}
case 962:
@@ -2195,29 +2196,29 @@ processArgs( const char * host, int port, int argc, const char ** argv )
tr_bencInitDict( top, 2 );
tr_bencDictAddStr( top, "method", "port-test" );
tr_bencDictAddInt( top, "tag", TAG_PORTTEST );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
break;
}
case 600:
{
tr_benc * top;
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( host, port, &tset ); }
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( rpcurl, &tset ); }
top = tr_new0( tr_benc, 1 );
tr_bencInitDict( top, 2 );
tr_bencDictAddStr( top, "method", "torrent-reannounce" );
addIdArg( tr_bencDictAddDict( top, ARGUMENTS, 1 ), id );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
break;
}
case 'v':
{
tr_benc * top;
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( host, port, &tset ); }
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( rpcurl, &tset ); }
top = tr_new0( tr_benc, 1 );
tr_bencInitDict( top, 2 );
tr_bencDictAddStr( top, "method", "torrent-verify" );
addIdArg( tr_bencDictAddDict( top, ARGUMENTS, 1 ), id );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
break;
}
case 'r':
@@ -2230,7 +2231,7 @@ processArgs( const char * host, int port, int argc, const char ** argv )
args = tr_bencDictAddDict( top, ARGUMENTS, 2 );
tr_bencDictAddBool( args, "delete-local-data", c=='R' );
addIdArg( args, id );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
break;
}
case 960:
@@ -2243,7 +2244,7 @@ processArgs( const char * host, int port, int argc, const char ** argv )
tr_bencDictAddStr( args, "location", optarg );
tr_bencDictAddBool( args, "move", TRUE );
addIdArg( args, id );
status |= flush( host, port, &top );
status |= flush( rpcurl, &top );
break;
}
default:
@@ -2255,22 +2256,27 @@ processArgs( const char * host, int port, int argc, const char ** argv )
}
}
if( tadd != 0 ) status |= flush( host, port, &tadd );
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( host, port, &tset ); }
if( sset != 0 ) status |= flush( host, port, &sset );
if( tadd != 0 ) status |= flush( rpcurl, &tadd );
if( tset != 0 ) { addIdArg( tr_bencDictFind( tset, ARGUMENTS ), id ); status |= flush( rpcurl, &tset ); }
if( sset != 0 ) status |= flush( rpcurl, &sset );
return status;
}
/* [host:port] or [host] or [port] */
/* [host:port] or [host] or [port] or [http://host:port/transmission/] */
static void
getHostAndPort( int * argc, char ** argv, char ** host, int * port )
getHostAndPortAndRpcUrl( int * argc, char ** argv,
char ** host, int * port, char ** rpcurl )
{
if( *argv[1] != '-' )
{
int i;
const char * s = argv[1];
const char * delim = strchr( s, ':' );
if( delim ) /* user passed in both host and port */
if( !strncmp(s, "http://", 7 ) ) /* user passed in full rpc url */
{
*rpcurl = tr_strdup_printf( "%s/rpc/", s );
}
else if( delim ) /* user passed in both host and port */
{
*host = tr_strndup( s, delim - s );
*port = atoi( delim + 1 );
@@ -2296,6 +2302,7 @@ main( int argc, char ** argv )
{
int port = DEFAULT_PORT;
char * host = NULL;
char * rpcurl = NULL;
int exit_status = EXIT_SUCCESS;
if( argc < 2 ) {
@@ -2307,12 +2314,15 @@ main( int argc, char ** argv )
tr_formatter_size_init( DISK_K,DISK_K_STR, DISK_M_STR, DISK_G_STR, DISK_T_STR );
tr_formatter_speed_init( SPEED_K, SPEED_K_STR, SPEED_M_STR, SPEED_G_STR, SPEED_T_STR );
getHostAndPort( &argc, argv, &host, &port );
getHostAndPortAndRpcUrl( &argc, argv, &host, &port, &rpcurl );
if( host == NULL )
host = tr_strdup( DEFAULT_HOST );
if( rpcurl == NULL )
rpcurl = tr_strdup_printf( "http://%s:%d%s", host, port, DEFAULT_URL );
exit_status = processArgs( host, port, argc, (const char**)argv );
exit_status = processArgs( rpcurl, argc, (const char**)argv );
tr_free( host );
tr_free( rpcurl );
return exit_status;
}