Qt 4.8
qtldurl.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qplatformdefs.h"
43 #include "qurl.h"
44 #include "private/qurltlds_p.h"
45 #include "private/qtldurl_p.h"
46 #include "QtCore/qstringlist.h"
47 
49 
50 static bool containsTLDEntry(const QString &entry)
51 {
52  int index = qHash(entry) % tldCount;
53  int currentDomainIndex = tldIndices[index];
54  while (currentDomainIndex < tldIndices[index+1]) {
55  QString currentEntry = QString::fromUtf8(tldData + currentDomainIndex);
56  if (currentEntry == entry)
57  return true;
58  currentDomainIndex += qstrlen(tldData + currentDomainIndex) + 1; // +1 for the ending \0
59  }
60  return false;
61 }
62 
74 {
75  QStringList sections = domain.toLower().split(QLatin1Char('.'), QString::SkipEmptyParts);
76  if (sections.isEmpty())
77  return QString();
78 
79  QString level, tld;
80  for (int j = sections.count() - 1; j >= 0; --j) {
81  level.prepend(QLatin1Char('.') + sections.at(j));
82  if (qIsEffectiveTLD(level.right(level.size() - 1)))
83  tld = level;
84  }
85  return tld;
86 }
87 
98 {
99  // for domain 'foo.bar.com':
100  // 1. return if TLD table contains 'foo.bar.com'
101  if (containsTLDEntry(domain))
102  return true;
103 
104  if (domain.contains(QLatin1Char('.'))) {
105  int count = domain.size() - domain.indexOf(QLatin1Char('.'));
106  QString wildCardDomain;
107  wildCardDomain.reserve(count + 1);
108  wildCardDomain.append(QLatin1Char('*'));
109  wildCardDomain.append(domain.right(count));
110  // 2. if table contains '*.bar.com',
111  // test if table contains '!foo.bar.com'
112  if (containsTLDEntry(wildCardDomain)) {
113  QString exceptionDomain;
114  exceptionDomain.reserve(domain.size() + 1);
115  exceptionDomain.append(QLatin1Char('!'));
116  exceptionDomain.append(domain);
117  return (! containsTLDEntry(exceptionDomain));
118  }
119  }
120  return false;
121 }
122 
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static bool containsTLDEntry(const QString &entry)
Definition: qtldurl.cpp:50
QString & prepend(QChar c)
Definition: qstring.h:261
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT QString qTopLevelDomain(const QString &domain)
Return the top-level-domain per Qt&#39;s copy of the Mozilla public suffix list of domain.
Definition: qtldurl.cpp:73
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
static const quint16 tldIndices[]
Definition: qurltlds_p.h:63
void reserve(int size)
Attempts to allocate memory for at least size characters.
Definition: qstring.h:881
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QString right(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n rightmost characters of the string.
Definition: qstring.cpp:3682
static const quint16 tldCount
Definition: qurltlds_p.h:62
static const char tldData[]
Definition: qurltlds_p.h:4353
uint qHash(const QUrl &url)
Definition: qurl.h:285
QString & append(QChar c)
Definition: qstring.cpp:1777
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
uint qstrlen(const char *str)
Definition: qbytearray.h:79
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
Q_CORE_EXPORT bool qIsEffectiveTLD(const QString &domain)
Return true if domain is a top-level-domain per Qt&#39;s copy of the Mozilla public suffix list...
Definition: qtldurl.cpp:97
quint16 index
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55