Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #53026 from qgis/backport-53004-to-release-3_30
  • Loading branch information
m-kuhn committed May 8, 2023
2 parents bca8021 + e2a090c commit fd77808
Showing 1 changed file with 9 additions and 47 deletions.
56 changes: 9 additions & 47 deletions src/server/qgsserverparameters.cpp
Expand Up @@ -15,9 +15,9 @@
* *
***************************************************************************/

#include "qgsblockingnetworkrequest.h"
#include "qgsserverparameters.h"
#include "qgsserverexception.h"
#include "qgsnetworkcontentfetcher.h"
#include "qgsmessagelog.h"
#include "qgsvariantutils.h"
#include <QObject>
Expand Down Expand Up @@ -332,65 +332,27 @@ QString QgsServerParameterDefinition::loadUrl( bool &ok ) const
return QString();
}

// fetching content
QgsNetworkContentFetcher fetcher;
QEventLoop loop;
QObject::connect( &fetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );

QgsMessageLog::logMessage(
QObject::tr( "Request started [url: %1]" ).arg( url.toString() ),
QStringLiteral( "Server" ) );
QNetworkRequest request( url );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
fetcher.fetchContent( request );

//wait until content fetched
loop.exec( QEventLoop::ExcludeUserInputEvents );

QNetworkReply *reply = fetcher.reply();
if ( !reply )
{
ok = false;
QgsMessageLog::logMessage(
QObject::tr( "Request failed [error: no reply - url: %1]" ).arg( url.toString() ),
QStringLiteral( "Server" ) );
return QString();
}

const QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
if ( status.isValid() && status.toInt() >= 400 )
{
ok = false;
if ( reply->error() != QNetworkReply::NoError )
{
QgsMessageLog::logMessage(
QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
QStringLiteral( "Server" ) );
}
const QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
QgsMessageLog::logMessage(
QObject::tr( "Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ),
QStringLiteral( "Server" ) );
return QString();
}
// fetching content
QgsBlockingNetworkRequest newReq;
const QgsBlockingNetworkRequest::ErrorCode errorCode = newReq.get( request, false );

if ( reply->error() != QNetworkReply::NoError )
if ( errorCode != QgsBlockingNetworkRequest::NoError )
{
ok = false;
QgsMessageLog::logMessage(
QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( newReq.errorMessage(), url.toString() ),
QStringLiteral( "Server" ) );
return QString();
}

QgsMessageLog::logMessage(
QObject::tr( "Request finished [url: %1]" ).arg( url.toString() ),
QStringLiteral( "Server" ) );
QgsNetworkReplyContent reply = newReq.reply();

QString content = fetcher.contentAsString();
ok = ( !content.isEmpty() );
return content;
ok = !reply.content().isEmpty();
return reply.content();
}

QUrl QgsServerParameterDefinition::toUrl( bool &ok ) const
Expand Down

0 comments on commit fd77808

Please sign in to comment.