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 )
106 if ( !mContentLoaded || !mReply )
111 QByteArray array = mReply->readAll();
114 QTextCodec *codec = codecForHtml( array );
115 return codec->toUnicode( array );
126 mReply->deleteLater();
136 QTextCodec *QgsNetworkContentFetcher::codecForHtml( QByteArray &array )
const
143 QTextCodec *codec = QTextCodec::codecForUtfText( array,
nullptr );
150 const QByteArray header = array.left( 1024 ).toLower();
151 int pos = header.indexOf(
"meta charset=" );
154 pos += int( strlen(
"meta charset=" ) ) + 1;
155 const int pos2 = header.indexOf(
'\"', pos );
156 const QByteArray cs = header.mid( pos, pos2 - pos );
157 codec = QTextCodec::codecForName( cs );
165 codec = QTextCodec::codecForHtml( array, codec );
172 return QTextCodec::codecForName(
"UTF-8" );
175 void QgsNetworkContentFetcher::contentLoaded(
bool ok )
185 if ( mReply->error() != QNetworkReply::NoError )
187 QgsMessageLog::logMessage( tr(
"HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), mReply->errorString() ) );
188 mContentLoaded =
true;
193 const QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
194 if ( redirect.isNull() )
197 const QVariant status = mReply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
198 if ( !status.isNull() && status.toInt() >= 400 )
200 QgsMessageLog::logMessage( tr(
"HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), status.toString() ) );
202 mContentLoaded =
true;
208 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::MessageLevel::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.
QString contentDispositionFilename() const
Returns the associated filename from the reply's content disposition header, if present.
void finished()
Emitted when content has loaded.
bool wasCanceled() const
Returns true if the fetching was canceled.
~QgsNetworkContentFetcher() override
void errorOccurred(QNetworkReply::NetworkError code, const QString &errorMsg)
Emitted when an error with code error occurred while processing the request errorMsg is a textual des...
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.
static QString extractFilenameFromContentDispositionHeader(QNetworkReply *reply)
Extracts the filename component of the content disposition header from a network reply.
#define QgsSetRequestInitiatorClass(request, _class)