summaryrefslogtreecommitdiffstats
path: root/src/corelib/io
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2017-09-21 13:27:51 -0700
committerThiago Macieira <thiago.macieira@intel.com>2017-10-06 03:49:40 +0000
commitf62768d046528636789f901ac79e2cfa1843a7b7 (patch)
treec3ae2dd1df60afb40db5af37a3ec63aaa4caa74c /src/corelib/io
parent70ee08951a7ff225b47829d9d2a0ee25a68725f7 (diff)
QUrl: re-fix the setPath("//path") case leading to scheme://path
Commits aba336c2b4ad8926dc8a000718bbb7f8a6d5a72d (in Qt 5.2) and aba336c2b4ad8926dc8a000718bbb7f8a6d5a72d (in 5.6) both tried to deal with this problem, with different levels of success. This is the third attempt (and hopefully the charm). Instead of modifying the path that the user provides, go straight ahead and declare it invalid. This is supported by RFC 3986, which declares this expansion impossible: relative-part = "//" authority path-abempty / path-absolute / path-noscheme / path-empty path-abempty = *( "/" segment ) path-absolute = "/" [ segment-nz *( "/" segment ) ] path-noscheme = segment-nz-nc *( "/" segment ) The "path-abempty" and "path-noscheme" cases are the two issues we already handle. This commit adds the third one: path-absolute, which requires that the first segment of the path be of non-zero length. That is, it is now possible again to have http://example.com//path constructed piece-wise, without it producing http://example.com/path. Additionally, it catches the case of http://example.com//path parsed from full URL then followed by setAuthority(""). Change-Id: I69f37f9304f24709a823fffd14e67a5e7212ddcd Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'src/corelib/io')
-rw-r--r--src/corelib/io/qurl.cpp31
1 files changed, 22 insertions, 9 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp
index ac694a464a..a499dc2d30 100644
--- a/src/corelib/io/qurl.cpp
+++ b/src/corelib/io/qurl.cpp
@@ -499,9 +499,10 @@ public:
InvalidFragmentError = Fragment << 8,
- // the following two cases are only possible in combination
- // with presence/absence of the authority and scheme. See validityError().
+ // the following three cases are only possible in combination with
+ // presence/absence of the path, authority and scheme. See validityError().
AuthorityPresentAndPathIsRelative = Authority << 8 | Path << 8 | 0x10000,
+ AuthorityAbsentAndPathIsDoubleSlash,
RelativeUrlPathContainsColonBeforeSlash = Scheme << 8 | Authority << 8 | Path << 8 | 0x10000,
NoError = 0
@@ -1627,19 +1628,32 @@ inline QUrlPrivate::ErrorCode QUrlPrivate::validityError(QString *source, int *p
return error->code;
}
- // There are two more cases of invalid URLs that QUrl recognizes and they
+ // There are three more cases of invalid URLs that QUrl recognizes and they
// are only possible with constructed URLs (setXXX methods), not with
// parsing. Therefore, they are tested here.
//
- // The two cases are a non-empty path that doesn't start with a slash and:
+ // Two cases are a non-empty path that doesn't start with a slash and:
// - with an authority
// - without an authority, without scheme but the path with a colon before
// the first slash
+ // The third case is an empty authority and a non-empty path that starts
+ // with "//".
// Those cases are considered invalid because toString() would produce a URL
// that wouldn't be parsed back to the same QUrl.
- if (path.isEmpty() || path.at(0) == QLatin1Char('/'))
+ if (path.isEmpty())
return NoError;
+ if (path.at(0) == QLatin1Char('/')) {
+ if (sectionIsPresent & QUrlPrivate::Authority || port != -1 ||
+ path.length() == 1 || path.at(1) != QLatin1Char('/'))
+ return NoError;
+ if (source) {
+ *source = path;
+ *position = 0;
+ }
+ return AuthorityAbsentAndPathIsDoubleSlash;
+ }
+
if (sectionIsPresent & QUrlPrivate::Host) {
if (source) {
*source = path;
@@ -2514,10 +2528,7 @@ void QUrl::setPath(const QString &path, ParsingMode mode)
mode = TolerantMode;
}
- int from = 0;
- while (from < data.length() - 2 && data.midRef(from, 2) == QLatin1String("//"))
- ++from;
- d->setPath(data, from, data.length());
+ d->setPath(data, 0, data.length());
// optimized out, since there is no path delimiter
// if (path.isNull())
@@ -3989,6 +4000,8 @@ static QString errorMessage(QUrlPrivate::ErrorCode errorCode, const QString &err
case QUrlPrivate::AuthorityPresentAndPathIsRelative:
return QStringLiteral("Path component is relative and authority is present");
+ case QUrlPrivate::AuthorityAbsentAndPathIsDoubleSlash:
+ return QStringLiteral("Path component starts with '//' and authority is absent");
case QUrlPrivate::RelativeUrlPathContainsColonBeforeSlash:
return QStringLiteral("Relative URL's path component contains ':' before any '/'");
}