Changeset 11109


Ignore:
Timestamp:
Aug 4, 2010, 1:56:52 PM (12 years ago)
Author:
charles
Message:

(trunk qt) #3481 "crash when accessing a remote session that's password protected" -- fixed

Location:
trunk/qt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/session.cc

    r11092 r11109  
    235235    myPrefs( prefs ),
    236236    mySession( 0 ),
    237     myConfigDir( configDir )
     237    myConfigDir( configDir ),
     238    myNAM( 0 )
    238239{
    239240    myStats.ratio = TR_RATIO_NA;
     
    245246    myCumulativeStats = myStats;
    246247
    247     connect( &myNAM, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)) );
    248     connect( &myNAM, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)), this, SIGNAL(httpAuthenticationRequired()) );
    249248    connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(updatePref(int)) );
    250249}
     
    253252{
    254253    stop( );
     254}
     255
     256QNetworkAccessManager *
     257Session :: networkAccessManager( )
     258{
     259    if( myNAM == 0 )
     260    {
     261        myNAM = new QNetworkAccessManager;
     262
     263        connect( myNAM, SIGNAL(finished(QNetworkReply*)),
     264                 this, SLOT(onFinished(QNetworkReply*)) );
     265
     266        connect( myNAM, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
     267                 this, SIGNAL(httpAuthenticationRequired()) );
     268    }
     269
     270    return myNAM;
    255271}
    256272
     
    262278Session :: stop( )
    263279{
    264     foreach( Reply myReply, myReplies )
    265         myReply.networkReply->abort();
     280    if( myNAM != 0 )
     281    {
     282        myNAM->deleteLater( );
     283        myNAM = 0;
     284    }
     285
    266286    myUrl.clear( );
    267287
     
    285305    if( myPrefs.get<bool>(Prefs::SESSION_IS_REMOTE) )
    286306    {
    287         const int port( myPrefs.get<int>(Prefs::SESSION_REMOTE_PORT) );
    288         const bool auth( myPrefs.get<bool>(Prefs::SESSION_REMOTE_AUTH) );
    289         const QString host( myPrefs.get<QString>(Prefs::SESSION_REMOTE_HOST) );
    290         const QString user( myPrefs.get<QString>(Prefs::SESSION_REMOTE_USERNAME) );
    291         const QString pass( myPrefs.get<QString>(Prefs::SESSION_REMOTE_PASSWORD) );
    292 
    293307        QUrl url;
    294308        url.setScheme( "http" );
    295         url.setHost( host );
    296         url.setPort( port );
     309        url.setHost( myPrefs.get<QString>(Prefs::SESSION_REMOTE_HOST) );
     310        url.setPort( myPrefs.get<int>(Prefs::SESSION_REMOTE_PORT) );
    297311        url.setPath( "/transmission/rpc" );
    298         if( auth ) {
    299             url.setUserName( user );
    300             url.setPassword( pass );
     312        if( myPrefs.get<bool>(Prefs::SESSION_REMOTE_AUTH) )
     313        {
     314            url.setUserName( myPrefs.get<QString>(Prefs::SESSION_REMOTE_USERNAME) );
     315            url.setPassword( myPrefs.get<QString>(Prefs::SESSION_REMOTE_PASSWORD) );
    301316        }
    302317        myUrl = url;
     
    603618}
    604619
     620#define REQUEST_DATA_PROPERTY_KEY "requestData"
     621
    605622void
    606623Session :: exec( const char * json )
     
    619636            request.setRawHeader( TR_RPC_SESSION_ID_HEADER, mySessionId.toAscii() );
    620637
    621         QBuffer * reqbuf = new QBuffer;
    622         reqbuf->setData( QByteArray( json ) );
    623 
    624         QNetworkReply * reply = myNAM.post( request, reqbuf );
     638        const QByteArray requestData( json );
     639        QNetworkReply * reply = networkAccessManager()->post( request, requestData );
     640        reply->setProperty( REQUEST_DATA_PROPERTY_KEY, requestData );
    625641        connect( reply, SIGNAL(downloadProgress(qint64,qint64)), this, SIGNAL(dataReadProgress()));
    626642        connect( reply, SIGNAL(uploadProgress(qint64,qint64)), this, SIGNAL(dataSendProgress()));
    627643
    628         Reply myReply;
    629         myReply.networkReply = reply;
    630         myReply.buffer = reqbuf;
    631         myReplies << myReply;
    632644#ifdef DEBUG_HTTP
    633645        std::cerr << "sending " << "POST " << qPrintable( myUrl.path() ) << std::endl;
     
    645657Session :: onFinished( QNetworkReply * reply )
    646658{
    647     QBuffer * buffer;
    648     for( QList<Reply>::iterator i = myReplies.begin(); i != myReplies.end(); ++i )
    649     {
    650         if( reply == i->networkReply )
    651         {
    652             buffer = i->buffer;
    653             myReplies.erase( i );
    654             break;
    655         }
    656     }
    657 
    658659#ifdef DEBUG_HTTP
    659660    std::cerr << "http response header: " << std::endl;
     
    672673        // update it and resubmit the request.
    673674        mySessionId = QString( reply->rawHeader( TR_RPC_SESSION_ID_HEADER ) );
    674         exec( buffer->buffer().constData() );
     675        exec( reply->property( REQUEST_DATA_PROPERTY_KEY ).toByteArray( ).constData( ) );
    675676    }
    676677    else if( reply->error() != QNetworkReply::NoError )
     
    687688    }
    688689
    689     delete buffer;
    690690    reply->deleteLater();
    691691}
  • trunk/qt/session.h

    r11092 r11109  
    3434
    3535class Prefs;
    36 
    37 struct Reply
    38 {
    39     QNetworkReply * networkReply;
    40     QBuffer * buffer;
    41 };
    42 typedef QList<Reply> ReplyList;
    4336
    4437class Session: public QObject
     
    9790        static void updateStats( struct tr_benc * d, struct tr_session_stats * stats );
    9891        void refreshTorrents( const QSet<int>& torrentIds );
     92        QNetworkAccessManager * networkAccessManager( );
    9993
    10094    public:
     
    150144        QString mySessionId;
    151145        QUrl myUrl;
    152         QNetworkAccessManager myNAM;
    153         ReplyList myReplies;
     146        QNetworkAccessManager * myNAM;
    154147        struct tr_session_stats myStats;
    155148        struct tr_session_stats myCumulativeStats;
Note: See TracChangeset for help on using the changeset viewer.