Media downloader: Fix case with redirect response

This commit is contained in:
Abs62 2017-03-13 17:40:19 +03:00
parent fcb63af661
commit afd3dc2014
2 changed files with 37 additions and 1 deletions

View file

@ -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 );

View file

@ -13,6 +13,8 @@ class WebMultimediaDownload: public DataRequest
Q_OBJECT
QNetworkReply * reply;
QNetworkAccessManager & mgr;
int redirectCount;
public: