Qt 4.8
Public Functions | Static Public Functions | List of all members
QNetworkProxyFactory Class Referenceabstract

The QNetworkProxyFactory class provides fine-grained proxy selection. More...

#include <qnetworkproxy.h>

Inheritance diagram for QNetworkProxyFactory:
Maemo::NetworkProxyFactory QSystemConfigurationProxyFactory

Public Functions

 QNetworkProxyFactory ()
 Creates a QNetworkProxyFactory object. More...
 
virtual QList< QNetworkProxyqueryProxy (const QNetworkProxyQuery &query=QNetworkProxyQuery())=0
 This function takes the query request, query, examines the details of the type of socket or request and returns a list of QNetworkProxy objects that indicate the proxy servers to be used, in order of preference. More...
 
virtual ~QNetworkProxyFactory ()
 Destroys the QNetworkProxyFactory object. More...
 

Static Public Functions

static QList< QNetworkProxyproxyForQuery (const QNetworkProxyQuery &query)
 This function takes the query request, query, examines the details of the type of socket or request and returns a list of QNetworkProxy objects that indicate the proxy servers to be used, in order of preference. More...
 
static void setApplicationProxyFactory (QNetworkProxyFactory *factory)
 Sets the application-wide proxy factory to be factory. More...
 
static void setUseSystemConfiguration (bool enable)
 Enables the use of the platform-specific proxy settings, and only those. More...
 
static QList< QNetworkProxysystemProxyForQuery (const QNetworkProxyQuery &query=QNetworkProxyQuery())
 This function takes the query request, query, examines the details of the type of socket or request and returns a list of QNetworkProxy objects that indicate the proxy servers to be used, in order of preference. More...
 

Detailed Description

The QNetworkProxyFactory class provides fine-grained proxy selection.

Since
4.5
Attention
Module: QtNetwork

QNetworkProxyFactory is an extension to QNetworkProxy, allowing applications to have a more fine-grained control over which proxy servers are used, depending on the socket requesting the proxy. This allows an application to apply different settings, according to the protocol or destination hostname, for instance.

QNetworkProxyFactory can be set globally for an application, in which case it will override any global proxies set with QNetworkProxy::setApplicationProxy(). If set globally, any sockets created with Qt will query the factory to determine the proxy to be used.

A factory can also be set in certain frameworks that support multiple connections, such as QNetworkAccessManager. When set on such object, the factory will be queried for sockets created by that framework only.

System Proxies

You can configure a factory to use the system proxy's settings. Call the setUseSystemConfiguration() function with true to enable this behavior, or false to disable it.

Similarly, you can use a factory to make queries directly to the system proxy by calling its systemProxyForQuery() function.

Warning
Depending on the configuration of the user's system, the use of system proxy features on certain platforms may be subject to limitations. The systemProxyForQuery() documentation contains a list of these limitations for those platforms that are affected.

Definition at line 182 of file qnetworkproxy.h.

Constructors and Destructors

◆ QNetworkProxyFactory()

QNetworkProxyFactory::QNetworkProxyFactory ( )

Creates a QNetworkProxyFactory object.

Since QNetworkProxyFactory is an abstract class, you cannot create objects of type QNetworkProxyFactory directly.

Definition at line 1337 of file qnetworkproxy.cpp.

1338 {
1339 }

◆ ~QNetworkProxyFactory()

QNetworkProxyFactory::~QNetworkProxyFactory ( )
virtual

Destroys the QNetworkProxyFactory object.

Definition at line 1344 of file qnetworkproxy.cpp.

1345 {
1346 }

Functions

◆ proxyForQuery()

QList< QNetworkProxy > QNetworkProxyFactory::proxyForQuery ( const QNetworkProxyQuery query)
static

This function takes the query request, query, examines the details of the type of socket or request and returns a list of QNetworkProxy objects that indicate the proxy servers to be used, in order of preference.

Definition at line 1492 of file qnetworkproxy.cpp.

Referenced by QNetworkAccessManagerPrivate::queryProxy(), QAbstractSocketPrivate::resolveProxy(), and QTcpServerPrivate::resolveProxy().

1493 {
1494  if (!globalNetworkProxy())
1496  return globalNetworkProxy()->proxyForQuery(query);
1497 }
The QNetworkProxy class provides a network layer proxy.

◆ queryProxy()

QList< QNetworkProxy > QNetworkProxyFactory::queryProxy ( const QNetworkProxyQuery query = QNetworkProxyQuery())
pure virtual

This function takes the query request, query, examines the details of the type of socket or request and returns a list of QNetworkProxy objects that indicate the proxy servers to be used, in order of preference.

When reimplementing this class, take care to return at least one element.

If you cannot determine a better proxy alternative, use QNetworkProxy::DefaultProxy, which tells the code querying for a proxy to use a higher alternative. For example, if this factory is set to a QNetworkAccessManager object, DefaultProxy will tell it to query the application-level proxy settings.

If this factory is set as the application proxy factory, DefaultProxy and NoProxy will have the same meaning.

Implemented in Maemo::NetworkProxyFactory, and QSystemConfigurationProxyFactory.

Referenced by QGlobalNetworkProxy::proxyForQuery(), and QNetworkAccessManagerPrivate::queryProxy().

◆ setApplicationProxyFactory()

void QNetworkProxyFactory::setApplicationProxyFactory ( QNetworkProxyFactory factory)
static

Sets the application-wide proxy factory to be factory.

This function will take ownership of that object and will delete it when necessary.

The application-wide proxy is used as a last-resort when all other proxy selection requests returned QNetworkProxy::DefaultProxy. For example, QTcpSocket objects can have a proxy set with QTcpSocket::setProxy, but if none is set, the proxy factory class set with this function will be queried.

If you set a proxy factory with this function, any application level proxies set with QNetworkProxy::setApplicationProxy will be overridden.

See also
QNetworkProxy::setApplicationProxy(), QAbstractSocket::proxy(), QAbstractSocket::setProxy()

Definition at line 1395 of file qnetworkproxy.cpp.

Referenced by Maemo::ProxyConf::clear(), and Maemo::ProxyConf::update().

1396 {
1397  if (globalNetworkProxy())
1398  globalNetworkProxy()->setApplicationProxyFactory(factory);
1399 }

◆ setUseSystemConfiguration()

void QNetworkProxyFactory::setUseSystemConfiguration ( bool  enable)
static

Enables the use of the platform-specific proxy settings, and only those.

Since
4.6

See systemProxyForQuery() for more information.

Internally, this method (when called with enable set to true) sets an application-wide proxy factory. For this reason, this method is mutually exclusive with setApplicationProxyFactory(): calling setApplicationProxyFactory() overrides the use of the system-wide proxy, and calling setUseSystemConfiguration() overrides any application proxy or proxy factory that was previously set.

Note
See the systemProxyForQuery() documentation for a list of limitations related to the use of system proxies.

Definition at line 1368 of file qnetworkproxy.cpp.

1369 {
1370  if (enable) {
1372  } else {
1374  }
1375 }
static void setApplicationProxyFactory(QNetworkProxyFactory *factory)
Sets the application-wide proxy factory to be factory.

◆ systemProxyForQuery()

QList< QNetworkProxy > QNetworkProxyFactory::systemProxyForQuery ( const QNetworkProxyQuery query = QNetworkProxyQuery())
static

This function takes the query request, query, examines the details of the type of socket or request and returns a list of QNetworkProxy objects that indicate the proxy servers to be used, in order of preference.

This function can be used to determine the platform-specific proxy settings. This function will use the libraries provided by the operating system to determine the proxy for a given connection, if such libraries exist. If they don't, this function will just return a QNetworkProxy of type QNetworkProxy::NoProxy.

On Windows, this function will use the WinHTTP DLL functions. Despite its name, Microsoft suggests using it for all applications that require network connections, not just HTTP. This will respect the proxy settings set on the registry with the proxycfg.exe tool. If those settings are not found, this function will attempt to obtain Internet Explorer's settings and use them.

On MacOS X, this function will obtain the proxy settings using the SystemConfiguration framework from Apple. It will apply the FTP, HTTP and HTTPS proxy configurations for queries that contain the protocol tag "ftp", "http" and "https", respectively. If the SOCKS proxy is enabled in that configuration, this function will use the SOCKS server for all queries. If SOCKS isn't enabled, it will use the HTTPS proxy for all TcpSocket and UrlRequest queries.

On BlackBerry, this function obtains proxy settings for the default configuration using system configuration. The type will be set based on protocol tag "http", "https", "ftp", respectively. By default, it assumes http type. Proxy username and password are also set during the query using system configuration.

On other systems, this function will pick up proxy settings from the "http_proxy" environment variable. This variable must be a URL using one of the following schemes: "http", "socks5" or "socks5h".

Limitations

These are the limitations for the current version of this function. Future versions of Qt may lift some of the limitations listed here.

  • On MacOS X, this function will ignore the Proxy Auto Configuration settings, since it cannot execute the associated ECMAScript code.

  • On Windows platforms, this function may take several seconds to execute depending on the configuration of the user's system.

    • On BlackBerry, only UrlRequest and TcpSocket queries are supported. SOCKS is not supported. The proxy credentials are only retrieved for the default configuration.

Some notes about the code:

** It is assumed that the system proxies are for url based requests ie. HTTP/HTTPS based.

Definition at line 63 of file qnetworkproxy_blackberry.cpp.

Referenced by QWindowsSystemProxy::init(), macQueryInternal(), and QSystemConfigurationProxyFactory::queryProxy().

64 {
65  if (query.url().scheme() == QLatin1String("file")
66  || query.url().scheme() == QLatin1String("qrc"))
68 
71  qWarning("Unsupported query type: %d", query.queryType());
73  }
74 
75  QUrl url;
77  url = query.url();
78  } else if (query.queryType() == QNetworkProxyQuery::TcpSocket
79  && !query.peerHostName().isEmpty()) {
80  url.setHost(query.peerHostName());
81  switch (query.peerPort()) {
82  case 443:
83  url.setScheme(QLatin1String("https"));
84  break;
85  case 21:
86  url.setScheme(QLatin1String("ftp"));
87  break;
88  default:
89  // for unknown ports, we just pretend we are dealing
90  // with a HTTP URL, otherwise we will not get a proxy
91  // from the netstatus API
92  url.setScheme(QLatin1String("http"));
93  }
94  }
95 
96  if (!url.isValid()) {
97  qWarning("Invalid URL: %s", qPrintable(url.toString()));
99  }
100 
101  netstatus_proxy_details_t details;
102  memset(&details, 0, sizeof(netstatus_proxy_details_t));
103 
104 #if BPS_VERSION >= 3001001
105 
106  QByteArray bUrl(url.toEncoded());
107  QString sInterface(query.networkConfiguration().name());
108  QByteArray bInterface;
109  if (!sInterface.isEmpty()) {
111  qWarning("Unsupported configuration type: %d", query.networkConfiguration().type());
113  }
114  bInterface = sInterface.toUtf8();
115  }
116 
117  if (netstatus_get_proxy_details_for_url(bUrl.constData(), (bInterface.isEmpty() ? NULL : bInterface.constData()), &details) != BPS_SUCCESS) {
118  qWarning("netstatus_get_proxy_details_for_url failed! errno: %d", errno);
120  }
121 
122 #else
123 
124  if (netstatus_get_proxy_details(&details) != BPS_SUCCESS) {
125  qWarning("netstatus_get_proxy_details failed! errno: %d", errno);
127  }
128 
129 #endif
130 
131  if (details.http_proxy_host == NULL) { // No proxy
132  netstatus_free_proxy_details(&details);
134  }
135 
136  QNetworkProxy proxy;
137 
138  QString protocol = query.protocolTag();
139  if (protocol.startsWith(QLatin1String("http"), Qt::CaseInsensitive)) { // http, https
141  } else if (protocol == QLatin1String("ftp")) {
143  } else { // assume http proxy
144  qDebug("Proxy type: %s assumed to be http proxy", qPrintable(protocol));
146  }
147 
148  // Set host
149  // Note: ftp and https proxy type fields *are* obsolete.
150  // The user interface allows only one host/port which gets duplicated
151  // to all proxy type fields.
152  proxy.setHostName(QString::fromUtf8(details.http_proxy_host));
153 
154  // Set port
155  proxy.setPort(details.http_proxy_port);
156 
157  // Set username
158  if (details.http_proxy_login_user)
159  proxy.setUser(QString::fromUtf8(details.http_proxy_login_user));
160 
161  // Set password
162  if (details.http_proxy_login_password)
163  proxy.setPassword(QString::fromUtf8(details.http_proxy_login_password));
164 
165  netstatus_free_proxy_details(&details);
166 
167  return QList<QNetworkProxy>() << proxy;
168 }
int peerPort() const
Returns the port number for the outgoing request or -1 if the port number is not known.
bool isValid() const
Returns true if the URL is valid; otherwise returns false.
Definition: qurl.cpp:4303
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
QString peerHostName() const
Returns the host name or IP address being of the outgoing connection being requested, or an empty string if the remote hostname is not known.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void setUser(const QString &userName)
Sets the user name for proxy authentication to be user.
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
void setHost(const QString &host)
Sets the host of the URL to host.
Definition: qurl.cpp:4821
Q_CORE_EXPORT void qDebug(const char *,...)
The QNetworkProxy class provides a network layer proxy.
QueryType queryType() const
Returns the query type.
QString protocolTag() const
Returns the protocol tag for this QNetworkProxyQuery object, or an empty QString in case the protocol...
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
Q_CORE_EXPORT void qWarning(const char *,...)
void setHostName(const QString &hostName)
Sets the host name of the proxy host to be hostName.
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
QNetworkConfiguration networkConfiguration() const
Returns the network configuration of the proxy query.
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
QString name() const
Returns the user visible name of this configuration.
void setType(QNetworkProxy::ProxyType type)
Sets the proxy type for this instance to be type.
void setScheme(const QString &scheme)
Sets the scheme of the URL to scheme.
Definition: qurl.cpp:4533
QUrl url() const
Returns the URL component of this QNetworkProxyQuery object in case of a query of type QNetworkProxyQ...
void setPassword(const QString &password)
Sets the password for proxy authentication to be password.
#define qPrintable(string)
Definition: qglobal.h:1750
void setPort(quint16 port)
Sets the port of the proxy host to be port.
int errno
Type type() const
Returns the type of the configuration.

The documentation for this class was generated from the following files: