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