32 #include <QNetworkReply>
33 #include <QNetworkDiskCache>
41 virtual QList<QNetworkProxy>
queryProxy(
const QNetworkProxyQuery & query = QNetworkProxyQuery() )
override
48 QList<QNetworkProxy> systemproxies = f->systemProxyForQuery( query );
49 if ( systemproxies.size() > 0 )
52 QList<QNetworkProxy> proxies = f->queryProxy( query );
53 if ( proxies.size() > 0 )
58 if ( query.queryType() != QNetworkProxyQuery::UrlRequest )
61 QString url = query.url().toString();
65 if ( url.startsWith( exclude ) )
67 QgsDebugMsg( QString(
"using default proxy for %1 [exclude %2]" ).arg( url ).arg( exclude ) );
68 return QList<QNetworkProxy>() << QNetworkProxy();
74 QgsDebugMsg( QString(
"requesting system proxy for query %1" ).arg( url ) );
75 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery( query );
76 if ( !proxies.isEmpty() )
78 QgsDebugMsg( QString(
"using system proxy %1:%2 for query" )
79 .arg( proxies.first().hostName() ).arg( proxies.first().port() ) );
84 QgsDebugMsg( QString(
"using fallback proxy for %1" ).arg( url ) );
90 : QNetworkAccessManager( parent )
91 , mUseSystemProxy( false )
102 mProxyFactories.insert( 0, factory );
107 mProxyFactories.removeAll( factory );
112 return mProxyFactories;
117 return mExcludedURLs;
122 return mFallbackProxy;
127 QgsDebugMsg( QString(
"proxy settings: (type:%1 host: %2:%3, user:%4, password:%5" )
128 .arg( proxy.type() == QNetworkProxy::DefaultProxy ?
"DefaultProxy" :
129 proxy.type() == QNetworkProxy::Socks5Proxy ?
"Socks5Proxy" :
130 proxy.type() == QNetworkProxy::NoProxy ?
"NoProxy" :
131 proxy.type() == QNetworkProxy::HttpProxy ?
"HttpProxy" :
132 proxy.type() == QNetworkProxy::HttpCachingProxy ?
"HttpCachingProxy" :
133 proxy.type() == QNetworkProxy::FtpCachingProxy ?
"FtpCachingProxy" :
135 .arg( proxy.hostName() )
138 .arg( proxy.password().isEmpty() ?
"not set" :
"set" ) );
140 mFallbackProxy = proxy;
141 mExcludedURLs = excludes;
148 QNetworkRequest *pReq(( QNetworkRequest * ) &req );
150 QString userAgent = s.value(
"/qgis/networkAndProxy/userAgent",
"Mozilla/5.0" ).toString();
151 if ( !userAgent.isEmpty() )
154 pReq->setRawHeader(
"User-Agent", userAgent.toUtf8() );
162 QTimer *timer =
new QTimer( reply );
163 connect( timer, SIGNAL( timeout() ),
this, SLOT( abortRequest() ) );
164 timer->setSingleShot(
true );
165 timer->start( s.value(
"/qgis/networkAndProxy/networkTimeout",
"20000" ).toInt() );
167 connect( reply, SIGNAL( downloadProgress( qint64, qint64 ) ), timer, SLOT( start() ) );
168 connect( reply, SIGNAL( uploadProgress( qint64, qint64 ) ), timer, SLOT( start() ) );
173 void QgsNetworkAccessManager::abortRequest()
175 QTimer *timer = qobject_cast<QTimer *>( sender() );
178 QNetworkReply *reply = qobject_cast<QNetworkReply *>( timer->parent() );
183 if ( reply->isRunning() )
191 switch ( theControl )
193 case QNetworkRequest::AlwaysNetwork:
194 return "AlwaysNetwork";
196 case QNetworkRequest::PreferNetwork:
197 return "PreferNetwork";
199 case QNetworkRequest::PreferCache:
200 return "PreferCache";
202 case QNetworkRequest::AlwaysCache:
203 return "AlwaysCache";
208 return "PreferNetwork";
213 if ( theName ==
"AlwaysNetwork" )
215 return QNetworkRequest::AlwaysNetwork;
217 else if ( theName ==
"PreferNetwork" )
219 return QNetworkRequest::PreferNetwork;
221 else if ( theName ==
"PreferCache" )
223 return QNetworkRequest::PreferCache;
225 else if ( theName ==
"AlwaysCache" )
227 return QNetworkRequest::AlwaysCache;
229 return QNetworkRequest::PreferNetwork;
235 QStringList excludes;
239 mUseSystemProxy =
false;
243 Qt::ConnectionType connectionType = thread() ==
instance()->thread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection;
245 connect(
this, SIGNAL( authenticationRequired( QNetworkReply *, QAuthenticator * ) ),
246 instance(), SIGNAL( authenticationRequired( QNetworkReply *, QAuthenticator * ) ),
249 connect(
this, SIGNAL( proxyAuthenticationRequired(
const QNetworkProxy &, QAuthenticator * ) ),
250 instance(), SIGNAL( proxyAuthenticationRequired(
const QNetworkProxy &, QAuthenticator * ) ),
256 #ifndef QT_NO_OPENSSL
257 connect(
this, SIGNAL( sslErrors( QNetworkReply *,
const QList<QSslError> & ) ),
258 instance(), SIGNAL( sslErrors( QNetworkReply *,
const QList<QSslError> & ) ),
264 bool proxyEnabled = settings.value(
"proxy/proxyEnabled",
false ).toBool();
267 excludes = settings.value(
"proxy/proxyExcludedUrls",
"" ).toString().split(
"|", QString::SkipEmptyParts );
270 QString proxyHost = settings.value(
"proxy/proxyHost",
"" ).toString();
271 int proxyPort = settings.value(
"proxy/proxyPort",
"" ).toString().toInt();
272 QString proxyUser = settings.value(
"proxy/proxyUser",
"" ).toString();
273 QString proxyPassword = settings.value(
"proxy/proxyPassword",
"" ).toString();
275 QString proxyTypeString = settings.value(
"proxy/proxyType",
"" ).toString();
277 if ( proxyTypeString ==
"DefaultProxy" )
279 mUseSystemProxy =
true;
280 QNetworkProxyFactory::setUseSystemConfiguration(
true );
281 QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery();
282 if ( !proxies.isEmpty() )
284 proxy = proxies.first();
290 QNetworkProxy::ProxyType proxyType = QNetworkProxy::DefaultProxy;
291 if ( proxyTypeString ==
"Socks5Proxy" )
293 proxyType = QNetworkProxy::Socks5Proxy;
295 else if ( proxyTypeString ==
"HttpProxy" )
297 proxyType = QNetworkProxy::HttpProxy;
299 else if ( proxyTypeString ==
"HttpCachingProxy" )
301 proxyType = QNetworkProxy::HttpCachingProxy;
303 else if ( proxyTypeString ==
"FtpCachingProxy" )
305 proxyType = QNetworkProxy::FtpCachingProxy;
307 QgsDebugMsg( QString(
"setting proxy %1 %2:%3 %4/%5" )
309 .arg( proxyHost ).arg( proxyPort )
310 .arg( proxyUser ).arg( proxyPassword )
312 proxy = QNetworkProxy( proxyType, proxyHost, proxyPort, proxyUser, proxyPassword );
316 #if QT_VERSION >= 0x40500
319 QNetworkDiskCache *newcache = qobject_cast<QNetworkDiskCache*>( cache() );
321 newcache =
new QNetworkDiskCache(
this );
324 qint64 cacheSize = settings.value(
"cache/size", 50 * 1024 * 1024 ).toULongLong();
325 QgsDebugMsg( QString(
"setCacheDirectory: %1" ).arg( cacheDirectory ) );
326 QgsDebugMsg( QString(
"setMaximumCacheSize: %1" ).arg( cacheSize ) );
327 newcache->setCacheDirectory( cacheDirectory );
328 newcache->setMaximumCacheSize( cacheSize );
329 QgsDebugMsg( QString(
"cacheDirectory: %1" ).arg( newcache->cacheDirectory() ) );
330 QgsDebugMsg( QString(
"maximumCacheSize: %1" ).arg( newcache->maximumCacheSize() ) );
332 if ( cache() != newcache )
333 setCache( newcache );