QUrl: Use decoded mode by default for individual getters/setters.

This fixes the wrong value for path() and fileName() when a
path or file name actually contains a '%'.

userInfo() and authority() are not individual getters, they combine
two or more fields, so full decoding isn't possible (e.g. username
containing a ':').

[ChangeLog][Important Behavior Changes][QUrl and QUrlQuery]QUrl now
defaults to decoded mode in the getters and setters for userName,
password, host, topLevelDomain, path and fileName. This means a '%'
in one of those fields is now returned (or set) as '%' rather than "%25".
In the unlikely case where the former behavior was expected, pass PrettyDecoded
to the getter and TolerantMode to the setter.

Change-Id: Iaeecbde9c269882e79f08b29ff8c661157c41743
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2013-07-25 12:52:27 +02:00 committed by The Qt Project
parent f874a5dd8b
commit 1de1470189
3 changed files with 32 additions and 27 deletions

View File

@ -3252,8 +3252,8 @@ QUrl QUrl::adjusted(QUrl::FormattingOptions options) const
that.setPath(QString());
} else if (options & (StripTrailingSlash | RemoveFilename | NormalizePathSegments)) {
QString path;
d->appendPath(path, options, QUrlPrivate::Path);
that.setPath(path);
d->appendPath(path, options | FullyEncoded, QUrlPrivate::Path);
that.setPath(path, TolerantMode);
}
return that;
}
@ -3967,9 +3967,9 @@ uint qHash(const QUrl &url, uint seed) Q_DECL_NOTHROW
static QUrl adjustFtpPath(QUrl url)
{
if (url.scheme() == ftpScheme()) {
QString path = url.path();
QString path = url.path(QUrl::PrettyDecoded);
if (path.startsWith(QLatin1String("//")))
url.setPath(QLatin1String("/%2F") + path.midRef(2));
url.setPath(QLatin1String("/%2F") + path.midRef(2), QUrl::TolerantMode);
}
return url;
}

View File

@ -209,22 +209,22 @@ public:
void setUserInfo(const QString &userInfo, ParsingMode mode = TolerantMode);
QString userInfo(ComponentFormattingOptions options = PrettyDecoded) const;
void setUserName(const QString &userName, ParsingMode mode = TolerantMode);
QString userName(ComponentFormattingOptions options = PrettyDecoded) const;
void setUserName(const QString &userName, ParsingMode mode = DecodedMode);
QString userName(ComponentFormattingOptions options = FullyDecoded) const;
void setPassword(const QString &password, ParsingMode mode = TolerantMode);
QString password(ComponentFormattingOptions = PrettyDecoded) const;
void setPassword(const QString &password, ParsingMode mode = DecodedMode);
QString password(ComponentFormattingOptions = FullyDecoded) const;
void setHost(const QString &host, ParsingMode mode = TolerantMode);
QString host(ComponentFormattingOptions = PrettyDecoded) const;
QString topLevelDomain(ComponentFormattingOptions options = PrettyDecoded) const;
void setHost(const QString &host, ParsingMode mode = DecodedMode);
QString host(ComponentFormattingOptions = FullyDecoded) const;
QString topLevelDomain(ComponentFormattingOptions options = FullyDecoded) const;
void setPort(int port);
int port(int defaultPort = -1) const;
void setPath(const QString &path, ParsingMode mode = TolerantMode);
QString path(ComponentFormattingOptions options = PrettyDecoded) const;
QString fileName(ComponentFormattingOptions options = PrettyDecoded) const;
void setPath(const QString &path, ParsingMode mode = DecodedMode);
QString path(ComponentFormattingOptions options = FullyDecoded) const;
QString fileName(ComponentFormattingOptions options = FullyDecoded) const;
bool hasQuery() const;
void setQuery(const QString &query, ParsingMode mode = TolerantMode);

View File

@ -316,6 +316,8 @@ void tst_QUrl::comparison()
QVERIFY(url3bisNoSlash.matches(url4bis, QUrl::NormalizePathSegments | QUrl::StripTrailingSlash));
QUrl url4EncodedDots = QUrl("example://a/.//b/%2E%2E%2F/b/c/");
QCOMPARE(url4EncodedDots.path(QUrl::PrettyDecoded), QString("/.//b/..%2F/b/c/"));
QCOMPARE(url4EncodedDots.path(QUrl::FullyDecoded), QString("/.//b/..//b/c/"));
QCOMPARE(QString::fromLatin1(url4EncodedDots.toEncoded()), QString::fromLatin1("example://a/.//b/..%2F/b/c/"));
QCOMPARE(url4EncodedDots.toString(), QString("example://a/.//b/..%2F/b/c/"));
QCOMPARE(url4EncodedDots.adjusted(QUrl::NormalizePathSegments).toString(), QString("example://a/b/..%2F/b/c/"));
@ -497,18 +499,20 @@ void tst_QUrl::setUrl()
}
{
QUrl url("http://user:pass@[56::56:56:56:127.0.0.1]:99");
QUrl url("http://user%3A:pass%40@[56::56:56:56:127.0.0.1]:99");
QVERIFY(url.isValid());
QCOMPARE(url.scheme(), QString::fromLatin1("http"));
QCOMPARE(url.path(), QString());
QVERIFY(url.encodedQuery().isEmpty());
QCOMPARE(url.userInfo(), QString::fromLatin1("user:pass"));
QCOMPARE(url.userName(), QString::fromLatin1("user:"));
QCOMPARE(url.password(), QString::fromLatin1("pass@"));
QCOMPARE(url.userInfo(), QString::fromLatin1("user%3A:pass@"));
QVERIFY(url.fragment().isEmpty());
QCOMPARE(url.host(), QString::fromLatin1("56::56:56:56:7f00:1"));
QCOMPARE(url.authority(), QString::fromLatin1("user:pass@[56::56:56:56:7f00:1]:99"));
QCOMPARE(url.authority(), QString::fromLatin1("user%3A:pass%40@[56::56:56:56:7f00:1]:99"));
QCOMPARE(url.port(), 99);
QCOMPARE(url.url(), QString::fromLatin1("http://user:pass@[56::56:56:56:7f00:1]:99"));
QCOMPARE(url.toDisplayString(), QString::fromLatin1("http://user@[56::56:56:56:7f00:1]:99"));
QCOMPARE(url.url(), QString::fromLatin1("http://user%3A:pass%40@[56::56:56:56:7f00:1]:99"));
QCOMPARE(url.toDisplayString(), QString::fromLatin1("http://user%3A@[56::56:56:56:7f00:1]:99"));
}
{
@ -686,8 +690,8 @@ void tst_QUrl::setUrl()
QUrl charles;
charles.setPath("/home/charles/foo%20moo");
QCOMPARE(charles.path(), QString::fromLatin1("/home/charles/foo moo"));
QCOMPARE(charles.path(QUrl::FullyEncoded), QString::fromLatin1("/home/charles/foo%20moo"));
QCOMPARE(charles.path(), QString::fromLatin1("/home/charles/foo%20moo"));
QCOMPARE(charles.path(QUrl::FullyEncoded), QString::fromLatin1("/home/charles/foo%2520moo"));
QUrl charles2("file:/home/charles/foo%20moo");
QCOMPARE(charles2.path(), QString::fromLatin1("/home/charles/foo moo"));
@ -760,7 +764,7 @@ void tst_QUrl::setUrl()
QVERIFY(url.isValid());
QCOMPARE(url.scheme(), QString("data"));
QCOMPARE(url.host(), QString());
QCOMPARE(url.path(), QString("text/javascript,d5 %3D 'five\\u0027s'%3B"));
QCOMPARE(url.path(), QString("text/javascript,d5 = 'five\\u0027s';"));
QCOMPARE(url.encodedPath().constData(), "text/javascript,d5%20%3D%20'five%5Cu0027s'%3B");
}
@ -1214,11 +1218,11 @@ void tst_QUrl::fromLocalFile_data()
QTest::newRow("data7") << QString::fromLatin1("/Mambo <#5>.mp3") << QString::fromLatin1("file:///Mambo <%235>.mp3")
<< QString::fromLatin1("/Mambo <#5>.mp3");
QTest::newRow("data8") << QString::fromLatin1("/a%.txt") << QString::fromLatin1("file:///a%25.txt")
<< QString::fromLatin1("/a%25.txt");
<< QString::fromLatin1("/a%.txt");
QTest::newRow("data9") << QString::fromLatin1("/a%25.txt") << QString::fromLatin1("file:///a%2525.txt")
<< QString::fromLatin1("/a%2525.txt");
<< QString::fromLatin1("/a%25.txt");
QTest::newRow("data10") << QString::fromLatin1("/%80.txt") << QString::fromLatin1("file:///%2580.txt")
<< QString::fromLatin1("/%2580.txt");
<< QString::fromLatin1("/%80.txt");
}
void tst_QUrl::fromLocalFile()
@ -2530,8 +2534,9 @@ void tst_QUrl::setEncodedFragment()
void tst_QUrl::fromEncoded()
{
QUrl qurl2 = QUrl::fromEncoded("print:/specials/Print%20To%20File%20(PDF%252FAcrobat)", QUrl::TolerantMode);
QCOMPARE(qurl2.path(), QString::fromLatin1("/specials/Print To File (PDF%252FAcrobat)"));
QCOMPARE(QFileInfo(qurl2.path()).fileName(), QString::fromLatin1("Print To File (PDF%252FAcrobat)"));
QCOMPARE(qurl2.path(), QString::fromLatin1("/specials/Print To File (PDF%2FAcrobat)"));
QCOMPARE(QFileInfo(qurl2.path()).fileName(), QString::fromLatin1("Print To File (PDF%2FAcrobat)"));
QCOMPARE(qurl2.fileName(), QString::fromLatin1("Print To File (PDF%2FAcrobat)"));
QCOMPARE(qurl2.toEncoded().constData(), "print:/specials/Print%20To%20File%20(PDF%252FAcrobat)");
QUrl qurl = QUrl::fromEncoded("http://\303\244.de");