~mardy/signon-ui/make-dist

« back to all changes in this revision

Viewing changes to src/my-network-proxy-factory.cpp

  • Committer: Alberto Mardegan
  • Date: 2012-09-17 10:24:50 UTC
  • mfrom: (60.1.2 proxy)
  • Revision ID: alberto.mardegan@canonical.com-20120917102450-bhtd8orxrmaiokmb
Use libproxy to detect proxy settings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copied from
 
3
 * https://codereview.qt-project.org/cat/29453%2C6%2Csrc/network/kernel/qnetworkproxy_libproxy.cpp%5E0
 
4
 *
 
5
 * With minor modifications by
 
6
 * Alberto Mardegan <alberto.mardegan@canonical.com>
 
7
 */
 
8
 
 
9
/****************************************************************************
 
10
**
 
11
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
 
12
** Contact: http://www.qt-project.org/
 
13
**
 
14
** This file is part of the QtNetwork module of the Qt Toolkit.
 
15
**
 
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.
 
24
**
 
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.
 
28
**
 
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.
 
36
**
 
37
** Other Usage
 
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.
 
40
**
 
41
**
 
42
**
 
43
**
 
44
**
 
45
**
 
46
** $QT_END_LICENSE$
 
47
**
 
48
****************************************************************************/
 
49
 
 
50
#include "my-network-proxy-factory.h"
 
51
 
 
52
#include <QNetworkProxy>
 
53
 
 
54
#include <QtCore/QByteArray>
 
55
#include <QtCore/QUrl>
 
56
 
 
57
#include <proxy.h>
 
58
 
 
59
class QLibProxyWrapper
 
60
{
 
61
public:
 
62
    QLibProxyWrapper()
 
63
        : factory(px_proxy_factory_new())
 
64
    {
 
65
    }
 
66
 
 
67
    ~QLibProxyWrapper()
 
68
    {
 
69
        px_proxy_factory_free(factory);
 
70
    }
 
71
 
 
72
    QList<QUrl> getProxies(const QUrl &url);
 
73
 
 
74
private:
 
75
    pxProxyFactory *factory;
 
76
};
 
77
 
 
78
Q_GLOBAL_STATIC(QLibProxyWrapper, libProxyWrapper);
 
79
 
 
80
/*
 
81
    Gets the list of proxies from libproxy, converted to QUrl list.
 
82
    Thread safe, according to libproxy documentation.
 
83
*/
 
84
QList<QUrl> QLibProxyWrapper::getProxies(const QUrl &url)
 
85
{
 
86
    QList<QUrl> ret;
 
87
 
 
88
    if (factory) {
 
89
        char **proxies = px_proxy_factory_get_proxies(factory, url.toEncoded());
 
90
        if (proxies) {
 
91
            for (int i = 0; proxies[i]; i++) {
 
92
                ret.append(QUrl::fromEncoded(proxies[i]));
 
93
                free(proxies[i]);
 
94
            }
 
95
            free(proxies);
 
96
        }
 
97
    }
 
98
 
 
99
    return ret;
 
100
}
 
101
 
 
102
QList<QNetworkProxy> MyNetworkProxyFactory::queryProxy(const QNetworkProxyQuery &query)
 
103
{
 
104
    QList<QNetworkProxy> proxyList;
 
105
 
 
106
    QUrl queryUrl;
 
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();
 
112
        break;
 
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;
 
119
        break;
 
120
    case QNetworkProxyQuery::UdpSocket:
 
121
        queryUrl.setScheme(QLatin1String("udp"));
 
122
        queryUrl.setHost(query.peerHostName());
 
123
        queryUrl.setPort(query.peerPort());
 
124
        requiredCapabilities |= QNetworkProxy::UdpTunnelingCapability;
 
125
        break;
 
126
    default:
 
127
        proxyList.append(QNetworkProxy(QNetworkProxy::NoProxy));
 
128
        return proxyList;
 
129
    }
 
130
 
 
131
    QList<QUrl> rawProxies = libProxyWrapper()->getProxies(query.url());
 
132
 
 
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;
 
146
        } else {
 
147
            continue; //unsupported proxy type e.g. socks4
 
148
        }
 
149
 
 
150
        QNetworkProxy proxy(type,
 
151
            url.host(),
 
152
            url.port(),
 
153
            url.userName(),
 
154
            url.password());
 
155
 
 
156
        if ((proxy.capabilities() & requiredCapabilities) == requiredCapabilities)
 
157
            proxyList.append(proxy);
 
158
    }
 
159
 
 
160
    // fallback is direct connection
 
161
    if (proxyList.isEmpty() || !haveDirectConnection)
 
162
        proxyList.append(QNetworkProxy(QNetworkProxy::NoProxy));
 
163
 
 
164
    return proxyList;
 
165
}