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