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();
#define QgsSetRequestInitiatorClass(request, _class)
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data is received.
bool wasCanceled() const
Returns true if the fetching was canceled.
QNetworkReply * reply()
Returns a reference to the network reply.
QString contentAsString() const
Returns the fetched content as a string.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
void finished()
Emitted when content has loaded.
void fetchContent(const QUrl &url, const QString &authcfg=QString())
Fetches content from a remote URL and handles redirects.
void cancel()
Cancels any ongoing request.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
bool updateNetworkRequest(QNetworkRequest &request, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QNetworkRequest with an authentication config.
~QgsNetworkContentFetcher() override
bool updateNetworkReply(QNetworkReply *reply, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QNetworkReply with an authentication config (used to skip known SSL errors...