Change the component formatting enum values so the default is zero

By having the default value equal to zero, we follow the principle of
least surprise. For example, if we had
      url.path()
and we refactored to
      url.path(QUrl::DecodeSpaces)

Then instead of ensuring spaces are decoded, we make spaces the only
thing encoded (unicode, delimiters and reserved characters are
encoded).

Besides, modifying the default can only be used to encode something
that wasn't encoded previously, so having the enums as Encode makes
more sense.

As a side-effect, toEncoded() does not support any extra encoding
options.

Change-Id: I2624ec446e65c2d979e9ca2f81bd3db22b00bb13
Reviewed-by: Shane Kearns <shane.kearns@accenture.com>
This commit is contained in:
Thiago Macieira 2012-03-30 17:48:42 -03:00 committed by Qt by Nokia
parent 997ac954ab
commit 1b7e9dba75
8 changed files with 111 additions and 76 deletions

View File

@ -467,16 +467,16 @@ static inline void appendToUser(QString &appendTo, const QString &value, QUrl::F
} }
const ushort *actions = 0; const ushort *actions = 0;
if (options & QUrl::DecodeDelimiters) if (options & QUrl::EncodeDelimiters)
actions = decodedActions;
else
actions = encodedActions; actions = encodedActions;
else
actions = decodedActions;
if (!qt_urlRecode(appendTo, value.constData(), value.constEnd(), options, actions)) if (!qt_urlRecode(appendTo, value.constData(), value.constEnd(), options, actions))
appendTo += value; appendTo += value;
} }
void QUrlPrivate::appendAuthority(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const inline void QUrlPrivate::appendAuthority(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const
{ {
if ((options & QUrl::RemoveUserInfo) != QUrl::RemoveUserInfo) { if ((options & QUrl::RemoveUserInfo) != QUrl::RemoveUserInfo) {
appendUserInfo(appendTo, options, appendingTo); appendUserInfo(appendTo, options, appendingTo);
@ -488,14 +488,17 @@ void QUrlPrivate::appendAuthority(QString &appendTo, QUrl::FormattingOptions opt
appendTo += QLatin1Char(':') + QString::number(port); appendTo += QLatin1Char(':') + QString::number(port);
} }
void QUrlPrivate::appendUserInfo(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const inline void QUrlPrivate::appendUserInfo(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const
{ {
if (Q_LIKELY(userName.isEmpty() && password.isEmpty())) if (Q_LIKELY(userName.isEmpty() && password.isEmpty()))
return; return;
const ushort *userNameActions; const ushort *userNameActions;
const ushort *passwordActions; const ushort *passwordActions;
if (options & QUrl::DecodeDelimiters) { if (options & QUrl::EncodeDelimiters) {
userNameActions = encodedUserNameActions;
passwordActions = encodedPasswordActions;
} else {
switch (appendingTo) { switch (appendingTo) {
case UserInfo: case UserInfo:
userNameActions = decodedUserNameInUserInfoActions; userNameActions = decodedUserNameInUserInfoActions;
@ -513,11 +516,11 @@ void QUrlPrivate::appendUserInfo(QString &appendTo, QUrl::FormattingOptions opti
passwordActions = decodedPasswordInUrlActions; passwordActions = decodedPasswordInUrlActions;
break; break;
} }
} else {
userNameActions = encodedUserNameActions;
passwordActions = encodedPasswordActions;
} }
if ((options & QUrl::EncodeReserved) == 0)
options |= QUrl::DecodeReserved;
if (!qt_urlRecode(appendTo, userName.constData(), userName.constEnd(), options, userNameActions)) if (!qt_urlRecode(appendTo, userName.constData(), userName.constEnd(), options, userNameActions))
appendTo += userName; appendTo += userName;
if (options & QUrl::RemovePassword || !hasPassword()) { if (options & QUrl::RemovePassword || !hasPassword()) {
@ -541,7 +544,7 @@ inline void QUrlPrivate::appendPassword(QString &appendTo, QUrl::FormattingOptio
inline void QUrlPrivate::appendPath(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const inline void QUrlPrivate::appendPath(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const
{ {
if (appendingTo != Path && options & QUrl::DecodeDelimiters) { if (appendingTo != Path && !(options & QUrl::EncodeDelimiters)) {
if (!qt_urlRecode(appendTo, path.constData(), path.constEnd(), options, decodedPathInUrlActions)) if (!qt_urlRecode(appendTo, path.constData(), path.constEnd(), options, decodedPathInUrlActions))
appendTo += path; appendTo += path;
@ -565,12 +568,12 @@ inline void QUrlPrivate::appendQuery(QString &appendTo, QUrl::FormattingOptions
} }
const ushort *actions = 0; const ushort *actions = 0;
if (options & QUrl::DecodeDelimiters) { if (options & QUrl::EncodeDelimiters) {
// reset to default qt_urlRecode behaviour (leave delimiters alone)
options &= ~QUrl::DecodeDelimiters;
actions = appendingTo == Query ? decodedQueryInIsolationActions : decodedQueryInUrlActions;
} else if ((options & QUrl::DecodeDelimiters) == 0) {
actions = encodedQueryActions; actions = encodedQueryActions;
} else {
// reset to default qt_urlRecode behaviour (leave delimiters alone)
options |= QUrl::EncodeDelimiters;
actions = appendingTo == Query ? decodedQueryInIsolationActions : decodedQueryInUrlActions;
} }
if (!qt_urlRecode(appendTo, query.constData(), query.constData() + query.length(), if (!qt_urlRecode(appendTo, query.constData(), query.constData() + query.length(),
@ -764,7 +767,9 @@ inline void QUrlPrivate::setQuery(const QString &value, int from, int iend)
QString output; QString output;
const QChar *begin = value.constData() + from; const QChar *begin = value.constData() + from;
const QChar *end = value.constData() + iend; const QChar *end = value.constData() + iend;
if (qt_urlRecode(output, begin, end, QUrl::DecodeUnicode | QUrl::DecodeSpaces,
// leave delimiters alone but decode the rest
if (qt_urlRecode(output, begin, end, QUrl::EncodeDelimiters,
decodedQueryInIsolationActions)) decodedQueryInIsolationActions))
query = output; query = output;
else else
@ -804,7 +809,7 @@ inline void QUrlPrivate::setQuery(const QString &value, int from, int iend)
inline void QUrlPrivate::appendHost(QString &appendTo, QUrl::FormattingOptions options) const inline void QUrlPrivate::appendHost(QString &appendTo, QUrl::FormattingOptions options) const
{ {
// this is the only flag that matters // this is the only flag that matters
options &= QUrl::DecodeUnicode; options &= QUrl::EncodeUnicode;
if (host.isEmpty()) if (host.isEmpty())
return; return;
if (host.at(0).unicode() == '[') { if (host.at(0).unicode() == '[') {
@ -813,10 +818,10 @@ inline void QUrlPrivate::appendHost(QString &appendTo, QUrl::FormattingOptions o
} else { } else {
// this is either an IPv4Address or a reg-name // this is either an IPv4Address or a reg-name
// if it is a reg-name, it is already stored in Unicode form // if it is a reg-name, it is already stored in Unicode form
if (options == QUrl::DecodeUnicode) if (options == QUrl::EncodeUnicode)
appendTo += host;
else
appendTo += qt_ACE_do(host, ToAceOnly); appendTo += qt_ACE_do(host, ToAceOnly);
else
appendTo += host;
} }
} }
@ -1915,7 +1920,7 @@ bool QUrl::hasFragment() const
QString QUrl::topLevelDomain(ComponentFormattingOptions options) const QString QUrl::topLevelDomain(ComponentFormattingOptions options) const
{ {
QString tld = qTopLevelDomain(host()); QString tld = qTopLevelDomain(host());
if ((options & DecodeUnicode) == 0) { if (options & EncodeUnicode) {
return qt_ACE_do(tld, ToAceOnly); return qt_ACE_do(tld, ToAceOnly);
} }
return tld; return tld;
@ -2055,8 +2060,12 @@ QString QUrl::toString(FormattingOptions options) const
} }
QString url; QString url;
if (!options.testFlag(DecodeReserved))
options &= ~DecodeReserved; // for the full URL, we consider that the reserved characters are prettier if encoded
if (options & DecodeReserved)
options &= ~EncodeReserved;
else
options |= EncodeReserved;
if (!(options & QUrl::RemoveScheme) && d->hasScheme()) if (!(options & QUrl::RemoveScheme) && d->hasScheme())
url += d->scheme + QLatin1Char(':'); url += d->scheme + QLatin1Char(':');
@ -2123,9 +2132,8 @@ QString QUrl::toDisplayString(FormattingOptions options) const
*/ */
QByteArray QUrl::toEncoded(FormattingOptions options) const QByteArray QUrl::toEncoded(FormattingOptions options) const
{ {
QString stringForm = toString(options); options &= ~DecodeReserved;
if (options & DecodeUnicode) QString stringForm = toString(options | FullyEncoded);
return stringForm.toUtf8();
return stringForm.toLatin1(); return stringForm.toLatin1();
} }

View File

@ -138,15 +138,15 @@ public:
}; };
enum ComponentFormattingOption { enum ComponentFormattingOption {
FullyEncoded = 0x000000, PrettyDecoded = 0x000000,
DecodeSpaces = 0x100000, EncodeSpaces = 0x100000,
DecodeUnicode = 0x200000, EncodeUnicode = 0x200000,
DecodeDelimiters = 0x400000 | 0x800000, EncodeDelimiters = 0x400000 | 0x800000,
PrettyDecodeReserved = 0x1000000, EncodeReserved = 0x1000000,
DecodeReserved = PrettyDecodeReserved | 0x2000000, DecodeReserved = 0x2000000,
PrettyDecoded = DecodeSpaces | DecodeDelimiters | PrettyDecodeReserved | DecodeUnicode, FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved,
MostDecoded = PrettyDecoded MostDecoded = PrettyDecoded | DecodeReserved
}; };
Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption) Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption)
#ifdef qdoc #ifdef qdoc
@ -331,8 +331,6 @@ inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2)
{ return QIncompatibleFlag(int(f1) | f2); } { return QIncompatibleFlag(int(f1) | f2); }
// add operators for OR'ing the two types of flags // add operators for OR'ing the two types of flags
inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOption f)
{ i |= QUrl::UrlFormattingOption(int(f)); return i; }
inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f) inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
{ i |= QUrl::UrlFormattingOption(int(f)); return i; } { i |= QUrl::UrlFormattingOption(int(f)); return i; }
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f) Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f)

View File

@ -226,7 +226,7 @@ inline QString QUrlQueryPrivate::recodeToUser(const QString &input, QUrl::Compon
if (idempotentRecodeToUser(encoding)) if (idempotentRecodeToUser(encoding))
return input; return input;
if (encoding & QUrl::DecodeDelimiters) { if (!(encoding & QUrl::EncodeDelimiters)) {
QString output; QString output;
if (qt_urlRecode(output, input.constData(), input.constData() + input.length(), if (qt_urlRecode(output, input.constData(), input.constData() + input.length(),
encoding, prettyDecodedActions)) encoding, prettyDecodedActions))
@ -466,10 +466,7 @@ QString QUrlQuery::query(QUrl::ComponentFormattingOptions encoding) const
decode('#'), // 3 decode('#'), // 3
0 0
}; };
if (encoding & QUrl::DecodeDelimiters) { if (encoding & QUrl::EncodeDelimiters) {
// full decoding: we only encode the characters above
tableActions[3] = 0;
} else {
tableActions[3] = encode('#'); tableActions[3] = encode('#');
} }

View File

@ -512,7 +512,7 @@ non_trivial:
if (decoded >= 0x80) { if (decoded >= 0x80) {
// decode the UTF-8 sequence // decode the UTF-8 sequence
if (encoding & QUrl::DecodeUnicode && if (!(encoding & QUrl::EncodeUnicode) &&
encodedUtf8ToUtf16(result, output, begin, input, end, decoded)) encodedUtf8ToUtf16(result, output, begin, input, end, decoded))
continue; continue;
@ -523,7 +523,7 @@ non_trivial:
} }
} else { } else {
decoded = c; decoded = c;
if (decoded >= 0x80 && (encoding & QUrl::DecodeUnicode) == 0) { if (decoded >= 0x80 && encoding & QUrl::EncodeUnicode) {
// encode the UTF-8 sequence // encode the UTF-8 sequence
unicodeToEncodedUtf8(result, output, begin, input, end, decoded); unicodeToEncodedUtf8(result, output, begin, input, end, decoded);
continue; continue;
@ -581,15 +581,42 @@ static void maskTable(uchar (&table)[N], const uchar (&mask)[N])
table[i] &= mask[i]; table[i] &= mask[i];
} }
/*!
\internal
Recodes the string from \a begin to \a end. If any transformations are
done, append them to \a appendTo and return the number of characters added.
If no transformations were required, return 0.
The \a encoding option modifies the default behaviour:
\list
\li QUrl::EncodeDelimiters: if set, delimiters will be left untransformed (note: not encoded!);
if unset, delimiters will be decoded
\li QUrl::DecodeReserved: if set, reserved characters will be decoded;
if unset, reserved characters will be encoded
\li QUrl::EncodeSpaces: if set, spaces will be encoded to "%20"; if unset, they will be " "
\li QUrl::EncodeUnicode: if set, characters above U+0080 will be encoded to their UTF-8
percent-encoded form; if unset, they will be decoded to UTF-16
\endlist
Other flags are ignored (including QUrl::EncodeReserved).
The \a tableModifications argument can be used to supply extra
modifications to the tables, to be applied after the flags above are
handled. It consists of a sequence of 16-bit values, where the low 8 bits
indicate the character in question and the high 8 bits are either \c
EncodeCharacter, \c LeaveCharacter or \c DecodeCharacter.
*/
Q_AUTOTEST_EXPORT int Q_AUTOTEST_EXPORT int
qt_urlRecode(QString &appendTo, const QChar *begin, const QChar *end, qt_urlRecode(QString &appendTo, const QChar *begin, const QChar *end,
QUrl::ComponentFormattingOptions encoding, const ushort *tableModifications) QUrl::ComponentFormattingOptions encoding, const ushort *tableModifications)
{ {
uchar actionTable[sizeof defaultActionTable]; uchar actionTable[sizeof defaultActionTable];
if (encoding & QUrl::DecodeDelimiters && encoding & QUrl::DecodeReserved) { if (!(encoding & QUrl::EncodeDelimiters) && encoding & QUrl::DecodeReserved) {
// reset the table // reset the table
memset(actionTable, DecodeCharacter, sizeof actionTable); memset(actionTable, DecodeCharacter, sizeof actionTable);
if (!(encoding & QUrl::DecodeSpaces)) if (encoding & QUrl::EncodeSpaces)
actionTable[0] = EncodeCharacter; actionTable[0] = EncodeCharacter;
// these are always encoded // these are always encoded
@ -597,11 +624,11 @@ qt_urlRecode(QString &appendTo, const QChar *begin, const QChar *end,
actionTable[0x7F - ' '] = EncodeCharacter; actionTable[0x7F - ' '] = EncodeCharacter;
} else { } else {
memcpy(actionTable, defaultActionTable, sizeof actionTable); memcpy(actionTable, defaultActionTable, sizeof actionTable);
if (encoding & QUrl::DecodeDelimiters) if (!(encoding & QUrl::EncodeDelimiters))
maskTable(actionTable, delimsMask); maskTable(actionTable, delimsMask);
if (encoding & QUrl::DecodeReserved) if (encoding & QUrl::DecodeReserved)
maskTable(actionTable, reservedMask); maskTable(actionTable, reservedMask);
if (encoding & QUrl::DecodeSpaces) if (!(encoding & QUrl::EncodeSpaces))
actionTable[0] = DecodeCharacter; // decode actionTable[0] = DecodeCharacter; // decode
} }

View File

@ -142,7 +142,7 @@ template<> inline char *toString(const QRectF &s)
template<> inline char *toString(const QUrl &uri) template<> inline char *toString(const QUrl &uri)
{ {
return qstrdup(uri.toEncoded(QUrl::DecodeDelimiters).constData()); return qstrdup(uri.toEncoded().constData());
} }
template<> inline char *toString(const QVariant &v) template<> inline char *toString(const QVariant &v)

View File

@ -2599,14 +2599,14 @@ void tst_QUrl::componentEncodings_data()
// hostname cannot contain spaces // hostname cannot contain spaces
QTest::newRow("encoded-space") << QUrl("x://user name:pass word@host/path name?query value#fragment value") QTest::newRow("encoded-space") << QUrl("x://user name:pass word@host/path name?query value#fragment value")
<< int(QUrl::FullyEncoded) << int(QUrl::EncodeSpaces)
<< "user%20name" << "pass%20word" << "user%20name:pass%20word" << "user%20name" << "pass%20word" << "user%20name:pass%20word"
<< "host" << "user%20name:pass%20word@host" << "host" << "user%20name:pass%20word@host"
<< "/path%20name" << "query%20value" << "fragment%20value" << "/path%20name" << "query%20value" << "fragment%20value"
<< "x://user%20name:pass%20word@host/path%20name?query%20value#fragment%20value"; << "x://user%20name:pass%20word@host/path%20name?query%20value#fragment%20value";
QTest::newRow("decoded-space") << QUrl("x://user%20name:pass%20word@host/path%20name?query%20value#fragment%20value") QTest::newRow("decoded-space") << QUrl("x://user%20name:pass%20word@host/path%20name?query%20value#fragment%20value")
<< int(QUrl::DecodeSpaces) << int(QUrl::MostDecoded)
<< "user name" << "pass word" << "user name:pass word" << "user name" << "pass word" << "user name:pass word"
<< "host" << "user name:pass word@host" << "host" << "user name:pass word@host"
<< "/path name" << "query value" << "fragment value" << "/path name" << "query value" << "fragment value"
@ -2624,13 +2624,13 @@ void tst_QUrl::componentEncodings_data()
// unicode tests // unicode tests
// hostnames can participate in this test, but we need a top-level domain that accepts Unicode // hostnames can participate in this test, but we need a top-level domain that accepts Unicode
QTest::newRow("encoded-unicode") << QUrl(QString::fromUtf8("x://\xc2\x80:\xc3\x90@smørbrød.example.no/\xe0\xa0\x80?\xf0\x90\x80\x80")) QTest::newRow("encoded-unicode") << QUrl(QString::fromUtf8("x://\xc2\x80:\xc3\x90@smørbrød.example.no/\xe0\xa0\x80?\xf0\x90\x80\x80"))
<< int(QUrl::FullyEncoded) << int(QUrl::EncodeUnicode)
<< "%C2%80" << "%C3%90" << "%C2%80:%C3%90" << "%C2%80" << "%C3%90" << "%C2%80:%C3%90"
<< "xn--smrbrd-cyad.example.no" << "%C2%80:%C3%90@xn--smrbrd-cyad.example.no" << "xn--smrbrd-cyad.example.no" << "%C2%80:%C3%90@xn--smrbrd-cyad.example.no"
<< "/%E0%A0%80" << "%F0%90%80%80" << "%C3%A9" << "/%E0%A0%80" << "%F0%90%80%80" << "%C3%A9"
<< "x://%C2%80:%C3%90@xn--smrbrd-cyad.example.no/%E0%A0%80?%F0%90%80%80#%C3%A9"; << "x://%C2%80:%C3%90@xn--smrbrd-cyad.example.no/%E0%A0%80?%F0%90%80%80#%C3%A9";
QTest::newRow("decoded-unicode") << QUrl("x://%C2%80:%C3%90@XN--SMRBRD-cyad.example.NO/%E0%A0%80?%F0%90%80%80#%C3%A9") QTest::newRow("decoded-unicode") << QUrl("x://%C2%80:%C3%90@XN--SMRBRD-cyad.example.NO/%E0%A0%80?%F0%90%80%80#%C3%A9")
<< int(QUrl::DecodeUnicode) << int(QUrl::MostDecoded)
<< QString::fromUtf8("\xc2\x80") << QString::fromUtf8("\xc3\x90") << QString::fromUtf8("\xc2\x80") << QString::fromUtf8("\xc3\x90")
<< QString::fromUtf8("\xc2\x80:\xc3\x90") << QString::fromUtf8("\xc2\x80:\xc3\x90")
<< QString::fromUtf8("smørbrød.example.no") << QString::fromUtf8("smørbrød.example.no")
@ -2661,8 +2661,10 @@ void tst_QUrl::componentEncodings_data()
// gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" // gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
// these are the separators between fields // these are the separators between fields
// they must appear encoded in proper URLs everywhere // they must appear encoded in certain positions, no exceptions
// 1) test the delimiters that, if they were decoded, would change the URL parsing // in other positions, they can appear decoded, so they always do
// 1) test the delimiters that must appear encoded
// (if they were decoded, they'd would change the URL parsing)
QTest::newRow("encoded-gendelims-changing") << QUrl("x://%5b%3a%2f%3f%23%40%5d:%5b%2f%3f%23%40%5d@host/%2f%3f%23?%23") QTest::newRow("encoded-gendelims-changing") << QUrl("x://%5b%3a%2f%3f%23%40%5d:%5b%2f%3f%23%40%5d@host/%2f%3f%23?%23")
<< int(QUrl::MostDecoded) << int(QUrl::MostDecoded)
<< "[:/?#@]" << "[/?#@]" << "[%3A/?#@]:[/?#@]" << "[:/?#@]" << "[/?#@]" << "[%3A/?#@]:[/?#@]"
@ -2674,7 +2676,7 @@ void tst_QUrl::componentEncodings_data()
// and test that %2f is *not* decoded to a slash in the path // and test that %2f is *not* decoded to a slash in the path
// don't test the query because in this mode it doesn't transform anything // don't test the query because in this mode it doesn't transform anything
QTest::newRow("decoded-gendelims-unchanging") << QUrl("x://:%3a@host/%2f%3a%40#%23%3a%2f%3f%40") QTest::newRow("decoded-gendelims-unchanging") << QUrl("x://:%3a@host/%2f%3a%40#%23%3a%2f%3f%40")
<< int(QUrl::DecodeDelimiters) << int(QUrl::FullyEncoded)
<< "" << ":" << "::" << "" << ":" << "::"
<< "host" << "::@host" << "host" << "::@host"
<< "/%2F:@" << "" << "#:/?@" << "/%2F:@" << "" << "#:/?@"
@ -2707,7 +2709,7 @@ void tst_QUrl::componentEncodings_data()
<< QString() << "!$()*+,;=:/?[]@" << QString() << QString() << "!$()*+,;=:/?[]@" << QString()
<< "?!$()*+,;=:/?[]@"; << "?!$()*+,;=:/?[]@";
QTest::newRow("undecoded-delims-query") << QUrl("?%21%24%26%27%28%29%2a%2b%2c%2f%3a%3b%3d%3f%40%5b%5d") QTest::newRow("undecoded-delims-query") << QUrl("?%21%24%26%27%28%29%2a%2b%2c%2f%3a%3b%3d%3f%40%5b%5d")
<< int(QUrl::DecodeDelimiters) << int(QUrl::MostDecoded)
<< QString() << QString() << QString() << QString() << QString() << QString()
<< QString() << QString() << QString() << QString()
<< QString() << "%21%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D" << QString() << QString() << "%21%24%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D" << QString()

View File

@ -812,7 +812,7 @@ void tst_QUrlInternal::correctEncodedMistakes()
QString output = QTest::currentDataTag(); QString output = QTest::currentDataTag();
expected.prepend(output); expected.prepend(output);
if (!qt_urlRecode(output, input.constData(), input.constData() + input.length(), QUrl::DecodeUnicode)) if (!qt_urlRecode(output, input.constData(), input.constData() + input.length(), 0))
output += input; output += input;
QCOMPARE(output, expected); QCOMPARE(output, expected);
} }
@ -822,7 +822,7 @@ static void addUtf8Data(const char *name, const char *data)
QString encoded = QByteArray(data).toPercentEncoding(); QString encoded = QByteArray(data).toPercentEncoding();
QString decoded = QString::fromUtf8(data); QString decoded = QString::fromUtf8(data);
QTest::newRow(QByteArray("decode-") + name) << encoded << QUrl::ComponentFormattingOptions(QUrl::DecodeUnicode) << decoded; QTest::newRow(QByteArray("decode-") + name) << encoded << QUrl::ComponentFormattingOptions(QUrl::MostDecoded) << decoded;
QTest::newRow(QByteArray("encode-") + name) << decoded << QUrl::ComponentFormattingOptions(QUrl::FullyEncoded) << encoded; QTest::newRow(QByteArray("encode-") + name) << decoded << QUrl::ComponentFormattingOptions(QUrl::FullyEncoded) << encoded;
} }
@ -877,17 +877,17 @@ void tst_QUrlInternal::encodingRecode_data()
QTest::newRow("encode-nul") << QString::fromLatin1("abc\0def", 7) << F(QUrl::MostDecoded) << "abc%00def"; QTest::newRow("encode-nul") << QString::fromLatin1("abc\0def", 7) << F(QUrl::MostDecoded) << "abc%00def";
// space // space
QTest::newRow("space-leave-decoded") << "Hello World " << F(QUrl::DecodeSpaces) << "Hello World "; QTest::newRow("space-leave-decoded") << "Hello World " << F(QUrl::MostDecoded) << "Hello World ";
QTest::newRow("space-leave-encoded") << "Hello%20World%20" << F(QUrl::FullyEncoded) << "Hello%20World%20"; QTest::newRow("space-leave-encoded") << "Hello%20World%20" << F(QUrl::FullyEncoded) << "Hello%20World%20";
QTest::newRow("space-encode") << "Hello World " << F(QUrl::FullyEncoded) << "Hello%20World%20"; QTest::newRow("space-encode") << "Hello World " << F(QUrl::FullyEncoded) << "Hello%20World%20";
QTest::newRow("space-decode") << "Hello%20World%20" << F(QUrl::DecodeSpaces) << "Hello World "; QTest::newRow("space-decode") << "Hello%20World%20" << F(QUrl::MostDecoded) << "Hello World ";
// decode unreserved // decode unreserved
QTest::newRow("unreserved-decode") << "%66%6f%6f%42a%72" << F(QUrl::FullyEncoded) << "fooBar"; QTest::newRow("unreserved-decode") << "%66%6f%6f%42a%72" << F(QUrl::FullyEncoded) << "fooBar";
// mix encoding with decoding // mix encoding with decoding
QTest::newRow("encode-control-decode-space") << "\1\2%200" << F(QUrl::DecodeSpaces) << "%01%02 0"; QTest::newRow("encode-control-decode-space") << "\1\2%200" << F(QUrl::MostDecoded) << "%01%02 0";
QTest::newRow("decode-space-encode-control") << "%20\1\2" << F(QUrl::DecodeSpaces) << " %01%02"; QTest::newRow("decode-space-encode-control") << "%20\1\2" << F(QUrl::MostDecoded) << " %01%02";
// decode and encode valid UTF-8 data // decode and encode valid UTF-8 data
// invalid is tested in encodingRecodeInvalidUtf8 // invalid is tested in encodingRecodeInvalidUtf8
@ -922,11 +922,11 @@ void tst_QUrlInternal::encodingRecode_data()
QTest::newRow("ff") << "%ff" << F(QUrl::FullyEncoded) << "%FF"; QTest::newRow("ff") << "%ff" << F(QUrl::FullyEncoded) << "%FF";
// decode UTF-8 mixed with non-UTF-8 and unreserved // decode UTF-8 mixed with non-UTF-8 and unreserved
QTest::newRow("utf8-mix-1") << "%80%C2%80" << F(QUrl::DecodeUnicode) << QString::fromUtf8("%80\xC2\x80"); QTest::newRow("utf8-mix-1") << "%80%C2%80" << F(QUrl::MostDecoded) << QString::fromUtf8("%80\xC2\x80");
QTest::newRow("utf8-mix-2") << "%C2%C2%80" << F(QUrl::DecodeUnicode) << QString::fromUtf8("%C2\xC2\x80"); QTest::newRow("utf8-mix-2") << "%C2%C2%80" << F(QUrl::MostDecoded) << QString::fromUtf8("%C2\xC2\x80");
QTest::newRow("utf8-mix-3") << "%E0%C2%80" << F(QUrl::DecodeUnicode) << QString::fromUtf8("%E0\xC2\x80"); QTest::newRow("utf8-mix-3") << "%E0%C2%80" << F(QUrl::MostDecoded) << QString::fromUtf8("%E0\xC2\x80");
QTest::newRow("utf8-mix-3") << "A%C2%80" << F(QUrl::DecodeUnicode) << QString::fromUtf8("A\xC2\x80"); QTest::newRow("utf8-mix-3") << "A%C2%80" << F(QUrl::MostDecoded) << QString::fromUtf8("A\xC2\x80");
QTest::newRow("utf8-mix-3") << "%C2%80A" << F(QUrl::DecodeUnicode) << QString::fromUtf8("\xC2\x80""A"); QTest::newRow("utf8-mix-3") << "%C2%80A" << F(QUrl::MostDecoded) << QString::fromUtf8("\xC2\x80""A");
} }
void tst_QUrlInternal::encodingRecode() void tst_QUrlInternal::encodingRecode()
@ -955,9 +955,9 @@ void tst_QUrlInternal::encodingRecodeInvalidUtf8_data()
extern void loadNonCharactersRows(); extern void loadNonCharactersRows();
loadNonCharactersRows(); loadNonCharactersRows();
QTest::newRow("utf8-mix-4") << QByteArray("\xE0!A2\x80"); QTest::newRow("utf8-mix-4") << QByteArray("\xE0.A2\x80");
QTest::newRow("utf8-mix-5") << QByteArray("\xE0\xA2!80"); QTest::newRow("utf8-mix-5") << QByteArray("\xE0\xA2.80");
QTest::newRow("utf8-mix-5") << QByteArray("\xE0\xA2\x33"); QTest::newRow("utf8-mix-6") << QByteArray("\xE0\xA2\x33");
} }
void tst_QUrlInternal::encodingRecodeInvalidUtf8() void tst_QUrlInternal::encodingRecodeInvalidUtf8()
@ -968,7 +968,7 @@ void tst_QUrlInternal::encodingRecodeInvalidUtf8()
// prepend some data to be sure that it remains there // prepend some data to be sure that it remains there
QString output = QTest::currentDataTag(); QString output = QTest::currentDataTag();
if (!qt_urlRecode(output, input.constData(), input.constData() + input.length(), QUrl::DecodeUnicode)) if (!qt_urlRecode(output, input.constData(), input.constData() + input.length(), QUrl::MostDecoded))
output += input; output += input;
QCOMPARE(output, QTest::currentDataTag() + input); QCOMPARE(output, QTest::currentDataTag() + input);

View File

@ -593,10 +593,13 @@ void tst_QUrlQuery::encodedSetQueryItems_data()
QTest::newRow("encode-space") << " = " << " " << " " << F(QUrl::FullyEncoded) QTest::newRow("encode-space") << " = " << " " << " " << F(QUrl::FullyEncoded)
<< "%20=%20" << "%20" << "%20"; << "%20=%20" << "%20" << "%20";
QTest::newRow("non-delimiters") << "%3C%5C%3E=%7B%7C%7D%5E%60" << "%3C%5C%3E" << "%7B%7C%7D%5E%60" << F(QUrl::PrettyDecoded) // tri-state
QTest::newRow("decode-non-delimiters") << "%3C%5C%3E=%7B%7C%7D%5E%60" << "%3C%5C%3E" << "%7B%7C%7D%5E%60" << F(QUrl::DecodeReserved)
<< "<\\>={|}^`" << "<\\>" << "{|}^`"; << "<\\>={|}^`" << "<\\>" << "{|}^`";
QTest::newRow("encode-non-delimiters") << "<\\>={|}^`" << "<\\>" << "{|}^`" << F(QUrl::FullyEncoded) QTest::newRow("encode-non-delimiters") << "<\\>={|}^`" << "<\\>" << "{|}^`" << F(QUrl::EncodeReserved)
<< "%3C%5C%3E=%7B%7C%7D%5E%60" << "%3C%5C%3E" << "%7B%7C%7D%5E%60"; << "%3C%5C%3E=%7B%7C%7D%5E%60" << "%3C%5C%3E" << "%7B%7C%7D%5E%60";
QTest::newRow("pretty-non-delimiters") << "<\\>={|}^`" << "<\\>" << "{|}^`" << F(QUrl::PrettyDecoded)
<< "%3C%5C%3E=%7B%7C%7D%5E%60" << "<\\>" << "{|}^`";
QTest::newRow("equals") << "%3D=%3D" << "%3D" << "%3D" << F(QUrl::PrettyDecoded) QTest::newRow("equals") << "%3D=%3D" << "%3D" << "%3D" << F(QUrl::PrettyDecoded)
<< "%3D=%3D" << "=" << "="; << "%3D=%3D" << "=" << "=";
@ -606,7 +609,7 @@ void tst_QUrlQuery::encodedSetQueryItems_data()
<< "%26=%26" << "&" << "&"; << "%26=%26" << "&" << "&";
QTest::newRow("hash") << "#=#" << "%23" << "%23" << F(QUrl::PrettyDecoded) QTest::newRow("hash") << "#=#" << "%23" << "%23" << F(QUrl::PrettyDecoded)
<< "#=#" << "#" << "#"; << "#=#" << "#" << "#";
QTest::newRow("decode-hash") << "%23=%23" << "%23" << "%23" << F(QUrl::DecodeDelimiters) QTest::newRow("decode-hash") << "%23=%23" << "%23" << "%23" << F(QUrl::PrettyDecoded)
<< "#=#" << "#" << "#"; << "#=#" << "#" << "#";
QTest::newRow("percent") << "%25=%25" << "%25" << "%25" << F(QUrl::PrettyDecoded) QTest::newRow("percent") << "%25=%25" << "%25" << "%25" << F(QUrl::PrettyDecoded)
@ -623,7 +626,7 @@ void tst_QUrlQuery::encodedSetQueryItems_data()
// plus signs must not be touched // plus signs must not be touched
QTest::newRow("encode-plus") << "+=+" << "+" << "+" << F(QUrl::FullyEncoded) QTest::newRow("encode-plus") << "+=+" << "+" << "+" << F(QUrl::FullyEncoded)
<< "+=+" << "+" << "+"; << "+=+" << "+" << "+";
QTest::newRow("decode-2b") << "%2b=%2b" << "%2b" << "%2b" << F(QUrl::DecodeDelimiters) QTest::newRow("decode-2b") << "%2b=%2b" << "%2b" << "%2b" << F(QUrl::MostDecoded)
<< "%2B=%2B" << "%2B" << "%2B"; << "%2B=%2B" << "%2B" << "%2B";
@ -680,7 +683,7 @@ void tst_QUrlQuery::differentDelimiters()
expected << qItem("foo", "bar") << qItem("hello", "world"); expected << qItem("foo", "bar") << qItem("hello", "world");
COMPARE_ITEMS(query.queryItems(), expected); COMPARE_ITEMS(query.queryItems(), expected);
COMPARE_ITEMS(query.queryItems(QUrl::FullyEncoded), expected); COMPARE_ITEMS(query.queryItems(QUrl::FullyEncoded), expected);
COMPARE_ITEMS(query.queryItems(QUrl::DecodeDelimiters), expected); COMPARE_ITEMS(query.queryItems(QUrl::MostDecoded), expected);
} }
{ {