24 #include <QNetworkReply>
29 if ( mReply && mReply->isRunning() )
36 mReply->deleteLater();
42 QNetworkRequest req( url );
50 QNetworkRequest request( r );
53 if ( !mAuthCfg.isEmpty() )
58 mContentLoaded =
false;
65 mReply->deleteLater();
70 if ( !mAuthCfg.isEmpty() )
74 mReply->setParent(
nullptr );
75 connect( mReply, &QNetworkReply::finished,
this, [ = ] { contentLoaded(); } );
81 if ( !mContentLoaded )
91 if ( !mContentLoaded || !mReply )
96 QByteArray array = mReply->readAll();
99 QTextCodec *codec = codecForHtml( array );
100 return codec->toUnicode( array );
111 mReply->deleteLater();
121 QTextCodec *QgsNetworkContentFetcher::codecForHtml( QByteArray &array )
const
128 QTextCodec *codec = QTextCodec::codecForUtfText( array,
nullptr );
135 QByteArray header = array.left( 1024 ).toLower();
136 int pos = header.indexOf(
"meta charset=" );
139 pos += int( strlen(
"meta charset=" ) ) + 1;
140 int pos2 = header.indexOf(
'\"', pos );
141 QByteArray cs = header.mid( pos, pos2 - pos );
142 codec = QTextCodec::codecForName( cs );
150 codec = QTextCodec::codecForHtml( array, codec );
157 return QTextCodec::codecForName(
"UTF-8" );
160 void QgsNetworkContentFetcher::contentLoaded(
bool ok )
170 if ( mReply->error() != QNetworkReply::NoError )
172 QgsMessageLog::logMessage( tr(
"HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), mReply->errorString() ) );
173 mContentLoaded =
true;
178 QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
179 if ( redirect.isNull() )
182 QVariant status = mReply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
183 if ( !status.isNull() && status.toInt() >= 400 )
185 QgsMessageLog::logMessage( tr(
"HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), status.toString() ) );
187 mContentLoaded =
true;
193 mReply->deleteLater();