diff options
-rw-r--r-- | src/corelib/io/qurl.cpp | 31 | ||||
-rw-r--r-- | tests/auto/corelib/io/qurl/tst_qurl.cpp | 17 |
2 files changed, 36 insertions, 12 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index ac694a464a..a499dc2d30 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -499,9 +499,10 @@ public: InvalidFragmentError = Fragment << 8, - // the following two cases are only possible in combination - // with presence/absence of the authority and scheme. See validityError(). + // the following three cases are only possible in combination with + // presence/absence of the path, authority and scheme. See validityError(). AuthorityPresentAndPathIsRelative = Authority << 8 | Path << 8 | 0x10000, + AuthorityAbsentAndPathIsDoubleSlash, RelativeUrlPathContainsColonBeforeSlash = Scheme << 8 | Authority << 8 | Path << 8 | 0x10000, NoError = 0 @@ -1627,19 +1628,32 @@ inline QUrlPrivate::ErrorCode QUrlPrivate::validityError(QString *source, int *p return error->code; } - // There are two more cases of invalid URLs that QUrl recognizes and they + // There are three more cases of invalid URLs that QUrl recognizes and they // are only possible with constructed URLs (setXXX methods), not with // parsing. Therefore, they are tested here. // - // The two cases are a non-empty path that doesn't start with a slash and: + // Two cases are a non-empty path that doesn't start with a slash and: // - with an authority // - without an authority, without scheme but the path with a colon before // the first slash + // The third case is an empty authority and a non-empty path that starts + // with "//". // Those cases are considered invalid because toString() would produce a URL // that wouldn't be parsed back to the same QUrl. - if (path.isEmpty() || path.at(0) == QLatin1Char('/')) + if (path.isEmpty()) return NoError; + if (path.at(0) == QLatin1Char('/')) { + if (sectionIsPresent & QUrlPrivate::Authority || port != -1 || + path.length() == 1 || path.at(1) != QLatin1Char('/')) + return NoError; + if (source) { + *source = path; + *position = 0; + } + return AuthorityAbsentAndPathIsDoubleSlash; + } + if (sectionIsPresent & QUrlPrivate::Host) { if (source) { *source = path; @@ -2514,10 +2528,7 @@ void QUrl::setPath(const QString &path, ParsingMode mode) mode = TolerantMode; } - int from = 0; - while (from < data.length() - 2 && data.midRef(from, 2) == QLatin1String("//")) - ++from; - d->setPath(data, from, data.length()); + d->setPath(data, 0, data.length()); // optimized out, since there is no path delimiter // if (path.isNull()) @@ -3989,6 +4000,8 @@ static QString errorMessage(QUrlPrivate::ErrorCode errorCode, const QString &err case QUrlPrivate::AuthorityPresentAndPathIsRelative: return QStringLiteral("Path component is relative and authority is present"); + case QUrlPrivate::AuthorityAbsentAndPathIsDoubleSlash: + return QStringLiteral("Path component starts with '//' and authority is absent"); case QUrlPrivate::RelativeUrlPathContainsColonBeforeSlash: return QStringLiteral("Relative URL's path component contains ':' before any '/'"); } diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index c5647752fd..1ff85fc6dc 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -2075,6 +2075,14 @@ void tst_QUrl::isValid() } { + QUrl url("http:"); + url.setPath("//example.com"); + QVERIFY(!url.isValid()); + QVERIFY(url.toString().isEmpty()); + QVERIFY(url.errorString().contains("Path component starts with '//' and authority is absent")); + } + + { QUrl url; url.setPath("http://example.com"); QVERIFY(!url.isValid()); @@ -3649,12 +3657,12 @@ void tst_QUrl::setComponents_data() QTest::newRow("path-%3A-before-slash") << QUrl() << int(Path) << "c%3A/" << Tolerant << true << PrettyDecoded << "c%3A/" << "c%3A/"; - QTest::newRow("path-doubleslash") << QUrl("trash:/") + QTest::newRow("path-doubleslash") << QUrl("http://example.com") << int(Path) << "//path" << Tolerant << true - << PrettyDecoded << "/path" << "trash:/path"; + << PrettyDecoded << "//path" << "http://example.com//path"; QTest::newRow("path-withdotdot") << QUrl("file:///tmp") << int(Path) << "//tmp/..///root/." << Tolerant << true - << PrettyDecoded << "/tmp/..///root/." << "file:///tmp/..///root/."; + << PrettyDecoded << "//tmp/..///root/." << "file:////tmp/..///root/."; // the other fields can be present and be empty // that is, their delimiters would be present, but there would be nothing to one side @@ -3777,6 +3785,9 @@ void tst_QUrl::setComponents_data() QTest::newRow("invalid-path-2") << QUrl("http://example.com") << int(Path) << "relative" << Strict << false << PrettyDecoded << "relative" << ""; + QTest::newRow("invalid-path-3") << QUrl("trash:/") + << int(Path) << "//path" << Tolerant << false + << PrettyDecoded << "//path" << ""; // -- test bad percent encoding -- // unnecessary to test the scheme, since percent-decoding is not performed in it; |