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 | |
52 | QT_BEGIN_NAMESPACE |
53 | |
54 | static bool ignoreProxyFor(const QNetworkProxyQuery &query) |
55 | { |
56 | const QByteArray noProxy = qgetenv(varName: "no_proxy" ).trimmed(); |
57 | if (noProxy.isEmpty()) |
58 | return false; |
59 | |
60 | const QList<QByteArray> noProxyTokens = noProxy.split(sep: ','); |
61 | |
62 | for (const QByteArray &rawToken : noProxyTokens) { |
63 | QByteArray token = rawToken.trimmed(); |
64 | QString peerHostName = query.peerHostName(); |
65 | |
66 | // Since we use suffix matching, "*" is our 'default' behaviour |
67 | if (token.startsWith(c: '*')) |
68 | token = token.mid(index: 1); |
69 | |
70 | // Harmonize trailing dot notation |
71 | if (token.endsWith(c: '.') && !peerHostName.endsWith(c: '.')) |
72 | token = token.left(len: token.length()-1); |
73 | |
74 | // We prepend a dot to both values, so that when we do a suffix match, |
75 | // we don't match "donotmatch.com" with "match.com" |
76 | if (!token.startsWith(c: '.')) |
77 | token.prepend(c: '.'); |
78 | |
79 | if (!peerHostName.startsWith(c: '.')) |
80 | peerHostName.prepend(c: '.'); |
81 | |
82 | if (peerHostName.endsWith(s: QLatin1String(token))) |
83 | return true; |
84 | } |
85 | |
86 | return false; |
87 | } |
88 | |
89 | QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query) |
90 | { |
91 | QList<QNetworkProxy> proxyList; |
92 | |
93 | if (ignoreProxyFor(query)) |
94 | return proxyList << QNetworkProxy::NoProxy; |
95 | |
96 | // No need to care about casing here, QUrl lowercases values already |
97 | const QString queryProtocol = query.protocolTag(); |
98 | QByteArray proxy_env; |
99 | |
100 | if (queryProtocol == QLatin1String("http" )) |
101 | proxy_env = qgetenv(varName: "http_proxy" ); |
102 | else if (queryProtocol == QLatin1String("https" )) |
103 | proxy_env = qgetenv(varName: "https_proxy" ); |
104 | else if (queryProtocol == QLatin1String("ftp" )) |
105 | proxy_env = qgetenv(varName: "ftp_proxy" ); |
106 | else |
107 | proxy_env = qgetenv(varName: "all_proxy" ); |
108 | |
109 | // Fallback to http_proxy is no protocol specific proxy was found |
110 | if (proxy_env.isEmpty()) |
111 | proxy_env = qgetenv(varName: "http_proxy" ); |
112 | |
113 | if (!proxy_env.isEmpty()) { |
114 | QUrl url = QUrl(QString::fromLocal8Bit(str: proxy_env)); |
115 | const QString scheme = url.scheme(); |
116 | if (scheme == QLatin1String("socks5" )) { |
117 | QNetworkProxy proxy(QNetworkProxy::Socks5Proxy, url.host(), |
118 | url.port() ? url.port() : 1080, url.userName(), url.password()); |
119 | proxyList << proxy; |
120 | } else if (scheme == QLatin1String("socks5h" )) { |
121 | QNetworkProxy proxy(QNetworkProxy::Socks5Proxy, url.host(), |
122 | url.port() ? url.port() : 1080, url.userName(), url.password()); |
123 | proxy.setCapabilities(QNetworkProxy::HostNameLookupCapability); |
124 | proxyList << proxy; |
125 | } else if ((scheme.isEmpty() || scheme == QLatin1String("http" )) |
126 | && query.queryType() != QNetworkProxyQuery::UdpSocket |
127 | && query.queryType() != QNetworkProxyQuery::TcpServer) { |
128 | QNetworkProxy proxy(QNetworkProxy::HttpProxy, url.host(), |
129 | url.port() ? url.port() : 8080, url.userName(), url.password()); |
130 | proxyList << proxy; |
131 | } |
132 | } |
133 | if (proxyList.isEmpty()) |
134 | proxyList << QNetworkProxy::NoProxy; |
135 | |
136 | return proxyList; |
137 | } |
138 | |
139 | QT_END_NAMESPACE |
140 | |
141 | #endif |
142 | |