220
240
myStats.secondsActive = 0;
221
241
myCumulativeStats = myStats;
223
connect( &myHttp, SIGNAL(requestStarted(int)), this, SLOT(onRequestStarted(int)));
224
connect( &myHttp, SIGNAL(requestFinished(int,bool)), this, SLOT(onRequestFinished(int,bool)));
225
connect( &myHttp, SIGNAL(dataReadProgress(int,int)), this, SIGNAL(dataReadProgress()));
226
connect( &myHttp, SIGNAL(dataSendProgress(int,int)), this, SIGNAL(dataSendProgress()));
227
connect( &myHttp, SIGNAL(authenticationRequired(QString, quint16, QAuthenticator*)), this, SIGNAL(httpAuthenticationRequired()) );
243
connect( &myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)) );
244
connect( &myNAM, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SIGNAL(httpAuthenticationRequired()) );
228
245
connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(updatePref(int)) );
230
myBuffer.open( QIODevice::ReadWrite );
233
248
Session :: ~Session( )
573
Session :: exec( const char * request )
587
Session :: exec( const char * json )
577
tr_rpc_request_exec_json( mySession, request, strlen( request ), localSessionCallback, this );
591
tr_rpc_request_exec_json( mySession, json, strlen( json ), localSessionCallback, this );
579
593
else if( !myUrl.isEmpty( ) )
581
static const QString path( "/transmission/rpc" );
582
QHttpRequestHeader header( "POST", path );
583
header.setValue( "User-Agent", QCoreApplication::instance()->applicationName() + "/" + LONG_VERSION_STRING );
584
header.setValue( "Content-Type", "application/json; charset=UTF-8" );
595
QNetworkRequest request;
596
request.setUrl( myUrl );
597
request.setRawHeader( "User-Agent", QString( QCoreApplication::instance()->applicationName() + "/" + LONG_VERSION_STRING ).toAscii() );
598
request.setRawHeader( "Content-Type", "application/json; charset=UTF-8" );
585
599
if( !mySessionId.isEmpty( ) )
586
header.setValue( TR_RPC_SESSION_ID_HEADER, mySessionId );
600
request.setRawHeader( TR_RPC_SESSION_ID_HEADER, mySessionId.toAscii() );
587
602
QBuffer * reqbuf = new QBuffer;
588
reqbuf->setData( QByteArray( request ) );
589
myHttp.request( header, reqbuf, &myBuffer );
591
std::cerr << "sending " << qPrintable(header.toString()) << "\nBody:\n" << request << std::endl;
597
Session :: onRequestStarted( int id )
601
assert( myBuffer.atEnd( ) );
605
Session :: onRequestFinished( int id, bool error )
608
QIODevice * sourceDevice = myHttp.currentSourceDevice( );
610
QHttpResponseHeader response = myHttp.lastResponse();
613
std::cerr << "http request " << id << " ended.. response header: "
614
<< qPrintable( myHttp.lastResponse().toString() )
616
<< "json: " << myBuffer.buffer( ).constData( )
620
if( ( response.statusCode() == 409 ) && ( myBuffer.buffer().indexOf("invalid session-id") != -1 ) )
603
reqbuf->setData( QByteArray( json ) );
605
QNetworkReply * reply = myNAM.post( request, reqbuf );
606
connect( reply, SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(dataReadProgress()));
607
connect( reply, SIGNAL(uploadProgress(qint64,qint64)), this, SIGNAL(dataSendProgress()));
610
myReply.networkReply = reply;
611
myReply.buffer = reqbuf;
612
myReplies << myReply;
614
std::cerr << "sending " << "POST " << qPrintable( myUrl.path() ) << std::endl;
615
foreach( QByteArray b, request.rawHeaderList() )
616
std::cerr << b.constData()
618
<< request.rawHeader( b ).constData()
620
std::cerr << "Body:\n" << json << std::endl;
626
Session :: onFinished( QNetworkReply * reply )
629
for( QList<Reply>::iterator i = myReplies.begin(); i != myReplies.end(); ++i )
631
if( reply == i->networkReply )
634
myReplies.erase( i );
640
std::cerr << "http response header: " << std::endl;
641
foreach( QByteArray b, reply->rawHeaderList() )
642
std::cerr << b.constData()
644
<< reply->rawHeader( b ).constData()
646
std::cerr << "json:\n" << reply->peek( reply->bytesAvailable() ).constData() << std::endl;
649
if( ( reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt() == 409 )
650
&& ( reply->hasRawHeader( TR_RPC_SESSION_ID_HEADER ) ) )
622
652
// we got a 409 telling us our session id has expired.
623
653
// update it and resubmit the request.
624
mySessionId = response.value( TR_RPC_SESSION_ID_HEADER );
625
exec( qobject_cast<QBuffer*>(sourceDevice)->buffer().constData() );
654
mySessionId = QString( reply->rawHeader( TR_RPC_SESSION_ID_HEADER ) );
655
exec( buffer->buffer().constData() );
657
else if( reply->error() != QNetworkReply::NoError )
629
std::cerr << "http error: " << qPrintable(myHttp.errorString()) << std::endl;
659
std::cerr << "http error: " << qPrintable( reply->errorString() ) << std::endl;
633
const QByteArray& response( myBuffer.buffer( ) );
663
const QByteArray response( reply->readAll() );
634
664
const char * json( response.constData( ) );
635
665
int jsonLength( response.size( ) );
636
666
if( jsonLength>0 && json[jsonLength-1] == '\n' ) --jsonLength;
637
667
parseResponse( json, jsonLength );
641
myBuffer.buffer( ).clear( );
643
assert( myBuffer.bytesAvailable( ) < 1 );
671
reply->deleteLater();