Utils: Fix FilePath::fromString host decoding

Previously FilePath::fromString did not decode %25 (%) and %2f (/),
so a round of ::fromString(::toString()) would not return the same host.

Change-Id: I99c7317fb149443c2e52c099f7da7c4ef6768aff
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2022-10-20 08:36:43 +02:00
parent 5c313b706f
commit 3ac5051af9
2 changed files with 61 additions and 5 deletions

View File

@@ -180,6 +180,11 @@ QString FilePath::encodedHost() const
return result;
}
QString decodeHost(QString host)
{
return host.replace("%25", "%").replace("%2f", "/");
}
/// \returns a QString for passing on to QString based APIs
QString FilePath::toString() const
{
@@ -765,10 +770,10 @@ void FilePath::setFromString(const QString &unnormalizedFileName)
const int firstSlash = withoutQtcDeviceRoot.indexOf(slash);
if (firstSlash != -1) {
QString scheme = withoutQtcDeviceRoot.left(firstSlash).toString();
const QString scheme = withoutQtcDeviceRoot.left(firstSlash).toString();
const int secondSlash = withoutQtcDeviceRoot.indexOf(slash, firstSlash + 1);
QString host = withoutQtcDeviceRoot.mid(firstSlash + 1, secondSlash - firstSlash - 1)
.toString();
const QString host = decodeHost(
withoutQtcDeviceRoot.mid(firstSlash + 1, secondSlash - firstSlash - 1).toString());
if (secondSlash != -1) {
QStringView path = withoutQtcDeviceRoot.mid(secondSlash);
setParts(scheme, host, path);
@@ -787,9 +792,9 @@ void FilePath::setFromString(const QString &unnormalizedFileName)
const int schemeEnd = fileName.indexOf(colonSlashSlash);
if (schemeEnd != -1 && schemeEnd < firstSlash) {
// This is a pseudo Url, we can't use QUrl here sadly.
QString scheme = fileName.left(schemeEnd);
const QString scheme = fileName.left(schemeEnd);
const int hostEnd = fileName.indexOf(slash, schemeEnd + 3);
QString host = fileName.mid(schemeEnd + 3, hostEnd - schemeEnd - 3);
const QString host = decodeHost(fileName.mid(schemeEnd + 3, hostEnd - schemeEnd - 3));
setParts(scheme, host, hostEnd != -1 ? QStringView(fileName).mid(hostEnd) : QStringView());
return;
}