Skip to content

Commit 00cfe7d

Browse files
committedSep 27, 2018
[Server] Use QgsNetworkContentFetcher to loadUrl
1 parent 34009d3 commit 00cfe7d

File tree

1 file changed

+24
-113
lines changed

1 file changed

+24
-113
lines changed
 

‎src/server/qgsserverparameters.cpp

Lines changed: 24 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
#include "qgsserverparameters.h"
1919
#include "qgsserverexception.h"
20-
#include "qgsnetworkaccessmanager.h"
20+
#include "qgsnetworkcontentfetcher.h"
2121
#include "qgsmessagelog.h"
22+
#include <QObject>
2223
#include <QUrl>
2324
#include <QNetworkReply>
2425
#include <QNetworkRequest>
25-
#include <QTextCodec>
2626

2727
//
2828
// QgsServerParameterDefinition
@@ -217,140 +217,51 @@ QString QgsServerParameterDefinition::loadUrl( bool &ok ) const
217217
return QString();
218218
}
219219

220-
// Do the request
221-
QNetworkReply *reply = nullptr;
222-
// The following code blocks until the file is downloaded...
223-
// with max redirections fixed to 5
224-
int countRedirections = 0;
225-
QDateTime start = QDateTime::currentDateTime();
226-
while ( 1 )
227-
{
228-
QgsMessageLog::logMessage( QStringLiteral( "Request started [url: %2]" ).arg( url.toString() ) );
229-
QNetworkRequest request( url );
230-
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
231-
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
232-
233-
reply = QgsNetworkAccessManager::instance()->get( request );
234-
235-
// wait until the SLD download finished
236-
while ( !reply->isFinished() )
237-
{
238-
if ( start.secsTo( QDateTime::currentDateTime() ) >= 5 * 60 )
239-
break;
240-
QCoreApplication::processEvents( QEventLoop::ExcludeUserInputEvents, 500 );
241-
}
242-
243-
if ( !reply->isFinished() )
244-
{
245-
ok = false;
246-
break;
247-
}
248-
249-
250-
if ( reply->error() != QNetworkReply::NoError )
251-
{
252-
ok = false;
253-
QgsMessageLog::logMessage( QStringLiteral( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ) );
254-
break;
255-
}
220+
// fetching content
221+
QgsNetworkContentFetcher fetcher;
222+
QEventLoop loop;
223+
QObject::connect( &fetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
256224

257-
QVariant redirect = reply->attribute( QNetworkRequest::RedirectionTargetAttribute );
258-
if ( redirect.isNull() )
259-
{
260-
break;
261-
}
225+
QgsMessageLog::logMessage( QStringLiteral( "Request started [url: %1]" ).arg( url.toString() ) );
226+
QNetworkRequest request( url );
227+
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
228+
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
229+
fetcher.fetchContent( request );
262230

263-
// max redirections
264-
countRedirections++;
265-
if ( countRedirections >= 5 )
266-
{
267-
ok = false;
268-
break;
269-
}
270-
271-
// do a new request to the redirect url
272-
url = redirect.toUrl();
273-
reply->deleteLater();
274-
}
231+
//wait until content fetched
232+
loop.exec( QEventLoop::ExcludeUserInputEvents );
275233

276-
if ( !reply->isFinished() )
234+
QNetworkReply *reply = fetcher.reply();
235+
if ( !reply )
277236
{
278237
ok = false;
279-
QgsMessageLog::logMessage( QStringLiteral( "Request timeout [redirections: %1 - url: %2]" ).arg( countRedirections ).arg( mValue.toString() ) );
280-
281-
reply->abort();
282-
reply->deleteLater();
283-
284-
return QString();
285-
}
286-
287-
if ( countRedirections >= 5 )
288-
{
289-
ok = false;
290-
QgsMessageLog::logMessage( QStringLiteral( "Request failed [max redirection raised - url: %1]" ).arg( mValue.toString() ) );
291-
292-
reply->deleteLater();
238+
QgsMessageLog::logMessage( QStringLiteral( "Request failed [error: no reply - url: %1]" ).arg( url.toString() ) );
293239
return QString();
294240
}
295241

296242
QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
297243
if ( !status.isNull() && status.toInt() >= 400 )
298244
{
299245
ok = false;
246+
if ( reply->error() != QNetworkReply::NoError )
247+
{
248+
QgsMessageLog::logMessage( QStringLiteral( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ) );
249+
}
300250
QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
301251
QgsMessageLog::logMessage( QStringLiteral( "Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ) );
302-
303-
reply->deleteLater();
304252
return QString();
305253
}
306254

307255
if ( reply->error() != QNetworkReply::NoError )
308256
{
309257
ok = false;
310-
311-
reply->deleteLater();
258+
QgsMessageLog::logMessage( QStringLiteral( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ) );
312259
return QString();
313260
}
314261

315-
// read content
316-
QByteArray ba = reply->readAll();
317-
reply->deleteLater();
318-
319-
ok = ( !ba.isEmpty() );
320-
321-
//QTextCodec::codecForHtml fails to detect "<meta charset="utf-8"/>" type tags
322-
//see https://bugreports.qt.io/browse/QTBUG-41011
323-
//so test for that ourselves
324-
325-
//basic check
326-
QTextCodec *codec = QTextCodec::codecForUtfText( ba, nullptr );
327-
if ( !codec )
328-
{
329-
//check for meta charset tag
330-
QByteArray header = ba.left( 1024 ).toLower();
331-
int pos = header.indexOf( "meta charset=" );
332-
if ( pos != -1 )
333-
{
334-
pos += int( strlen( "meta charset=" ) ) + 1;
335-
int pos2 = header.indexOf( '\"', pos );
336-
QByteArray cs = header.mid( pos, pos2 - pos );
337-
codec = QTextCodec::codecForName( cs );
338-
}
339-
}
340-
341-
if ( !codec )
342-
{
343-
//fallback to QTextCodec::codecForHtml
344-
codec = QTextCodec::codecForHtml( ba, codec );
345-
}
346-
347-
if ( !codec )
348-
{
349-
//no luck, default to utf-8
350-
codec = QTextCodec::codecForName( "UTF-8" );
351-
}
352-
353-
return codec->toUnicode( ba );
262+
QString content = fetcher.contentAsString();
263+
ok = ( !content.isEmpty() );
264+
return content;
354265
}
355266

356267
QUrl QgsServerParameterDefinition::toUrl( bool &ok ) const

0 commit comments

Comments
 (0)
Please sign in to comment.