| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | /*! |
| 6 | \class QNetworkProxy |
| 7 | |
| 8 | \since 4.1 |
| 9 | |
| 10 | \brief The QNetworkProxy class provides a network layer proxy. |
| 11 | |
| 12 | \reentrant |
| 13 | \ingroup network |
| 14 | \ingroup shared |
| 15 | \inmodule QtNetwork |
| 16 | |
| 17 | QNetworkProxy provides the method for configuring network layer |
| 18 | proxy support to the Qt network classes. The currently supported |
| 19 | classes are QAbstractSocket, QTcpSocket, QUdpSocket, QTcpServer |
| 20 | and QNetworkAccessManager. The proxy support is designed to |
| 21 | be as transparent as possible. This means that existing |
| 22 | network-enabled applications that you have written should |
| 23 | automatically support network proxy using the following code. |
| 24 | |
| 25 | \snippet code/src_network_kernel_qnetworkproxy.cpp 0 |
| 26 | |
| 27 | An alternative to setting an application wide proxy is to specify |
| 28 | the proxy for individual sockets using QAbstractSocket::setProxy() |
| 29 | and QTcpServer::setProxy(). In this way, it is possible to disable |
| 30 | the use of a proxy for specific sockets using the following code: |
| 31 | |
| 32 | \snippet code/src_network_kernel_qnetworkproxy.cpp 1 |
| 33 | |
| 34 | Network proxy is not used if the address used in \l |
| 35 | {QAbstractSocket::connectToHost()}{connectToHost()}, \l |
| 36 | {QUdpSocket::bind()}{bind()} or \l |
| 37 | {QTcpServer::listen()}{listen()} is equivalent to |
| 38 | QHostAddress::LocalHost or QHostAddress::LocalHostIPv6. |
| 39 | |
| 40 | Each type of proxy support has certain restrictions associated with it. |
| 41 | You should read the \l{ProxyType} documentation carefully before |
| 42 | selecting a proxy type to use. |
| 43 | |
| 44 | \note Changes made to currently connected sockets do not take effect. |
| 45 | If you need to change a connected socket, you should reconnect it. |
| 46 | |
| 47 | \section1 SOCKS5 |
| 48 | |
| 49 | The SOCKS5 support since Qt 4 is based on |
| 50 | \l{http://www.rfc-editor.org/rfc/rfc1928.txt}{RFC 1928} and |
| 51 | \l{http://www.rfc-editor.org/rfc/rfc1929.txt}{RFC 1929}. |
| 52 | The supported authentication methods are no authentication and |
| 53 | username/password authentication. Both IPv4 and IPv6 are |
| 54 | supported. Domain names are resolved through the SOCKS5 server if |
| 55 | the QNetworkProxy::HostNameLookupCapability is enabled, otherwise |
| 56 | they are resolved locally and the IP address is sent to the |
| 57 | server. There are several things to remember when using SOCKS5 |
| 58 | with QUdpSocket and QTcpServer: |
| 59 | |
| 60 | With QUdpSocket, a call to \l {QUdpSocket::bind()}{bind()} may fail |
| 61 | with a timeout error. If a port number other than 0 is passed to |
| 62 | \l {QUdpSocket::bind()}{bind()}, it is not guaranteed that it is the |
| 63 | specified port that will be used. |
| 64 | Use \l{QUdpSocket::localPort()}{localPort()} and |
| 65 | \l{QUdpSocket::localAddress()}{localAddress()} to get the actual |
| 66 | address and port number in use. Because proxied UDP goes through |
| 67 | two UDP connections, it is more likely that packets will be dropped. |
| 68 | |
| 69 | With QTcpServer a call to \l{QTcpServer::listen()}{listen()} may |
| 70 | fail with a timeout error. If a port number other than 0 is passed |
| 71 | to \l{QTcpServer::listen()}{listen()}, then it is not guaranteed |
| 72 | that it is the specified port that will be used. |
| 73 | Use \l{QTcpServer::serverPort()}{serverPort()} and |
| 74 | \l{QTcpServer::serverAddress()}{serverAddress()} to get the actual |
| 75 | address and port used to listen for connections. SOCKS5 only supports |
| 76 | one accepted connection per call to \l{QTcpServer::listen()}{listen()}, |
| 77 | and each call is likely to result in a different |
| 78 | \l{QTcpServer::serverPort()}{serverPort()} being used. |
| 79 | |
| 80 | \sa QAbstractSocket, QTcpServer |
| 81 | */ |
| 82 | |
| 83 | /*! |
| 84 | \enum QNetworkProxy::ProxyType |
| 85 | |
| 86 | This enum describes the types of network proxying provided in Qt. |
| 87 | |
| 88 | There are two types of proxies that Qt understands: |
| 89 | transparent proxies and caching proxies. The first group consists |
| 90 | of proxies that can handle any arbitrary data transfer, while the |
| 91 | second can only handle specific requests. The caching proxies only |
| 92 | make sense for the specific classes where they can be used. |
| 93 | |
| 94 | \value NoProxy No proxying is used |
| 95 | \value DefaultProxy Proxy is determined based on the application proxy set using setApplicationProxy() |
| 96 | \value Socks5Proxy \l Socks5 proxying is used |
| 97 | \value HttpProxy HTTP transparent proxying is used |
| 98 | \value HttpCachingProxy Proxying for HTTP requests only |
| 99 | \value FtpCachingProxy Proxying for FTP requests only |
| 100 | |
| 101 | The table below lists different proxy types and their |
| 102 | capabilities. Since each proxy type has different capabilities, it |
| 103 | is important to understand them before choosing a proxy type. |
| 104 | |
| 105 | \table |
| 106 | \header |
| 107 | \li Proxy type |
| 108 | \li Description |
| 109 | \li Default capabilities |
| 110 | |
| 111 | \row |
| 112 | \li SOCKS 5 |
| 113 | \li Generic proxy for any kind of connection. Supports TCP, |
| 114 | UDP, binding to a port (incoming connections) and |
| 115 | authentication. |
| 116 | \li TunnelingCapability, ListeningCapability, |
| 117 | UdpTunnelingCapability, HostNameLookupCapability |
| 118 | |
| 119 | \row |
| 120 | \li HTTP |
| 121 | \li Implemented using the "CONNECT" command, supports only |
| 122 | outgoing TCP connections; supports authentication. |
| 123 | \li TunnelingCapability, CachingCapability, HostNameLookupCapability |
| 124 | |
| 125 | \row |
| 126 | \li Caching-only HTTP |
| 127 | \li Implemented using normal HTTP commands, it is useful only |
| 128 | in the context of HTTP requests (see QNetworkAccessManager) |
| 129 | \li CachingCapability, HostNameLookupCapability |
| 130 | |
| 131 | \row |
| 132 | \li Caching FTP |
| 133 | \li Implemented using an FTP proxy, it is useful only in the |
| 134 | context of FTP requests (see QNetworkAccessManager) |
| 135 | \li CachingCapability, HostNameLookupCapability |
| 136 | |
| 137 | \endtable |
| 138 | |
| 139 | Also note that you shouldn't set the application default proxy |
| 140 | (setApplicationProxy()) to a proxy that doesn't have the |
| 141 | TunnelingCapability capability. If you do, QTcpSocket will not |
| 142 | know how to open connections. |
| 143 | |
| 144 | \sa setType(), type(), capabilities(), setCapabilities() |
| 145 | */ |
| 146 | |
| 147 | /*! |
| 148 | \enum QNetworkProxy::Capability |
| 149 | \since 4.5 |
| 150 | |
| 151 | These flags indicate the capabilities that a given proxy server |
| 152 | supports. |
| 153 | |
| 154 | QNetworkProxy sets different capabilities by default when the |
| 155 | object is created (see QNetworkProxy::ProxyType for a list of the |
| 156 | defaults). However, it is possible to change the capabilities |
| 157 | after the object has been created with setCapabilities(). |
| 158 | |
| 159 | The capabilities that QNetworkProxy supports are: |
| 160 | |
| 161 | \value TunnelingCapability Ability to open transparent, tunneled |
| 162 | TCP connections to a remote host. The proxy server relays the |
| 163 | transmission verbatim from one side to the other and does no |
| 164 | caching. |
| 165 | |
| 166 | \value ListeningCapability Ability to create a listening socket |
| 167 | and wait for an incoming TCP connection from a remote host. |
| 168 | |
| 169 | \value UdpTunnelingCapability Ability to relay UDP datagrams via |
| 170 | the proxy server to and from a remote host. |
| 171 | |
| 172 | \value CachingCapability Ability to cache the contents of the |
| 173 | transfer. This capability is specific to each protocol and proxy |
| 174 | type. For example, HTTP proxies can cache the contents of web data |
| 175 | transferred with "GET" commands. |
| 176 | |
| 177 | \value HostNameLookupCapability Ability to connect to perform the |
| 178 | lookup on a remote host name and connect to it, as opposed to |
| 179 | requiring the application to perform the name lookup and request |
| 180 | connection to IP addresses only. |
| 181 | |
| 182 | \value SctpTunnelingCapability Ability to open transparent, tunneled |
| 183 | SCTP connections to a remote host. |
| 184 | |
| 185 | \value SctpListeningCapability Ability to create a listening socket |
| 186 | and wait for an incoming SCTP connection from a remote host. |
| 187 | */ |
| 188 | |
| 189 | #include "qnetworkproxy.h" |
| 190 | |
| 191 | #ifndef QT_NO_NETWORKPROXY |
| 192 | |
| 193 | #include "private/qnetworkrequest_p.h" |
| 194 | #if QT_CONFIG(socks5) |
| 195 | #include "private/qsocks5socketengine_p.h" |
| 196 | #endif |
| 197 | |
| 198 | #if QT_CONFIG(http) |
| 199 | #include "private/qhttpsocketengine_p.h" |
| 200 | #endif |
| 201 | |
| 202 | #include "qauthenticator.h" |
| 203 | #include "qdebug.h" |
| 204 | #include "qmutex.h" |
| 205 | #include "qstringlist.h" |
| 206 | #include "qurl.h" |
| 207 | |
| 208 | QT_BEGIN_NAMESPACE |
| 209 | |
| 210 | using namespace Qt::StringLiterals; |
| 211 | |
| 212 | QT_IMPL_METATYPE_EXTERN(QNetworkProxy) |
| 213 | |
| 214 | class QSocks5SocketEngineHandler; |
| 215 | class QHttpSocketEngineHandler; |
| 216 | |
| 217 | class QGlobalNetworkProxy |
| 218 | { |
| 219 | public: |
| 220 | QGlobalNetworkProxy() |
| 221 | : applicationLevelProxy(nullptr) |
| 222 | , applicationLevelProxyFactory(nullptr) |
| 223 | #if QT_CONFIG(socks5) |
| 224 | , socks5SocketEngineHandler(nullptr) |
| 225 | #endif |
| 226 | #if QT_CONFIG(http) |
| 227 | , httpSocketEngineHandler(nullptr) |
| 228 | #endif |
| 229 | #ifdef QT_USE_SYSTEM_PROXIES |
| 230 | , useSystemProxies(true) |
| 231 | #else |
| 232 | , useSystemProxies(false) |
| 233 | #endif |
| 234 | { |
| 235 | #if QT_CONFIG(socks5) |
| 236 | socks5SocketEngineHandler = new QSocks5SocketEngineHandler(); |
| 237 | #endif |
| 238 | #if QT_CONFIG(http) |
| 239 | httpSocketEngineHandler = new QHttpSocketEngineHandler(); |
| 240 | #endif |
| 241 | } |
| 242 | |
| 243 | ~QGlobalNetworkProxy() |
| 244 | { |
| 245 | delete applicationLevelProxy; |
| 246 | delete applicationLevelProxyFactory; |
| 247 | #if QT_CONFIG(socks5) |
| 248 | delete socks5SocketEngineHandler; |
| 249 | #endif |
| 250 | #if QT_CONFIG(http) |
| 251 | delete httpSocketEngineHandler; |
| 252 | #endif |
| 253 | } |
| 254 | |
| 255 | bool usesSystemConfiguration() const |
| 256 | { |
| 257 | return useSystemProxies; |
| 258 | } |
| 259 | |
| 260 | void setUseSystemConfiguration(bool enable) |
| 261 | { |
| 262 | QMutexLocker lock(&mutex); |
| 263 | useSystemProxies = enable; |
| 264 | |
| 265 | if (useSystemProxies) { |
| 266 | if (applicationLevelProxy) |
| 267 | *applicationLevelProxy = QNetworkProxy(); |
| 268 | delete applicationLevelProxyFactory; |
| 269 | applicationLevelProxyFactory = nullptr; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void setApplicationProxy(const QNetworkProxy &proxy) |
| 274 | { |
| 275 | QMutexLocker lock(&mutex); |
| 276 | if (!applicationLevelProxy) |
| 277 | applicationLevelProxy = new QNetworkProxy; |
| 278 | *applicationLevelProxy = proxy; |
| 279 | delete applicationLevelProxyFactory; |
| 280 | applicationLevelProxyFactory = nullptr; |
| 281 | useSystemProxies = false; |
| 282 | } |
| 283 | |
| 284 | void setApplicationProxyFactory(QNetworkProxyFactory *factory) |
| 285 | { |
| 286 | QMutexLocker lock(&mutex); |
| 287 | if (factory == applicationLevelProxyFactory) |
| 288 | return; |
| 289 | if (applicationLevelProxy) |
| 290 | *applicationLevelProxy = QNetworkProxy(); |
| 291 | delete applicationLevelProxyFactory; |
| 292 | applicationLevelProxyFactory = factory; |
| 293 | useSystemProxies = false; |
| 294 | } |
| 295 | |
| 296 | QNetworkProxy applicationProxy() |
| 297 | { |
| 298 | return proxyForQuery(query: QNetworkProxyQuery()).constFirst(); |
| 299 | } |
| 300 | |
| 301 | QList<QNetworkProxy> proxyForQuery(const QNetworkProxyQuery &query); |
| 302 | |
| 303 | private: |
| 304 | QRecursiveMutex mutex; |
| 305 | QNetworkProxy *applicationLevelProxy; |
| 306 | QNetworkProxyFactory *applicationLevelProxyFactory; |
| 307 | #if QT_CONFIG(socks5) |
| 308 | QSocks5SocketEngineHandler *socks5SocketEngineHandler; |
| 309 | #endif |
| 310 | #if QT_CONFIG(http) |
| 311 | QHttpSocketEngineHandler *httpSocketEngineHandler; |
| 312 | #endif |
| 313 | bool useSystemProxies; |
| 314 | }; |
| 315 | |
| 316 | QList<QNetworkProxy> QGlobalNetworkProxy::proxyForQuery(const QNetworkProxyQuery &query) |
| 317 | { |
| 318 | QMutexLocker locker(&mutex); |
| 319 | |
| 320 | QList<QNetworkProxy> result; |
| 321 | |
| 322 | // don't look for proxies for a local connection |
| 323 | QHostAddress parsed; |
| 324 | QString hostname = query.url().host(); |
| 325 | if (hostname == "localhost"_L1 || hostname.startsWith(s: "localhost."_L1 ) |
| 326 | || (parsed.setAddress(hostname) && (parsed.isLoopback()))) { |
| 327 | result << QNetworkProxy(QNetworkProxy::NoProxy); |
| 328 | return result; |
| 329 | } |
| 330 | |
| 331 | if (!applicationLevelProxyFactory) { |
| 332 | if (applicationLevelProxy |
| 333 | && applicationLevelProxy->type() != QNetworkProxy::DefaultProxy) { |
| 334 | result << *applicationLevelProxy; |
| 335 | } else if (useSystemProxies) { |
| 336 | result = QNetworkProxyFactory::systemProxyForQuery(query); |
| 337 | |
| 338 | // Make sure NoProxy is in the list, so that QTcpServer can work: |
| 339 | // it searches for the first proxy that can has the ListeningCapability capability |
| 340 | // if none have (as is the case with HTTP proxies), it fails to bind. |
| 341 | // NoProxy allows it to fallback to the 'no proxy' case and bind. |
| 342 | result << QNetworkProxy(QNetworkProxy::NoProxy); |
| 343 | } else { |
| 344 | result << QNetworkProxy(QNetworkProxy::NoProxy); |
| 345 | } |
| 346 | return result; |
| 347 | } |
| 348 | |
| 349 | // we have a factory |
| 350 | result = applicationLevelProxyFactory->queryProxy(query); |
| 351 | if (result.isEmpty()) { |
| 352 | qWarning(msg: "QNetworkProxyFactory: factory %p has returned an empty result set" , |
| 353 | applicationLevelProxyFactory); |
| 354 | result << QNetworkProxy(QNetworkProxy::NoProxy); |
| 355 | } |
| 356 | return result; |
| 357 | } |
| 358 | |
| 359 | Q_GLOBAL_STATIC(QGlobalNetworkProxy, globalNetworkProxy) |
| 360 | |
| 361 | namespace { |
| 362 | template<bool> struct StaticAssertTest; |
| 363 | template<> struct StaticAssertTest<true> { enum { Value = 1 }; }; |
| 364 | } |
| 365 | |
| 366 | static inline void qt_noop_with_arg(int) {} |
| 367 | #define q_static_assert(expr) qt_noop_with_arg(sizeof(StaticAssertTest< expr >::Value)) |
| 368 | |
| 369 | static QNetworkProxy::Capabilities defaultCapabilitiesForType(QNetworkProxy::ProxyType type) |
| 370 | { |
| 371 | q_static_assert(int(QNetworkProxy::DefaultProxy) == 0); |
| 372 | q_static_assert(int(QNetworkProxy::FtpCachingProxy) == 5); |
| 373 | static const int defaults[] = |
| 374 | { |
| 375 | /* [QNetworkProxy::DefaultProxy] = */ |
| 376 | (int(QNetworkProxy::ListeningCapability) | |
| 377 | int(QNetworkProxy::TunnelingCapability) | |
| 378 | int(QNetworkProxy::UdpTunnelingCapability) | |
| 379 | int(QNetworkProxy::SctpTunnelingCapability) | |
| 380 | int(QNetworkProxy::SctpListeningCapability)), |
| 381 | /* [QNetworkProxy::Socks5Proxy] = */ |
| 382 | (int(QNetworkProxy::TunnelingCapability) | |
| 383 | int(QNetworkProxy::ListeningCapability) | |
| 384 | int(QNetworkProxy::UdpTunnelingCapability) | |
| 385 | int(QNetworkProxy::HostNameLookupCapability)), |
| 386 | // it's weird to talk about the proxy capabilities of a "not proxy"... |
| 387 | /* [QNetworkProxy::NoProxy] = */ |
| 388 | (int(QNetworkProxy::ListeningCapability) | |
| 389 | int(QNetworkProxy::TunnelingCapability) | |
| 390 | int(QNetworkProxy::UdpTunnelingCapability) | |
| 391 | int(QNetworkProxy::SctpTunnelingCapability) | |
| 392 | int(QNetworkProxy::SctpListeningCapability)), |
| 393 | /* [QNetworkProxy::HttpProxy] = */ |
| 394 | (int(QNetworkProxy::TunnelingCapability) | |
| 395 | int(QNetworkProxy::CachingCapability) | |
| 396 | int(QNetworkProxy::HostNameLookupCapability)), |
| 397 | /* [QNetworkProxy::HttpCachingProxy] = */ |
| 398 | (int(QNetworkProxy::CachingCapability) | |
| 399 | int(QNetworkProxy::HostNameLookupCapability)), |
| 400 | /* [QNetworkProxy::FtpCachingProxy] = */ |
| 401 | (int(QNetworkProxy::CachingCapability) | |
| 402 | int(QNetworkProxy::HostNameLookupCapability)), |
| 403 | }; |
| 404 | |
| 405 | if (int(type) < 0 || int(type) > int(QNetworkProxy::FtpCachingProxy)) |
| 406 | type = QNetworkProxy::DefaultProxy; |
| 407 | return QNetworkProxy::Capabilities(defaults[int(type)]); |
| 408 | } |
| 409 | |
| 410 | class QNetworkProxyPrivate: public QSharedData |
| 411 | { |
| 412 | public: |
| 413 | QString hostName; |
| 414 | QString user; |
| 415 | QString password; |
| 416 | QNetworkProxy::Capabilities capabilities; |
| 417 | quint16 port; |
| 418 | QNetworkProxy::ProxyType type; |
| 419 | bool capabilitiesSet; |
| 420 | QNetworkHeadersPrivate ; |
| 421 | |
| 422 | inline QNetworkProxyPrivate(QNetworkProxy::ProxyType t = QNetworkProxy::DefaultProxy, |
| 423 | const QString &h = QString(), quint16 p = 0, |
| 424 | const QString &u = QString(), const QString &pw = QString()) |
| 425 | : hostName(h), |
| 426 | user(u), |
| 427 | password(pw), |
| 428 | capabilities(defaultCapabilitiesForType(type: t)), |
| 429 | port(p), |
| 430 | type(t), |
| 431 | capabilitiesSet(false) |
| 432 | { } |
| 433 | |
| 434 | inline bool operator==(const QNetworkProxyPrivate &other) const |
| 435 | { |
| 436 | return type == other.type && |
| 437 | port == other.port && |
| 438 | hostName == other.hostName && |
| 439 | user == other.user && |
| 440 | password == other.password && |
| 441 | capabilities == other.capabilities; |
| 442 | } |
| 443 | }; |
| 444 | |
| 445 | template<> void QSharedDataPointer<QNetworkProxyPrivate>::detach() |
| 446 | { |
| 447 | if (d && d->ref.loadRelaxed() == 1) |
| 448 | return; |
| 449 | QNetworkProxyPrivate *x = (d ? new QNetworkProxyPrivate(*d) |
| 450 | : new QNetworkProxyPrivate); |
| 451 | x->ref.ref(); |
| 452 | if (d && !d->ref.deref()) |
| 453 | delete d; |
| 454 | d = x; |
| 455 | } |
| 456 | |
| 457 | /*! |
| 458 | Constructs a QNetworkProxy with DefaultProxy type. |
| 459 | |
| 460 | The proxy type is determined by applicationProxy(), which defaults to |
| 461 | NoProxy or a system-wide proxy if one is configured. |
| 462 | |
| 463 | \sa setType(), setApplicationProxy() |
| 464 | */ |
| 465 | QNetworkProxy::QNetworkProxy() |
| 466 | : d(nullptr) |
| 467 | { |
| 468 | // make sure we have QGlobalNetworkProxy singleton created, otherwise |
| 469 | // you don't have any socket engine handler created when directly setting |
| 470 | // a proxy to a socket |
| 471 | globalNetworkProxy(); |
| 472 | } |
| 473 | |
| 474 | /*! |
| 475 | Constructs a QNetworkProxy with \a type, \a hostName, \a port, |
| 476 | \a user and \a password. |
| 477 | |
| 478 | The default capabilities for proxy type \a type are set automatically. |
| 479 | |
| 480 | \sa capabilities() |
| 481 | */ |
| 482 | QNetworkProxy::QNetworkProxy(ProxyType type, const QString &hostName, quint16 port, |
| 483 | const QString &user, const QString &password) |
| 484 | : d(new QNetworkProxyPrivate(type, hostName, port, user, password)) |
| 485 | { |
| 486 | // make sure we have QGlobalNetworkProxy singleton created, otherwise |
| 487 | // you don't have any socket engine handler created when directly setting |
| 488 | // a proxy to a socket |
| 489 | globalNetworkProxy(); |
| 490 | } |
| 491 | |
| 492 | /*! |
| 493 | Constructs a copy of \a other. |
| 494 | */ |
| 495 | QNetworkProxy::QNetworkProxy(const QNetworkProxy &other) |
| 496 | : d(other.d) |
| 497 | { |
| 498 | } |
| 499 | |
| 500 | /*! |
| 501 | Destroys the QNetworkProxy object. |
| 502 | */ |
| 503 | QNetworkProxy::~QNetworkProxy() |
| 504 | { |
| 505 | // QSharedDataPointer takes care of deleting for us |
| 506 | } |
| 507 | |
| 508 | /*! |
| 509 | \since 4.4 |
| 510 | |
| 511 | Compares the value of this network proxy to \a other and returns \c true |
| 512 | if they are equal (same proxy type, server as well as username and password) |
| 513 | */ |
| 514 | bool QNetworkProxy::operator==(const QNetworkProxy &other) const |
| 515 | { |
| 516 | return d == other.d || (d && other.d && *d == *other.d); |
| 517 | } |
| 518 | |
| 519 | /*! |
| 520 | \fn bool QNetworkProxy::operator!=(const QNetworkProxy &other) const |
| 521 | \since 4.4 |
| 522 | |
| 523 | Compares the value of this network proxy to \a other and returns \c true |
| 524 | if they differ. |
| 525 | \*/ |
| 526 | |
| 527 | /*! |
| 528 | \since 4.2 |
| 529 | |
| 530 | Assigns the value of the network proxy \a other to this network proxy. |
| 531 | */ |
| 532 | QNetworkProxy &QNetworkProxy::operator=(const QNetworkProxy &other) |
| 533 | { |
| 534 | d = other.d; |
| 535 | return *this; |
| 536 | } |
| 537 | |
| 538 | /*! |
| 539 | \fn void QNetworkProxy::swap(QNetworkProxy &other) |
| 540 | \since 5.0 |
| 541 | \memberswap{network proxy instance} |
| 542 | */ |
| 543 | |
| 544 | /*! |
| 545 | Sets the proxy type for this instance to be \a type. |
| 546 | |
| 547 | Note that changing the type of a proxy does not change |
| 548 | the set of capabilities this QNetworkProxy object holds if any |
| 549 | capabilities have been set with setCapabilities(). |
| 550 | |
| 551 | \sa type(), setCapabilities() |
| 552 | */ |
| 553 | void QNetworkProxy::setType(QNetworkProxy::ProxyType type) |
| 554 | { |
| 555 | d->type = type; |
| 556 | if (!d->capabilitiesSet) |
| 557 | d->capabilities = defaultCapabilitiesForType(type); |
| 558 | } |
| 559 | |
| 560 | /*! |
| 561 | Returns the proxy type for this instance. |
| 562 | |
| 563 | \sa setType() |
| 564 | */ |
| 565 | QNetworkProxy::ProxyType QNetworkProxy::type() const |
| 566 | { |
| 567 | return d ? d->type : DefaultProxy; |
| 568 | } |
| 569 | |
| 570 | /*! |
| 571 | \since 4.5 |
| 572 | |
| 573 | Sets the capabilities of this proxy to \a capabilities. |
| 574 | |
| 575 | \sa setType(), capabilities() |
| 576 | */ |
| 577 | void QNetworkProxy::setCapabilities(Capabilities capabilities) |
| 578 | { |
| 579 | d->capabilities = capabilities; |
| 580 | d->capabilitiesSet = true; |
| 581 | } |
| 582 | |
| 583 | /*! |
| 584 | \since 4.5 |
| 585 | |
| 586 | Returns the capabilities of this proxy server. |
| 587 | |
| 588 | \sa setCapabilities(), type() |
| 589 | */ |
| 590 | QNetworkProxy::Capabilities QNetworkProxy::capabilities() const |
| 591 | { |
| 592 | return d ? d->capabilities : defaultCapabilitiesForType(type: DefaultProxy); |
| 593 | } |
| 594 | |
| 595 | /*! |
| 596 | \since 4.4 |
| 597 | |
| 598 | Returns \c true if this proxy supports the |
| 599 | QNetworkProxy::CachingCapability capability. |
| 600 | |
| 601 | In Qt 4.4, the capability was tied to the proxy type, but since Qt |
| 602 | 4.5 it is possible to remove the capability of caching from a |
| 603 | proxy by calling setCapabilities(). |
| 604 | |
| 605 | \sa capabilities(), type(), isTransparentProxy() |
| 606 | */ |
| 607 | bool QNetworkProxy::isCachingProxy() const |
| 608 | { |
| 609 | return capabilities() & CachingCapability; |
| 610 | } |
| 611 | |
| 612 | /*! |
| 613 | \since 4.4 |
| 614 | |
| 615 | Returns \c true if this proxy supports transparent tunneling of TCP |
| 616 | connections. This matches the QNetworkProxy::TunnelingCapability |
| 617 | capability. |
| 618 | |
| 619 | In Qt 4.4, the capability was tied to the proxy type, but since Qt |
| 620 | 4.5 it is possible to remove the capability of caching from a |
| 621 | proxy by calling setCapabilities(). |
| 622 | |
| 623 | \sa capabilities(), type(), isCachingProxy() |
| 624 | */ |
| 625 | bool QNetworkProxy::isTransparentProxy() const |
| 626 | { |
| 627 | return capabilities() & TunnelingCapability; |
| 628 | } |
| 629 | |
| 630 | /*! |
| 631 | Sets the user name for proxy authentication to be \a user. |
| 632 | |
| 633 | \sa user(), setPassword(), password() |
| 634 | */ |
| 635 | void QNetworkProxy::setUser(const QString &user) |
| 636 | { |
| 637 | d->user = user; |
| 638 | } |
| 639 | |
| 640 | /*! |
| 641 | Returns the user name used for authentication. |
| 642 | |
| 643 | \sa setUser(), setPassword(), password() |
| 644 | */ |
| 645 | QString QNetworkProxy::user() const |
| 646 | { |
| 647 | return d ? d->user : QString(); |
| 648 | } |
| 649 | |
| 650 | /*! |
| 651 | Sets the password for proxy authentication to be \a password. |
| 652 | |
| 653 | \sa user(), setUser(), password() |
| 654 | */ |
| 655 | void QNetworkProxy::setPassword(const QString &password) |
| 656 | { |
| 657 | d->password = password; |
| 658 | } |
| 659 | |
| 660 | /*! |
| 661 | Returns the password used for authentication. |
| 662 | |
| 663 | \sa user(), setPassword(), setUser() |
| 664 | */ |
| 665 | QString QNetworkProxy::password() const |
| 666 | { |
| 667 | return d ? d->password : QString(); |
| 668 | } |
| 669 | |
| 670 | /*! |
| 671 | Sets the host name of the proxy host to be \a hostName. |
| 672 | |
| 673 | \sa hostName(), setPort(), port() |
| 674 | */ |
| 675 | void QNetworkProxy::setHostName(const QString &hostName) |
| 676 | { |
| 677 | d->hostName = hostName; |
| 678 | } |
| 679 | |
| 680 | /*! |
| 681 | Returns the host name of the proxy host. |
| 682 | |
| 683 | \sa setHostName(), setPort(), port() |
| 684 | */ |
| 685 | QString QNetworkProxy::hostName() const |
| 686 | { |
| 687 | return d ? d->hostName : QString(); |
| 688 | } |
| 689 | |
| 690 | /*! |
| 691 | Sets the port of the proxy host to be \a port. |
| 692 | |
| 693 | \sa hostName(), setHostName(), port() |
| 694 | */ |
| 695 | void QNetworkProxy::setPort(quint16 port) |
| 696 | { |
| 697 | d->port = port; |
| 698 | } |
| 699 | |
| 700 | /*! |
| 701 | Returns the port of the proxy host. |
| 702 | |
| 703 | \sa setHostName(), setPort(), hostName() |
| 704 | */ |
| 705 | quint16 QNetworkProxy::port() const |
| 706 | { |
| 707 | return d ? d->port : 0; |
| 708 | } |
| 709 | |
| 710 | /*! |
| 711 | Sets the application level network proxying to be \a networkProxy. |
| 712 | |
| 713 | If a QAbstractSocket or QTcpSocket has the |
| 714 | QNetworkProxy::DefaultProxy type, then the QNetworkProxy set with |
| 715 | this function is used. If you want more flexibility in determining |
| 716 | which proxy is used, use the QNetworkProxyFactory class. |
| 717 | |
| 718 | Setting a default proxy value with this function will override the |
| 719 | application proxy factory set with |
| 720 | QNetworkProxyFactory::setApplicationProxyFactory, and disable the |
| 721 | use of a system proxy. |
| 722 | |
| 723 | \sa QNetworkProxyFactory, applicationProxy(), QAbstractSocket::setProxy(), QTcpServer::setProxy() |
| 724 | */ |
| 725 | void QNetworkProxy::setApplicationProxy(const QNetworkProxy &networkProxy) |
| 726 | { |
| 727 | if (globalNetworkProxy()) { |
| 728 | // don't accept setting the proxy to DefaultProxy |
| 729 | if (networkProxy.type() == DefaultProxy) |
| 730 | globalNetworkProxy()->setApplicationProxy(QNetworkProxy::NoProxy); |
| 731 | else |
| 732 | globalNetworkProxy()->setApplicationProxy(networkProxy); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /*! |
| 737 | Returns the application level network proxying. |
| 738 | |
| 739 | If a QAbstractSocket or QTcpSocket has the |
| 740 | QNetworkProxy::DefaultProxy type, then the QNetworkProxy returned |
| 741 | by this function is used. |
| 742 | |
| 743 | \sa QNetworkProxyFactory, setApplicationProxy(), QAbstractSocket::proxy(), QTcpServer::proxy() |
| 744 | */ |
| 745 | QNetworkProxy QNetworkProxy::applicationProxy() |
| 746 | { |
| 747 | if (globalNetworkProxy()) |
| 748 | return globalNetworkProxy()->applicationProxy(); |
| 749 | return QNetworkProxy(); |
| 750 | } |
| 751 | |
| 752 | /*! |
| 753 | \since 6.8 |
| 754 | |
| 755 | Returns headers that are set in this network request. |
| 756 | |
| 757 | If the proxy is not of type HttpProxy or HttpCachingProxy, |
| 758 | default constructed QHttpHeaders is returned. |
| 759 | |
| 760 | \sa setHeaders() |
| 761 | */ |
| 762 | QHttpHeaders QNetworkProxy::() const |
| 763 | { |
| 764 | if (d->type != HttpProxy && d->type != HttpCachingProxy) |
| 765 | return {}; |
| 766 | return d->headers.headers(); |
| 767 | } |
| 768 | |
| 769 | /*! |
| 770 | \since 6.8 |
| 771 | |
| 772 | Sets \a newHeaders as headers in this network request, overriding |
| 773 | any previously set headers. |
| 774 | |
| 775 | If some headers correspond to the known headers, the values will |
| 776 | be parsed and the corresponding parsed form will also be set. |
| 777 | |
| 778 | If the proxy is not of type HttpProxy or HttpCachingProxy this has no |
| 779 | effect. |
| 780 | |
| 781 | \sa headers(), QNetworkRequest::KnownHeaders |
| 782 | */ |
| 783 | void QNetworkProxy::(QHttpHeaders &&) |
| 784 | { |
| 785 | if (d->type == HttpProxy || d->type == HttpCachingProxy) |
| 786 | d->headers.setHeaders(std::move(newHeaders)); |
| 787 | } |
| 788 | |
| 789 | /*! |
| 790 | \overload |
| 791 | \since 6.8 |
| 792 | */ |
| 793 | void QNetworkProxy::(const QHttpHeaders &) |
| 794 | { |
| 795 | if (d->type == HttpProxy || d->type == HttpCachingProxy) |
| 796 | d->headers.setHeaders(newHeaders); |
| 797 | } |
| 798 | |
| 799 | /*! |
| 800 | \since 5.0 |
| 801 | Returns the value of the known network header \a header if it is |
| 802 | in use for this proxy. If it is not present, returns QVariant() |
| 803 | (i.e., an invalid variant). |
| 804 | |
| 805 | \sa QNetworkRequest::KnownHeaders, rawHeader(), setHeader() |
| 806 | */ |
| 807 | QVariant QNetworkProxy::(QNetworkRequest::KnownHeaders ) const |
| 808 | { |
| 809 | if (d->type != HttpProxy && d->type != HttpCachingProxy) |
| 810 | return QVariant(); |
| 811 | return d->headers.cookedHeaders.value(key: header); |
| 812 | } |
| 813 | |
| 814 | /*! |
| 815 | \since 5.0 |
| 816 | Sets the value of the known header \a header to be \a value, |
| 817 | overriding any previously set headers. This operation also sets |
| 818 | the equivalent raw HTTP header. |
| 819 | |
| 820 | If the proxy is not of type HttpProxy or HttpCachingProxy this has no |
| 821 | effect. |
| 822 | |
| 823 | \sa QNetworkRequest::KnownHeaders, setRawHeader(), header() |
| 824 | */ |
| 825 | void QNetworkProxy::(QNetworkRequest::KnownHeaders , const QVariant &value) |
| 826 | { |
| 827 | if (d->type == HttpProxy || d->type == HttpCachingProxy) |
| 828 | d->headers.setCookedHeader(header, value); |
| 829 | } |
| 830 | |
| 831 | /*! |
| 832 | \since 5.0 |
| 833 | Returns \c true if the raw header \a headerName is in use for this |
| 834 | proxy. Returns \c false if the proxy is not of type HttpProxy or |
| 835 | HttpCachingProxy. |
| 836 | |
| 837 | \sa rawHeader(), setRawHeader() |
| 838 | */ |
| 839 | bool QNetworkProxy::(const QByteArray &) const |
| 840 | { |
| 841 | if (d->type != HttpProxy && d->type != HttpCachingProxy) |
| 842 | return false; |
| 843 | return d->headers.headers().contains(name: headerName); |
| 844 | } |
| 845 | |
| 846 | /*! |
| 847 | \since 5.0 |
| 848 | Returns the raw form of header \a headerName. If no such header is |
| 849 | present or the proxy is not of type HttpProxy or HttpCachingProxy, |
| 850 | an empty QByteArray is returned, which may be indistinguishable |
| 851 | from a header that is present but has no content (use hasRawHeader() |
| 852 | to find out if the header exists or not). |
| 853 | |
| 854 | Raw headers can be set with setRawHeader() or with setHeader(). |
| 855 | |
| 856 | \sa header(), setRawHeader() |
| 857 | */ |
| 858 | QByteArray QNetworkProxy::(const QByteArray &) const |
| 859 | { |
| 860 | if (d->type != HttpProxy && d->type != HttpCachingProxy) |
| 861 | return QByteArray(); |
| 862 | return d->headers.rawHeader(headerName); |
| 863 | } |
| 864 | |
| 865 | /*! |
| 866 | \since 5.0 |
| 867 | Returns a list of all raw headers that are set in this network |
| 868 | proxy. The list is in the order that the headers were set. |
| 869 | |
| 870 | If the proxy is not of type HttpProxy or HttpCachingProxy an empty |
| 871 | QList is returned. |
| 872 | |
| 873 | \sa hasRawHeader(), rawHeader() |
| 874 | */ |
| 875 | QList<QByteArray> QNetworkProxy::() const |
| 876 | { |
| 877 | if (d->type != HttpProxy && d->type != HttpCachingProxy) |
| 878 | return QList<QByteArray>(); |
| 879 | return d->headers.rawHeadersKeys(); |
| 880 | } |
| 881 | |
| 882 | /*! |
| 883 | \since 5.0 |
| 884 | Sets the header \a headerName to be of value \a headerValue. If \a |
| 885 | headerName corresponds to a known header (see |
| 886 | QNetworkRequest::KnownHeaders), the raw format will be parsed and |
| 887 | the corresponding "cooked" header will be set as well. |
| 888 | |
| 889 | For example: |
| 890 | \snippet code/src_network_access_qnetworkrequest.cpp 0 |
| 891 | |
| 892 | will also set the known header LastModifiedHeader to be the |
| 893 | QDateTime object of the parsed date. |
| 894 | |
| 895 | \note Setting the same header twice overrides the previous |
| 896 | setting. To accomplish the behaviour of multiple HTTP headers of |
| 897 | the same name, you should concatenate the two values, separating |
| 898 | them with a comma (",") and set one single raw header. |
| 899 | |
| 900 | If the proxy is not of type HttpProxy or HttpCachingProxy this has no |
| 901 | effect. |
| 902 | |
| 903 | \sa QNetworkRequest::KnownHeaders, setHeader(), hasRawHeader(), rawHeader() |
| 904 | */ |
| 905 | void QNetworkProxy::(const QByteArray &, const QByteArray &) |
| 906 | { |
| 907 | if (d->type == HttpProxy || d->type == HttpCachingProxy) |
| 908 | d->headers.setRawHeader(key: headerName, value: headerValue); |
| 909 | } |
| 910 | |
| 911 | class QNetworkProxyQueryPrivate: public QSharedData |
| 912 | { |
| 913 | public: |
| 914 | inline QNetworkProxyQueryPrivate() |
| 915 | : localPort(-1), type(QNetworkProxyQuery::TcpSocket) |
| 916 | { } |
| 917 | |
| 918 | bool operator==(const QNetworkProxyQueryPrivate &other) const |
| 919 | { |
| 920 | return type == other.type && |
| 921 | localPort == other.localPort && |
| 922 | remote == other.remote; |
| 923 | } |
| 924 | |
| 925 | QUrl remote; |
| 926 | int localPort; |
| 927 | QNetworkProxyQuery::QueryType type; |
| 928 | }; |
| 929 | |
| 930 | template<> void QSharedDataPointer<QNetworkProxyQueryPrivate>::detach() |
| 931 | { |
| 932 | if (d && d->ref.loadRelaxed() == 1) |
| 933 | return; |
| 934 | QNetworkProxyQueryPrivate *x = (d ? new QNetworkProxyQueryPrivate(*d) |
| 935 | : new QNetworkProxyQueryPrivate); |
| 936 | x->ref.ref(); |
| 937 | if (d && !d->ref.deref()) |
| 938 | delete d; |
| 939 | d = x; |
| 940 | } |
| 941 | |
| 942 | /*! |
| 943 | \class QNetworkProxyQuery |
| 944 | \since 4.5 |
| 945 | \ingroup shared |
| 946 | \inmodule QtNetwork |
| 947 | \brief The QNetworkProxyQuery class is used to query the proxy |
| 948 | settings for a socket. |
| 949 | |
| 950 | QNetworkProxyQuery holds the details of a socket being created or |
| 951 | request being made. It is used by QNetworkProxy and |
| 952 | QNetworkProxyFactory to allow applications to have a more |
| 953 | fine-grained control over which proxy servers are used, depending |
| 954 | on the details of the query. This allows an application to apply |
| 955 | different settings, according to the protocol or destination |
| 956 | hostname, for instance. |
| 957 | |
| 958 | QNetworkProxyQuery supports the following criteria for selecting |
| 959 | the proxy: |
| 960 | |
| 961 | \list |
| 962 | \li the type of query |
| 963 | \li the local port number to use |
| 964 | \li the destination host name |
| 965 | \li the destination port number |
| 966 | \li the protocol name, such as "http" or "ftp" |
| 967 | \li the URL being requested |
| 968 | \endlist |
| 969 | |
| 970 | The destination host name is the host in the connection in the |
| 971 | case of outgoing connection sockets. It is the \c hostName |
| 972 | parameter passed to QTcpSocket::connectToHost() or the host |
| 973 | component of a URL requested with QNetworkRequest. |
| 974 | |
| 975 | The destination port number is the requested port to connect to in |
| 976 | the case of outgoing sockets, while the local port number is the |
| 977 | port the socket wishes to use locally before attempting the |
| 978 | external connection. In most cases, the local port number is used |
| 979 | by listening sockets only (QTcpSocket) or by datagram sockets |
| 980 | (QUdpSocket). |
| 981 | |
| 982 | The protocol name is an arbitrary string that indicates the type |
| 983 | of connection being attempted. For example, it can match the |
| 984 | scheme of a URL, like "http", "https" and "ftp". In most cases, |
| 985 | the proxy selection will not change depending on the protocol, but |
| 986 | this information is provided in case a better choice can be made, |
| 987 | like choosing an caching HTTP proxy for HTTP-based connections, |
| 988 | but a more powerful SOCKSv5 proxy for all others. |
| 989 | |
| 990 | Some of the criteria may not make sense in all of the types of |
| 991 | query. The following table lists the criteria that are most |
| 992 | commonly used, according to the type of query. |
| 993 | |
| 994 | \table |
| 995 | \header |
| 996 | \li Query type |
| 997 | \li Description |
| 998 | |
| 999 | \row |
| 1000 | \li TcpSocket |
| 1001 | \li Normal sockets requesting a connection to a remote server, |
| 1002 | like QTcpSocket. The peer hostname and peer port match the |
| 1003 | values passed to QTcpSocket::connectToHost(). The local port |
| 1004 | is usually -1, indicating the socket has no preference in |
| 1005 | which port should be used. The URL component is not used. |
| 1006 | |
| 1007 | \row |
| 1008 | \li UdpSocket |
| 1009 | \li Datagram-based sockets, which can both send and |
| 1010 | receive. The local port, remote host or remote port fields |
| 1011 | can all be used or be left unused, depending on the |
| 1012 | characteristics of the socket. The URL component is not used. |
| 1013 | |
| 1014 | \row |
| 1015 | \li SctpSocket |
| 1016 | \li Message-oriented sockets requesting a connection to a remote |
| 1017 | server. The peer hostname and peer port match the values passed |
| 1018 | to QSctpSocket::connectToHost(). The local port is usually -1, |
| 1019 | indicating the socket has no preference in which port should be |
| 1020 | used. The URL component is not used. |
| 1021 | |
| 1022 | \row |
| 1023 | \li TcpServer |
| 1024 | \li Passive server sockets that listen on a port and await |
| 1025 | incoming connections from the network. Normally, only the |
| 1026 | local port is used, but the remote address could be used in |
| 1027 | specific circumstances, for example to indicate which remote |
| 1028 | host a connection is expected from. The URL component is not used. |
| 1029 | |
| 1030 | \row |
| 1031 | \li UrlRequest |
| 1032 | \li A more high-level request, such as those coming from |
| 1033 | QNetworkAccessManager. These requests will inevitably use an |
| 1034 | outgoing TCP socket, but the this query type is provided to |
| 1035 | indicate that more detailed information is present in the URL |
| 1036 | component. For ease of implementation, the URL's host and |
| 1037 | port are set as the destination address. |
| 1038 | |
| 1039 | \row |
| 1040 | \li SctpServer |
| 1041 | \li Passive server sockets that listen on an SCTP port and await |
| 1042 | incoming connections from the network. Normally, only the |
| 1043 | local port is used, but the remote address could be used in |
| 1044 | specific circumstances, for example to indicate which remote |
| 1045 | host a connection is expected from. The URL component is not used. |
| 1046 | \endtable |
| 1047 | |
| 1048 | It should be noted that any of the criteria may be missing or |
| 1049 | unknown (an empty QString for the hostname or protocol name, -1 |
| 1050 | for the port numbers). If that happens, the functions executing |
| 1051 | the query should make their best guess or apply some |
| 1052 | implementation-defined default values. |
| 1053 | |
| 1054 | \sa QNetworkProxy, QNetworkProxyFactory, QNetworkAccessManager, |
| 1055 | QAbstractSocket::setProxy() |
| 1056 | */ |
| 1057 | |
| 1058 | /*! |
| 1059 | \enum QNetworkProxyQuery::QueryType |
| 1060 | |
| 1061 | Describes the type of one QNetworkProxyQuery query. |
| 1062 | |
| 1063 | \value TcpSocket a normal, outgoing TCP socket |
| 1064 | \value UdpSocket a datagram-based UDP socket, which could send |
| 1065 | to multiple destinations |
| 1066 | \value SctpSocket a message-oriented, outgoing SCTP socket |
| 1067 | \value TcpServer a TCP server that listens for incoming |
| 1068 | connections from the network |
| 1069 | \value UrlRequest a more complex request which involves loading |
| 1070 | of a URL |
| 1071 | \value SctpServer an SCTP server that listens for incoming |
| 1072 | connections from the network |
| 1073 | |
| 1074 | \sa queryType(), setQueryType() |
| 1075 | */ |
| 1076 | |
| 1077 | /*! |
| 1078 | Constructs a default QNetworkProxyQuery object. By default, the |
| 1079 | query type will be QNetworkProxyQuery::TcpSocket. |
| 1080 | */ |
| 1081 | QNetworkProxyQuery::QNetworkProxyQuery() |
| 1082 | { |
| 1083 | } |
| 1084 | |
| 1085 | /*! |
| 1086 | Constructs a QNetworkProxyQuery with the URL \a requestUrl and |
| 1087 | sets the query type to \a queryType. |
| 1088 | |
| 1089 | \sa protocolTag(), peerHostName(), peerPort() |
| 1090 | */ |
| 1091 | QNetworkProxyQuery::QNetworkProxyQuery(const QUrl &requestUrl, QueryType queryType) |
| 1092 | { |
| 1093 | d->remote = requestUrl; |
| 1094 | d->type = queryType; |
| 1095 | } |
| 1096 | |
| 1097 | /*! |
| 1098 | Constructs a QNetworkProxyQuery of type \a queryType and sets the |
| 1099 | protocol tag to be \a protocolTag. This constructor is suitable |
| 1100 | for QNetworkProxyQuery::TcpSocket queries, because it sets the |
| 1101 | peer hostname to \a hostname and the peer's port number to \a |
| 1102 | port. |
| 1103 | */ |
| 1104 | QNetworkProxyQuery::QNetworkProxyQuery(const QString &hostname, int port, |
| 1105 | const QString &protocolTag, |
| 1106 | QueryType queryType) |
| 1107 | { |
| 1108 | d->remote.setScheme(protocolTag); |
| 1109 | d->remote.setHost(host: hostname); |
| 1110 | d->remote.setPort(port); |
| 1111 | d->type = queryType; |
| 1112 | } |
| 1113 | |
| 1114 | /*! |
| 1115 | Constructs a QNetworkProxyQuery of type \a queryType and sets the |
| 1116 | protocol tag to be \a protocolTag. This constructor is suitable |
| 1117 | for QNetworkProxyQuery::TcpSocket queries because it sets the |
| 1118 | local port number to \a bindPort. |
| 1119 | |
| 1120 | Note that \a bindPort is of type quint16 to indicate the exact |
| 1121 | port number that is requested. The value of -1 (unknown) is not |
| 1122 | allowed in this context. |
| 1123 | |
| 1124 | \sa localPort() |
| 1125 | */ |
| 1126 | QNetworkProxyQuery::QNetworkProxyQuery(quint16 bindPort, const QString &protocolTag, |
| 1127 | QueryType queryType) |
| 1128 | { |
| 1129 | d->remote.setScheme(protocolTag); |
| 1130 | d->localPort = bindPort; |
| 1131 | d->type = queryType; |
| 1132 | } |
| 1133 | |
| 1134 | /*! |
| 1135 | Constructs a QNetworkProxyQuery object that is a copy of \a other. |
| 1136 | */ |
| 1137 | QNetworkProxyQuery::QNetworkProxyQuery(const QNetworkProxyQuery &other) |
| 1138 | : d(other.d) |
| 1139 | { |
| 1140 | } |
| 1141 | |
| 1142 | /*! |
| 1143 | Destroys this QNetworkProxyQuery object. |
| 1144 | */ |
| 1145 | QNetworkProxyQuery::~QNetworkProxyQuery() |
| 1146 | { |
| 1147 | // QSharedDataPointer automatically deletes |
| 1148 | } |
| 1149 | |
| 1150 | /*! |
| 1151 | Copies the contents of \a other. |
| 1152 | */ |
| 1153 | QNetworkProxyQuery &QNetworkProxyQuery::operator=(const QNetworkProxyQuery &other) |
| 1154 | { |
| 1155 | d = other.d; |
| 1156 | return *this; |
| 1157 | } |
| 1158 | |
| 1159 | /*! |
| 1160 | \fn void QNetworkProxyQuery::swap(QNetworkProxyQuery &other) |
| 1161 | \since 5.0 |
| 1162 | \memberswap{network proxy query instance} |
| 1163 | */ |
| 1164 | |
| 1165 | /*! |
| 1166 | Returns \c true if this QNetworkProxyQuery object contains the same |
| 1167 | data as \a other. |
| 1168 | */ |
| 1169 | bool QNetworkProxyQuery::operator==(const QNetworkProxyQuery &other) const |
| 1170 | { |
| 1171 | return d == other.d || (d && other.d && *d == *other.d); |
| 1172 | } |
| 1173 | |
| 1174 | /*! |
| 1175 | \fn bool QNetworkProxyQuery::operator!=(const QNetworkProxyQuery &other) const |
| 1176 | |
| 1177 | Returns \c true if this QNetworkProxyQuery object does not contain |
| 1178 | the same data as \a other. |
| 1179 | */ |
| 1180 | |
| 1181 | /*! |
| 1182 | Returns the query type. |
| 1183 | */ |
| 1184 | QNetworkProxyQuery::QueryType QNetworkProxyQuery::queryType() const |
| 1185 | { |
| 1186 | return d ? d->type : TcpSocket; |
| 1187 | } |
| 1188 | |
| 1189 | /*! |
| 1190 | Sets the query type of this object to be \a type. |
| 1191 | */ |
| 1192 | void QNetworkProxyQuery::setQueryType(QueryType type) |
| 1193 | { |
| 1194 | d->type = type; |
| 1195 | } |
| 1196 | |
| 1197 | /*! |
| 1198 | Returns the port number for the outgoing request or -1 if the port |
| 1199 | number is not known. |
| 1200 | |
| 1201 | If the query type is QNetworkProxyQuery::UrlRequest, this function |
| 1202 | returns the port number of the URL being requested. In general, |
| 1203 | frameworks will fill in the port number from their default values. |
| 1204 | |
| 1205 | \sa peerHostName(), localPort(), setPeerPort() |
| 1206 | */ |
| 1207 | int QNetworkProxyQuery::peerPort() const |
| 1208 | { |
| 1209 | return d ? d->remote.port() : -1; |
| 1210 | } |
| 1211 | |
| 1212 | /*! |
| 1213 | Sets the requested port number for the outgoing connection to be |
| 1214 | \a port. Valid values are 1 to 65535, or -1 to indicate that the |
| 1215 | remote port number is unknown. |
| 1216 | |
| 1217 | The peer port number can also be used to indicate the expected |
| 1218 | port number of an incoming connection in the case of |
| 1219 | QNetworkProxyQuery::UdpSocket or QNetworkProxyQuery::TcpServer |
| 1220 | query types. |
| 1221 | |
| 1222 | \sa peerPort(), setPeerHostName(), setLocalPort() |
| 1223 | */ |
| 1224 | void QNetworkProxyQuery::setPeerPort(int port) |
| 1225 | { |
| 1226 | d->remote.setPort(port); |
| 1227 | } |
| 1228 | |
| 1229 | /*! |
| 1230 | Returns the host name or IP address being of the outgoing |
| 1231 | connection being requested, or an empty string if the remote |
| 1232 | hostname is not known. |
| 1233 | |
| 1234 | If the query type is QNetworkProxyQuery::UrlRequest, this function |
| 1235 | returns the host component of the URL being requested. |
| 1236 | |
| 1237 | \sa peerPort(), localPort(), setPeerHostName() |
| 1238 | */ |
| 1239 | QString QNetworkProxyQuery::peerHostName() const |
| 1240 | { |
| 1241 | return d ? d->remote.host() : QString(); |
| 1242 | } |
| 1243 | |
| 1244 | /*! |
| 1245 | Sets the hostname of the outgoing connection being requested to \a |
| 1246 | hostname. An empty hostname can be used to indicate that the |
| 1247 | remote host is unknown. |
| 1248 | |
| 1249 | The peer host name can also be used to indicate the expected |
| 1250 | source address of an incoming connection in the case of |
| 1251 | QNetworkProxyQuery::UdpSocket or QNetworkProxyQuery::TcpServer |
| 1252 | query types. |
| 1253 | |
| 1254 | \sa peerHostName(), setPeerPort(), setLocalPort() |
| 1255 | */ |
| 1256 | void QNetworkProxyQuery::setPeerHostName(const QString &hostname) |
| 1257 | { |
| 1258 | d->remote.setHost(host: hostname); |
| 1259 | } |
| 1260 | |
| 1261 | /*! |
| 1262 | Returns the port number of the socket that will accept incoming |
| 1263 | packets from remote servers or -1 if the port is not known. |
| 1264 | |
| 1265 | \sa peerPort(), peerHostName(), setLocalPort() |
| 1266 | */ |
| 1267 | int QNetworkProxyQuery::localPort() const |
| 1268 | { |
| 1269 | return d ? d->localPort : -1; |
| 1270 | } |
| 1271 | |
| 1272 | /*! |
| 1273 | Sets the port number that the socket wishes to use locally to |
| 1274 | accept incoming packets from remote servers to \a port. The local |
| 1275 | port is most often used with the QNetworkProxyQuery::TcpServer |
| 1276 | and QNetworkProxyQuery::UdpSocket query types. |
| 1277 | |
| 1278 | Valid values are 0 to 65535 (with 0 indicating that any port |
| 1279 | number will be acceptable) or -1, which means the local port |
| 1280 | number is unknown or not applicable. |
| 1281 | |
| 1282 | In some circumstances, for special protocols, it's the local port |
| 1283 | number can also be used with a query of type |
| 1284 | QNetworkProxyQuery::TcpSocket. When that happens, the socket is |
| 1285 | indicating it wishes to use the port number \a port when |
| 1286 | connecting to a remote host. |
| 1287 | |
| 1288 | \sa localPort(), setPeerPort(), setPeerHostName() |
| 1289 | */ |
| 1290 | void QNetworkProxyQuery::setLocalPort(int port) |
| 1291 | { |
| 1292 | d->localPort = port; |
| 1293 | } |
| 1294 | |
| 1295 | /*! |
| 1296 | Returns the protocol tag for this QNetworkProxyQuery object, or an |
| 1297 | empty QString in case the protocol tag is unknown. |
| 1298 | |
| 1299 | In the case of queries of type QNetworkProxyQuery::UrlRequest, |
| 1300 | this function returns the value of the scheme component of the |
| 1301 | URL. |
| 1302 | |
| 1303 | \sa setProtocolTag(), url() |
| 1304 | */ |
| 1305 | QString QNetworkProxyQuery::protocolTag() const |
| 1306 | { |
| 1307 | return d ? d->remote.scheme() : QString(); |
| 1308 | } |
| 1309 | |
| 1310 | /*! |
| 1311 | Sets the protocol tag for this QNetworkProxyQuery object to be \a |
| 1312 | protocolTag. |
| 1313 | |
| 1314 | The protocol tag is an arbitrary string that indicates which |
| 1315 | protocol is being talked over the socket, such as "http", "xmpp", |
| 1316 | "telnet", etc. The protocol tag is used by the backend to |
| 1317 | return a request that is more specific to the protocol in |
| 1318 | question: for example, a HTTP connection could be use a caching |
| 1319 | HTTP proxy server, while all other connections use a more powerful |
| 1320 | SOCKSv5 proxy server. |
| 1321 | |
| 1322 | \sa protocolTag() |
| 1323 | */ |
| 1324 | void QNetworkProxyQuery::setProtocolTag(const QString &protocolTag) |
| 1325 | { |
| 1326 | d->remote.setScheme(protocolTag); |
| 1327 | } |
| 1328 | |
| 1329 | /*! |
| 1330 | Returns the URL component of this QNetworkProxyQuery object in |
| 1331 | case of a query of type QNetworkProxyQuery::UrlRequest. |
| 1332 | |
| 1333 | \sa setUrl() |
| 1334 | */ |
| 1335 | QUrl QNetworkProxyQuery::url() const |
| 1336 | { |
| 1337 | return d ? d->remote : QUrl(); |
| 1338 | } |
| 1339 | |
| 1340 | /*! |
| 1341 | Sets the URL component of this QNetworkProxyQuery object to be \a |
| 1342 | url. Setting the URL will also set the protocol tag, the remote |
| 1343 | host name and port number. This is done so as to facilitate the |
| 1344 | implementation of the code that determines the proxy server to be |
| 1345 | used. |
| 1346 | |
| 1347 | \sa url(), peerHostName(), peerPort() |
| 1348 | */ |
| 1349 | void QNetworkProxyQuery::setUrl(const QUrl &url) |
| 1350 | { |
| 1351 | d->remote = url; |
| 1352 | } |
| 1353 | |
| 1354 | /*! |
| 1355 | \class QNetworkProxyFactory |
| 1356 | \brief The QNetworkProxyFactory class provides fine-grained proxy selection. |
| 1357 | \since 4.5 |
| 1358 | |
| 1359 | \ingroup network |
| 1360 | \inmodule QtNetwork |
| 1361 | |
| 1362 | QNetworkProxyFactory is an extension to QNetworkProxy, allowing |
| 1363 | applications to have a more fine-grained control over which proxy |
| 1364 | servers are used, depending on the socket requesting the |
| 1365 | proxy. This allows an application to apply different settings, |
| 1366 | according to the protocol or destination hostname, for instance. |
| 1367 | |
| 1368 | QNetworkProxyFactory can be set globally for an application, in |
| 1369 | which case it will override any global proxies set with |
| 1370 | QNetworkProxy::setApplicationProxy(). If set globally, any sockets |
| 1371 | created with Qt will query the factory to determine the proxy to |
| 1372 | be used. |
| 1373 | |
| 1374 | A factory can also be set in certain frameworks that support |
| 1375 | multiple connections, such as QNetworkAccessManager. When set on |
| 1376 | such object, the factory will be queried for sockets created by |
| 1377 | that framework only. |
| 1378 | |
| 1379 | \section1 System Proxies |
| 1380 | |
| 1381 | You can configure a factory to use the system proxy's settings. |
| 1382 | Call the setUseSystemConfiguration() function with true to enable |
| 1383 | this behavior, or false to disable it. |
| 1384 | |
| 1385 | Similarly, you can use a factory to make queries directly to the |
| 1386 | system proxy by calling its systemProxyForQuery() function. |
| 1387 | |
| 1388 | \warning Depending on the configuration of the user's system, the |
| 1389 | use of system proxy features on certain platforms may be subject |
| 1390 | to limitations. The systemProxyForQuery() documentation contains a |
| 1391 | list of these limitations for those platforms that are affected. |
| 1392 | */ |
| 1393 | |
| 1394 | /*! |
| 1395 | Creates a QNetworkProxyFactory object. |
| 1396 | |
| 1397 | Since QNetworkProxyFactory is an abstract class, you cannot create |
| 1398 | objects of type QNetworkProxyFactory directly. |
| 1399 | */ |
| 1400 | QNetworkProxyFactory::QNetworkProxyFactory() |
| 1401 | { |
| 1402 | } |
| 1403 | |
| 1404 | /*! |
| 1405 | Destroys the QNetworkProxyFactory object. |
| 1406 | */ |
| 1407 | QNetworkProxyFactory::~QNetworkProxyFactory() |
| 1408 | { |
| 1409 | } |
| 1410 | |
| 1411 | /*! |
| 1412 | \since 5.8 |
| 1413 | |
| 1414 | Returns whether the use of platform-specific proxy settings are enabled. |
| 1415 | */ |
| 1416 | bool QNetworkProxyFactory::usesSystemConfiguration() |
| 1417 | { |
| 1418 | if (globalNetworkProxy()) |
| 1419 | return globalNetworkProxy()->usesSystemConfiguration(); |
| 1420 | return false; |
| 1421 | } |
| 1422 | |
| 1423 | /*! |
| 1424 | \since 4.6 |
| 1425 | |
| 1426 | Enables the use of the platform-specific proxy settings, and only those. |
| 1427 | See systemProxyForQuery() for more information. |
| 1428 | |
| 1429 | Calling this function with \a enable set to \c true resets any proxy |
| 1430 | or QNetworkProxyFactory that is already set. |
| 1431 | |
| 1432 | \note See the systemProxyForQuery() documentation for a list of |
| 1433 | limitations related to the use of system proxies. |
| 1434 | */ |
| 1435 | void QNetworkProxyFactory::setUseSystemConfiguration(bool enable) |
| 1436 | { |
| 1437 | if (globalNetworkProxy()) |
| 1438 | globalNetworkProxy()->setUseSystemConfiguration(enable); |
| 1439 | } |
| 1440 | |
| 1441 | /*! |
| 1442 | Sets the application-wide proxy factory to be \a factory. This |
| 1443 | function will take ownership of that object and will delete it |
| 1444 | when necessary. |
| 1445 | |
| 1446 | The application-wide proxy is used as a last-resort when all other |
| 1447 | proxy selection requests returned QNetworkProxy::DefaultProxy. For |
| 1448 | example, QTcpSocket objects can have a proxy set with |
| 1449 | QTcpSocket::setProxy, but if none is set, the proxy factory class |
| 1450 | set with this function will be queried. |
| 1451 | |
| 1452 | If you set a proxy factory with this function, any application |
| 1453 | level proxies set with QNetworkProxy::setApplicationProxy will be |
| 1454 | overridden, and usesSystemConfiguration() will return \c{false}. |
| 1455 | |
| 1456 | \sa QNetworkProxy::setApplicationProxy(), |
| 1457 | QAbstractSocket::proxy(), QAbstractSocket::setProxy() |
| 1458 | */ |
| 1459 | void QNetworkProxyFactory::setApplicationProxyFactory(QNetworkProxyFactory *factory) |
| 1460 | { |
| 1461 | if (globalNetworkProxy()) |
| 1462 | globalNetworkProxy()->setApplicationProxyFactory(factory); |
| 1463 | } |
| 1464 | |
| 1465 | /*! |
| 1466 | \fn QList<QNetworkProxy> QNetworkProxyFactory::queryProxy(const QNetworkProxyQuery &query) |
| 1467 | |
| 1468 | This function takes the query request, \a query, |
| 1469 | examines the details of the type of socket or request and returns |
| 1470 | a list of QNetworkProxy objects that indicate the proxy servers to |
| 1471 | be used, in order of preference. |
| 1472 | |
| 1473 | When reimplementing this class, take care to return at least one |
| 1474 | element. |
| 1475 | |
| 1476 | If you cannot determine a better proxy alternative, use |
| 1477 | QNetworkProxy::DefaultProxy, which tells the code querying for a |
| 1478 | proxy to use a higher alternative. For example, if this factory is |
| 1479 | set to a QNetworkAccessManager object, DefaultProxy will tell it |
| 1480 | to query the application-level proxy settings. |
| 1481 | |
| 1482 | If this factory is set as the application proxy factory, |
| 1483 | DefaultProxy and NoProxy will have the same meaning. |
| 1484 | */ |
| 1485 | |
| 1486 | /*! |
| 1487 | \fn QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query) |
| 1488 | |
| 1489 | This function takes the query request, \a query, |
| 1490 | examines the details of the type of socket or request and returns |
| 1491 | a list of QNetworkProxy objects that indicate the proxy servers to |
| 1492 | be used, in order of preference. |
| 1493 | |
| 1494 | This function can be used to determine the platform-specific proxy |
| 1495 | settings. This function will use the libraries provided by the |
| 1496 | operating system to determine the proxy for a given connection, if |
| 1497 | such libraries exist. If they don't, this function will just return a |
| 1498 | QNetworkProxy of type QNetworkProxy::NoProxy. |
| 1499 | |
| 1500 | On Windows, this function will use the WinHTTP DLL functions. Despite |
| 1501 | its name, Microsoft suggests using it for all applications that |
| 1502 | require network connections, not just HTTP. This will respect the |
| 1503 | proxy settings set on the registry with the proxycfg.exe tool. If |
| 1504 | those settings are not found, this function will attempt to obtain |
| 1505 | Internet Explorer's settings and use them. |
| 1506 | |
| 1507 | On \macos, this function will obtain the proxy settings using the |
| 1508 | CFNetwork framework from Apple. It will apply the FTP, |
| 1509 | HTTP and HTTPS proxy configurations for queries that contain the |
| 1510 | protocol tag "ftp", "http" and "https", respectively. If the SOCKS |
| 1511 | proxy is enabled in that configuration, this function will use the |
| 1512 | SOCKS server for all queries. If SOCKS isn't enabled, it will use |
| 1513 | the HTTPS proxy for all TcpSocket and UrlRequest queries. |
| 1514 | |
| 1515 | On systems configured with libproxy support, this function will |
| 1516 | rely on libproxy to obtain the proxy settings. Depending on |
| 1517 | libproxy configurations, this can in turn delegate to desktop |
| 1518 | settings, environment variables, etc. |
| 1519 | |
| 1520 | On other systems, this function will pick up proxy settings from |
| 1521 | the "http_proxy" environment variable. This variable must be a URL |
| 1522 | using one of the following schemes: "http", "socks5" or "socks5h". |
| 1523 | |
| 1524 | \section1 Limitations |
| 1525 | |
| 1526 | These are the limitations for the current version of this |
| 1527 | function. Future versions of Qt may lift some of the limitations |
| 1528 | listed here. |
| 1529 | |
| 1530 | \list |
| 1531 | \li On Windows platforms, this function may take several seconds to |
| 1532 | execute depending on the configuration of the user's system. |
| 1533 | \endlist |
| 1534 | */ |
| 1535 | |
| 1536 | /*! |
| 1537 | This function takes the query request, \a query, |
| 1538 | examines the details of the type of socket or request and returns |
| 1539 | a list of QNetworkProxy objects that indicate the proxy servers to |
| 1540 | be used, in order of preference. |
| 1541 | */ |
| 1542 | QList<QNetworkProxy> QNetworkProxyFactory::proxyForQuery(const QNetworkProxyQuery &query) |
| 1543 | { |
| 1544 | if (!globalNetworkProxy()) |
| 1545 | return QList<QNetworkProxy>() << QNetworkProxy(QNetworkProxy::NoProxy); |
| 1546 | return globalNetworkProxy()->proxyForQuery(query); |
| 1547 | } |
| 1548 | |
| 1549 | #ifndef QT_NO_DEBUG_STREAM |
| 1550 | QDebug operator<<(QDebug debug, const QNetworkProxy &proxy) |
| 1551 | { |
| 1552 | QDebugStateSaver saver(debug); |
| 1553 | debug.resetFormat().nospace(); |
| 1554 | QNetworkProxy::ProxyType type = proxy.type(); |
| 1555 | switch (type) { |
| 1556 | case QNetworkProxy::NoProxy: |
| 1557 | debug << "NoProxy " ; |
| 1558 | break; |
| 1559 | case QNetworkProxy::DefaultProxy: |
| 1560 | debug << "DefaultProxy " ; |
| 1561 | break; |
| 1562 | case QNetworkProxy::Socks5Proxy: |
| 1563 | debug << "Socks5Proxy " ; |
| 1564 | break; |
| 1565 | case QNetworkProxy::HttpProxy: |
| 1566 | debug << "HttpProxy " ; |
| 1567 | break; |
| 1568 | case QNetworkProxy::HttpCachingProxy: |
| 1569 | debug << "HttpCachingProxy " ; |
| 1570 | break; |
| 1571 | case QNetworkProxy::FtpCachingProxy: |
| 1572 | debug << "FtpCachingProxy " ; |
| 1573 | break; |
| 1574 | default: |
| 1575 | debug << "Unknown proxy " << int(type); |
| 1576 | break; |
| 1577 | } |
| 1578 | debug << '"' << proxy.hostName() << ':' << proxy.port() << "\" " ; |
| 1579 | QNetworkProxy::Capabilities caps = proxy.capabilities(); |
| 1580 | QStringList scaps; |
| 1581 | if (caps & QNetworkProxy::TunnelingCapability) |
| 1582 | scaps << QStringLiteral("Tunnel" ); |
| 1583 | if (caps & QNetworkProxy::ListeningCapability) |
| 1584 | scaps << QStringLiteral("Listen" ); |
| 1585 | if (caps & QNetworkProxy::UdpTunnelingCapability) |
| 1586 | scaps << QStringLiteral("UDP" ); |
| 1587 | if (caps & QNetworkProxy::CachingCapability) |
| 1588 | scaps << QStringLiteral("Caching" ); |
| 1589 | if (caps & QNetworkProxy::HostNameLookupCapability) |
| 1590 | scaps << QStringLiteral("NameLookup" ); |
| 1591 | if (caps & QNetworkProxy::SctpTunnelingCapability) |
| 1592 | scaps << QStringLiteral("SctpTunnel" ); |
| 1593 | if (caps & QNetworkProxy::SctpListeningCapability) |
| 1594 | scaps << QStringLiteral("SctpListen" ); |
| 1595 | debug << '[' << scaps.join(sep: u' ') << ']'; |
| 1596 | return debug; |
| 1597 | } |
| 1598 | |
| 1599 | QDebug operator<<(QDebug debug, const QNetworkProxyQuery &proxyQuery) |
| 1600 | { |
| 1601 | QDebugStateSaver saver(debug); |
| 1602 | debug.resetFormat().nospace() |
| 1603 | << "ProxyQuery(" |
| 1604 | << "type: " << proxyQuery.queryType() |
| 1605 | << ", protocol: " << proxyQuery.protocolTag() |
| 1606 | << ", peerPort: " << proxyQuery.peerPort() |
| 1607 | << ", peerHostName: " << proxyQuery.peerHostName() |
| 1608 | << ", localPort: " << proxyQuery.localPort() |
| 1609 | << ", url: " << proxyQuery.url() |
| 1610 | << ')'; |
| 1611 | return debug; |
| 1612 | } |
| 1613 | #endif |
| 1614 | |
| 1615 | QT_END_NAMESPACE |
| 1616 | |
| 1617 | #include "moc_qnetworkproxy.cpp" |
| 1618 | |
| 1619 | #endif // QT_NO_NETWORKPROXY |
| 1620 | |