Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qnetworkproxy_android.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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// Qt-Security score:significant reason:trusted-data
4
6
7#include <QtCore/qapplicationstatic.h>
8#include <QtCore/qcoreapplication_platform.h>
9#include <QtCore/qjnienvironment.h>
10#include <QtCore/qjniobject.h>
11
12#ifndef QT_NO_NETWORKPROXY
13
14QT_BEGIN_NAMESPACE
15
16struct ProxyInfoObject
17{
18public:
19 ProxyInfoObject();
20 ~ProxyInfoObject();
21};
22
23using namespace QNativeInterface;
24using namespace QtJniTypes;
25
26Q_APPLICATION_STATIC(ProxyInfoObject, proxyInfoInstance)
27
28Q_DECLARE_JNI_CLASS(QtNetwork, "org/qtproject/qt/android/network/QtNetwork")
29Q_DECLARE_JNI_CLASS(ProxyInfo, "android/net/ProxyInfo")
30
31ProxyInfoObject::ProxyInfoObject()
32{
33 QtNetwork::callStaticMethod<void>("registerReceiver", QAndroidApplication::context());
34}
35
36ProxyInfoObject::~ProxyInfoObject()
37{
38 QtNetwork::callStaticMethod<void>("unregisterReceiver", QAndroidApplication::context());
39}
40
41// nonProxyHosts entries, as ignoreProxyFor() in qnetworkproxy_generic.cpp.
42static bool isExcluded(QStringView host, QStringView entry)
43{
44 QStringView token = entry.trimmed();
45 if (token.isEmpty())
46 return false;
47 if (token.size() == 1 && token.front() == u'*')
48 return true;
49
50 if (token.startsWith(u'*'))
51 token = token.sliced(1);
52 if (token.endsWith(u'.') && !host.endsWith(u'.'))
53 token = token.chopped(1);
54 if (token.startsWith(u'.'))
55 token = token.sliced(1);
56
57 if (token.isEmpty() || !host.endsWith(token, Qt::CaseInsensitive))
58 return false;
59
60 // Keep "example.com" from matching "notexample.com".
61 return (host.size() == token.size()) || (host[host.size() - token.size() - 1] == u'.');
62}
63
64QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query)
65{
66 QList<QNetworkProxy> proxyList;
67 if (!proxyInfoInstance)
68 return proxyList;
69
70 QJniObject proxyInfo = QtNetwork::callStaticMethod<ProxyInfo>("getProxyInfo",
71 QAndroidApplication::context());
72 if (proxyInfo.isValid()) {
73 const QJniArray exclusionList = proxyInfo.callMethod<String[]>("getExclusionList");
74 bool exclude = false;
75 if (exclusionList.isValid()) {
76 const QString host = query.peerHostName();
77 for (const auto &entry : exclusionList) {
78 if (isExcluded(host, entry.toString())) {
79 exclude = true;
80 break;
81 }
82 }
83 }
84 if (!exclude) {
85 const QString hostName = proxyInfo.callMethod<QString>("getHost");
86 const int port = proxyInfo.callMethod<jint>("getPort");
87 QNetworkProxy proxy(QNetworkProxy::HttpProxy, hostName, port);
88 proxyList << proxy;
89 }
90 }
91 if (proxyList.isEmpty())
92 proxyList << QNetworkProxy::NoProxy;
93
94 return proxyList;
95}
96
97QT_END_NAMESPACE
98
99#endif