Qt 4.8
qnetworkproxy.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 
225 #include "qnetworkproxy.h"
226 
227 #ifndef QT_NO_NETWORKPROXY
228 
229 #include "private/qnetworkproxy_p.h"
230 #include "private/qsocks5socketengine_p.h"
231 #include "private/qhttpsocketengine_p.h"
232 #include "qauthenticator.h"
233 #include "qhash.h"
234 #include "qmutex.h"
235 #include "qurl.h"
236 
237 #ifndef QT_NO_BEARERMANAGEMENT
238 #include <QtNetwork/QNetworkConfiguration>
239 #endif
240 
242 
245 
247 {
248 public:
250  : mutex(QMutex::Recursive)
255  {
256 #ifdef QT_USE_SYSTEM_PROXIES
258 #endif
259 #ifndef QT_NO_SOCKS5
261 #endif
262 #ifndef QT_NO_HTTP
264 #endif
265  }
266 
268  {
269  delete applicationLevelProxy;
273  }
274 
276  {
280  *applicationLevelProxy = proxy;
283  }
284 
286  {
292  }
293 
295  {
297  }
298 
300 
301 private:
307 };
308 
310 {
311  QMutexLocker locker(&mutex);
312 
313  QList<QNetworkProxy> result;
314 
315  // don't look for proxies for a local connection
316  QHostAddress parsed;
317  QString hostname = query.url().host();
318  if (hostname == QLatin1String("localhost")
319  || hostname.startsWith(QLatin1String("localhost."))
320  || (parsed.setAddress(hostname)
321  && (parsed == QHostAddress::LocalHost
322  || parsed == QHostAddress::LocalHostIPv6))) {
324  return result;
325  }
326 
330  result << *applicationLevelProxy;
331  else
333  return result;
334  }
335 
336  // we have a factory
337  result = applicationLevelProxyFactory->queryProxy(query);
338  if (result.isEmpty()) {
339  qWarning("QNetworkProxyFactory: factory %p has returned an empty result set",
342  }
343  return result;
344 }
345 
346 Q_GLOBAL_STATIC(QGlobalNetworkProxy, globalNetworkProxy)
347 
348 namespace {
349  template<bool> struct StaticAssertTest;
350  template<> struct StaticAssertTest<true> { enum { Value = 1 }; };
351 }
352 
353 static inline void qt_noop_with_arg(int) {}
354 #define q_static_assert(expr) qt_noop_with_arg(sizeof(StaticAssertTest< expr >::Value))
355 
356 static QNetworkProxy::Capabilities defaultCapabilitiesForType(QNetworkProxy::ProxyType type)
357 {
360  static const int defaults[] =
361  {
362  /* [QNetworkProxy::DefaultProxy] = */
366  /* [QNetworkProxy::Socks5Proxy] = */
371  // it's weird to talk about the proxy capabilities of a "not proxy"...
372  /* [QNetworkProxy::NoProxy] = */
376  /* [QNetworkProxy::HttpProxy] = */
380  /* [QNetworkProxy::HttpCachingProxy] = */
383  /* [QNetworkProxy::FtpCachingProxy] = */
386  };
387 
388  if (int(type) < 0 || int(type) > int(QNetworkProxy::FtpCachingProxy))
390  return QNetworkProxy::Capabilities(defaults[int(type)]);
391 }
392 
394 {
395 public:
399  QNetworkProxy::Capabilities capabilities;
403 
405  const QString &h = QString(), quint16 p = 0,
406  const QString &u = QString(), const QString &pw = QString())
407  : hostName(h),
408  user(u),
409  password(pw),
410  capabilities(defaultCapabilitiesForType(t)),
411  port(p),
412  type(t),
413  capabilitiesSet(false)
414  { }
415 
416  inline bool operator==(const QNetworkProxyPrivate &other) const
417  {
418  return type == other.type &&
419  port == other.port &&
420  hostName == other.hostName &&
421  user == other.user &&
422  password == other.password &&
423  capabilities == other.capabilities;
424  }
425 };
426 
428 {
429  if (d && d->ref == 1)
430  return;
432  : new QNetworkProxyPrivate);
433  x->ref.ref();
434  if (d && !d->ref.deref())
435  delete d;
436  d = x;
437 }
438 
446  : d(0)
447 {
448  // make sure we have QGlobalNetworkProxy singleton created, otherwise
449  // you don't have any socket engine handler created when directly setting
450  // a proxy to the socket
451  globalNetworkProxy();
452 }
453 
463  const QString &user, const QString &password)
464  : d(new QNetworkProxyPrivate(type, hostName, port, user, password))
465 {
466  // make sure we have QGlobalNetworkProxy singleton created, otherwise
467  // you don't have any socket engine handler created when directly setting
468  // a proxy to a socket
469  globalNetworkProxy();
470 }
471 
476  : d(other.d)
477 {
478 }
479 
484 {
485  // QSharedDataPointer takes care of deleting for us
486 }
487 
494 bool QNetworkProxy::operator==(const QNetworkProxy &other) const
495 {
496  return d == other.d || (d && other.d && *d == *other.d);
497 }
498 
519 {
520  d = other.d;
521  return *this;
522 }
523 
534 {
535  d->type = type;
536  if (!d->capabilitiesSet)
538 }
539 
546 {
547  return d ? d->type : DefaultProxy;
548 }
549 
560 void QNetworkProxy::setCapabilities(Capabilities capabilities)
561 {
563  d->capabilitiesSet = true;
564 }
565 
576 QNetworkProxy::Capabilities QNetworkProxy::capabilities() const
577 {
579 }
580 
597 {
598  return capabilities() & CachingCapability;
599 }
600 
618 {
620 }
621 
628 {
629  d->user = user;
630 }
631 
638 {
639  return d ? d->user : QString();
640 }
641 
647 void QNetworkProxy::setPassword(const QString &password)
648 {
649  d->password = password;
650 }
651 
658 {
659  return d ? d->password : QString();
660 }
661 
667 void QNetworkProxy::setHostName(const QString &hostName)
668 {
669  d->hostName = hostName;
670 }
671 
678 {
679  return d ? d->hostName : QString();
680 }
681 
688 {
689  d->port = port;
690 }
691 
698 {
699  return d ? d->port : 0;
700 }
701 
717 {
718  if (globalNetworkProxy()) {
719  // don't accept setting the proxy to DefaultProxy
720  if (networkProxy.type() == DefaultProxy)
721  globalNetworkProxy()->setApplicationProxy(QNetworkProxy::NoProxy);
722  else
723  globalNetworkProxy()->setApplicationProxy(networkProxy);
724  }
725 }
726 
737 {
738  if (globalNetworkProxy())
739  return globalNetworkProxy()->applicationProxy();
740  return QNetworkProxy();
741 }
742 
744 {
745 public:
747  : localPort(-1), type(QNetworkProxyQuery::TcpSocket)
748  { }
749 
750  bool operator==(const QNetworkProxyQueryPrivate &other) const
751  {
752  return type == other.type &&
753  localPort == other.localPort &&
754  remote == other.remote;
755  }
756 
760 #ifndef QT_NO_BEARERMANAGEMENT
762 #endif
763 };
764 
766 {
767  if (d && d->ref == 1)
768  return;
770  : new QNetworkProxyQueryPrivate);
771  x->ref.ref();
772  if (d && !d->ref.deref())
773  delete d;
774  d = x;
775 }
776 
908 {
909 }
910 
918 {
919  d->remote = requestUrl;
920  d->type = queryType;
921 }
922 
931  const QString &protocolTag,
932  QueryType queryType)
933 {
934  d->remote.setScheme(protocolTag);
935  d->remote.setHost(hostname);
936  d->remote.setPort(port);
937  d->type = queryType;
938 }
939 
953  QueryType queryType)
954 {
955  d->remote.setScheme(protocolTag);
956  d->localPort = bindPort;
957  d->type = queryType;
958 }
959 
960 #ifndef QT_NO_BEARERMANAGEMENT
961 
974  const QUrl &requestUrl, QueryType queryType)
975 {
976  d->config = networkConfiguration;
977  d->remote = requestUrl;
978  d->type = queryType;
979 }
980 
997  const QString &hostname, int port,
998  const QString &protocolTag,
999  QueryType queryType)
1000 {
1001  d->config = networkConfiguration;
1002  d->remote.setScheme(protocolTag);
1003  d->remote.setHost(hostname);
1004  d->remote.setPort(port);
1005  d->type = queryType;
1006 }
1007 
1027  quint16 bindPort, const QString &protocolTag,
1028  QueryType queryType)
1029 {
1030  d->config = networkConfiguration;
1031  d->remote.setScheme(protocolTag);
1032  d->localPort = bindPort;
1033  d->type = queryType;
1034 }
1035 #endif
1036 
1041  : d(other.d)
1042 {
1043 }
1044 
1049 {
1050  // QSharedDataPointer automatically deletes
1051 }
1052 
1057 {
1058  d = other.d;
1059  return *this;
1060 }
1061 
1067 {
1068  return d == other.d || (d && other.d && *d == *other.d);
1069 }
1070 
1085 {
1086  return d ? d->type : TcpSocket;
1087 }
1088 
1093 {
1094  d->type = type;
1095 }
1096 
1108 {
1109  return d ? d->remote.port() : -1;
1110 }
1111 
1125 {
1126  d->remote.setPort(port);
1127 }
1128 
1140 {
1141  return d ? d->remote.host() : QString();
1142 }
1143 
1157 {
1158  d->remote.setHost(hostname);
1159 }
1160 
1168 {
1169  return d ? d->localPort : -1;
1170 }
1171 
1191 {
1192  d->localPort = port;
1193 }
1194 
1206 {
1207  return d ? d->remote.scheme() : QString();
1208 }
1209 
1225 {
1226  d->remote.setScheme(protocolTag);
1227 }
1228 
1236 {
1237  return d ? d->remote : QUrl();
1238 }
1239 
1250 {
1251  d->remote = url;
1252 }
1253 
1254 #ifndef QT_NO_BEARERMANAGEMENT
1255 
1261 {
1262  return d ? d->config : QNetworkConfiguration();
1263 }
1264 
1283 {
1285 }
1286 #endif
1287 
1338 {
1339 }
1340 
1345 {
1346 }
1347 
1348 
1369 {
1370  if (enable) {
1371  setApplicationProxyFactory(new QSystemConfigurationProxyFactory);
1372  } else {
1373  setApplicationProxyFactory(0);
1374  }
1375 }
1376 
1396 {
1397  if (globalNetworkProxy())
1398  globalNetworkProxy()->setApplicationProxyFactory(factory);
1399 }
1400 
1493 {
1494  if (!globalNetworkProxy())
1496  return globalNetworkProxy()->proxyForQuery(query);
1497 }
1498 
1500 
1501 #endif // QT_NO_NETWORKPROXY
virtual ~QNetworkProxyFactory()
Destroys the QNetworkProxyFactory object.
QNetworkProxy::ProxyType type() const
Returns the proxy type for this instance.
void setProtocolTag(const QString &protocolTag)
Sets the protocol tag for this QNetworkProxyQuery object to be protocolTag.
QAtomicInt ref
Definition: qshareddata.h:59
double d
Definition: qnumeric_p.h:62
int peerPort() const
Returns the port number for the outgoing request or -1 if the port number is not known.
bool operator==(const QNetworkProxyPrivate &other) const
QNetworkProxyQuery()
Constructs a default QNetworkProxyQuery object.
~QNetworkProxy()
Destroys the QNetworkProxy object.
QHttpSocketEngineHandler * httpSocketEngineHandler
bool operator==(const QNetworkProxyQuery &other) const
Returns true if this QNetworkProxyQuery object contains the same data as other.
QNetworkProxy & operator=(const QNetworkProxy &other)
Assigns the value of the network proxy other to this network proxy.
bool operator==(const QNetworkProxyQueryPrivate &other) const
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QNetworkProxy applicationProxy()
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
QNetworkProxyPrivate(QNetworkProxy::ProxyType t=QNetworkProxy::DefaultProxy, const QString &h=QString(), quint16 p=0, const QString &u=QString(), const QString &pw=QString())
void setPeerHostName(const QString &hostname)
Sets the hostname of the outgoing connection being requested to hostname.
QNetworkProxy()
Constructs a QNetworkProxy with DefaultProxy type; the proxy type is determined by applicationProxy()...
QString peerHostName() const
Returns the host name or IP address being of the outgoing connection being requested, or an empty string if the remote hostname is not known.
void detach()
If the shared data object&#39;s reference count is greater than 1, this function creates a deep copy of t...
Definition: qshareddata.h:75
QNetworkProxy * applicationLevelProxy
QSharedDataPointer< QNetworkProxyPrivate > d
bool isCachingProxy() const
Returns true if this proxy supports the QNetworkProxy::CachingCapability capability.
void setQueryType(QueryType type)
Sets the query type of this object to be type.
virtual QList< QNetworkProxy > queryProxy(const QNetworkProxyQuery &query=QNetworkProxyQuery())=0
This function takes the query request, query, examines the details of the type of socket or request a...
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QSocks5SocketEngineHandler * socks5SocketEngineHandler
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
quint16 u
void setUser(const QString &userName)
Sets the user name for proxy authentication to be user.
bool ref()
Atomically increments the value of this QAtomicInt.
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QNetworkProxyFactory class provides fine-grained proxy selection.
QString host() const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition: qurl.cpp:4837
void setHost(const QString &host)
Sets the host of the URL to host.
Definition: qurl.cpp:4821
static QList< QNetworkProxy > proxyForQuery(const QNetworkProxyQuery &query)
This function takes the query request, query, examines the details of the type of socket or request a...
QueryType
Describes the type of one QNetworkProxyQuery query.
Definition: qnetworkproxy.h:63
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
int localPort() const
Returns the port number of the socket that will accept incoming packets from remote servers or -1 if ...
int port() const
Returns the port of the URL, or -1 if the port is unspecified.
Definition: qurl.cpp:4916
static QNetworkProxy applicationProxy()
Returns the application level network proxying.
void setNetworkConfiguration(const QNetworkConfiguration &networkConfiguration)
Sets the network configuration component of this QNetworkProxyQuery object to be networkConfiguration...
QNetworkProxyFactory * applicationLevelProxyFactory
The QNetworkProxy class provides a network layer proxy.
QueryType queryType() const
Returns the query type.
The QNetworkConfiguration class provides an abstraction of one or more access point configurations...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QString protocolTag() const
Returns the protocol tag for this QNetworkProxyQuery object, or an empty QString in case the protocol...
QSharedDataPointer< QNetworkProxyQueryPrivate > d
Capabilities capabilities() const
Returns the capabilities of this proxy server.
void setApplicationProxy(const QNetworkProxy &proxy)
static void setApplicationProxyFactory(QNetworkProxyFactory *factory)
Sets the application-wide proxy factory to be factory.
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
bool operator==(const QNetworkProxy &other) const
bool deref()
Atomically decrements the value of this QAtomicInt.
void setApplicationProxyFactory(QNetworkProxyFactory *factory)
unsigned short quint16
Definition: qglobal.h:936
static void qt_noop_with_arg(int)
Q_CORE_EXPORT void qWarning(const char *,...)
void setUrl(const QUrl &url)
Sets the URL component of this QNetworkProxyQuery object to be url.
void setHostName(const QString &hostName)
Sets the host name of the proxy host to be hostName.
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
void setLocalPort(int port)
Sets the port number that the socket wishes to use locally to accept incoming packets from remote ser...
static QNetworkProxy::Capabilities defaultCapabilitiesForType(QNetworkProxy::ProxyType type)
QNetworkProxy::ProxyType type
QList< QNetworkProxy > proxyForQuery(const QNetworkProxyQuery &query)
The QNetworkProxyQuery class is used to query the proxy settings for a socket.
Definition: qnetworkproxy.h:60
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
QNetworkProxyFactory()
Creates a QNetworkProxyFactory object.
void setPeerPort(int port)
Sets the requested port number for the outgoing connection to be port.
QNetworkConfiguration networkConfiguration() const
Returns the network configuration of the proxy query.
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
void setPort(int port)
Sets the port of the URL to port.
Definition: qurl.cpp:4897
#define q_static_assert(expr)
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
The QSharedData class is a base class for shared data objects.
Definition: qshareddata.h:56
void setCapabilities(Capabilities capab)
Sets the capabilities of this proxy to capabilities.
QNetworkProxyQuery & operator=(const QNetworkProxyQuery &other)
Copies the contents of other.
static void setApplicationProxy(const QNetworkProxy &proxy)
Sets the application level network proxying to be networkProxy.
~QNetworkProxyQuery()
Destroys this QNetworkProxyQuery object.
static void setUseSystemConfiguration(bool enable)
Enables the use of the platform-specific proxy settings, and only those.
bool isTransparentProxy() const
Returns true if this proxy supports transparent tunneling of TCP connections.
quint16 port() const
Returns the port of the proxy host.
static QReadWriteLock lock
Definition: proxyconf.cpp:399
void setType(QNetworkProxy::ProxyType type)
Sets the proxy type for this instance to be type.
void setScheme(const QString &scheme)
Sets the scheme of the URL to scheme.
Definition: qurl.cpp:4533
QUrl url() const
Returns the URL component of this QNetworkProxyQuery object in case of a query of type QNetworkProxyQ...
QString password() const
Returns the password used for authentication.
QNetworkProxyQuery::QueryType type
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
QString user() const
Returns the user name used for authentication.
void setPassword(const QString &password)
Sets the password for proxy authentication to be password.
void setPort(quint16 port)
Sets the port of the proxy host to be port.
QNetworkProxy::Capabilities capabilities
ProxyType
This enum describes the types of network proxying provided in Qt.
QNetworkConfiguration config
QString hostName() const
Returns the host name of the proxy host.