QGIS API Documentation 4.3.0-Master (0cef2469b72)
Loading...
Searching...
No Matches
qgsnetworkcontentfetcher.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsnetworkcontentfetcher.cpp
3 -------------------
4 begin : July, 2014
5 copyright : (C) 2014 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7
8 ***************************************************************************/
9
10/***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
20
21#include "qgsapplication.h"
22#include "qgsauthmanager.h"
23#include "qgsmessagelog.h"
26#include "qgsvariantutils.h"
27
28#include <QNetworkReply>
29#include <QString>
30#include <QTextCodec>
31
32#include "moc_qgsnetworkcontentfetcher.cpp"
33
34using namespace Qt::StringLiterals;
35
37 : QObject( parent )
38{}
39
41{
42 if ( mReply && mReply->isRunning() )
43 {
44 //cancel running request
45 mReply->abort();
46 }
47}
48
49void QgsNetworkContentFetcher::fetchContent( const QUrl &url, const QString &authcfg, const QgsHttpHeaders &headers )
50{
51 QNetworkRequest req( url );
52 QgsSetRequestInitiatorClass( req, u"QgsNetworkContentFetcher"_s );
53
54 // Apply custom headers
55 headers.updateNetworkRequest( req );
56
57 fetchContent( req, authcfg );
58}
59
60void QgsNetworkContentFetcher::fetchContent( const QNetworkRequest &r, const QString &authcfg )
61{
62 QNetworkRequest request( r );
63 request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy );
64
65 mAuthCfg = authcfg;
66 if ( !mAuthCfg.isEmpty() )
67 {
69 }
70
71 mContentLoaded = false;
72 mIsCanceled = false;
73
74 if ( mReply )
75 {
76 //cancel any in progress requests
77 mReply->abort();
78 mReply.release()->deleteLater();
79 mReply = nullptr;
80 }
81
82 mReply.reset( QgsNetworkAccessManager::instance()->get( request ) );
83 if ( !mAuthCfg.isEmpty() )
84 {
85 QgsApplication::authManager()->updateNetworkReply( mReply.get(), mAuthCfg );
86 }
87 mReply->setParent( nullptr ); // we don't want thread locale QgsNetworkAccessManagers to delete the reply - we want ownership of it to belong to this object
88 connect( mReply.get(), &QNetworkReply::finished, this, [this] { contentLoaded(); } );
89 connect( mReply.get(), &QNetworkReply::downloadProgress, this, &QgsNetworkContentFetcher::downloadProgress );
90
91 auto onError = [this]( QNetworkReply::NetworkError code ) {
92 // could have been canceled in the meantime
93 if ( mReply )
94 emit errorOccurred( code, mReply->errorString() );
95 };
96
97 connect( mReply.get(), &QNetworkReply::errorOccurred, this, onError );
98}
99
101{
102 if ( !mContentLoaded )
103 {
104 return nullptr;
105 }
106
107 return mReply.get();
108}
109
114
116{
117 if ( !mContentLoaded || !mReply )
118 {
119 return QString();
120 }
121
122 QByteArray array = mReply->readAll();
123
124 //correctly encode reply as unicode
125 QTextCodec *codec = codecForHtml( array );
126 return codec->toUnicode( array );
127}
128
130{
131 mIsCanceled = true;
132
133 if ( mReply )
134 {
135 //cancel any in progress requests
136 mReply->abort();
137 mReply.release()->deleteLater();
138 mReply = nullptr;
139 }
140}
141
143{
144 return mIsCanceled;
145}
146
147QTextCodec *QgsNetworkContentFetcher::codecForHtml( QByteArray &array ) const
148{
149 //QTextCodec::codecForHtml fails to detect "<meta charset="utf-8"/>" type tags
150 //see https://bugreports.qt.io/browse/QTBUG-41011
151 //so test for that ourselves
152
153 //basic check
154 QTextCodec *codec = QTextCodec::codecForUtfText( array, nullptr );
155 if ( codec )
156 {
157 return codec;
158 }
159
160 //check for meta charset tag
161 const QByteArray header = array.left( 1024 ).toLower();
162 int pos = header.indexOf( "meta charset=" );
163 if ( pos != -1 )
164 {
165 pos += int( strlen( "meta charset=" ) ) + 1;
166 const int pos2 = header.indexOf( '\"', pos );
167 const QByteArray cs = header.mid( pos, pos2 - pos );
168 codec = QTextCodec::codecForName( cs );
169 if ( codec )
170 {
171 return codec;
172 }
173 }
174
175 //fallback to QTextCodec::codecForHtml
176 codec = QTextCodec::codecForHtml( array, codec );
177 if ( codec )
178 {
179 return codec;
180 }
181
182 //no luck, default to utf-8
183 return QTextCodec::codecForName( "UTF-8" );
184}
185
186void QgsNetworkContentFetcher::contentLoaded( bool ok )
187{
188 Q_UNUSED( ok )
189
190 if ( mIsCanceled )
191 {
192 emit finished();
193 return;
194 }
195
196 if ( mReply->error() != QNetworkReply::NoError )
197 {
198 QgsMessageLog::logMessage( tr( "HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), mReply->errorString() ) );
199 mContentLoaded = true;
200 emit finished();
201 return;
202 }
203
204 const QVariant redirect = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
205 if ( QgsVariantUtils::isNull( redirect ) )
206 {
207 //no error or redirect, got target
208 const QVariant status = mReply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
209 if ( !QgsVariantUtils::isNull( status ) && status.toInt() >= 400 )
210 {
211 QgsMessageLog::logMessage( tr( "HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), status.toString() ) );
212 }
213 mContentLoaded = true;
214 emit finished();
215 return;
216 }
217
218 //redirect, so fetch redirect target
219 mReply.release()->deleteLater();
220 fetchContent( redirect.toUrl(), mAuthCfg );
221}
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,...
Implements simple HTTP header management.
bool updateNetworkRequest(QNetworkRequest &request) const
Updates a request by adding all the HTTP headers.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
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 fetchContent(const QUrl &url, const QString &authcfg=QString(), const QgsHttpHeaders &headers=QgsHttpHeaders())
Fetches content from a remote URL and handles redirects.
void finished()
Emitted when content has loaded.
bool wasCanceled() const
Returns true if the fetching was canceled.
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.
QgsNetworkContentFetcher(QObject *parent=nullptr)
Constructor for QgsNetworkContentFetcher, with the specified parent object.
static QString extractFilenameFromContentDispositionHeader(QNetworkReply *reply)
Extracts the filename component of the content disposition header from a network reply.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
#define QgsSetRequestInitiatorClass(request, _class)