qDecodeDataUrl(): treat ";base64" marker as case-insensitive

RFC2397 doesn't explicitly mention it, but references RFC2045, which,
in Section 2, states:

> All media type values, subtype values, and parameter names as
> defined are case-insensitive.

and goes on, in 6.1:

>   mechanism := "7bit" / "8bit" / "binary" /
>                  "quoted-printable" / "base64" /
>                  ietf-token / x-token
>
>   These values are not case sensitive

So regardless of whether "base64" is a parameter name, or a mechanism,
we need to treat it case-insensitively.

Use QLatin1String::endsWith() instead of QByteArray::endsWith(),
because the former takes Qt::CaseInsensitive while the latter would
need a toLower().

Add a test.

As a drive-by, use the same trick for the existing case-insensitive
comparison with "charset".

As a further drive-by, fix inappropriate uses of QLatin1String (=
where they don't prevent allocations).

[ChangeLog][QtCore][QUrl] Now recognizes the ";base64" marker in
"data:" URLs case-insensitively.

Pick-to: 6.3 6.2
Change-Id: Ife6ba771553aaad3b7c119c1fa631f41ffa8f590
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2022-03-09 14:21:36 +01:00 committed by Thiago Macieira
parent cfa4879b41
commit d743fd0d0a
2 changed files with 8 additions and 4 deletions

View File

@ -43,6 +43,8 @@
QT_BEGIN_NAMESPACE
using namespace Qt::Literals;
/*!
\internal
@ -54,7 +56,7 @@ Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray
if (uri.scheme() != QLatin1String("data") || !uri.host().isEmpty())
return false;
mimeType = QLatin1String("text/plain;charset=US-ASCII");
mimeType = QStringLiteral("text/plain;charset=US-ASCII");
// the following would have been the correct thing, but
// reality often differs from the specification. People have
@ -70,12 +72,12 @@ Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray
data = data.trimmed();
// find out if the payload is encoded in Base64
if (data.endsWith(";base64")) {
if (QLatin1String{data}.endsWith(";base64"_L1, Qt::CaseInsensitive)) {
payload = QByteArray::fromBase64(payload);
data.chop(7);
}
if (data.toLower().startsWith("charset")) {
if (QLatin1String{data}.startsWith("charset"_L1, Qt::CaseInsensitive)) {
int i = 7; // strlen("charset")
while (data.at(i) == ' ')
++i;
@ -84,7 +86,7 @@ Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray
}
if (!data.isEmpty())
mimeType = QLatin1String(data.trimmed());
mimeType = QString::fromLatin1(data.trimmed());
}

View File

@ -57,6 +57,8 @@ void tst_QDataUrl::decode_data()
"text/plain;charset=US-ASCII"_L1);
row("alreadyPercentageEncoded", "data:text/plain,%E2%88%9A", true,
"text/plain"_L1, QByteArray::fromPercentEncoding("%E2%88%9A"));
row("everythingIsCaseInsensitive", "Data:texT/PlaiN;charSet=iSo-8859-1;Base64,SGVsbG8=", true,
"texT/PlaiN;charSet=iSo-8859-1"_L1, QByteArrayLiteral("Hello"));
}
void tst_QDataUrl::decode()