Skip to content

Instantly share code, notes, and snippets.

@mwenge
Created April 3, 2011 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwenge/900765 to your computer and use it in GitHub Desktop.
Save mwenge/900765 to your computer and use it in GitHub Desktop.
From df2bced26569f085a5c3dd8353567f8b1f1a93c6 Mon Sep 17 00:00:00 2001
From: Robert Hogan <robert@roberthogan.net>
Date: Sun, 3 Apr 2011 21:13:11 +0100
Subject: [PATCH] QUrl::topLevelDomain()
---
qmake/Makefile.unix | 6 +-
qmake/qmake.pri | 2 +
src/corelib/io/io.pri | 3 +
src/corelib/io/qtldurl.cpp | 114 +
src/corelib/io/qtldurl_p.h | 66 +
src/corelib/io/qurl.cpp | 13 +-
src/corelib/io/qurl.h | 1 +
src/corelib/io/qurltlds_p.h | 6481 ++++++++++++++++++++
src/corelib/io/qurltlds_p.h.INFO | 17 +
src/network/access/access.pri | 1 -
src/network/access/qnetworkcookiejar.cpp | 43 +-
src/network/access/qnetworkcookiejar_p.h | 3 -
src/network/access/qnetworkcookiejartlds_p.h | 6481 --------------------
src/network/access/qnetworkcookiejartlds_p.h.INFO | 17 -
.../qnetworkcookiejar/tst_qnetworkcookiejar.cpp | 4 +-
tests/auto/qurl/tst_qurl.cpp | 22 +
16 files changed, 6727 insertions(+), 6547 deletions(-)
create mode 100644 src/corelib/io/qtldurl.cpp
create mode 100644 src/corelib/io/qtldurl_p.h
create mode 100644 src/corelib/io/qurltlds_p.h
create mode 100644 src/corelib/io/qurltlds_p.h.INFO
delete mode 100644 src/network/access/qnetworkcookiejartlds_p.h
delete mode 100644 src/network/access/qnetworkcookiejartlds_p.h.INFO
diff --git a/qmake/Makefile.unix b/qmake/Makefile.unix
index fd502e0..c1f5c11 100644
--- a/qmake/Makefile.unix
+++ b/qmake/Makefile.unix
@@ -21,7 +21,7 @@ QOBJS=qtextcodec.o qutfcodec.o qstring.o qtextstream.o qiodevice.o qmalloc.o qgl
qfsfileengine_iterator.o qregexp.o qvector.o qbitarray.o qdir.o qdiriterator.o quuid.o qhash.o \
qfileinfo.o qdatetime.o qstringlist.o qabstractfileengine.o qtemporaryfile.o \
qmap.o qmetatype.o qsettings.o qsystemerror.o qlibraryinfo.o qvariant.o qvsnprintf.o \
- qlocale.o qlinkedlist.o qurl.o qnumeric.o qcryptographichash.o qxmlstream.o qxmlutils.o \
+ qlocale.o qlinkedlist.o qurl.o qtldurl.o qnumeric.o qcryptographichash.o qxmlstream.o qxmlutils.o \
$(QTOBJS)
@@ -58,6 +58,7 @@ DEPEND_SRC=project.cpp property.cpp meta.cpp main.cpp generators/makefile.cpp ge
$(SOURCE_PATH)/src/corelib/io/qfileinfo.cpp $(SOURCE_PATH)/src/corelib/tools/qdatetime.cpp \
$(SOURCE_PATH)/src/corelib/tools/qstringlist.cpp $(SOURCE_PATH)/src/corelib/tools/qmap.cpp \
$(SOURCE_PATH)/src/corelib/global/qconfig.cpp $(SOURCE_PATH)/src/corelib/io/qurl.cpp \
+ $(SOURCE_PATH)/src/corelib/io/qtldurl.cpp \
$(SOURCE_PATH)/src/corelib/tools/qlocale.cpp $(SOURCE_PATH)/src/corelib/tools/qlinkedlist.cpp \
$(SOURCE_PATH)/src/corelib/tools/qhash.cpp $(SOURCE_PATH)/src/corelib/kernel/qcore_mac.cpp \
$(SOURCE_PATH)/src/corelib/io/qtemporaryfile.cpp $(SOURCE_PATH)/src/corelib/kernel/qmetatype.cpp \
@@ -146,6 +147,9 @@ qcore_mac.o: $(SOURCE_PATH)/src/corelib/kernel/qcore_mac.cpp
qurl.o: $(SOURCE_PATH)/src/corelib/io/qurl.cpp
$(CXX) -c -o $@ $(CXXFLAGS) $(SOURCE_PATH)/src/corelib/io/qurl.cpp
+qtldurl.o: $(SOURCE_PATH)/src/corelib/io/qtldurl.cpp
+ $(CXX) -c -o $@ $(CXXFLAGS) $(SOURCE_PATH)/src/corelib/io/qtldurl.cpp
+
qutfcodec.o: $(SOURCE_PATH)/src/corelib/codecs/qutfcodec.cpp
$(CXX) -c -o $@ $(CXXFLAGS) $(SOURCE_PATH)/src/corelib/codecs/qutfcodec.cpp
diff --git a/qmake/qmake.pri b/qmake/qmake.pri
index bc7ddb8..1c21592 100644
--- a/qmake/qmake.pri
+++ b/qmake/qmake.pri
@@ -79,6 +79,7 @@ bootstrap { #Qt code
qtemporaryfile.cpp \
qtextstream.cpp \
qurl.cpp \
+ qtldurl.cpp \
quuid.cpp \
qsettings.cpp \
qlibraryinfo.cpp \
@@ -124,6 +125,7 @@ bootstrap { #Qt code
qtemporaryfile.h \
qtextstream.h \
qurl.h \
+ qtldurl_p.h \
quuid.h \
qvector.h \
qxmlstream.h \
diff --git a/src/corelib/io/io.pri b/src/corelib/io/io.pri
index 2b61192..21816c4 100644
--- a/src/corelib/io/io.pri
+++ b/src/corelib/io/io.pri
@@ -24,6 +24,8 @@ HEADERS += \
io/qresource_p.h \
io/qresource_iterator_p.h \
io/qurl.h \
+ io/qurltlds_p.h \
+ io/qtldurl_p.h \
io/qsettings.h \
io/qsettings_p.h \
io/qfsfileengine.h \
@@ -41,6 +43,7 @@ SOURCES += \
io/qbuffer.cpp \
io/qdatastream.cpp \
io/qdataurl.cpp \
+ io/qtldurl.cpp \
io/qdebug.cpp \
io/qdir.cpp \
io/qdiriterator.cpp \
diff --git a/src/corelib/io/qtldurl.cpp b/src/corelib/io/qtldurl.cpp
new file mode 100644
index 0000000..963e0a9
--- /dev/null
+++ b/src/corelib/io/qtldurl.cpp
@@ -0,0 +1,114 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qplatformdefs.h"
+#include "qurl.h"
+#include "private/qurltlds_p.h"
+#include "private/qtldurl_p.h"
+#include "QtCore/qstringlist.h"
+
+QT_BEGIN_NAMESPACE
+
+/*!
+ \internal
+
+ Decode a data: URL into its mimetype and payload. Returns a null string if
+ the URL could not be decoded.
+*/
+
+static bool containsTLDEntry(const QString &entry)
+{
+ int index = qHash(entry) % tldCount;
+ int currentDomainIndex = tldIndices[index];
+ while (currentDomainIndex < tldIndices[index+1]) {
+ QString currentEntry = QString::fromUtf8(tldData + currentDomainIndex);
+ if (currentEntry == entry)
+ return true;
+ currentDomainIndex += qstrlen(tldData + currentDomainIndex) + 1; // +1 for the ending \0
+ }
+ return false;
+}
+
+Q_CORE_EXPORT QString qTLD(const QString &domain)
+{
+ QStringList sections = domain.toLower().split(QLatin1Char('.'), QString::SkipEmptyParts);
+ if (sections.isEmpty())
+ return domain;
+
+ QString tld;
+ for (int j = sections.count() - 1; j >= 0; --j) {
+ if (containsTLDEntry(QLatin1String("*.") + sections.at(j))
+ && !containsTLDEntry(QLatin1String("!") + sections.at(j))) {
+ tld.prepend(QLatin1Char('.') + sections.at(j));
+ continue;
+ }
+ break;
+ }
+ return tld;
+}
+
+Q_CORE_EXPORT bool qIsEffectiveTLD(const QString &domain)
+{
+ // for domain 'foo.bar.com':
+ // 1. return if TLD table contains 'foo.bar.com'
+ if (containsTLDEntry(domain))
+ return true;
+
+ if (domain.contains(QLatin1Char('.'))) {
+ int count = domain.size() - domain.indexOf(QLatin1Char('.'));
+ QString wildCardDomain;
+ wildCardDomain.reserve(count + 1);
+ wildCardDomain.append(QLatin1Char('*'));
+ wildCardDomain.append(domain.right(count));
+ // 2. if table contains '*.bar.com',
+ // test if table contains '!foo.bar.com'
+ if (containsTLDEntry(wildCardDomain)) {
+ QString exceptionDomain;
+ exceptionDomain.reserve(domain.size() + 1);
+ exceptionDomain.append(QLatin1Char('!'));
+ exceptionDomain.append(domain);
+ return (! containsTLDEntry(exceptionDomain));
+ }
+ }
+ return false;
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/io/qtldurl_p.h b/src/corelib/io/qtldurl_p.h
new file mode 100644
index 0000000..e167cd6
--- /dev/null
+++ b/src/corelib/io/qtldurl_p.h
@@ -0,0 +1,66 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QTLDURL_P_H
+#define QTLDURL_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists for the convenience
+// of qDecodeDataUrl. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include "QtCore/qurl.h"
+#include "QtCore/qstring.h"
+
+QT_BEGIN_NAMESPACE
+
+Q_CORE_EXPORT QString qTLD(const QString &domain);
+Q_CORE_EXPORT bool qIsEffectiveTLD(const QString &domain);
+
+QT_END_NAMESPACE
+
+#endif // QDATAURL_P_H
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp
index efd3f45..a54ccaa 100644
--- a/src/corelib/io/qurl.cpp
+++ b/src/corelib/io/qurl.cpp
@@ -192,7 +192,7 @@
#if defined QT3_SUPPORT
#include "qfileinfo.h"
#endif
-
+#include "qtldurl_p.h"
#if defined(Q_OS_WINCE_WM)
#pragma optimize("g", off)
#endif
@@ -5593,6 +5593,17 @@ bool QUrl::hasFragment() const
}
/*!
+ \since 4.8
+
+ Returns the TLD (Top-Level Domain) of the URL, (e.g. .co.uk, .net).
+
+*/
+QString QUrl::topLevelDomain() const
+{
+ return qTLD(d->host);
+}
+
+/*!
Returns the result of the merge of this URL with \a relative. This
URL is used as a base to convert \a relative to an absolute URL.
diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h
index 7534b8d..b0a214f 100644
--- a/src/corelib/io/qurl.h
+++ b/src/corelib/io/qurl.h
@@ -181,6 +181,7 @@ public:
void setEncodedFragment(const QByteArray &fragment);
QByteArray encodedFragment() const;
bool hasFragment() const;
+ QString topLevelDomain() const;
QUrl resolved(const QUrl &relative) const;
diff --git a/src/corelib/io/qurltlds_p.h b/src/corelib/io/qurltlds_p.h
new file mode 100644
index 0000000..b06d881
--- /dev/null
+++ b/src/corelib/io/qurltlds_p.h
diff --git a/src/corelib/io/qurltlds_p.h.INFO b/src/corelib/io/qurltlds_p.h.INFO
new file mode 100644
index 0000000..57a8d0e
--- /dev/null
+++ b/src/corelib/io/qurltlds_p.h.INFO
@@ -0,0 +1,17 @@
+The file qnetworkcookiejartlds_p.h is generated from the Public Suffix
+List (see [1] and [2]), by the program residing at
+util/network/cookiejar-generateTLDs in the Qt source tree.
+
+That program generates a character array and an index array from the
+list to provide fast lookups of elements within C++.
+
+Those arrays in qnetworkcookiejartlds_p.h are derived from the Public
+Suffix List ([2]), which was originally provided by
+Jo Hermans <jo.hermans@gmail.com>.
+
+The file qnetworkcookiejartlds_p.h was last generated Friday,
+November 19th 15:24 2010.
+
+----
+[1] list: http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1
+[2] homepage: http://publicsuffix.org/
diff --git a/src/network/access/access.pri b/src/network/access/access.pri
index 57a79b3..290294a 100644
--- a/src/network/access/access.pri
+++ b/src/network/access/access.pri
@@ -22,7 +22,6 @@ HEADERS += \
access/qnetworkcookie_p.h \
access/qnetworkcookiejar.h \
access/qnetworkcookiejar_p.h \
- access/qnetworkcookiejartlds_p.h \
access/qnetworkrequest.h \
access/qnetworkrequest_p.h \
access/qnetworkreply.h \
diff --git a/src/network/access/qnetworkcookiejar.cpp b/src/network/access/qnetworkcookiejar.cpp
index 53fab9f..f2b1714 100644
--- a/src/network/access/qnetworkcookiejar.cpp
+++ b/src/network/access/qnetworkcookiejar.cpp
@@ -40,12 +40,12 @@
****************************************************************************/
#include "qnetworkcookiejar.h"
-#include "qnetworkcookiejartlds_p.h"
#include "qnetworkcookiejar_p.h"
#include "QtNetwork/qnetworkcookie.h"
#include "QtCore/qurl.h"
#include "QtCore/qdatetime.h"
+#include "private/qtldurl_p.h"
QT_BEGIN_NAMESPACE
@@ -216,7 +216,7 @@ bool QNetworkCookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieLis
// the check for effective TLDs makes the "embedded dot" rule from RFC 2109 section 4.3.2
// redundant; the "leading dot" rule has been relaxed anyway, see above
// we remove the leading dot for this check
- if (QNetworkCookieJarPrivate::isEffectiveTLD(domain.remove(0, 1)))
+ if (qIsEffectiveTLD(domain.remove(0, 1)))
continue; // not accepted
}
@@ -304,43 +304,4 @@ QList<QNetworkCookie> QNetworkCookieJar::cookiesForUrl(const QUrl &url) const
return result;
}
-bool QNetworkCookieJarPrivate::isEffectiveTLD(const QString &domain)
-{
- // for domain 'foo.bar.com':
- // 1. return if TLD table contains 'foo.bar.com'
- if (containsTLDEntry(domain))
- return true;
-
- if (domain.contains(QLatin1Char('.'))) {
- int count = domain.size() - domain.indexOf(QLatin1Char('.'));
- QString wildCardDomain;
- wildCardDomain.reserve(count + 1);
- wildCardDomain.append(QLatin1Char('*'));
- wildCardDomain.append(domain.right(count));
- // 2. if table contains '*.bar.com',
- // test if table contains '!foo.bar.com'
- if (containsTLDEntry(wildCardDomain)) {
- QString exceptionDomain;
- exceptionDomain.reserve(domain.size() + 1);
- exceptionDomain.append(QLatin1Char('!'));
- exceptionDomain.append(domain);
- return (! containsTLDEntry(exceptionDomain));
- }
- }
- return false;
-}
-
-bool QNetworkCookieJarPrivate::containsTLDEntry(const QString &entry)
-{
- int index = qHash(entry) % tldCount;
- int currentDomainIndex = tldIndices[index];
- while (currentDomainIndex < tldIndices[index+1]) {
- QString currentEntry = QString::fromUtf8(tldData + currentDomainIndex);
- if (currentEntry == entry)
- return true;
- currentDomainIndex += qstrlen(tldData + currentDomainIndex) + 1; // +1 for the ending \0
- }
- return false;
-}
-
QT_END_NAMESPACE
diff --git a/src/network/access/qnetworkcookiejar_p.h b/src/network/access/qnetworkcookiejar_p.h
index d6dc450..ce5a87a 100644
--- a/src/network/access/qnetworkcookiejar_p.h
+++ b/src/network/access/qnetworkcookiejar_p.h
@@ -63,9 +63,6 @@ class QNetworkCookieJarPrivate: public QObjectPrivate
public:
QList<QNetworkCookie> allCookies;
- static bool Q_AUTOTEST_EXPORT isEffectiveTLD(const QString &domain);
- static bool containsTLDEntry(const QString &entry);
-
Q_DECLARE_PUBLIC(QNetworkCookieJar)
};
diff --git a/src/network/access/qnetworkcookiejartlds_p.h b/src/network/access/qnetworkcookiejartlds_p.h
deleted file mode 100644
index b06d881..0000000
--- a/src/network/access/qnetworkcookiejartlds_p.h
+++ /dev/null
@@ -1,6481 +0,0 @@
diff --git a/src/network/access/qnetworkcookiejartlds_p.h.INFO b/src/network/access/qnetworkcookiejartlds_p.h.INFO
deleted file mode 100644
index 57a8d0e..0000000
--- a/src/network/access/qnetworkcookiejartlds_p.h.INFO
+++ /dev/null
@@ -1,17 +0,0 @@
-The file qnetworkcookiejartlds_p.h is generated from the Public Suffix
-List (see [1] and [2]), by the program residing at
-util/network/cookiejar-generateTLDs in the Qt source tree.
-
-That program generates a character array and an index array from the
-list to provide fast lookups of elements within C++.
-
-Those arrays in qnetworkcookiejartlds_p.h are derived from the Public
-Suffix List ([2]), which was originally provided by
-Jo Hermans <jo.hermans@gmail.com>.
-
-The file qnetworkcookiejartlds_p.h was last generated Friday,
-November 19th 15:24 2010.
-
-----
-[1] list: http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1
-[2] homepage: http://publicsuffix.org/
diff --git a/tests/auto/qnetworkcookiejar/tst_qnetworkcookiejar.cpp b/tests/auto/qnetworkcookiejar/tst_qnetworkcookiejar.cpp
index 178f6d8..1f7c269 100644
--- a/tests/auto/qnetworkcookiejar/tst_qnetworkcookiejar.cpp
+++ b/tests/auto/qnetworkcookiejar/tst_qnetworkcookiejar.cpp
@@ -42,7 +42,7 @@
#include <QtTest/QtTest>
#include <QtNetwork/QNetworkCookieJar>
-#include "private/qnetworkcookiejar_p.h"
+#include "private/qtldurl_p.h"
class tst_QNetworkCookieJar: public QObject
{
@@ -438,7 +438,7 @@ void tst_QNetworkCookieJar::effectiveTLDs()
#endif
QFETCH(QString, domain);
QFETCH(bool, isTLD);
- QCOMPARE(QNetworkCookieJarPrivate::isEffectiveTLD(domain), isTLD);
+ QCOMPARE(qIsEffectiveTLD(domain), isTLD);
}
QTEST_MAIN(tst_QNetworkCookieJar)
diff --git a/tests/auto/qurl/tst_qurl.cpp b/tests/auto/qurl/tst_qurl.cpp
index 336ee36..da9ad11 100644
--- a/tests/auto/qurl/tst_qurl.cpp
+++ b/tests/auto/qurl/tst_qurl.cpp
@@ -49,6 +49,7 @@
#include <qurl.h>
#include <qtextcodec.h>
#include <qmap.h>
+#include "private/qtldurl_p.h"
// For testsuites
#define IDNA_ACE_PREFIX "xn--"
@@ -88,6 +89,8 @@ public slots:
void init();
void cleanup();
private slots:
+ void effectiveTLDs_data();
+ void effectiveTLDs();
void getSetCheck();
void constructing();
void assignment();
@@ -4005,5 +4008,24 @@ void tst_QUrl::taskQTBUG_8701()
QCOMPARE(foo_uni_bar, QUrl(foo_uni_bar, QUrl::StrictMode).toString());
}
+void tst_QUrl::effectiveTLDs_data()
+{
+ QTest::addColumn<QString>("domain");
+ QTest::addColumn<QString>("TLD");
+
+ QTest::newRow("yes-wildcard5") << "test.co.uk" << ".co.uk";
+ QTest::newRow("yes-wildcard5") << "test.com" << ".com";
+}
+
+void tst_QUrl::effectiveTLDs()
+{
+#ifndef QT_BUILD_INTERNAL
+ QSKIP("Test requires private API", SkipAll);
+#endif
+ QFETCH(QString, domain);
+ QFETCH(QString, TLD);
+ QCOMPARE(qTLD(domain), TLD);
+}
+
QTEST_MAIN(tst_QUrl)
#include "tst_qurl.moc"
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment