24 #include <QNetworkReply>
29 if ( mReply && mReply->isRunning() )
39 QNetworkRequest req( url );
47 QNetworkRequest request( r );
50 if ( !mAuthCfg.isEmpty() )
55 mContentLoaded =
false;
62 mReply->deleteLater();
67 if ( !mAuthCfg.isEmpty() )
71 mReply->setParent(
nullptr );
72 connect( mReply, &QNetworkReply::finished,
this, [ = ] { contentLoaded(); } );
75 auto onError = [ = ]( QNetworkReply::NetworkError code )
82 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
83 connect( mReply, qOverload<QNetworkReply::NetworkError>( &QNetworkReply::error ),
this, onError );
85 connect( mReply, &QNetworkReply::errorOccurred,
this, onError );
91 if ( !mContentLoaded )
101 if ( !mContentLoaded || !mReply )
106 QByteArray array = mReply->readAll();
109 QTextCodec *codec = codecForHtml( array );
110 return codec->toUnicode( array );
121 mReply->deleteLater();
131 QTextCodec *QgsNetworkContentFetcher::codecForHtml( QByteArray &array )
const
138 QTextCodec *codec = QTextCodec::codecForUtfText( array,
nullptr );
145 const QByteArray header = array.left( 1024 ).toLower();
146 int pos = header.indexOf(
"meta charset=" );
149 pos += int( strlen(
"meta charset=" ) ) + 1;
150 const int pos2 = header.indexOf(
'\"', pos );
151 const QByteArray cs = header.mid( pos, pos2 - pos );
152 codec = QTextCodec::codecForName( cs );
160 codec = QTextCodec::codecForHtml( array, codec );
167 return QTextCodec::codecForName(
"UTF-8" );
170 void QgsNetworkContentFetcher::contentLoaded(
bool ok )
180 if ( mReply->error() != QNetworkReply::NoError )
182 QgsMessageLog::logMessage( tr(
"HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), mReply->errorString() ) );
183 mContentLoaded =
true;
188 const QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
189 if ( redirect.isNull() )
192 const QVariant status = mReply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
193 if ( !status.isNull() && status.toInt() >= 400 )
195 QgsMessageLog::logMessage( tr(
"HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), status.toString() ) );
197 mContentLoaded =
true;
203 mReply->deleteLater();