3
* https://codereview.qt-project.org/cat/29453%2C6%2Csrc/network/kernel/qnetworkproxy_libproxy.cpp%5E0
5
* With minor modifications by
6
* Alberto Mardegan <alberto.mardegan@canonical.com>
9
/****************************************************************************
11
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
12
** Contact: http://www.qt-project.org/
14
** This file is part of the QtNetwork module of the Qt Toolkit.
16
** $QT_BEGIN_LICENSE:LGPL$
17
** GNU Lesser General Public License Usage
18
** This file may be used under the terms of the GNU Lesser General Public
19
** License version 2.1 as published by the Free Software Foundation and
20
** appearing in the file LICENSE.LGPL included in the packaging of this
21
** file. Please review the following information to ensure the GNU Lesser
22
** General Public License version 2.1 requirements will be met:
23
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Nokia gives you certain additional
26
** rights. These rights are described in the Nokia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU General
31
** Public License version 3.0 as published by the Free Software Foundation
32
** and appearing in the file LICENSE.GPL included in the packaging of this
33
** file. Please review the following information to ensure the GNU General
34
** Public License version 3.0 requirements will be met:
35
** http://www.gnu.org/copyleft/gpl.html.
38
** Alternatively, this file may be used in accordance with the terms and
39
** conditions contained in a signed written agreement between you and Nokia.
48
****************************************************************************/
50
#include "my-network-proxy-factory.h"
52
#include <QNetworkProxy>
54
#include <QtCore/QByteArray>
55
#include <QtCore/QUrl>
59
class QLibProxyWrapper
63
: factory(px_proxy_factory_new())
69
px_proxy_factory_free(factory);
72
QList<QUrl> getProxies(const QUrl &url);
75
pxProxyFactory *factory;
78
Q_GLOBAL_STATIC(QLibProxyWrapper, libProxyWrapper);
81
Gets the list of proxies from libproxy, converted to QUrl list.
82
Thread safe, according to libproxy documentation.
84
QList<QUrl> QLibProxyWrapper::getProxies(const QUrl &url)
89
char **proxies = px_proxy_factory_get_proxies(factory, url.toEncoded());
91
for (int i = 0; proxies[i]; i++) {
92
ret.append(QUrl::fromEncoded(proxies[i]));
102
QList<QNetworkProxy> MyNetworkProxyFactory::queryProxy(const QNetworkProxyQuery &query)
104
QList<QNetworkProxy> proxyList;
107
QNetworkProxy::Capabilities requiredCapabilities(0);
108
switch (query.queryType()) {
109
//URL requests are directly supported by libproxy
110
case QNetworkProxyQuery::UrlRequest:
111
queryUrl = query.url();
113
// fake URLs to get libproxy to tell us the SOCKS proxy
114
case QNetworkProxyQuery::TcpSocket:
115
queryUrl.setScheme(QLatin1String("tcp"));
116
queryUrl.setHost(query.peerHostName());
117
queryUrl.setPort(query.peerPort());
118
requiredCapabilities |= QNetworkProxy::TunnelingCapability;
120
case QNetworkProxyQuery::UdpSocket:
121
queryUrl.setScheme(QLatin1String("udp"));
122
queryUrl.setHost(query.peerHostName());
123
queryUrl.setPort(query.peerPort());
124
requiredCapabilities |= QNetworkProxy::UdpTunnelingCapability;
127
proxyList.append(QNetworkProxy(QNetworkProxy::NoProxy));
131
QList<QUrl> rawProxies = libProxyWrapper()->getProxies(query.url());
133
bool haveDirectConnection = false;
134
foreach (const QUrl& url, rawProxies) {
135
QNetworkProxy::ProxyType type;
136
if (url.scheme() == QLatin1String("http")) {
137
type = QNetworkProxy::HttpProxy;
138
} else if (url.scheme() == QLatin1String("socks")
139
|| url.scheme() == QLatin1String("socks5")) {
140
type = QNetworkProxy::Socks5Proxy;
141
} else if (url.scheme() == QLatin1String("ftp")) {
142
type = QNetworkProxy::FtpCachingProxy;
143
} else if (url.scheme() == QLatin1String("direct")) {
144
type = QNetworkProxy::NoProxy;
145
haveDirectConnection = true;
147
continue; //unsupported proxy type e.g. socks4
150
QNetworkProxy proxy(type,
156
if ((proxy.capabilities() & requiredCapabilities) == requiredCapabilities)
157
proxyList.append(proxy);
160
// fallback is direct connection
161
if (proxyList.isEmpty() || !haveDirectConnection)
162
proxyList.append(QNetworkProxy(QNetworkProxy::NoProxy));