1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qnetworkproxy.h"
41
42#include <QtCore/QByteArray>
43#include <QtCore/QUrl>
44
45#ifndef QT_NO_NETWORKPROXY
46
47/*
48 * Construct a proxy from the environment variables http_proxy and no_proxy.
49 * Or no system proxy. Just return a list with NoProxy.
50 */
51
52QT_BEGIN_NAMESPACE
53
54static bool ignoreProxyFor(const QNetworkProxyQuery &query)
55{
56 const QByteArray noProxy = qgetenv("no_proxy").trimmed();
57 if (noProxy.isEmpty())
58 return false;
59
60 const QString host = query.peerHostName();
61
62 const QList<QByteArray> noProxyTokens = noProxy.split(',');
63
64 for (const QByteArray &rawToken : noProxyTokens) {
65 auto token = QLatin1String(rawToken).trimmed();
66
67 // Since we use suffix matching, "*" is our 'default' behaviour
68 if (token.startsWith(u'*'))
69 token = token.mid(1);
70
71 // Harmonize trailing dot notation
72 if (token.endsWith(u'.') && !host.endsWith(u'.'))
73 token = token.chopped(1);
74
75 if (token.startsWith(u'.')) // leading dot is implied
76 token = token.mid(1);
77
78 if (host.endsWith(token)) {
79
80 // Make sure that when we have a suffix match,
81 // we don't match "donotmatch.com" with "match.com"
82
83 if (host.size() == token.size()) // iow: host == token
84 return true;
85 if (host[host.size() - token.size() - 1] == u'.') // match follows a dot
86 return true;
87 }
88 }
89
90 return false;
91}
92
93QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query)
94{
95 QList<QNetworkProxy> proxyList;
96
97 if (ignoreProxyFor(query))
98 return proxyList << QNetworkProxy::NoProxy;
99
100 // No need to care about casing here, QUrl lowercases values already
101 const QString queryProtocol = query.protocolTag();
102 QByteArray proxy_env;
103
104 if (queryProtocol == QLatin1String("http"))
105 proxy_env = qgetenv("http_proxy");
106 else if (queryProtocol == QLatin1String("https"))
107 proxy_env = qgetenv("https_proxy");
108 else if (queryProtocol == QLatin1String("ftp"))
109 proxy_env = qgetenv("ftp_proxy");
110 else
111 proxy_env = qgetenv("all_proxy");
112
113 // Fallback to http_proxy is no protocol specific proxy was found
114 if (proxy_env.isEmpty())
115 proxy_env = qgetenv("http_proxy");
116
117 if (!proxy_env.isEmpty()) {
118 QUrl url = QUrl(QString::fromLocal8Bit(proxy_env));
119 const QString scheme = url.scheme();
120 if (scheme == QLatin1String("socks5")) {
121 QNetworkProxy proxy(QNetworkProxy::Socks5Proxy, url.host(),
122 url.port() ? url.port() : 1080, url.userName(), url.password());
123 proxyList << proxy;
124 } else if (scheme == QLatin1String("socks5h")) {
125 QNetworkProxy proxy(QNetworkProxy::Socks5Proxy, url.host(),
126 url.port() ? url.port() : 1080, url.userName(), url.password());
127 proxy.setCapabilities(QNetworkProxy::HostNameLookupCapability);
128 proxyList << proxy;
129 } else if ((scheme.isEmpty() || scheme == QLatin1String("http"))
130 && query.queryType() != QNetworkProxyQuery::UdpSocket
131 && query.queryType() != QNetworkProxyQuery::TcpServer) {
132 QNetworkProxy proxy(QNetworkProxy::HttpProxy, url.host(),
133 url.port() ? url.port() : 8080, url.userName(), url.password());
134 proxyList << proxy;
135 }
136 }
137 if (proxyList.isEmpty())
138 proxyList << QNetworkProxy::NoProxy;
139
140 return proxyList;
141}
142
143QT_END_NAMESPACE
144
145#endif
146