Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor to simply and avoid QObject based QgsSslErrorHandler
  • Loading branch information
nyalldawson committed Jan 28, 2019
1 parent 610d865 commit 18f04fb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 45 deletions.
1 change: 0 additions & 1 deletion src/app/CMakeLists.txt
Expand Up @@ -252,7 +252,6 @@ SET (QGIS_APP_MOC_HDRS
qgsalignrasterdialog.h
qgsappbrowserproviders.h
qgsappscreenshots.h
qgsappsslerrorhandler.h
qgsjoindialog.h
qgsaddtaborgroup.h
qgsannotationwidget.h
Expand Down
3 changes: 1 addition & 2 deletions src/app/qgsappsslerrorhandler.h
Expand Up @@ -19,9 +19,8 @@

class QgsAppSslErrorHandler : public QgsSslErrorHandler
{
Q_OBJECT

public slots:
public:

void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors ) override;

Expand Down
26 changes: 14 additions & 12 deletions src/core/qgsnetworkaccessmanager.cpp
Expand Up @@ -127,10 +127,7 @@ QgsNetworkAccessManager::QgsNetworkAccessManager( QObject *parent )
void QgsNetworkAccessManager::setSslErrorHandler( std::unique_ptr<QgsSslErrorHandler> handler )
{
Q_ASSERT( sMainNAM == this );

mSslErrorHandler = std::move( handler );
connect( this, &QgsNetworkAccessManager::sslErrorsOccurred, mSslErrorHandler.get(), &QgsSslErrorHandler::onSslErrors );
connect( mSslErrorHandler.get(), &QgsSslErrorHandler::sslErrorsHandled, this, &QgsNetworkAccessManager::afterSslErrorHandled );
}

void QgsNetworkAccessManager::insertProxyFactory( QNetworkProxyFactory *factory )
Expand Down Expand Up @@ -256,11 +253,13 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
return reply;
}

#ifndef QT_NO_SSL
void QgsNetworkAccessManager::unlockAfterSslErrorHandled()
{
Q_ASSERT( QThread::currentThread() == QApplication::instance()->thread() );
mSslErrorWaitCondition.wakeOne();
}
#endif

void QgsNetworkAccessManager::abortRequest()
{
Expand Down Expand Up @@ -346,6 +345,12 @@ void QgsNetworkAccessManager::afterSslErrorHandled( QNetworkReply *reply )
qobject_cast< QgsNetworkAccessManager *>( reply->manager() )->unlockAfterSslErrorHandled(); // safe to call directly - the other thread will be stuck waiting for us
}
}

void QgsNetworkAccessManager::handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors )
{
mSslErrorHandler->handleSslErrors( reply, errors );
afterSslErrorHandled( reply );
}
#endif

QString QgsNetworkAccessManager::cacheLoadControlName( QNetworkRequest::CacheLoadControl control )
Expand Down Expand Up @@ -422,14 +427,17 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
connectionType );

connect( this, &QgsNetworkAccessManager::requestEncounteredSslErrors, sMainNAM, &QgsNetworkAccessManager::requestEncounteredSslErrors );
connect( this, &QgsNetworkAccessManager::sslErrorsOccurred, sMainNAM, &QgsNetworkAccessManager::sslErrorsOccurred );
#endif
}
else
{
#ifndef QT_NO_SSL
setSslErrorHandler( qgis::make_unique< QgsSslErrorHandler >() );
#endif
}

#ifndef QT_NO_SSL
connect( this, &QgsNetworkAccessManager::sslErrorsOccurred, sMainNAM, &QgsNetworkAccessManager::handleSslErrors );
#endif
connect( this, &QNetworkAccessManager::finished, this, &QgsNetworkAccessManager::onReplyFinished );

// check if proxy is enabled
Expand Down Expand Up @@ -539,14 +547,8 @@ QgsNetworkRequestParameters::QgsNetworkRequestParameters( QNetworkAccessManager:
// QgsSslErrorHandler
//

void QgsSslErrorHandler::onSslErrors( QNetworkReply *reply, const QList<QSslError> &errors )
{
Q_ASSERT( QThread::currentThread() == QApplication::instance()->thread() );
handleSslErrors( reply, errors );
emit sslErrorsHandled( reply );
}

void QgsSslErrorHandler::handleSslErrors( QNetworkReply *reply, const QList<QSslError> & )
{
Q_UNUSED( reply );
QgsDebugMsg( QStringLiteral( "SSL errors occurred accessing URL:\n%1" ).arg( reply->request().url().toString() ) );
}
36 changes: 9 additions & 27 deletions src/core/qgsnetworkaccessmanager.h
Expand Up @@ -162,22 +162,14 @@ class QgsNetworkAccessManager;
* SSL error handler is set by calling QgsNetworkAccessManager::setSslErrorHandler().
* By default an instance of the logging-only QgsSslErrorHandler base class is used.
*
* \since 3.6
* \since QGIS 3.6
*/
class CORE_EXPORT QgsSslErrorHandler : public QObject
class CORE_EXPORT QgsSslErrorHandler
{
Q_OBJECT

///@cond PRIVATE
public slots:

/**
* Called whenever SSL \a errors are encountered during a network \a reply.
*/
void onSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
///@endcond
public:

protected:
virtual ~QgsSslErrorHandler() = default;

/**
* Called whenever SSL \a errors are encountered during a network \a reply.
Expand All @@ -191,17 +183,6 @@ class CORE_EXPORT QgsSslErrorHandler : public QObject
*/
virtual void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );

signals:

/**
* Emitted when the SSL errors for a \a reply have been handled (i.e. the
* subclass' handleSslErrors() implementation has completed).
*
* It is not necessary for subclasses to emit this signal manually, it will
* automatically be emitted at the correct time.
*/
void sslErrorsHandled( QNetworkReply *reply );

};
#endif

Expand Down Expand Up @@ -414,16 +395,17 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
void onReplyDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
#ifndef QT_NO_SSL
void onReplySslErrors( const QList<QSslError> &errors );
#endif

void afterSslErrorHandled( QNetworkReply *reply );

void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors );
#endif
protected:
QNetworkReply *createRequest( QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData = nullptr ) override;

private:
#ifndef QT_NO_SSL
void unlockAfterSslErrorHandled();

void afterSslErrorHandled( QNetworkReply *reply );
#endif
QList<QNetworkProxyFactory *> mProxyFactories;
QNetworkProxy mFallbackProxy;
QStringList mExcludedURLs;
Expand Down
4 changes: 1 addition & 3 deletions tests/src/core/testqgsnetworkaccessmanager.cpp
Expand Up @@ -61,9 +61,7 @@ class BackgroundRequest : public QThread

class TestSslErrorHandler : public QgsSslErrorHandler
{
Q_OBJECT

protected:
public:

void handleSslErrors( QNetworkReply *reply, const QList<QSslError> &errors ) override
{
Expand Down

0 comments on commit 18f04fb

Please sign in to comment.