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(); } );
78 if ( !mContentLoaded )
88 if ( !mContentLoaded || !mReply )
93 QByteArray array = mReply->readAll();
96 QTextCodec *codec = codecForHtml( array );
97 return codec->toUnicode( array );
108 mReply->deleteLater();
118 QTextCodec *QgsNetworkContentFetcher::codecForHtml( QByteArray &array )
const
125 QTextCodec *codec = QTextCodec::codecForUtfText( array,
nullptr );
132 QByteArray header = array.left( 1024 ).toLower();
133 int pos = header.indexOf(
"meta charset=" );
136 pos += int( strlen(
"meta charset=" ) ) + 1;
137 int pos2 = header.indexOf(
'\"', pos );
138 QByteArray cs = header.mid( pos, pos2 - pos );
139 codec = QTextCodec::codecForName( cs );
147 codec = QTextCodec::codecForHtml( array, codec );
154 return QTextCodec::codecForName(
"UTF-8" );
157 void QgsNetworkContentFetcher::contentLoaded(
bool ok )
167 if ( mReply->error() != QNetworkReply::NoError )
169 QgsMessageLog::logMessage( tr(
"HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), mReply->errorString() ) );
170 mContentLoaded =
true;
175 QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
176 if ( redirect.isNull() )
179 QVariant status = mReply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
180 if ( !status.isNull() && status.toInt() >= 400 )
182 QgsMessageLog::logMessage( tr(
"HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), status.toString() ) );
184 mContentLoaded =
true;
190 mReply->deleteLater();