diff --git a/webmultimediadownload.cc b/webmultimediadownload.cc index 326c3c46..47043e3e 100644 --- a/webmultimediadownload.cc +++ b/webmultimediadownload.cc @@ -3,13 +3,22 @@ namespace Dictionary { +#define MAX_REDIRECTS 10 + WebMultimediaDownload::WebMultimediaDownload( QUrl const & url, - QNetworkAccessManager & mgr ) + QNetworkAccessManager & _mgr ) : +mgr( _mgr ), +redirectCount( 0 ) { connect( &mgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)), Qt::QueuedConnection ); reply = mgr.get( QNetworkRequest( url ) ); + +#ifndef QT_NO_OPENSSL + connect( reply, SIGNAL( sslErrors( QList< QSslError > ) ), + reply, SLOT( ignoreSslErrors() ) ); +#endif } void WebMultimediaDownload::cancel() @@ -26,6 +35,31 @@ void WebMultimediaDownload::replyFinished( QNetworkReply * r ) if ( r->error() == QNetworkReply::NoError ) { + // Check for redirect reply + + QVariant possibleRedirectUrl = reply->attribute( QNetworkRequest::RedirectionTargetAttribute ); + QUrl redirectUrl = possibleRedirectUrl.toUrl(); + if( !redirectUrl.isEmpty() ) + { + reply->deleteLater(); + + if( ++redirectCount > MAX_REDIRECTS ) + { + setErrorString( "Too many redirects detected" ); + finish(); + return; + } + + reply = mgr.get( QNetworkRequest( redirectUrl ) ); +#ifndef QT_NO_OPENSSL + connect( reply, SIGNAL( sslErrors( QList< QSslError > ) ), + reply, SLOT( ignoreSslErrors() ) ); +#endif + return; + } + + // Handle reply data + QByteArray all = r->readAll(); Mutex::Lock _( dataMutex ); diff --git a/webmultimediadownload.hh b/webmultimediadownload.hh index 10e4831e..be3890a7 100644 --- a/webmultimediadownload.hh +++ b/webmultimediadownload.hh @@ -13,6 +13,8 @@ class WebMultimediaDownload: public DataRequest Q_OBJECT QNetworkReply * reply; + QNetworkAccessManager & mgr; + int redirectCount; public: