Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qnetworkproxy_generic.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qnetworkproxy.h"
5
6#include <QtCore/QByteArray>
7#include <QtCore/QUrl>
8
9#ifndef QT_NO_NETWORKPROXY
10
11/*
12 * Construct a proxy from the environment variables http_proxy and no_proxy.
13 * Or no system proxy. Just return a list with NoProxy.
14 */
15
17
18using namespace Qt::StringLiterals;
19
21{
22 const QByteArray noProxy = qgetenv("no_proxy").trimmed();
23 if (noProxy.isEmpty())
24 return false;
25
26 const QString host = query.peerHostName();
27
28 const QList<QByteArray> noProxyTokens = noProxy.split(',');
29
30 for (const QByteArray &rawToken : noProxyTokens) {
31 auto token = QLatin1StringView(rawToken).trimmed();
32
33 // Since we use suffix matching, "*" is our 'default' behaviour
34 if (token.startsWith(u'*'))
35 token = token.mid(1);
36
37 // Harmonize trailing dot notation
38 if (token.endsWith(u'.') && !host.endsWith(u'.'))
39 token = token.chopped(1);
40
41 if (token.startsWith(u'.')) // leading dot is implied
42 token = token.mid(1);
43
44 if (host.endsWith(token)) {
45
46 // Make sure that when we have a suffix match,
47 // we don't match "donotmatch.com" with "match.com"
48
49 if (host.size() == token.size()) // iow: host == token
50 return true;
51 if (host[host.size() - token.size() - 1] == u'.') // match follows a dot
52 return true;
53 }
54 }
55
56 return false;
57}
58
60{
61 QList<QNetworkProxy> proxyList;
62
64 return proxyList << QNetworkProxy::NoProxy;
65
66 // No need to care about casing here, QUrl lowercases values already
67 const QString queryProtocol = query.protocolTag();
68 QByteArray proxy_env;
69
70 if (queryProtocol == "http"_L1)
71 proxy_env = qgetenv("http_proxy");
72 else if (queryProtocol == "https"_L1)
73 proxy_env = qgetenv("https_proxy");
74 else if (queryProtocol == "ftp"_L1)
75 proxy_env = qgetenv("ftp_proxy");
76 else
77 proxy_env = qgetenv("all_proxy");
78
79 // Fallback to http_proxy is no protocol specific proxy was found
80 if (proxy_env.isEmpty())
81 proxy_env = qgetenv("http_proxy");
82
83 if (!proxy_env.isEmpty()) {
84 QUrl url = QUrl(QString::fromLocal8Bit(proxy_env));
85 const QString scheme = url.scheme();
86 if (scheme == "socks5"_L1) {
88 url.port() ? url.port() : 1080, url.userName(), url.password());
89 proxyList << proxy;
90 } else if (scheme == "socks5h"_L1) {
92 url.port() ? url.port() : 1080, url.userName(), url.password());
94 proxyList << proxy;
95 } else if ((scheme.isEmpty() || scheme == "http"_L1)
96 && query.queryType() != QNetworkProxyQuery::UdpSocket
97 && query.queryType() != QNetworkProxyQuery::TcpServer) {
99 url.port() ? url.port() : 8080, url.userName(), url.password());
100 proxyList << proxy;
101 }
102 }
103 if (proxyList.isEmpty())
104 proxyList << QNetworkProxy::NoProxy;
105
106 return proxyList;
107}
108
110
111#endif
\inmodule QtCore
Definition qbytearray.h:57
QByteArray trimmed() const &
Definition qbytearray.h:198
QList< QByteArray > split(char sep) const
Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays.
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:106
QLatin1StringView trimmed() const noexcept
Definition qlist.h:74
bool isEmpty() const noexcept
Definition qlist.h:390
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...
The QNetworkProxyQuery class is used to query the proxy settings for a socket.
The QNetworkProxy class provides a network layer proxy.
void setCapabilities(Capabilities capab)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5788
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5350
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
\inmodule QtCore
Definition qurl.h:94
QString host(ComponentFormattingOptions=FullyDecoded) const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition qurl.cpp:2337
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1983
int port(int defaultPort=-1) const
Definition qurl.cpp:2380
Token token
Definition keywords.cpp:444
Combined button and popup list for selecting options.
static bool ignoreProxyFor(const QNetworkProxyQuery &query)
GLenum query
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
QUrl url("example.com")
[constructor-url-reference]
QNetworkProxy proxy
[0]