diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2009-10-29 13:41:12 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2009-10-29 13:41:12 (GMT) |
commit | 244c27d22cb7c0e686210ceb2973862ae514f95b (patch) | |
tree | 9580ab0fbd4fd888fe19a6933272e5fcba08a5b9 | |
parent | ef4276442f8421771c827ad4e29bc69baf24de2d (diff) | |
download | Qt-244c27d22cb7c0e686210ceb2973862ae514f95b.zip Qt-244c27d22cb7c0e686210ceb2973862ae514f95b.tar.gz Qt-244c27d22cb7c0e686210ceb2973862ae514f95b.tar.bz2 |
Import a new implementation of fromUserInput.
Imported from http://github.com/icefox/guessurlfromstring
Licensed under the 3-clause BSD license by the copyright holder.
-rw-r--r-- | src/corelib/io/qurl.cpp | 57 | ||||
-rw-r--r-- | tests/auto/qurl/tst_qurl.cpp | 60 |
2 files changed, 117 insertions, 0 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index a865d8d..86680a5 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -6154,6 +6154,34 @@ QString QUrl::errorString() const \internal */ +// The following code has the following copyright: +/* + Copyright (C) Research In Motion Limited 2009. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Research In Motion Limited nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY Research In Motion Limited ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL Research In Motion Limited BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + /*! Returns a valid URL from a user supplied \a userInput string if one can be @@ -6198,6 +6226,35 @@ QString QUrl::errorString() const */ QUrl QUrl::fromUserInput(const QString &userInput) { + QString trimmedString = userInput.trimmed(); + + // Check the most common case of a valid url with scheme and host first + QUrl url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); + if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) + return url; + + // Absolute files that exists + if (QDir::isAbsolutePath(trimmedString) && QFile::exists(trimmedString)) + return QUrl::fromLocalFile(trimmedString); + + // If the string is missing the scheme or the scheme is not valid prepend a scheme + QString scheme = url.scheme(); + if (scheme.isEmpty() || scheme.contains(QLatin1Char('.')) || scheme == QLatin1String("localhost")) { + // Do not do anything for strings such as "foo", only "foo.com" + int dotIndex = trimmedString.indexOf(QLatin1Char('.')); + if (dotIndex != -1 || trimmedString.startsWith(QLatin1String("localhost"))) { + const QString hostscheme = trimmedString.left(dotIndex).toLower(); + QByteArray scheme = (hostscheme == QLatin1String("ftp")) ? "ftp" : "http"; + trimmedString = QLatin1String(scheme) + QLatin1String("://") + trimmedString; + } + url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); + } + + if (url.isValid()) + return url; + + return QUrl(); } +// end of BSD code QT_END_NAMESPACE diff --git a/tests/auto/qurl/tst_qurl.cpp b/tests/auto/qurl/tst_qurl.cpp index 178e5e5..6424dcc 100644 --- a/tests/auto/qurl/tst_qurl.cpp +++ b/tests/auto/qurl/tst_qurl.cpp @@ -3679,10 +3679,70 @@ void tst_QUrl::binaryData() void tst_QUrl::fromUserInput_data() { + // + // most of this test is: + // Copyright (C) Research In Motion Limited 2009. All rights reserved. + // Distributed under the BSD license. + // See qurl.cpp + // + + QTest::addColumn<QString>("string"); + QTest::addColumn<QUrl>("guessUrlFromString"); + + // Null + QTest::newRow("null") << QString() << QUrl(); + + // File + QDirIterator it(QDir::homePath()); + QString fileString; + int c = 0; + while (it.hasNext()) { + it.next(); + QTest::newRow(QString("file-%1").arg(c++).toLatin1()) << it.filePath() << QUrl::fromLocalFile(it.filePath()); + } + + // basic latin1 + QTest::newRow("unicode-0") << QString::fromUtf8("å.com/") << QUrl::fromEncoded(QString::fromUtf8("http://å.com/").toUtf8(), QUrl::TolerantMode); + // unicode + QTest::newRow("unicode-1") << QString::fromUtf8("?.com/") << QUrl::fromEncoded(QString::fromUtf8("http://?.com/").toUtf8(), QUrl::TolerantMode); + + // no scheme + QTest::newRow("add scheme-0") << "webkit.org" << QUrl("http://webkit.org"); + QTest::newRow("add scheme-1") << "www.webkit.org" << QUrl("http://www.webkit.org"); + QTest::newRow("add scheme-2") << "ftp.webkit.org" << QUrl("ftp://ftp.webkit.org"); + QTest::newRow("add scheme-3") << "webkit" << QUrl("webkit"); + + // QUrl's tolerant parser should already handle this + QTest::newRow("not-encoded-0") << "http://webkit.org/test page.html" << QUrl("http://webkit.org/test%20page.html"); + + // Make sure the :80, i.e. port doesn't screw anything up + QUrl portUrl("http://webkit.org"); + portUrl.setPort(80); + QTest::newRow("port-0") << "webkit.org:80" << portUrl; + QTest::newRow("port-1") << "http://webkit.org:80" << portUrl; + + // mailto doesn't have a ://, but is valid + QUrl mailto("ben@meyerhome.net"); + mailto.setScheme("mailto"); + QTest::newRow("mailto") << "mailto:ben@meyerhome.net" << mailto; + + // misc + QTest::newRow("localhost-0") << "localhost" << QUrl("http://localhost"); + QTest::newRow("localhost-1") << "localhost:80" << QUrl("http://localhost:80"); + QTest::newRow("spaces-0") << " http://webkit.org/test page.html " << QUrl("http://webkit.org/test%20page.html"); + + // FYI: The scheme in the resulting url user + QUrl authUrl("user:pass@domain.com"); + QTest::newRow("misc-1") << "user:pass@domain.com" << authUrl; } void tst_QUrl::fromUserInput() { + QFETCH(QString, string); + QFETCH(QUrl, guessUrlFromString); + + QUrl url = QUrl::fromUserInput(string); + QCOMPARE(url, guessUrlFromString); } void tst_QUrl::task_199967() |