|
17 | 17 |
|
18 | 18 | #include "qgsserverparameters.h"
|
19 | 19 | #include "qgsserverexception.h"
|
20 |
| -#include "qgsnetworkaccessmanager.h" |
| 20 | +#include "qgsnetworkcontentfetcher.h" |
21 | 21 | #include "qgsmessagelog.h"
|
| 22 | +#include <QObject> |
22 | 23 | #include <QUrl>
|
23 | 24 | #include <QNetworkReply>
|
24 | 25 | #include <QNetworkRequest>
|
25 |
| -#include <QTextCodec> |
26 | 26 |
|
27 | 27 | //
|
28 | 28 | // QgsServerParameterDefinition
|
@@ -217,140 +217,51 @@ QString QgsServerParameterDefinition::loadUrl( bool &ok ) const
|
217 | 217 | return QString();
|
218 | 218 | }
|
219 | 219 |
|
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 ); |
256 | 224 |
|
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 ); |
262 | 230 |
|
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 ); |
275 | 233 |
|
276 |
| - if ( !reply->isFinished() ) |
| 234 | + QNetworkReply *reply = fetcher.reply(); |
| 235 | + if ( !reply ) |
277 | 236 | {
|
278 | 237 | 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() ) ); |
293 | 239 | return QString();
|
294 | 240 | }
|
295 | 241 |
|
296 | 242 | QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
|
297 | 243 | if ( !status.isNull() && status.toInt() >= 400 )
|
298 | 244 | {
|
299 | 245 | 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 | + } |
300 | 250 | QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
|
301 | 251 | 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(); |
304 | 252 | return QString();
|
305 | 253 | }
|
306 | 254 |
|
307 | 255 | if ( reply->error() != QNetworkReply::NoError )
|
308 | 256 | {
|
309 | 257 | ok = false;
|
310 |
| - |
311 |
| - reply->deleteLater(); |
| 258 | + QgsMessageLog::logMessage( QStringLiteral( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ) ); |
312 | 259 | return QString();
|
313 | 260 | }
|
314 | 261 |
|
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; |
354 | 265 | }
|
355 | 266 |
|
356 | 267 | QUrl QgsServerParameterDefinition::toUrl( bool &ok ) const
|
|
0 commit comments