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 QtCore 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 <qglobal.h>
41
42#include <QtNetwork/private/qtnetworkglobal_p.h>
43
44#if QT_CONFIG(topleveldomain)
45
46#include "qplatformdefs.h"
47#include "qurl.h"
48#include "private/qurltlds_p.h"
49#include "private/qtldurl_p.h"
50#include "QtCore/qlist.h"
51#include "QtCore/qstring.h"
52
53QT_BEGIN_NAMESPACE
54
55enum TLDMatchType {
56 ExactMatch,
57 SuffixMatch,
58 ExceptionMatch,
59};
60
61// Scan the auto-generated table of TLDs for an entry. For more details
62// see comments in file: util/corelib/qurl-generateTLDs/main.cpp
63static bool containsTLDEntry(QStringView entry, TLDMatchType match)
64{
65 const QStringView matchSymbols[] = {
66 u"",
67 u"*",
68 u"!",
69 };
70 const auto symbol = matchSymbols[match];
71 const int index = qt_hash(entry, qt_hash(symbol)) % tldCount;
72
73 // select the right chunk from the big table
74 short chunk = 0;
75 uint chunkIndex = tldIndices[index], offset = 0;
76
77 // The offset in the big string, of the group that our entry hashes into.
78 const auto tldGroupOffset = tldIndices[index];
79
80 // It should always be inside all chunks' total size.
81 Q_ASSERT(tldGroupOffset < tldChunks[tldChunkCount - 1]);
82 // All offsets are stored in non-decreasing order.
83 // This check is within bounds as tldIndices has length tldCount+1.
84 Q_ASSERT(tldGroupOffset <= tldIndices[index + 1]);
85 // The last extra entry in tldIndices
86 // should be equal to the total of all chunks' lengths.
87 static_assert(tldIndices[tldCount] == tldChunks[tldChunkCount - 1]);
88
89 // Find which chunk contains the tldGroupOffset
90 while (tldGroupOffset >= tldChunks[chunk]) {
91 chunkIndex -= tldChunks[chunk];
92 offset += tldChunks[chunk];
93 chunk++;
94
95 // We can not go above the number of chunks we have, since all our
96 // indices are less than the total chunks' size (see asserts above).
97 Q_ASSERT(chunk < tldChunkCount);
98 }
99
100 // check all the entries from the given offset
101 while (chunkIndex < tldIndices[index+1] - offset) {
102 const auto utf8 = tldData[chunk] + chunkIndex;
103 if ((symbol.isEmpty() || QLatin1Char(*utf8) == symbol) && entry == QString::fromUtf8(utf8 + symbol.size()))
104 return true;
105 chunkIndex += uint(qstrlen(utf8)) + 1; // +1 for the ending \0
106 }
107 return false;
108}
109
110/*!
111 \internal
112
113 Return true if \a domain is a top-level-domain per Qt's copy of the Mozilla public suffix list.
114
115 The \a domain must be in lower-case format (as per QString::toLower()).
116*/
117
118Q_NETWORK_EXPORT bool qIsEffectiveTLD(QStringView domain)
119{
120 // for domain 'foo.bar.com':
121 // 1. return if TLD table contains 'foo.bar.com'
122 // 2. else if table contains '*.bar.com',
123 // 3. test that table does not contain '!foo.bar.com'
124
125 if (containsTLDEntry(domain, ExactMatch)) // 1
126 return true;
127
128 const auto dot = domain.indexOf(QLatin1Char('.'));
129 if (dot < 0) // Actual TLD: may be effective if the subject of a wildcard rule:
130 return containsTLDEntry(QString(QLatin1Char('.') + domain), SuffixMatch);
131 if (containsTLDEntry(domain.mid(dot), SuffixMatch)) // 2
132 return !containsTLDEntry(domain, ExceptionMatch); // 3
133 return false;
134}
135
136QT_END_NAMESPACE
137
138#endif
139