diff options
Diffstat (limited to 'src/corelib/io/qurlquery.cpp')
-rw-r--r-- | src/corelib/io/qurlquery.cpp | 147 |
1 files changed, 81 insertions, 66 deletions
diff --git a/src/corelib/io/qurlquery.cpp b/src/corelib/io/qurlquery.cpp index c469c51ea9..31f3ee1d90 100644 --- a/src/corelib/io/qurlquery.cpp +++ b/src/corelib/io/qurlquery.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qurlquery.h" #include "qurl_p.h" @@ -60,6 +24,8 @@ QT_BEGIN_NAMESPACE \ingroup network \ingroup shared + \compares equality + It is used to parse the query strings found in URLs like the following: \image qurl-querystring.png @@ -89,6 +55,19 @@ QT_BEGIN_NAMESPACE sequences not decodable to UTF-8). For that reason, the percent character is always represented by the string "%25". + All of the setter methods and the query methods like hasQueryItem() in + QUrlQuery take encoded forms only. Unlike in QUrl, there's no optional + parameter to specify that the strings being passed are decoded. If + improperly-encoded strings are passed to the setter or query methods, + QUrlQuery will attempt to recover instead of failing. That is to say, all + functions in this class parse their string arguments as if the + QUrl::TolerantMode decoding mode was specified. + + Application code should strive to always ensure proper encoding and not rely + on TolerantMode parsing fixing the strings. Notably, all user input must be + first percent-encoded using QUrl::toPercentEncoding() or similar functions + before being passed to the functions in this class. + \section2 Handling of spaces and plus ("+") Web browsers usually encode spaces found in HTML FORM elements to a plus sign @@ -146,14 +125,14 @@ QT_BEGIN_NAMESPACE */ /*! - \fn QUrlQuery::QUrlQuery(std::initializer_list<QPair<QString, QString>> list) + \fn QUrlQuery::QUrlQuery(std::initializer_list<std::pair<QString, QString>> list) \since 5.13 Constructs a QUrlQuery object from the \a list of key/value pair. */ -typedef QList<QPair<QString, QString> > Map; +typedef QList<std::pair<QString, QString> > Map; class QUrlQueryPrivate : public QSharedData { @@ -169,7 +148,7 @@ public: void setQuery(const QString &query); void addQueryItem(const QString &key, const QString &value) - { itemList.append(qMakePair(recodeFromUser(key), recodeFromUser(value))); } + { itemList.append(std::make_pair(recodeFromUser(key), recodeFromUser(value))); } int findRecodedKey(const QString &key, int from = 0) const { for (int i = from; i < itemList.size(); ++i) @@ -313,17 +292,17 @@ void QUrlQueryPrivate::setQuery(const QString &query) if (delimiter == pos) { // the value delimiter wasn't found, store a null value - itemList.append(qMakePair(key, QString())); + itemList.append(std::make_pair(key, QString())); } else if (delimiter + 1 == pos) { // if the delimiter was found but the value is empty, store empty-but-not-null - itemList.append(qMakePair(key, QString(0, Qt::Uninitialized))); + itemList.append(std::make_pair(key, QString(0, Qt::Uninitialized))); } else { QString value; if (!qt_urlRecode(value, QStringView{delimiter + 1, pos}, QUrl::DecodeReserved, prettyDecodedActions)) value = QString(delimiter + 1, pos - delimiter - 1); - itemList.append(qMakePair(key, value)); + itemList.append(std::make_pair(key, value)); } if (pos != end) @@ -387,6 +366,16 @@ QUrlQuery::QUrlQuery(const QUrlQuery &other) } /*! + \since 6.5 + Moves the contents of the \a other QUrlQuery object, including the query + delimiters. +*/ +QUrlQuery::QUrlQuery(QUrlQuery &&other) noexcept + : d(std::move(other.d)) +{ +} + +/*! Copies the contents of the \a other QUrlQuery object, including the query delimiters. */ @@ -412,19 +401,26 @@ QUrlQuery::~QUrlQuery() } /*! - Returns \c true if this object and the \a other object contain the same + \fn bool QUrlQuery::operator==(const QUrlQuery &lhs, const QUrlQuery &rhs) + + Returns \c true if QUrlQuery objects \a lhs and \a rhs contain the same contents, in the same order, and use the same query delimiters. */ -bool QUrlQuery::operator ==(const QUrlQuery &other) const + +bool comparesEqual(const QUrlQuery &lhs, const QUrlQuery &rhs) { - if (d == other.d) + if (lhs.d == rhs.d) return true; - if (d && other.d) + if (lhs.d && rhs.d) // keep in sync with qHash(QUrlQuery): - return d->valueDelimiter == other.d->valueDelimiter && - d->pairDelimiter == other.d->pairDelimiter && - d->itemList == other.d->itemList; - return false; + return lhs.d->valueDelimiter == rhs.d->valueDelimiter && + lhs.d->pairDelimiter == rhs.d->pairDelimiter && + lhs.d->itemList == rhs.d->itemList; + + const QUrlQueryPrivate *x = lhs.d ? lhs.d.data() : rhs.d.data(); + return x->valueDelimiter == QUrlQuery::defaultQueryValueDelimiter() && + x->pairDelimiter == QUrlQuery::defaultQueryPairDelimiter() && + x->itemList.isEmpty(); } /*! @@ -538,7 +534,7 @@ QString QUrlQuery::query(QUrl::ComponentFormattingOptions encoding) const { int size = 0; for ( ; it != end; ++it) - size += it->first.length() + 1 + it->second.length() + 1; + size += it->first.size() + 1 + it->second.size() + 1; result.reserve(size + size / 4); } @@ -567,7 +563,7 @@ QString QUrlQuery::query(QUrl::ComponentFormattingOptions encoding) const representation of the keys and values of the query string are percent encoded when returned in query(). - If \a valueDelimiter is set to '(' and \a pairDelimiter is ')', + If \a valueDelimiter is set to ',' and \a pairDelimiter is ';', the above query string would instead be represented like this: \snippet code/src_corelib_io_qurl.cpp 4 @@ -578,7 +574,7 @@ QString QUrlQuery::query(QUrl::ComponentFormattingOptions encoding) const \snippet code/src_corelib_io_qurlquery.cpp 0 Use of other characters is not supported and may result in unexpected - behaviour. This method does not verify that you passed a valid delimiter. + behavior. This method does not verify that you passed a valid delimiter. \sa queryValueDelimiter(), queryPairDelimiter() */ @@ -618,16 +614,18 @@ QChar QUrlQuery::queryPairDelimiter() const as the same, like HTML forms do. If you need spaces to be represented as plus signs, use actual plus signs. + \note The keys and values are expected to be in percent-encoded form. + \sa queryItems(), isEmpty() */ -void QUrlQuery::setQueryItems(const QList<QPair<QString, QString> > &query) +void QUrlQuery::setQueryItems(const QList<std::pair<QString, QString> > &query) { clear(); if (query.isEmpty()) return; QUrlQueryPrivate *dd = d; - QList<QPair<QString, QString> >::const_iterator it = query.constBegin(), + QList<std::pair<QString, QString> >::const_iterator it = query.constBegin(), end = query.constEnd(); for ( ; it != end; ++it) dd->addQueryItem(it->first, it->second); @@ -641,20 +639,20 @@ void QUrlQuery::setQueryItems(const QList<QPair<QString, QString> > &query) \sa setQueryItems(), {encoding}{Encoding} */ -QList<QPair<QString, QString> > QUrlQuery::queryItems(QUrl::ComponentFormattingOptions encoding) const +QList<std::pair<QString, QString> > QUrlQuery::queryItems(QUrl::ComponentFormattingOptions encoding) const { if (!d) - return QList<QPair<QString, QString> >(); + return QList<std::pair<QString, QString> >(); if (idempotentRecodeToUser(encoding)) return d->itemList; - QList<QPair<QString, QString> > result; + QList<std::pair<QString, QString> > result; Map::const_iterator it = d->itemList.constBegin(); Map::const_iterator end = d->itemList.constEnd(); - result.reserve(d->itemList.count()); + result.reserve(d->itemList.size()); for ( ; it != end; ++it) - result << qMakePair(d->recodeToUser(it->first, encoding), - d->recodeToUser(it->second, encoding)); + result << std::make_pair(d->recodeToUser(it->first, encoding), + d->recodeToUser(it->second, encoding)); return result; } @@ -662,6 +660,8 @@ QList<QPair<QString, QString> > QUrlQuery::queryItems(QUrl::ComponentFormattingO Returns \c true if there is a query string pair whose key is equal to \a key from the URL. + \note The key expected to be in percent-encoded form. + \sa addQueryItem(), queryItemValue() */ bool QUrlQuery::hasQueryItem(const QString &key) const @@ -680,6 +680,8 @@ bool QUrlQuery::hasQueryItem(const QString &key) const as the same, like HTML forms do. If you need spaces to be represented as plus signs, use actual plus signs. + \note The key and value strings are expected to be in percent-encoded form. + \sa hasQueryItem(), queryItemValue() */ void QUrlQuery::addQueryItem(const QString &key, const QString &value) @@ -698,6 +700,8 @@ void QUrlQuery::addQueryItem(const QString &key, const QString &value) one found, in the order they were present in the query string or added using addQueryItem(). + \note The key is expected to be in percent-encoded form. + \sa addQueryItem(), allQueryItemValues(), {encoding}{Encoding} */ QString QUrlQuery::queryItemValue(const QString &key, QUrl::ComponentFormattingOptions encoding) const @@ -716,6 +720,8 @@ QString QUrlQuery::queryItemValue(const QString &key, QUrl::ComponentFormattingO the URL, using the options specified in \a encoding to encode the return value. If the key \a key is not found, this function returns an empty list. + \note The key is expected to be in percent-encoded form. + \sa queryItemValue(), addQueryItem() */ QStringList QUrlQuery::allQueryItemValues(const QString &key, QUrl::ComponentFormattingOptions encoding) const @@ -738,6 +744,8 @@ QStringList QUrlQuery::allQueryItemValues(const QString &key, QUrl::ComponentFor item in the order they were present in the query string or added with addQueryItem(). + \note The key is expected to be in percent-encoded form. + \sa removeAllQueryItems() */ void QUrlQuery::removeQueryItem(const QString &key) @@ -754,6 +762,8 @@ void QUrlQuery::removeQueryItem(const QString &key) Removes all the query string pairs whose key is equal to \a key from the URL. + \note The key is expected to be in percent-encoded form. + \sa removeQueryItem() */ void QUrlQuery::removeAllQueryItems(const QString &key) @@ -761,7 +771,7 @@ void QUrlQuery::removeAllQueryItems(const QString &key) if (d.constData()) { auto *p = d.data(); const QString encodedKey = p->recodeFromUser(key); - auto firstEqualsEncodedKey = [&encodedKey](const QPair<QString, QString> &item) { + auto firstEqualsEncodedKey = [&encodedKey](const std::pair<QString, QString> &item) { return item.first == encodedKey; }; p->itemList.removeIf(firstEqualsEncodedKey); @@ -805,10 +815,15 @@ void QUrlQuery::removeAllQueryItems(const QString &key) */ /*! - \fn bool QUrlQuery::operator!=(const QUrlQuery &other) const + \fn bool QUrlQuery::operator!=(const QUrlQuery &lhs, const QUrlQuery &rhs) - Returns \c true if \a other is not equal to this QUrlQuery. Otherwise, returns \c false. + Returns \c true if the QUrlQuery object \a rhs is not equal to \a lhs. + Otherwise, returns \c false. \sa operator==() */ QT_END_NAMESPACE + +#undef decode +#undef leave +#undef encode |