[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | /*!
|
---|
| 44 | \class QNetworkProxy
|
---|
| 45 |
|
---|
| 46 | \since 4.1
|
---|
| 47 |
|
---|
| 48 | \brief The QNetworkProxy class provides a network layer proxy.
|
---|
| 49 |
|
---|
| 50 | \reentrant
|
---|
[561] | 51 | \ingroup network
|
---|
[2] | 52 | \inmodule QtNetwork
|
---|
| 53 |
|
---|
| 54 | QNetworkProxy provides the method for configuring network layer
|
---|
| 55 | proxy support to the Qt network classes. The currently supported
|
---|
| 56 | classes are QAbstractSocket, QTcpSocket, QUdpSocket, QTcpServer,
|
---|
[561] | 57 | QNetworkAccessManager and QFtp. The proxy support is designed to
|
---|
| 58 | be as transparent as possible. This means that existing
|
---|
| 59 | network-enabled applications that you have written should
|
---|
| 60 | automatically support network proxy using the following code.
|
---|
[2] | 61 |
|
---|
| 62 | \snippet doc/src/snippets/code/src_network_kernel_qnetworkproxy.cpp 0
|
---|
| 63 |
|
---|
| 64 | An alternative to setting an application wide proxy is to specify
|
---|
| 65 | the proxy for individual sockets using QAbstractSocket::setProxy()
|
---|
| 66 | and QTcpServer::setProxy(). In this way, it is possible to disable
|
---|
| 67 | the use of a proxy for specific sockets using the following code:
|
---|
| 68 |
|
---|
| 69 | \snippet doc/src/snippets/code/src_network_kernel_qnetworkproxy.cpp 1
|
---|
| 70 |
|
---|
| 71 | Network proxy is not used if the address used in \l
|
---|
| 72 | {QAbstractSocket::connectToHost()}{connectToHost()}, \l
|
---|
| 73 | {QUdpSocket::bind()}{bind()} or \l
|
---|
| 74 | {QTcpServer::listen()}{listen()} is equivalent to
|
---|
| 75 | QHostAddress::LocalHost or QHostAddress::LocalHostIPv6.
|
---|
| 76 |
|
---|
| 77 | Each type of proxy support has certain restrictions associated with it.
|
---|
| 78 | You should read the \l{ProxyType} documentation carefully before
|
---|
| 79 | selecting a proxy type to use.
|
---|
| 80 |
|
---|
| 81 | \note Changes made to currently connected sockets do not take effect.
|
---|
| 82 | If you need to change a connected socket, you should reconnect it.
|
---|
| 83 |
|
---|
| 84 | \section1 SOCKS5
|
---|
| 85 |
|
---|
| 86 | The SOCKS5 support in Qt 4 is based on \l{RFC 1928} and \l{RFC 1929}.
|
---|
| 87 | The supported authentication methods are no authentication and
|
---|
| 88 | username/password authentication. Both IPv4 and IPv6 are
|
---|
[561] | 89 | supported. Domain names are resolved through the SOCKS5 server if
|
---|
| 90 | the QNetworkProxy::HostNameLookupCapability is enabled, otherwise
|
---|
| 91 | they are resolved locally and the IP address is sent to the
|
---|
| 92 | server. There are several things to remember when using SOCKS5
|
---|
| 93 | with QUdpSocket and QTcpServer:
|
---|
[2] | 94 |
|
---|
| 95 | With QUdpSocket, a call to \l {QUdpSocket::bind()}{bind()} may fail
|
---|
| 96 | with a timeout error. If a port number other than 0 is passed to
|
---|
| 97 | \l {QUdpSocket::bind()}{bind()}, it is not guaranteed that it is the
|
---|
| 98 | specified port that will be used.
|
---|
| 99 | Use \l{QUdpSocket::localPort()}{localPort()} and
|
---|
| 100 | \l{QUdpSocket::localAddress()}{localAddress()} to get the actual
|
---|
| 101 | address and port number in use. Because proxied UDP goes through
|
---|
| 102 | two UDP connections, it is more likely that packets will be dropped.
|
---|
| 103 |
|
---|
| 104 | With QTcpServer a call to \l{QTcpServer::listen()}{listen()} may
|
---|
| 105 | fail with a timeout error. If a port number other than 0 is passed
|
---|
| 106 | to \l{QTcpServer::listen()}{listen()}, then it is not guaranteed
|
---|
| 107 | that it is the specified port that will be used.
|
---|
| 108 | Use \l{QTcpServer::serverPort()}{serverPort()} and
|
---|
| 109 | \l{QTcpServer::serverAddress()}{serverAddress()} to get the actual
|
---|
| 110 | address and port used to listen for connections. SOCKS5 only supports
|
---|
| 111 | one accepted connection per call to \l{QTcpServer::listen()}{listen()},
|
---|
| 112 | and each call is likely to result in a different
|
---|
| 113 | \l{QTcpServer::serverPort()}{serverPort()} being used.
|
---|
| 114 |
|
---|
| 115 | \sa QAbstractSocket, QTcpServer
|
---|
| 116 | */
|
---|
| 117 |
|
---|
| 118 | /*!
|
---|
| 119 | \enum QNetworkProxy::ProxyType
|
---|
| 120 |
|
---|
| 121 | This enum describes the types of network proxying provided in Qt.
|
---|
| 122 |
|
---|
| 123 | There are two types of proxies that Qt understands:
|
---|
| 124 | transparent proxies and caching proxies. The first group consists
|
---|
| 125 | of proxies that can handle any arbitrary data transfer, while the
|
---|
| 126 | second can only handle specific requests. The caching proxies only
|
---|
| 127 | make sense for the specific classes where they can be used.
|
---|
| 128 |
|
---|
| 129 | \value NoProxy No proxying is used
|
---|
| 130 | \value DefaultProxy Proxy is determined based on the application proxy set using setApplicationProxy()
|
---|
| 131 | \value Socks5Proxy \l Socks5 proxying is used
|
---|
| 132 | \value HttpProxy HTTP transparent proxying is used
|
---|
| 133 | \value HttpCachingProxy Proxying for HTTP requests only
|
---|
| 134 | \value FtpCachingProxy Proxying for FTP requests only
|
---|
| 135 |
|
---|
| 136 | The table below lists different proxy types and their
|
---|
| 137 | capabilities. Since each proxy type has different capabilities, it
|
---|
| 138 | is important to understand them before choosing a proxy type.
|
---|
| 139 |
|
---|
| 140 | \table
|
---|
| 141 | \header
|
---|
| 142 | \o Proxy type
|
---|
| 143 | \o Description
|
---|
| 144 | \o Default capabilities
|
---|
| 145 |
|
---|
| 146 | \row
|
---|
| 147 | \o SOCKS 5
|
---|
| 148 | \o Generic proxy for any kind of connection. Supports TCP,
|
---|
| 149 | UDP, binding to a port (incoming connections) and
|
---|
| 150 | authentication.
|
---|
| 151 | \o TunnelingCapability, ListeningCapability,
|
---|
| 152 | UdpTunnelingCapability, HostNameLookupCapability
|
---|
| 153 |
|
---|
| 154 | \row
|
---|
| 155 | \o HTTP
|
---|
| 156 | \o Implemented using the "CONNECT" command, supports only
|
---|
| 157 | outgoing TCP connections; supports authentication.
|
---|
| 158 | \o TunnelingCapability, CachingCapability, HostNameLookupCapability
|
---|
| 159 |
|
---|
| 160 | \row
|
---|
| 161 | \o Caching-only HTTP
|
---|
| 162 | \o Implemented using normal HTTP commands, it is useful only
|
---|
[561] | 163 | in the context of HTTP requests (see QNetworkAccessManager)
|
---|
[2] | 164 | \o CachingCapability, HostNameLookupCapability
|
---|
| 165 |
|
---|
| 166 | \row
|
---|
| 167 | \o Caching FTP
|
---|
| 168 | \o Implemented using an FTP proxy, it is useful only in the
|
---|
| 169 | context of FTP requests (see QFtp,
|
---|
| 170 | QNetworkAccessManager)
|
---|
| 171 | \o CachingCapability, HostNameLookupCapability
|
---|
| 172 |
|
---|
| 173 | \endtable
|
---|
| 174 |
|
---|
| 175 | Also note that you shouldn't set the application default proxy
|
---|
| 176 | (setApplicationProxy()) to a proxy that doesn't have the
|
---|
| 177 | TunnelingCapability capability. If you do, QTcpSocket will not
|
---|
| 178 | know how to open connections.
|
---|
| 179 |
|
---|
| 180 | \sa setType(), type(), capabilities(), setCapabilities()
|
---|
| 181 | */
|
---|
| 182 |
|
---|
| 183 | /*!
|
---|
| 184 | \enum QNetworkProxy::Capability
|
---|
| 185 | \since 4.5
|
---|
| 186 |
|
---|
| 187 | These flags indicate the capabilities that a given proxy server
|
---|
| 188 | supports.
|
---|
| 189 |
|
---|
| 190 | QNetworkProxy sets different capabilities by default when the
|
---|
| 191 | object is created (see QNetworkProxy::ProxyType for a list of the
|
---|
| 192 | defaults). However, it is possible to change the capabitilies
|
---|
| 193 | after the object has been created with setCapabilities().
|
---|
| 194 |
|
---|
| 195 | The capabilities that QNetworkProxy supports are:
|
---|
| 196 |
|
---|
| 197 | \value TunnelingCapability Ability to open transparent, tunneled
|
---|
| 198 | TCP connections to a remote host. The proxy server relays the
|
---|
| 199 | transmission verbatim from one side to the other and does no
|
---|
| 200 | caching.
|
---|
| 201 |
|
---|
| 202 | \value ListeningCapability Ability to create a listening socket
|
---|
| 203 | and wait for an incoming TCP connection from a remote host.
|
---|
| 204 |
|
---|
| 205 | \value UdpTunnelingCapability Ability to relay UDP datagrams via
|
---|
| 206 | the proxy server to and from a remote host.
|
---|
| 207 |
|
---|
| 208 | \value CachingCapability Ability to cache the contents of the
|
---|
| 209 | transfer. This capability is specific to each protocol and proxy
|
---|
| 210 | type. For example, HTTP proxies can cache the contents of web data
|
---|
| 211 | transferred with "GET" commands.
|
---|
| 212 |
|
---|
| 213 | \value HostNameLookupCapability Ability to connect to perform the
|
---|
| 214 | lookup on a remote host name and connect to it, as opposed to
|
---|
| 215 | requiring the application to perform the name lookup and request
|
---|
| 216 | connection to IP addresses only.
|
---|
| 217 | */
|
---|
| 218 |
|
---|
| 219 | #include "qnetworkproxy.h"
|
---|
| 220 |
|
---|
| 221 | #ifndef QT_NO_NETWORKPROXY
|
---|
| 222 |
|
---|
[561] | 223 | #include "private/qnetworkproxy_p.h"
|
---|
[2] | 224 | #include "private/qsocks5socketengine_p.h"
|
---|
| 225 | #include "private/qhttpsocketengine_p.h"
|
---|
| 226 | #include "qauthenticator.h"
|
---|
| 227 | #include "qhash.h"
|
---|
| 228 | #include "qmutex.h"
|
---|
| 229 | #include "qurl.h"
|
---|
| 230 |
|
---|
| 231 | QT_BEGIN_NAMESPACE
|
---|
| 232 |
|
---|
| 233 | class QSocks5SocketEngineHandler;
|
---|
| 234 | class QHttpSocketEngineHandler;
|
---|
| 235 |
|
---|
| 236 | class QGlobalNetworkProxy
|
---|
| 237 | {
|
---|
| 238 | public:
|
---|
| 239 | QGlobalNetworkProxy()
|
---|
| 240 | : mutex(QMutex::Recursive)
|
---|
| 241 | , applicationLevelProxy(0)
|
---|
| 242 | , applicationLevelProxyFactory(0)
|
---|
| 243 | , socks5SocketEngineHandler(0)
|
---|
| 244 | , httpSocketEngineHandler(0)
|
---|
| 245 | {
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | ~QGlobalNetworkProxy()
|
---|
| 249 | {
|
---|
| 250 | delete applicationLevelProxy;
|
---|
| 251 | delete applicationLevelProxyFactory;
|
---|
| 252 | delete socks5SocketEngineHandler;
|
---|
| 253 | delete httpSocketEngineHandler;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | void init()
|
---|
| 257 | {
|
---|
| 258 | QMutexLocker lock(&mutex);
|
---|
| 259 | #ifndef QT_NO_SOCKS5
|
---|
| 260 | if (!socks5SocketEngineHandler)
|
---|
| 261 | socks5SocketEngineHandler = new QSocks5SocketEngineHandler();
|
---|
| 262 | #endif
|
---|
| 263 | #ifndef QT_NO_HTTP
|
---|
| 264 | if (!httpSocketEngineHandler)
|
---|
| 265 | httpSocketEngineHandler = new QHttpSocketEngineHandler();
|
---|
| 266 | #endif
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | void setApplicationProxy(const QNetworkProxy &proxy)
|
---|
| 270 | {
|
---|
| 271 | QMutexLocker lock(&mutex);
|
---|
| 272 | if (!applicationLevelProxy)
|
---|
| 273 | applicationLevelProxy = new QNetworkProxy;
|
---|
| 274 | *applicationLevelProxy = proxy;
|
---|
| 275 | delete applicationLevelProxyFactory;
|
---|
| 276 | applicationLevelProxyFactory = 0;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | void setApplicationProxyFactory(QNetworkProxyFactory *factory)
|
---|
| 280 | {
|
---|
| 281 | QMutexLocker lock(&mutex);
|
---|
| 282 | if (applicationLevelProxy)
|
---|
| 283 | *applicationLevelProxy = QNetworkProxy();
|
---|
| 284 | delete applicationLevelProxyFactory;
|
---|
| 285 | applicationLevelProxyFactory = factory;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | QNetworkProxy applicationProxy()
|
---|
| 289 | {
|
---|
| 290 | return proxyForQuery(QNetworkProxyQuery()).first();
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | QList<QNetworkProxy> proxyForQuery(const QNetworkProxyQuery &query);
|
---|
| 294 |
|
---|
| 295 | private:
|
---|
| 296 | QMutex mutex;
|
---|
| 297 | QNetworkProxy *applicationLevelProxy;
|
---|
| 298 | QNetworkProxyFactory *applicationLevelProxyFactory;
|
---|
| 299 | QSocks5SocketEngineHandler *socks5SocketEngineHandler;
|
---|
| 300 | QHttpSocketEngineHandler *httpSocketEngineHandler;
|
---|
| 301 | };
|
---|
| 302 |
|
---|
| 303 | QList<QNetworkProxy> QGlobalNetworkProxy::proxyForQuery(const QNetworkProxyQuery &query)
|
---|
| 304 | {
|
---|
| 305 | QMutexLocker locker(&mutex);
|
---|
| 306 |
|
---|
| 307 | QList<QNetworkProxy> result;
|
---|
| 308 | if (!applicationLevelProxyFactory) {
|
---|
| 309 | if (applicationLevelProxy
|
---|
| 310 | && applicationLevelProxy->type() != QNetworkProxy::DefaultProxy)
|
---|
| 311 | result << *applicationLevelProxy;
|
---|
| 312 | else
|
---|
| 313 | result << QNetworkProxy(QNetworkProxy::NoProxy);
|
---|
| 314 | return result;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | // we have a factory
|
---|
| 318 | result = applicationLevelProxyFactory->queryProxy(query);
|
---|
| 319 | if (result.isEmpty()) {
|
---|
| 320 | qWarning("QNetworkProxyFactory: factory %p has returned an empty result set",
|
---|
| 321 | applicationLevelProxyFactory);
|
---|
| 322 | result << QNetworkProxy(QNetworkProxy::NoProxy);
|
---|
| 323 | }
|
---|
| 324 | return result;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[561] | 327 | Q_GLOBAL_STATIC(QGlobalNetworkProxy, globalNetworkProxy)
|
---|
[2] | 328 |
|
---|
| 329 | namespace {
|
---|
| 330 | template<bool> struct StaticAssertTest;
|
---|
| 331 | template<> struct StaticAssertTest<true> { enum { Value = 1 }; };
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | static inline void qt_noop_with_arg(int) {}
|
---|
| 335 | #define q_static_assert(expr) qt_noop_with_arg(sizeof(StaticAssertTest< expr >::Value))
|
---|
| 336 |
|
---|
| 337 | static QNetworkProxy::Capabilities defaultCapabilitiesForType(QNetworkProxy::ProxyType type)
|
---|
| 338 | {
|
---|
| 339 | q_static_assert(int(QNetworkProxy::DefaultProxy) == 0);
|
---|
| 340 | q_static_assert(int(QNetworkProxy::FtpCachingProxy) == 5);
|
---|
| 341 | static const int defaults[] =
|
---|
| 342 | {
|
---|
| 343 | /* [QNetworkProxy::DefaultProxy] = */
|
---|
| 344 | (int(QNetworkProxy::ListeningCapability) |
|
---|
| 345 | int(QNetworkProxy::TunnelingCapability) |
|
---|
| 346 | int(QNetworkProxy::UdpTunnelingCapability)),
|
---|
| 347 | /* [QNetworkProxy::Socks5Proxy] = */
|
---|
| 348 | (int(QNetworkProxy::TunnelingCapability) |
|
---|
| 349 | int(QNetworkProxy::ListeningCapability) |
|
---|
| 350 | int(QNetworkProxy::UdpTunnelingCapability) |
|
---|
| 351 | int(QNetworkProxy::HostNameLookupCapability)),
|
---|
| 352 | // it's weird to talk about the proxy capabilities of a "not proxy"...
|
---|
| 353 | /* [QNetworkProxy::NoProxy] = */
|
---|
| 354 | (int(QNetworkProxy::ListeningCapability) |
|
---|
| 355 | int(QNetworkProxy::TunnelingCapability) |
|
---|
| 356 | int(QNetworkProxy::UdpTunnelingCapability)),
|
---|
| 357 | /* [QNetworkProxy::HttpProxy] = */
|
---|
| 358 | (int(QNetworkProxy::TunnelingCapability) |
|
---|
| 359 | int(QNetworkProxy::CachingCapability) |
|
---|
| 360 | int(QNetworkProxy::HostNameLookupCapability)),
|
---|
| 361 | /* [QNetworkProxy::HttpCachingProxy] = */
|
---|
| 362 | (int(QNetworkProxy::CachingCapability) |
|
---|
| 363 | int(QNetworkProxy::HostNameLookupCapability)),
|
---|
| 364 | /* [QNetworkProxy::FtpCachingProxy] = */
|
---|
| 365 | (int(QNetworkProxy::CachingCapability) |
|
---|
| 366 | int(QNetworkProxy::HostNameLookupCapability)),
|
---|
| 367 | };
|
---|
| 368 |
|
---|
[561] | 369 | if (int(type) < 0 || int(type) > int(QNetworkProxy::FtpCachingProxy))
|
---|
| 370 | type = QNetworkProxy::DefaultProxy;
|
---|
[2] | 371 | return QNetworkProxy::Capabilities(defaults[int(type)]);
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | class QNetworkProxyPrivate: public QSharedData
|
---|
| 375 | {
|
---|
| 376 | public:
|
---|
| 377 | QString hostName;
|
---|
| 378 | QString user;
|
---|
| 379 | QString password;
|
---|
| 380 | QNetworkProxy::Capabilities capabilities;
|
---|
| 381 | quint16 port;
|
---|
| 382 | QNetworkProxy::ProxyType type;
|
---|
[561] | 383 | bool capabilitiesSet;
|
---|
[2] | 384 |
|
---|
| 385 | inline QNetworkProxyPrivate(QNetworkProxy::ProxyType t = QNetworkProxy::DefaultProxy,
|
---|
| 386 | const QString &h = QString(), quint16 p = 0,
|
---|
| 387 | const QString &u = QString(), const QString &pw = QString())
|
---|
| 388 | : hostName(h),
|
---|
| 389 | user(u),
|
---|
| 390 | password(pw),
|
---|
| 391 | capabilities(defaultCapabilitiesForType(t)),
|
---|
| 392 | port(p),
|
---|
[561] | 393 | type(t),
|
---|
| 394 | capabilitiesSet(false)
|
---|
[2] | 395 | { }
|
---|
| 396 |
|
---|
| 397 | inline bool operator==(const QNetworkProxyPrivate &other) const
|
---|
| 398 | {
|
---|
| 399 | return type == other.type &&
|
---|
| 400 | port == other.port &&
|
---|
| 401 | hostName == other.hostName &&
|
---|
| 402 | user == other.user &&
|
---|
| 403 | password == other.password &&
|
---|
| 404 | capabilities == other.capabilities;
|
---|
| 405 | }
|
---|
| 406 | };
|
---|
| 407 |
|
---|
| 408 | template<> void QSharedDataPointer<QNetworkProxyPrivate>::detach()
|
---|
| 409 | {
|
---|
| 410 | if (d && d->ref == 1)
|
---|
| 411 | return;
|
---|
| 412 | QNetworkProxyPrivate *x = (d ? new QNetworkProxyPrivate(*d)
|
---|
| 413 | : new QNetworkProxyPrivate);
|
---|
| 414 | x->ref.ref();
|
---|
| 415 | if (d && !d->ref.deref())
|
---|
| 416 | delete d;
|
---|
| 417 | d = x;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | /*!
|
---|
| 421 | Constructs a QNetworkProxy with DefaultProxy type; the proxy type is
|
---|
| 422 | determined by applicationProxy(), which defaults to NoProxy.
|
---|
| 423 |
|
---|
| 424 | \sa setType(), setApplicationProxy()
|
---|
| 425 | */
|
---|
| 426 | QNetworkProxy::QNetworkProxy()
|
---|
| 427 | : d(0)
|
---|
| 428 | {
|
---|
[846] | 429 | if (QGlobalNetworkProxy *globalProxy = globalNetworkProxy())
|
---|
| 430 | globalProxy->init();
|
---|
[2] | 431 | }
|
---|
| 432 |
|
---|
| 433 | /*!
|
---|
| 434 | Constructs a QNetworkProxy with \a type, \a hostName, \a port,
|
---|
| 435 | \a user and \a password.
|
---|
| 436 |
|
---|
| 437 | The default capabilities for proxy type \a type are set automatically.
|
---|
| 438 |
|
---|
| 439 | \sa capabilities()
|
---|
| 440 | */
|
---|
| 441 | QNetworkProxy::QNetworkProxy(ProxyType type, const QString &hostName, quint16 port,
|
---|
| 442 | const QString &user, const QString &password)
|
---|
| 443 | : d(new QNetworkProxyPrivate(type, hostName, port, user, password))
|
---|
| 444 | {
|
---|
[846] | 445 | if (QGlobalNetworkProxy *globalProxy = globalNetworkProxy())
|
---|
| 446 | globalProxy->init();
|
---|
[2] | 447 | }
|
---|
| 448 |
|
---|
| 449 | /*!
|
---|
| 450 | Constructs a copy of \a other.
|
---|
| 451 | */
|
---|
| 452 | QNetworkProxy::QNetworkProxy(const QNetworkProxy &other)
|
---|
| 453 | : d(other.d)
|
---|
| 454 | {
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | /*!
|
---|
| 458 | Destroys the QNetworkProxy object.
|
---|
| 459 | */
|
---|
| 460 | QNetworkProxy::~QNetworkProxy()
|
---|
| 461 | {
|
---|
| 462 | // QSharedDataPointer takes care of deleting for us
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | /*!
|
---|
| 466 | \since 4.4
|
---|
| 467 |
|
---|
| 468 | Compares the value of this network proxy to \a other and returns true
|
---|
| 469 | if they are equal (same proxy type, server as well as username and password)
|
---|
| 470 | */
|
---|
| 471 | bool QNetworkProxy::operator==(const QNetworkProxy &other) const
|
---|
| 472 | {
|
---|
| 473 | return d == other.d || (d && other.d && *d == *other.d);
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | /*!
|
---|
| 477 | \fn bool QNetworkProxy::operator!=(const QNetworkProxy &other) const
|
---|
| 478 | \since 4.4
|
---|
| 479 |
|
---|
| 480 | Compares the value of this network proxy to \a other and returns true
|
---|
| 481 | if they differ.
|
---|
| 482 | \*/
|
---|
| 483 |
|
---|
| 484 | /*!
|
---|
| 485 | \since 4.2
|
---|
| 486 |
|
---|
| 487 | Assigns the value of the network proxy \a other to this network proxy.
|
---|
| 488 | */
|
---|
| 489 | QNetworkProxy &QNetworkProxy::operator=(const QNetworkProxy &other)
|
---|
| 490 | {
|
---|
| 491 | d = other.d;
|
---|
| 492 | return *this;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | /*!
|
---|
| 496 | Sets the proxy type for this instance to be \a type.
|
---|
| 497 |
|
---|
| 498 | Note that changing the type of a proxy does not change
|
---|
[561] | 499 | the set of capabilities this QNetworkProxy object holds if any
|
---|
| 500 | capabilities have been set with setCapabilities().
|
---|
[2] | 501 |
|
---|
| 502 | \sa type(), setCapabilities()
|
---|
| 503 | */
|
---|
| 504 | void QNetworkProxy::setType(QNetworkProxy::ProxyType type)
|
---|
| 505 | {
|
---|
| 506 | d->type = type;
|
---|
[561] | 507 | if (!d->capabilitiesSet)
|
---|
| 508 | d->capabilities = defaultCapabilitiesForType(type);
|
---|
[2] | 509 | }
|
---|
| 510 |
|
---|
| 511 | /*!
|
---|
| 512 | Returns the proxy type for this instance.
|
---|
| 513 |
|
---|
| 514 | \sa setType()
|
---|
| 515 | */
|
---|
| 516 | QNetworkProxy::ProxyType QNetworkProxy::type() const
|
---|
| 517 | {
|
---|
| 518 | return d ? d->type : DefaultProxy;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | /*!
|
---|
| 522 | \since 4.5
|
---|
| 523 |
|
---|
| 524 | Sets the capabilities of this proxy to \a capabilities.
|
---|
| 525 |
|
---|
| 526 | \sa setType(), capabilities()
|
---|
| 527 | */
|
---|
| 528 | void QNetworkProxy::setCapabilities(Capabilities capabilities)
|
---|
| 529 | {
|
---|
| 530 | d->capabilities = capabilities;
|
---|
[561] | 531 | d->capabilitiesSet = true;
|
---|
[2] | 532 | }
|
---|
| 533 |
|
---|
| 534 | /*!
|
---|
| 535 | \since 4.5
|
---|
| 536 |
|
---|
| 537 | Returns the capabilities of this proxy server.
|
---|
| 538 |
|
---|
| 539 | \sa setCapabilities(), type()
|
---|
| 540 | */
|
---|
| 541 | QNetworkProxy::Capabilities QNetworkProxy::capabilities() const
|
---|
| 542 | {
|
---|
| 543 | return d ? d->capabilities : defaultCapabilitiesForType(DefaultProxy);
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | /*!
|
---|
| 547 | \since 4.4
|
---|
| 548 |
|
---|
| 549 | Returns true if this proxy supports the
|
---|
| 550 | QNetworkProxy::CachingCapability capability.
|
---|
| 551 |
|
---|
| 552 | In Qt 4.4, the capability was tied to the proxy type, but since Qt
|
---|
| 553 | 4.5 it is possible to remove the capability of caching from a
|
---|
| 554 | proxy by calling setCapabilities().
|
---|
| 555 |
|
---|
| 556 | \sa capabilities(), type(), isTransparentProxy()
|
---|
| 557 | */
|
---|
| 558 | bool QNetworkProxy::isCachingProxy() const
|
---|
| 559 | {
|
---|
| 560 | return capabilities() & CachingCapability;
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | /*!
|
---|
| 564 | \since 4.4
|
---|
| 565 |
|
---|
| 566 | Returns true if this proxy supports transparent tunneling of TCP
|
---|
| 567 | connections. This matches the QNetworkProxy::TunnelingCapability
|
---|
| 568 | capability.
|
---|
| 569 |
|
---|
| 570 | In Qt 4.4, the capability was tied to the proxy type, but since Qt
|
---|
| 571 | 4.5 it is possible to remove the capability of caching from a
|
---|
| 572 | proxy by calling setCapabilities().
|
---|
| 573 |
|
---|
| 574 | \sa capabilities(), type(), isCachingProxy()
|
---|
| 575 | */
|
---|
| 576 | bool QNetworkProxy::isTransparentProxy() const
|
---|
| 577 | {
|
---|
| 578 | return capabilities() & TunnelingCapability;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | /*!
|
---|
| 582 | Sets the user name for proxy authentication to be \a user.
|
---|
| 583 |
|
---|
| 584 | \sa user(), setPassword(), password()
|
---|
| 585 | */
|
---|
| 586 | void QNetworkProxy::setUser(const QString &user)
|
---|
| 587 | {
|
---|
| 588 | d->user = user;
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | /*!
|
---|
| 592 | Returns the user name used for authentication.
|
---|
| 593 |
|
---|
| 594 | \sa setUser(), setPassword(), password()
|
---|
| 595 | */
|
---|
| 596 | QString QNetworkProxy::user() const
|
---|
| 597 | {
|
---|
| 598 | return d ? d->user : QString();
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | /*!
|
---|
| 602 | Sets the password for proxy authentication to be \a password.
|
---|
| 603 |
|
---|
| 604 | \sa user(), setUser(), password()
|
---|
| 605 | */
|
---|
| 606 | void QNetworkProxy::setPassword(const QString &password)
|
---|
| 607 | {
|
---|
| 608 | d->password = password;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | /*!
|
---|
| 612 | Returns the password used for authentication.
|
---|
| 613 |
|
---|
| 614 | \sa user(), setPassword(), setUser()
|
---|
| 615 | */
|
---|
| 616 | QString QNetworkProxy::password() const
|
---|
| 617 | {
|
---|
| 618 | return d ? d->password : QString();
|
---|
| 619 | }
|
---|
| 620 |
|
---|
| 621 | /*!
|
---|
| 622 | Sets the host name of the proxy host to be \a hostName.
|
---|
| 623 |
|
---|
| 624 | \sa hostName(), setPort(), port()
|
---|
| 625 | */
|
---|
| 626 | void QNetworkProxy::setHostName(const QString &hostName)
|
---|
| 627 | {
|
---|
| 628 | d->hostName = hostName;
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | /*!
|
---|
| 632 | Returns the host name of the proxy host.
|
---|
| 633 |
|
---|
| 634 | \sa setHostName(), setPort(), port()
|
---|
| 635 | */
|
---|
| 636 | QString QNetworkProxy::hostName() const
|
---|
| 637 | {
|
---|
| 638 | return d ? d->hostName : QString();
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | /*!
|
---|
| 642 | Sets the port of the proxy host to be \a port.
|
---|
| 643 |
|
---|
| 644 | \sa hostName(), setHostName(), port()
|
---|
| 645 | */
|
---|
| 646 | void QNetworkProxy::setPort(quint16 port)
|
---|
| 647 | {
|
---|
| 648 | d->port = port;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | /*!
|
---|
| 652 | Returns the port of the proxy host.
|
---|
| 653 |
|
---|
| 654 | \sa setHostName(), setPort(), hostName()
|
---|
| 655 | */
|
---|
| 656 | quint16 QNetworkProxy::port() const
|
---|
| 657 | {
|
---|
| 658 | return d ? d->port : 0;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | /*!
|
---|
| 662 | Sets the application level network proxying to be \a networkProxy.
|
---|
| 663 |
|
---|
| 664 | If a QAbstractSocket or QTcpSocket has the
|
---|
| 665 | QNetworkProxy::DefaultProxy type, then the QNetworkProxy set with
|
---|
| 666 | this function is used. If you want more flexibility in determining
|
---|
| 667 | which the proxy, use the QNetworkProxyFactory class.
|
---|
| 668 |
|
---|
| 669 | Setting a default proxy value with this function will override the
|
---|
| 670 | application proxy factory set with
|
---|
| 671 | QNetworkProxyFactory::setApplicationProxyFactory.
|
---|
| 672 |
|
---|
| 673 | \sa QNetworkProxyFactory, applicationProxy(), QAbstractSocket::setProxy(), QTcpServer::setProxy()
|
---|
| 674 | */
|
---|
| 675 | void QNetworkProxy::setApplicationProxy(const QNetworkProxy &networkProxy)
|
---|
| 676 | {
|
---|
| 677 | if (globalNetworkProxy()) {
|
---|
| 678 | // don't accept setting the proxy to DefaultProxy
|
---|
| 679 | if (networkProxy.type() == DefaultProxy)
|
---|
| 680 | globalNetworkProxy()->setApplicationProxy(QNetworkProxy::NoProxy);
|
---|
| 681 | else
|
---|
| 682 | globalNetworkProxy()->setApplicationProxy(networkProxy);
|
---|
| 683 | }
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 | /*!
|
---|
| 687 | Returns the application level network proxying.
|
---|
| 688 |
|
---|
| 689 | If a QAbstractSocket or QTcpSocket has the
|
---|
| 690 | QNetworkProxy::DefaultProxy type, then the QNetworkProxy returned
|
---|
| 691 | by this function is used.
|
---|
| 692 |
|
---|
| 693 | \sa QNetworkProxyFactory, setApplicationProxy(), QAbstractSocket::proxy(), QTcpServer::proxy()
|
---|
| 694 | */
|
---|
| 695 | QNetworkProxy QNetworkProxy::applicationProxy()
|
---|
| 696 | {
|
---|
| 697 | if (globalNetworkProxy())
|
---|
| 698 | return globalNetworkProxy()->applicationProxy();
|
---|
| 699 | return QNetworkProxy();
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | class QNetworkProxyQueryPrivate: public QSharedData
|
---|
| 703 | {
|
---|
| 704 | public:
|
---|
| 705 | inline QNetworkProxyQueryPrivate()
|
---|
| 706 | : localPort(-1), type(QNetworkProxyQuery::TcpSocket)
|
---|
| 707 | { }
|
---|
| 708 |
|
---|
| 709 | bool operator==(const QNetworkProxyQueryPrivate &other) const
|
---|
| 710 | {
|
---|
| 711 | return type == other.type &&
|
---|
| 712 | localPort == other.localPort &&
|
---|
| 713 | remote == other.remote;
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | QUrl remote;
|
---|
| 717 | int localPort;
|
---|
| 718 | QNetworkProxyQuery::QueryType type;
|
---|
| 719 | };
|
---|
| 720 |
|
---|
| 721 | template<> void QSharedDataPointer<QNetworkProxyQueryPrivate>::detach()
|
---|
| 722 | {
|
---|
| 723 | if (d && d->ref == 1)
|
---|
| 724 | return;
|
---|
| 725 | QNetworkProxyQueryPrivate *x = (d ? new QNetworkProxyQueryPrivate(*d)
|
---|
| 726 | : new QNetworkProxyQueryPrivate);
|
---|
| 727 | x->ref.ref();
|
---|
| 728 | if (d && !d->ref.deref())
|
---|
| 729 | delete d;
|
---|
| 730 | d = x;
|
---|
| 731 | }
|
---|
| 732 |
|
---|
| 733 | /*!
|
---|
| 734 | \class QNetworkProxyQuery
|
---|
| 735 | \since 4.5
|
---|
| 736 | \inmodule QtNetwork
|
---|
| 737 | \brief The QNetworkProxyQuery class is used to query the proxy
|
---|
| 738 | settings for a socket
|
---|
| 739 |
|
---|
| 740 | QNetworkProxyQuery holds the details of a socket being created or
|
---|
| 741 | request being made. It is used by QNetworkProxy and
|
---|
| 742 | QNetworkProxyFactory to allow applications to have a more
|
---|
| 743 | fine-grained control over which proxy servers are used, depending
|
---|
| 744 | on the details of the query. This allows an application to apply
|
---|
| 745 | different settings, according to the protocol or destination
|
---|
| 746 | hostname, for instance.
|
---|
| 747 |
|
---|
| 748 | QNetworkProxyQuery supports the following criteria for selecting
|
---|
| 749 | the proxy:
|
---|
| 750 |
|
---|
| 751 | \list
|
---|
| 752 | \o the type of query
|
---|
| 753 | \o the local port number to use
|
---|
| 754 | \o the destination host name
|
---|
| 755 | \o the destination port number
|
---|
| 756 | \o the protocol name, such as "http" or "ftp"
|
---|
| 757 | \o the URL being requested
|
---|
| 758 | \endlist
|
---|
| 759 |
|
---|
| 760 | The destination host name is the host in the connection in the
|
---|
| 761 | case of outgoing connection sockets. It is the \c hostName
|
---|
| 762 | parameter passed to QTcpSocket::connectToHost() or the host
|
---|
| 763 | component of a URL requested with QNetworkRequest.
|
---|
| 764 |
|
---|
| 765 | The destination port number is the requested port to connect to in
|
---|
| 766 | the case of outgoing sockets, while the local port number is the
|
---|
| 767 | port the socket wishes to use locally before attempting the
|
---|
| 768 | external connection. In most cases, the local port number is used
|
---|
| 769 | by listening sockets only (QTcpSocket) or by datagram sockets
|
---|
| 770 | (QUdpSocket).
|
---|
| 771 |
|
---|
| 772 | The protocol name is an arbitrary string that indicates the type
|
---|
| 773 | of connection being attempted. For example, it can match the
|
---|
| 774 | scheme of a URL, like "http", "https" and "ftp". In most cases,
|
---|
| 775 | the proxy selection will not change depending on the protocol, but
|
---|
| 776 | this information is provided in case a better choice can be made,
|
---|
| 777 | like choosing an caching HTTP proxy for HTTP-based connections,
|
---|
| 778 | but a more powerful SOCKSv5 proxy for all others.
|
---|
| 779 |
|
---|
| 780 | Some of the criteria may not make sense in all of the types of
|
---|
| 781 | query. The following table lists the criteria that are most
|
---|
| 782 | commonly used, according to the type of query.
|
---|
| 783 |
|
---|
| 784 | \table
|
---|
| 785 | \header
|
---|
| 786 | \o Query type
|
---|
| 787 | \o Description
|
---|
| 788 |
|
---|
| 789 | \row
|
---|
| 790 | \o TcpSocket
|
---|
| 791 | \o Normal sockets requesting a connection to a remote server,
|
---|
| 792 | like QTcpSocket. The peer hostname and peer port match the
|
---|
| 793 | values passed to QTcpSocket::connectToHost(). The local port
|
---|
| 794 | is usually -1, indicating the socket has no preference in
|
---|
| 795 | which port should be used. The URL component is not used.
|
---|
| 796 |
|
---|
| 797 | \row
|
---|
| 798 | \o UdpSocket
|
---|
| 799 | \o Datagram-based sockets, which can both send and
|
---|
| 800 | receive. The local port, remote host or remote port fields
|
---|
| 801 | can all be used or be left unused, depending on the
|
---|
| 802 | characteristics of the socket. The URL component is not used.
|
---|
| 803 |
|
---|
| 804 | \row
|
---|
| 805 | \o TcpServer
|
---|
| 806 | \o Passive server sockets that listen on a port and await
|
---|
| 807 | incoming connections from the network. Normally, only the
|
---|
| 808 | local port is used, but the remote address could be used in
|
---|
| 809 | specific circumstances, for example to indicate which remote
|
---|
| 810 | host a connection is expected from. The URL component is not used.
|
---|
| 811 |
|
---|
| 812 | \row
|
---|
| 813 | \o UrlRequest
|
---|
| 814 | \o A more high-level request, such as those coming from
|
---|
| 815 | QNetworkAccessManager. These requests will inevitably use an
|
---|
| 816 | outgoing TCP socket, but the this query type is provided to
|
---|
| 817 | indicate that more detailed information is present in the URL
|
---|
| 818 | component. For ease of implementation, the URL's host and
|
---|
| 819 | port are set as the destination address.
|
---|
| 820 | \endtable
|
---|
| 821 |
|
---|
| 822 | It should be noted that any of the criteria may be missing or
|
---|
| 823 | unknown (an empty QString for the hostname or protocol name, -1
|
---|
| 824 | for the port numbers). If that happens, the functions executing
|
---|
| 825 | the query should make their best guess or apply some
|
---|
| 826 | implementation-defined default values.
|
---|
| 827 |
|
---|
| 828 | \sa QNetworkProxy, QNetworkProxyFactory, QNetworkAccessManager,
|
---|
| 829 | QAbstractSocket::setProxy()
|
---|
| 830 | */
|
---|
| 831 |
|
---|
| 832 | /*!
|
---|
| 833 | \enum QNetworkProxyQuery::QueryType
|
---|
| 834 |
|
---|
| 835 | Describes the type of one QNetworkProxyQuery query.
|
---|
| 836 |
|
---|
| 837 | \value TcpSocket a normal, outgoing TCP socket
|
---|
| 838 | \value UdpSocket a datagram-based UDP socket, which could send
|
---|
| 839 | to multiple destinations
|
---|
| 840 | \value TcpServer a TCP server that listens for incoming
|
---|
| 841 | connections from the network
|
---|
| 842 | \value UrlRequest a more complex request which involves loading
|
---|
| 843 | of a URL
|
---|
| 844 |
|
---|
| 845 | \sa queryType(), setQueryType()
|
---|
| 846 | */
|
---|
| 847 |
|
---|
| 848 | /*!
|
---|
| 849 | Constructs a default QNetworkProxyQuery object. By default, the
|
---|
| 850 | query type will be QNetworkProxyQuery::TcpSocket.
|
---|
| 851 | */
|
---|
| 852 | QNetworkProxyQuery::QNetworkProxyQuery()
|
---|
| 853 | {
|
---|
| 854 | }
|
---|
| 855 |
|
---|
| 856 | /*!
|
---|
| 857 | Constructs a QNetworkProxyQuery with the URL \a requestUrl and
|
---|
| 858 | sets the query type to \a queryType.
|
---|
| 859 |
|
---|
| 860 | \sa protocolTag(), peerHostName(), peerPort()
|
---|
| 861 | */
|
---|
| 862 | QNetworkProxyQuery::QNetworkProxyQuery(const QUrl &requestUrl, QueryType queryType)
|
---|
| 863 | {
|
---|
| 864 | d->remote = requestUrl;
|
---|
| 865 | d->type = queryType;
|
---|
| 866 | }
|
---|
| 867 |
|
---|
| 868 | /*!
|
---|
| 869 | Constructs a QNetworkProxyQuery of type \a queryType and sets the
|
---|
| 870 | protocol tag to be \a protocolTag. This constructor is suitable
|
---|
| 871 | for QNetworkProxyQuery::TcpSocket queries, because it sets the
|
---|
| 872 | peer hostname to \a hostname and the peer's port number to \a
|
---|
| 873 | port.
|
---|
| 874 | */
|
---|
| 875 | QNetworkProxyQuery::QNetworkProxyQuery(const QString &hostname, int port,
|
---|
| 876 | const QString &protocolTag,
|
---|
| 877 | QueryType queryType)
|
---|
| 878 | {
|
---|
| 879 | d->remote.setScheme(protocolTag);
|
---|
| 880 | d->remote.setHost(hostname);
|
---|
| 881 | d->remote.setPort(port);
|
---|
| 882 | d->type = queryType;
|
---|
| 883 | }
|
---|
| 884 |
|
---|
| 885 | /*!
|
---|
| 886 | Constructs a QNetworkProxyQuery of type \a queryType and sets the
|
---|
| 887 | protocol tag to be \a protocolTag. This constructor is suitable
|
---|
| 888 | for QNetworkProxyQuery::TcpSocket queries because it sets the
|
---|
| 889 | local port number to \a bindPort.
|
---|
| 890 |
|
---|
| 891 | Note that \a bindPort is of type quint16 to indicate the exact
|
---|
| 892 | port number that is requested. The value of -1 (unknown) is not
|
---|
| 893 | allowed in this context.
|
---|
| 894 |
|
---|
| 895 | \sa localPort()
|
---|
| 896 | */
|
---|
| 897 | QNetworkProxyQuery::QNetworkProxyQuery(quint16 bindPort, const QString &protocolTag,
|
---|
| 898 | QueryType queryType)
|
---|
| 899 | {
|
---|
| 900 | d->remote.setScheme(protocolTag);
|
---|
| 901 | d->localPort = bindPort;
|
---|
| 902 | d->type = queryType;
|
---|
| 903 | }
|
---|
| 904 |
|
---|
| 905 | /*!
|
---|
| 906 | Constructs a QNetworkProxyQuery object that is a copy of \a other.
|
---|
| 907 | */
|
---|
| 908 | QNetworkProxyQuery::QNetworkProxyQuery(const QNetworkProxyQuery &other)
|
---|
| 909 | : d(other.d)
|
---|
| 910 | {
|
---|
| 911 | }
|
---|
| 912 |
|
---|
| 913 | /*!
|
---|
| 914 | Destroys this QNetworkProxyQuery object.
|
---|
| 915 | */
|
---|
| 916 | QNetworkProxyQuery::~QNetworkProxyQuery()
|
---|
| 917 | {
|
---|
| 918 | // QSharedDataPointer automatically deletes
|
---|
| 919 | }
|
---|
| 920 |
|
---|
| 921 | /*!
|
---|
| 922 | Copies the contents of \a other.
|
---|
| 923 | */
|
---|
| 924 | QNetworkProxyQuery &QNetworkProxyQuery::operator=(const QNetworkProxyQuery &other)
|
---|
| 925 | {
|
---|
| 926 | d = other.d;
|
---|
| 927 | return *this;
|
---|
| 928 | }
|
---|
| 929 |
|
---|
| 930 | /*!
|
---|
| 931 | Returns true if this QNetworkProxyQuery object contains the same
|
---|
| 932 | data as \a other.
|
---|
| 933 | */
|
---|
| 934 | bool QNetworkProxyQuery::operator==(const QNetworkProxyQuery &other) const
|
---|
| 935 | {
|
---|
| 936 | return d == other.d || (d && other.d && *d == *other.d);
|
---|
| 937 | }
|
---|
| 938 |
|
---|
| 939 | /*!
|
---|
| 940 | \fn bool QNetworkProxyQuery::operator!=(const QNetworkProxyQuery &other) const
|
---|
| 941 |
|
---|
| 942 | Returns true if this QNetworkProxyQuery object does not contain
|
---|
| 943 | the same data as \a other.
|
---|
| 944 | */
|
---|
| 945 |
|
---|
| 946 | /*!
|
---|
| 947 | Returns the query type.
|
---|
| 948 | */
|
---|
| 949 | QNetworkProxyQuery::QueryType QNetworkProxyQuery::queryType() const
|
---|
| 950 | {
|
---|
| 951 | return d ? d->type : TcpSocket;
|
---|
| 952 | }
|
---|
| 953 |
|
---|
| 954 | /*!
|
---|
| 955 | Sets the query type of this object to be \a type.
|
---|
| 956 | */
|
---|
| 957 | void QNetworkProxyQuery::setQueryType(QueryType type)
|
---|
| 958 | {
|
---|
| 959 | d->type = type;
|
---|
| 960 | }
|
---|
| 961 |
|
---|
| 962 | /*!
|
---|
| 963 | Returns the port number for the outgoing request or -1 if the port
|
---|
| 964 | number is not known.
|
---|
| 965 |
|
---|
| 966 | If the query type is QNetworkProxyQuery::UrlRequest, this function
|
---|
| 967 | returns the port number of the URL being requested. In general,
|
---|
| 968 | frameworks will fill in the port number from their default values.
|
---|
| 969 |
|
---|
| 970 | \sa peerHostName(), localPort(), setPeerPort()
|
---|
| 971 | */
|
---|
| 972 | int QNetworkProxyQuery::peerPort() const
|
---|
| 973 | {
|
---|
| 974 | return d ? d->remote.port() : -1;
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 | /*!
|
---|
| 978 | Sets the requested port number for the outgoing connection to be
|
---|
| 979 | \a port. Valid values are 1 to 65535, or -1 to indicate that the
|
---|
| 980 | remote port number is unknown.
|
---|
| 981 |
|
---|
| 982 | The peer port number can also be used to indicate the expected
|
---|
| 983 | port number of an incoming connection in the case of
|
---|
| 984 | QNetworkProxyQuery::UdpSocket or QNetworkProxyQuery::TcpServer
|
---|
| 985 | query types.
|
---|
| 986 |
|
---|
| 987 | \sa peerPort(), setPeerHostName(), setLocalPort()
|
---|
| 988 | */
|
---|
| 989 | void QNetworkProxyQuery::setPeerPort(int port)
|
---|
| 990 | {
|
---|
| 991 | d->remote.setPort(port);
|
---|
| 992 | }
|
---|
| 993 |
|
---|
| 994 | /*!
|
---|
| 995 | Returns the host name or IP address being of the outgoing
|
---|
| 996 | connection being requested, or an empty string if the remote
|
---|
| 997 | hostname is not known.
|
---|
| 998 |
|
---|
| 999 | If the query type is QNetworkProxyQuery::UrlRequest, this function
|
---|
| 1000 | returns the host component of the URL being requested.
|
---|
| 1001 |
|
---|
| 1002 | \sa peerPort(), localPort(), setPeerHostName()
|
---|
| 1003 | */
|
---|
| 1004 | QString QNetworkProxyQuery::peerHostName() const
|
---|
| 1005 | {
|
---|
| 1006 | return d ? d->remote.host() : QString();
|
---|
| 1007 | }
|
---|
| 1008 |
|
---|
| 1009 | /*!
|
---|
| 1010 | Sets the hostname of the outgoing connection being requested to \a
|
---|
| 1011 | hostname. An empty hostname can be used to indicate that the
|
---|
| 1012 | remote host is unknown.
|
---|
| 1013 |
|
---|
| 1014 | The peer host name can also be used to indicate the expected
|
---|
| 1015 | source address of an incoming connection in the case of
|
---|
| 1016 | QNetworkProxyQuery::UdpSocket or QNetworkProxyQuery::TcpServer
|
---|
| 1017 | query types.
|
---|
| 1018 |
|
---|
| 1019 | \sa peerHostName(), setPeerPort(), setLocalPort()
|
---|
| 1020 | */
|
---|
| 1021 | void QNetworkProxyQuery::setPeerHostName(const QString &hostname)
|
---|
| 1022 | {
|
---|
| 1023 | d->remote.setHost(hostname);
|
---|
| 1024 | }
|
---|
| 1025 |
|
---|
| 1026 | /*!
|
---|
| 1027 | Returns the port number of the socket that will accept incoming
|
---|
| 1028 | packets from remote servers or -1 if the port is not known.
|
---|
| 1029 |
|
---|
| 1030 | \sa peerPort(), peerHostName(), setLocalPort()
|
---|
| 1031 | */
|
---|
| 1032 | int QNetworkProxyQuery::localPort() const
|
---|
| 1033 | {
|
---|
| 1034 | return d ? d->localPort : -1;
|
---|
| 1035 | }
|
---|
| 1036 |
|
---|
| 1037 | /*!
|
---|
| 1038 | Sets the port number that the socket wishes to use locally to
|
---|
| 1039 | accept incoming packets from remote servers to \a port. The local
|
---|
| 1040 | port is most often used with the QNetworkProxyQuery::TcpServer
|
---|
| 1041 | and QNetworkProxyQuery::UdpSocket query types.
|
---|
| 1042 |
|
---|
| 1043 | Valid values are 0 to 65535 (with 0 indicating that any port
|
---|
| 1044 | number will be acceptable) or -1, which means the local port
|
---|
| 1045 | number is unknown or not applicable.
|
---|
| 1046 |
|
---|
| 1047 | In some circumstances, for special protocols, it's the local port
|
---|
| 1048 | number can also be used with a query of type
|
---|
| 1049 | QNetworkProxyQuery::TcpSocket. When that happens, the socket is
|
---|
| 1050 | indicating it wishes to use the port number \a port when
|
---|
| 1051 | connecting to a remote host.
|
---|
| 1052 |
|
---|
| 1053 | \sa localPort(), setPeerPort(), setPeerHostName()
|
---|
| 1054 | */
|
---|
| 1055 | void QNetworkProxyQuery::setLocalPort(int port)
|
---|
| 1056 | {
|
---|
| 1057 | d->localPort = port;
|
---|
| 1058 | }
|
---|
| 1059 |
|
---|
| 1060 | /*!
|
---|
| 1061 | Returns the protocol tag for this QNetworkProxyQuery object, or an
|
---|
| 1062 | empty QString in case the protocol tag is unknown.
|
---|
| 1063 |
|
---|
| 1064 | In the case of queries of type QNetworkProxyQuery::UrlRequest,
|
---|
| 1065 | this function returns the value of the scheme component of the
|
---|
| 1066 | URL.
|
---|
| 1067 |
|
---|
| 1068 | \sa setProtocolTag(), url()
|
---|
| 1069 | */
|
---|
| 1070 | QString QNetworkProxyQuery::protocolTag() const
|
---|
| 1071 | {
|
---|
| 1072 | return d ? d->remote.scheme() : QString();
|
---|
| 1073 | }
|
---|
| 1074 |
|
---|
| 1075 | /*!
|
---|
| 1076 | Sets the protocol tag for this QNetworkProxyQuery object to be \a
|
---|
| 1077 | protocolTag.
|
---|
| 1078 |
|
---|
| 1079 | The protocol tag is an arbitrary string that indicates which
|
---|
| 1080 | protocol is being talked over the socket, such as "http", "xmpp",
|
---|
| 1081 | "telnet", etc. The protocol tag is used by the backend to
|
---|
| 1082 | return a request that is more specific to the protocol in
|
---|
| 1083 | question: for example, a HTTP connection could be use a caching
|
---|
| 1084 | HTTP proxy server, while all other connections use a more powerful
|
---|
| 1085 | SOCKSv5 proxy server.
|
---|
| 1086 |
|
---|
| 1087 | \sa protocolTag()
|
---|
| 1088 | */
|
---|
| 1089 | void QNetworkProxyQuery::setProtocolTag(const QString &protocolTag)
|
---|
| 1090 | {
|
---|
| 1091 | d->remote.setScheme(protocolTag);
|
---|
| 1092 | }
|
---|
| 1093 |
|
---|
| 1094 | /*!
|
---|
| 1095 | Returns the URL component of this QNetworkProxyQuery object in
|
---|
| 1096 | case of a query of type QNetworkProxyQuery::UrlRequest.
|
---|
| 1097 |
|
---|
| 1098 | \sa setUrl()
|
---|
| 1099 | */
|
---|
| 1100 | QUrl QNetworkProxyQuery::url() const
|
---|
| 1101 | {
|
---|
| 1102 | return d ? d->remote : QUrl();
|
---|
| 1103 | }
|
---|
| 1104 |
|
---|
| 1105 | /*!
|
---|
| 1106 | Sets the URL component of this QNetworkProxyQuery object to be \a
|
---|
| 1107 | url. Setting the URL will also set the protocol tag, the remote
|
---|
| 1108 | host name and port number. This is done so as to facilitate the
|
---|
| 1109 | implementation of the code that determines the proxy server to be
|
---|
| 1110 | used.
|
---|
| 1111 |
|
---|
| 1112 | \sa url(), peerHostName(), peerPort()
|
---|
| 1113 | */
|
---|
| 1114 | void QNetworkProxyQuery::setUrl(const QUrl &url)
|
---|
| 1115 | {
|
---|
| 1116 | d->remote = url;
|
---|
| 1117 | }
|
---|
| 1118 |
|
---|
| 1119 | /*!
|
---|
| 1120 | \class QNetworkProxyFactory
|
---|
| 1121 | \brief The QNetworkProxyFactory class provides fine-grained proxy selection.
|
---|
| 1122 | \since 4.5
|
---|
| 1123 |
|
---|
[561] | 1124 | \ingroup network
|
---|
[2] | 1125 | \inmodule QtNetwork
|
---|
| 1126 |
|
---|
| 1127 | QNetworkProxyFactory is an extension to QNetworkProxy, allowing
|
---|
| 1128 | applications to have a more fine-grained control over which proxy
|
---|
| 1129 | servers are used, depending on the socket requesting the
|
---|
| 1130 | proxy. This allows an application to apply different settings,
|
---|
| 1131 | according to the protocol or destination hostname, for instance.
|
---|
| 1132 |
|
---|
| 1133 | QNetworkProxyFactory can be set globally for an application, in
|
---|
| 1134 | which case it will override any global proxies set with
|
---|
| 1135 | QNetworkProxy::setApplicationProxy(). If set globally, any sockets
|
---|
| 1136 | created with Qt will query the factory to determine the proxy to
|
---|
| 1137 | be used.
|
---|
| 1138 |
|
---|
| 1139 | A factory can also be set in certain frameworks that support
|
---|
| 1140 | multiple connections, such as QNetworkAccessManager. When set on
|
---|
| 1141 | such object, the factory will be queried for sockets created by
|
---|
| 1142 | that framework only.
|
---|
[846] | 1143 |
|
---|
| 1144 | \section1 System Proxies
|
---|
| 1145 |
|
---|
| 1146 | You can configure a factory to use the system proxy's settings.
|
---|
| 1147 | Call the setUseSystemConfiguration() function with true to enable
|
---|
| 1148 | this behavior, or false to disable it.
|
---|
| 1149 |
|
---|
| 1150 | Similarly, you can use a factory to make queries directly to the
|
---|
| 1151 | system proxy by calling its systemProxyForQuery() function.
|
---|
| 1152 |
|
---|
| 1153 | \warning Depending on the configuration of the user's system, the
|
---|
| 1154 | use of system proxy features on certain platforms may be subject
|
---|
| 1155 | to limitations. The systemProxyForQuery() documentation contains a
|
---|
| 1156 | list of these limitations for those platforms that are affected.
|
---|
[2] | 1157 | */
|
---|
| 1158 |
|
---|
| 1159 | /*!
|
---|
| 1160 | Creates a QNetworkProxyFactory object.
|
---|
| 1161 |
|
---|
| 1162 | Since QNetworkProxyFactory is an abstract class, you cannot create
|
---|
| 1163 | objects of type QNetworkProxyFactory directly.
|
---|
| 1164 | */
|
---|
| 1165 | QNetworkProxyFactory::QNetworkProxyFactory()
|
---|
| 1166 | {
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | /*!
|
---|
| 1170 | Destroys the QNetworkProxyFactory object.
|
---|
| 1171 | */
|
---|
| 1172 | QNetworkProxyFactory::~QNetworkProxyFactory()
|
---|
| 1173 | {
|
---|
| 1174 | }
|
---|
| 1175 |
|
---|
[561] | 1176 |
|
---|
[2] | 1177 | /*!
|
---|
[846] | 1178 | \since 4.6
|
---|
| 1179 |
|
---|
[561] | 1180 | Enables the use of the platform-specific proxy settings, and only those.
|
---|
| 1181 | See systemProxyForQuery() for more information.
|
---|
| 1182 |
|
---|
| 1183 | Internally, this method (when called with \a enable set to true)
|
---|
| 1184 | sets an application-wide proxy factory. For this reason, this method
|
---|
[846] | 1185 | is mutually exclusive with setApplicationProxyFactory(): calling
|
---|
| 1186 | setApplicationProxyFactory() overrides the use of the system-wide proxy,
|
---|
| 1187 | and calling setUseSystemConfiguration() overrides any
|
---|
[561] | 1188 | application proxy or proxy factory that was previously set.
|
---|
| 1189 |
|
---|
[846] | 1190 | \note See the systemProxyForQuery() documentation for a list of
|
---|
| 1191 | limitations related to the use of system proxies.
|
---|
[561] | 1192 | */
|
---|
| 1193 | void QNetworkProxyFactory::setUseSystemConfiguration(bool enable)
|
---|
| 1194 | {
|
---|
| 1195 | if (enable) {
|
---|
| 1196 | setApplicationProxyFactory(new QSystemConfigurationProxyFactory);
|
---|
| 1197 | } else {
|
---|
| 1198 | setApplicationProxyFactory(0);
|
---|
| 1199 | }
|
---|
| 1200 | }
|
---|
| 1201 |
|
---|
| 1202 | /*!
|
---|
[2] | 1203 | Sets the application-wide proxy factory to be \a factory. This
|
---|
| 1204 | function will take ownership of that object and will delete it
|
---|
| 1205 | when necessary.
|
---|
| 1206 |
|
---|
| 1207 | The application-wide proxy is used as a last-resort when all other
|
---|
| 1208 | proxy selection requests returned QNetworkProxy::DefaultProxy. For
|
---|
| 1209 | example, QTcpSocket objects can have a proxy set with
|
---|
| 1210 | QTcpSocket::setProxy, but if none is set, the proxy factory class
|
---|
| 1211 | set with this function will be queried.
|
---|
| 1212 |
|
---|
| 1213 | If you set a proxy factory with this function, any application
|
---|
| 1214 | level proxies set with QNetworkProxy::setApplicationProxy will be
|
---|
| 1215 | overridden.
|
---|
| 1216 |
|
---|
| 1217 | \sa QNetworkProxy::setApplicationProxy(),
|
---|
| 1218 | QAbstractSocket::proxy(), QAbstractSocket::setProxy()
|
---|
| 1219 | */
|
---|
| 1220 | void QNetworkProxyFactory::setApplicationProxyFactory(QNetworkProxyFactory *factory)
|
---|
| 1221 | {
|
---|
| 1222 | if (globalNetworkProxy())
|
---|
| 1223 | globalNetworkProxy()->setApplicationProxyFactory(factory);
|
---|
| 1224 | }
|
---|
| 1225 |
|
---|
| 1226 | /*!
|
---|
| 1227 | \fn QList<QNetworkProxy> QNetworkProxyFactory::queryProxy(const QNetworkProxyQuery &query)
|
---|
| 1228 |
|
---|
| 1229 | This function examines takes the query request, \a query,
|
---|
| 1230 | examines the details of the type of socket or request and returns
|
---|
| 1231 | a list of QNetworkProxy objects that indicate the proxy servers to
|
---|
| 1232 | be used, in order of preference.
|
---|
| 1233 |
|
---|
| 1234 | When reimplementing this class, take care to return at least one
|
---|
| 1235 | element.
|
---|
| 1236 |
|
---|
| 1237 | If you cannot determine a better proxy alternative, use
|
---|
| 1238 | QNetworkProxy::DefaultProxy, which tells the code querying for a
|
---|
| 1239 | proxy to use a higher alternative. For example, if this factory is
|
---|
| 1240 | set to a QNetworkAccessManager object, DefaultProxy will tell it
|
---|
| 1241 | to query the application-level proxy settings.
|
---|
| 1242 |
|
---|
| 1243 | If this factory is set as the application proxy factory,
|
---|
| 1244 | DefaultProxy and NoProxy will have the same meaning.
|
---|
| 1245 | */
|
---|
| 1246 |
|
---|
| 1247 | /*!
|
---|
| 1248 | \fn QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query)
|
---|
| 1249 |
|
---|
| 1250 | This function examines takes the query request, \a query,
|
---|
| 1251 | examines the details of the type of socket or request and returns
|
---|
| 1252 | a list of QNetworkProxy objects that indicate the proxy servers to
|
---|
| 1253 | be used, in order of preference.
|
---|
| 1254 |
|
---|
| 1255 | This function can be used to determine the platform-specific proxy
|
---|
| 1256 | settings. This function will use the libraries provided by the
|
---|
| 1257 | operating system to determine the proxy for a given connection, if
|
---|
| 1258 | such libraries exist. If they don't, this function will just return a
|
---|
| 1259 | QNetworkProxy of type QNetworkProxy::NoProxy.
|
---|
| 1260 |
|
---|
| 1261 | On Windows, this function will use the WinHTTP DLL functions. Despite
|
---|
| 1262 | its name, Microsoft suggests using it for all applications that
|
---|
| 1263 | require network connections, not just HTTP. This will respect the
|
---|
| 1264 | proxy settings set on the registry with the proxycfg.exe tool. If
|
---|
| 1265 | those settings are not found, this function will attempt to obtain
|
---|
| 1266 | Internet Explorer's settings and use them.
|
---|
| 1267 |
|
---|
| 1268 | On MacOS X, this function will obtain the proxy settings using the
|
---|
| 1269 | SystemConfiguration framework from Apple. It will apply the FTP,
|
---|
| 1270 | HTTP and HTTPS proxy configurations for queries that contain the
|
---|
| 1271 | protocol tag "ftp", "http" and "https", respectively. If the SOCKS
|
---|
| 1272 | proxy is enabled in that configuration, this function will use the
|
---|
| 1273 | SOCKS server for all queries. If SOCKS isn't enabled, it will use
|
---|
| 1274 | the HTTPS proxy for all TcpSocket and UrlRequest queries.
|
---|
| 1275 |
|
---|
| 1276 | On other systems, there is no standardised method of obtaining the
|
---|
| 1277 | system proxy configuration. This function may be improved in
|
---|
| 1278 | future versions to support those systems.
|
---|
| 1279 |
|
---|
| 1280 | \section1 Limitations
|
---|
| 1281 |
|
---|
| 1282 | These are the limitations for the current version of this
|
---|
| 1283 | function. Future versions of Qt may lift some of the limitations
|
---|
| 1284 | listed here.
|
---|
| 1285 |
|
---|
[846] | 1286 | \list
|
---|
| 1287 | \o On MacOS X, this function will ignore the Proxy Auto Configuration
|
---|
[2] | 1288 | settings, since it cannot execute the associated ECMAScript code.
|
---|
[846] | 1289 |
|
---|
| 1290 | \o On Windows platforms, this function may take several seconds to
|
---|
| 1291 | execute depending on the configuration of the user's system.
|
---|
| 1292 | \endlist
|
---|
[2] | 1293 | */
|
---|
| 1294 |
|
---|
| 1295 | /*!
|
---|
| 1296 | This function examines takes the query request, \a query,
|
---|
| 1297 | examines the details of the type of socket or request and returns
|
---|
| 1298 | a list of QNetworkProxy objects that indicate the proxy servers to
|
---|
| 1299 | be used, in order of preference.
|
---|
| 1300 | */
|
---|
| 1301 | QList<QNetworkProxy> QNetworkProxyFactory::proxyForQuery(const QNetworkProxyQuery &query)
|
---|
| 1302 | {
|
---|
| 1303 | if (!globalNetworkProxy())
|
---|
| 1304 | return QList<QNetworkProxy>() << QNetworkProxy(QNetworkProxy::NoProxy);
|
---|
| 1305 | return globalNetworkProxy()->proxyForQuery(query);
|
---|
| 1306 | }
|
---|
| 1307 |
|
---|
[561] | 1308 | QT_END_NAMESPACE
|
---|
| 1309 |
|
---|
[2] | 1310 | #endif // QT_NO_NETWORKPROXY
|
---|