Commit 9bbbec3b authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Fix parsing mailto url with =?utf-8?...?=

parent b0877c58
......@@ -400,6 +400,20 @@ void StringUtilTest::test_parseMailtoUrlExtra()
}
}
void StringUtilTest::test_parseMailToWithUtf8Encoded()
{
const QByteArray ba("mailto:=?utf-8?B?TWnFgm9zeg==?= Vo %3Craco.cki@foo.com%3E");
QUrl url = QUrl(QUrl::fromPercentEncoding(ba));
QVector<QPair<QString, QString> > data = StringUtil::parseMailtoUrl(url);
QCOMPARE(data.size(), 1);
for (int i = 0; i < 1; ++i) {
if (data.at(i).first == QLatin1String("to")) {
QCOMPARE(data.at(i).second, QString::fromUtf8("Mi\u0142osz Vo <raco.cki@foo.com>"));
}
}
}
void StringUtilTest::test_parseMailToBug366981()
{
const QString ba(QStringLiteral("mailto:test@test.com?subject=test&body=line1%0D%0Aline2"));
......@@ -473,7 +487,7 @@ void StringUtilTest::test_parseMailToBug406208()
QString ba(QStringLiteral(
"mailto:?body=http%3A%2F%2Fwww.lecourrierdelarchitecte.com%2Farticle_8428&subject=Le%20Courrier%20l'effet%20%23metoo%20%3F"));
QUrl urlDecoded(QUrl::fromPercentEncoding(ba.toUtf8()));
qDebug() << " urlDecoded" << urlDecoded.authority(QUrl::FullyDecoded);
//qDebug() << " urlDecoded" << urlDecoded.authority(QUrl::FullyDecoded);
QVector<QPair<QString, QString> > data = StringUtil::parseMailtoUrl(urlDecoded);
QCOMPARE(data.size(), 2);
QCOMPARE(data.at(0).first, QLatin1String("body"));
......@@ -486,7 +500,7 @@ void StringUtilTest::test_parseMailToBug406208()
QString ba(QStringLiteral(
"mailto:?body=http%3A%2F%2Fwww.lecourrierdelarchitecte.com%2Farticle_8428%20%23%23bla&subject=Le%20Courrier%20l'effet%20%23metoo%20%3F"));
QUrl urlDecoded(QUrl::fromPercentEncoding(ba.toUtf8()));
qDebug() << " urlDecoded" << urlDecoded.authority(QUrl::FullyDecoded);
//qDebug() << " urlDecoded" << urlDecoded.authority(QUrl::FullyDecoded);
QVector<QPair<QString, QString> > data = StringUtil::parseMailtoUrl(urlDecoded);
QCOMPARE(data.size(), 2);
QCOMPARE(data.at(0).first, QLatin1String("body"));
......
......@@ -42,6 +42,7 @@ private Q_SLOTS:
void test_parseDuplicateQueryItems();
void test_parseMAilToBug402378();
void test_parseMailToBug406208();
void test_parseMailToWithUtf8Encoded();
};
#endif
......@@ -181,6 +181,29 @@ QVector<QPair<QString, QString> > parseMailtoUrl(const QUrl &url)
QString str = url.toString();
QString toStr;
int i = 0;
//Convert =?utf-8?B...?=
const QString utf8CharsetPrefix = QStringLiteral("=?utf-8?B?");
int indexUtf8 = str.indexOf(utf8CharsetPrefix);
bool utf8EncodingStrFound = false;
while (indexUtf8 != 1) {
const int indexEndEncoding = str.indexOf(QStringLiteral("?="));
if (indexEndEncoding == -1) {
break;
}
const QString encodedStr = str.mid(indexUtf8 + utf8CharsetPrefix.size(), indexEndEncoding - indexUtf8 - utf8CharsetPrefix.size());
const QByteArray ba = QByteArray::fromBase64(encodedStr.toLatin1(), QByteArray::Base64UrlEncoding);
const QString decodedStr = QString::fromUtf8(ba);
// +2 as "?="
str.replace(indexUtf8, indexEndEncoding + 2 -indexUtf8, decodedStr);
indexUtf8 = str.indexOf(utf8CharsetPrefix);
utf8EncodingStrFound = true;
}
QUrl newUrl = url;
if (utf8EncodingStrFound) {
newUrl = QUrl::fromUserInput(str);
}
int indexTo = -1;
//Workaround line with # see bug 406208
const int indexOf = str.indexOf(QLatin1Char('?'));
......@@ -201,8 +224,7 @@ QVector<QPair<QString, QString> > parseMailtoUrl(const QUrl &url)
i++;
}
}
QStringList to = {KEmailAddress::decodeMailtoUrl(url)};
QStringList to = {KEmailAddress::decodeMailtoUrl(newUrl)};
if (!toStr.isEmpty()) {
to << toStr;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment