Qt 4.8
qnetworkproxy_generic.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qnetworkproxy.h"
43 
44 #include <QtCore/QByteArray>
45 #include <QtCore/QUrl>
46 
47 #ifndef QT_NO_NETWORKPROXY
48 
49 /*
50  * Construct a proxy from the environment variable http_proxy.
51  * Or no system proxy. Just return a list with NoProxy.
52  */
53 
55 
57 {
58  QList<QNetworkProxy> proxyList;
59 
60  const QString queryProtocol = query.protocolTag().toLower();
61  QByteArray proxy_env;
62 
63  if (queryProtocol == QLatin1String("http"))
64  proxy_env = qgetenv("http_proxy");
65  else if (queryProtocol == QLatin1String("https"))
66  proxy_env = qgetenv("https_proxy");
67  else if (queryProtocol == QLatin1String("ftp"))
68  proxy_env = qgetenv("ftp_proxy");
69  else
70  proxy_env = qgetenv("all_proxy");
71 
72  // Fallback to http_proxy is no protocol specific proxy was found
73  if (proxy_env.isEmpty())
74  proxy_env = qgetenv("http_proxy");
75 
76  if (!proxy_env.isEmpty()) {
77  QUrl url = QUrl(QString::fromLocal8Bit(proxy_env));
78  if (url.scheme() == QLatin1String("socks5")) {
80  url.port() ? url.port() : 1080, url.userName(), url.password());
81  proxyList << proxy;
82  } else if (url.scheme() == QLatin1String("socks5h")) {
84  url.port() ? url.port() : 1080, url.userName(), url.password());
86  proxyList << proxy;
87  } else if (url.scheme() == QLatin1String("http") || url.scheme().isEmpty()) {
89  url.port() ? url.port() : 8080, url.userName(), url.password());
90  proxyList << proxy;
91  }
92  }
93  if (proxyList.isEmpty())
94  proxyList << QNetworkProxy::NoProxy;
95 
96  return proxyList;
97 }
98 
100 
101 #endif
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QList< QNetworkProxy > systemProxyForQuery(const QNetworkProxyQuery &query=QNetworkProxyQuery())
This function takes the query request, query, examines the details of the type of socket or request a...
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
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
QString host() const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition: qurl.cpp:4837
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
int port() const
Returns the port of the URL, or -1 if the port is unspecified.
Definition: qurl.cpp:4916
The QNetworkProxy class provides a network layer proxy.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QString protocolTag() const
Returns the protocol tag for this QNetworkProxyQuery object, or an empty QString in case the protocol...
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QNetworkProxyQuery class is used to query the proxy settings for a socket.
Definition: qnetworkproxy.h:60
QString userName() const
Returns the user name of the URL if it is defined; otherwise an empty string is returned.
Definition: qurl.cpp:4667
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
void setCapabilities(Capabilities capab)
Sets the capabilities of this proxy to capabilities.
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QString password() const
Returns the password of the URL if it is defined; otherwise an empty string is returned.
Definition: qurl.cpp:4754