mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
Media downloader: Fix case with redirect response
This commit is contained in:
parent
fcb63af661
commit
afd3dc2014
|
@ -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 );
|
||||
|
|
|
@ -13,6 +13,8 @@ class WebMultimediaDownload: public DataRequest
|
|||
Q_OBJECT
|
||||
|
||||
QNetworkReply * reply;
|
||||
QNetworkAccessManager & mgr;
|
||||
int redirectCount;
|
||||
|
||||
public:
|
||||
|
||||
|
|
Loading…
Reference in a new issue