(trunk libT) handle URLs, as well as filenames, via the rpc server's /transmission/upload mechanism

This commit is contained in:
Charles Kerr 2010-08-21 12:50:13 +00:00
parent 2d5ac5b150
commit 07e37057f2
2 changed files with 48 additions and 27 deletions

View File

@ -232,47 +232,68 @@ handle_upload( struct evhttp_request * req,
if( !hasSessionId ) if( !hasSessionId )
{ {
send_simple_response( req, 409, NULL ); int code = 409;
const char * codetext = tr_webGetResponseStr( code );
struct evbuffer * body = evbuffer_new( );
evbuffer_add_printf( body, "%s", "{ \"success\": false, \"msg\": \"Bad Session-Id\" }" );;
evhttp_send_reply( req, code, codetext, body );
evbuffer_free( body );
} }
else for( i=0; i<n; ++i ) else for( i=0; i<n; ++i )
{ {
struct tr_mimepart * p = tr_ptrArrayNth( &parts, i ); struct tr_mimepart * p = tr_ptrArrayNth( &parts, i );
if( strstr( p->headers, "filename=\"" ) )
{
char * b64;
int body_len = p->body_len; int body_len = p->body_len;
tr_benc top, *args; tr_benc top, *args;
const char * body = p->body; tr_benc test;
struct evbuffer * json = evbuffer_new( ); tr_bool have_source = FALSE;
char * body = p->body;
if( body_len >= 2 && !memcmp( &body[body_len - 2], "\r\n", 2 ) ) if( body_len >= 2 && !memcmp( &body[body_len - 2], "\r\n", 2 ) )
body_len -= 2; body_len -= 2;
tr_bencInitDict( &top, 2 ); tr_bencInitDict( &top, 2 );
args = tr_bencDictAddDict( &top, "arguments", 2 );
tr_bencDictAddStr( &top, "method", "torrent-add" ); tr_bencDictAddStr( &top, "method", "torrent-add" );
b64 = tr_base64_encode( body, body_len, NULL ); args = tr_bencDictAddDict( &top, "arguments", 2 );
tr_bencDictAddStr( args, "metainfo", b64 );
tr_bencDictAddBool( args, "paused", paused ); tr_bencDictAddBool( args, "paused", paused );
tr_bencToBuf( &top, TR_FMT_JSON_LEAN, json );
if( tr_urlIsValid( body, body_len ) )
{
tr_bencDictAddRaw( args, "filename", body, body_len );
have_source = TRUE;
}
else if( !tr_bencLoad( body, body_len, &test, NULL ) )
{
char * b64 = tr_base64_encode( body, body_len, NULL );
tr_bencDictAddStr( args, "metainfo", b64 );
tr_free( b64 );
have_source = TRUE;
}
if( have_source )
{
struct evbuffer * json = evbuffer_new( );
tr_bencToBuf( &top, TR_FMT_JSON, json );
tr_rpc_request_exec_json( server->session, tr_rpc_request_exec_json( server->session,
EVBUFFER_DATA( json ), EVBUFFER_DATA( json ),
EVBUFFER_LENGTH( json ), EVBUFFER_LENGTH( json ),
NULL, NULL ); NULL, NULL );
evbuffer_free( json ); evbuffer_free( json );
tr_free( b64 );
tr_bencFree( &top );
} }
tr_bencFree( &top );
} }
tr_ptrArrayDestruct( &parts, (PtrArrayForeachFunc)tr_mimepart_free ); tr_ptrArrayDestruct( &parts, (PtrArrayForeachFunc)tr_mimepart_free );
/* use xml here because json responses to file uploads is trouble. /* send "success" response */
* see http://www.malsup.com/jquery/form/#sample7 for details */ {
evhttp_add_header( req->output_headers, "Content-Type", int code = HTTP_OK;
"text/xml; charset=UTF-8" ); const char * codetext = tr_webGetResponseStr( code );
send_simple_response( req, HTTP_OK, NULL ); struct evbuffer * body = evbuffer_new( );
evbuffer_add_printf( body, "%s", "{ \"success\": true, \"msg\": \"Torrent Added\" }" );;
evhttp_send_reply( req, code, codetext, body );
evbuffer_free( body );
}
} }
} }

View File

@ -826,7 +826,7 @@ addTrackerUrls( tr_torrent * tor, tr_benc * urls )
const char * announce = NULL; const char * announce = NULL;
if( tr_bencGetStr( val, &announce ) if( tr_bencGetStr( val, &announce )
&& tr_urlIsValid( announce ) && tr_urlIsValid( announce, -1 )
&& !findAnnounceUrl( trackers, n, announce, NULL ) ) && !findAnnounceUrl( trackers, n, announce, NULL ) )
{ {
trackers[n].tier = ++tier; /* add a new tier */ trackers[n].tier = ++tier; /* add a new tier */
@ -871,7 +871,7 @@ replaceTrackerUrls( tr_torrent * tor, tr_benc * urls )
if( tr_bencGetStr( pair[0], &oldval ) if( tr_bencGetStr( pair[0], &oldval )
&& tr_bencGetStr( pair[1], &newval ) && tr_bencGetStr( pair[1], &newval )
&& strcmp( oldval, newval ) && strcmp( oldval, newval )
&& tr_urlIsValid( newval ) && tr_urlIsValid( newval, -1 )
&& findAnnounceUrl( trackers, n, oldval, &i ) ) && findAnnounceUrl( trackers, n, oldval, &i ) )
{ {
tr_free( trackers[i].announce ); tr_free( trackers[i].announce );