From 2e97dd401a09e4a9027dea885db7fd95e961a0c5 Mon Sep 17 00:00:00 2001 From: David Faure Date: Fri, 21 Dec 2012 17:38:19 +0100 Subject: QUrl::fromUserInput: fix for urls without a host. QUrl::fromUserInput("http://") was invalid, which doesn't make sense since QUrl("http://") is valid. Same for "smb:" which is actually even more a valid URL from a user's point of view. Change-Id: I371ac393d61b49499edf5adbbc2a90b426fe9e5d (cherry-picked from qt5 commit 8b2728ec382565c5bd57148600e34740a3c3fe52) Reviewed-by: David Faure (KDE) --- src/corelib/io/qurl.cpp | 3 +-- tests/auto/qurl/tst_qurl.cpp | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 4b0bc39..551a990 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -6684,12 +6684,11 @@ QUrl QUrl::fromUserInput(const QString &userInput) QUrl url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); QUrl urlPrepended = QUrl::fromEncoded("http://" + trimmedString.toUtf8(), QUrl::TolerantMode); - // Check the most common case of a valid url with scheme and host + // Check the most common case of a valid url with a scheme // We check if the port would be valid by adding the scheme to handle the case host:port // where the host would be interpretted as the scheme if (url.isValid() && !url.scheme().isEmpty() - && (!url.host().isEmpty() || !url.path().isEmpty()) && urlPrepended.port() == -1) return url; diff --git a/tests/auto/qurl/tst_qurl.cpp b/tests/auto/qurl/tst_qurl.cpp index 737f0e9..64c579c 100644 --- a/tests/auto/qurl/tst_qurl.cpp +++ b/tests/auto/qurl/tst_qurl.cpp @@ -3908,6 +3908,10 @@ void tst_QUrl::fromUserInput_data() QTest::newRow("add scheme-2") << "ftp.example.org" << QUrl("ftp://ftp.example.org"); QTest::newRow("add scheme-3") << "hostname" << QUrl("http://hostname"); + // no host + QTest::newRow("nohost-1") << "http://" << QUrl("http://"); + QTest::newRow("nohost-2") << "smb:" << QUrl("smb:"); + // QUrl's tolerant parser should already handle this QTest::newRow("not-encoded-0") << "http://example.org/test page.html" << QUrl::fromEncoded("http://example.org/test%20page.html"); -- cgit v0.12