Add QHttpHeaders::nameAt() function
We need a way for users to consume the complete contents of QHttpHeaders. For now, the only way is for (const auto &name : h.names()) use(h.value/combinedValue(name)); which is quadratic. Adding the usual iterators and operator[] would require us to expose the underlying value_type, which we're not ready to do, yet. So add nameAt() and (in a previous commit) valueAt() functions to enable efficient indexed iteration without the need to expose a value_type. Return by QLatin1StringView, not QAnyStringView, because that statically encodes the actual encoding used (and required by HTTP specs and promised by the class documentation, so it won't need to change). For the setters, we want to be accomodating QString, QByteArray, etc, which is why those take QAnyStringView. Resulted from API-review. Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Juha Vuolle <juha.vuolle@qt.io> (cherry picked from commit 196594003742f0bf1c075ed5b2ec93e06f237206) Change-Id: I0af34c6da06dad1d14b2d25e9230a1c04b4e7c23
This commit is contained in:
parent
6c0d08d16a
commit
5119d4dab8
@ -991,7 +991,7 @@ QList<QByteArray> QHttpHeaders::values(WellKnownHeader name) const
|
||||
Returns the header value at index \a i. The index \a i must be valid
|
||||
(see \l size()).
|
||||
|
||||
\sa size(), value(), values(), combinedValue()
|
||||
\sa size(), value(), values(), combinedValue(), nameAt()
|
||||
*/
|
||||
QByteArrayView QHttpHeaders::valueAt(qsizetype i) const noexcept
|
||||
{
|
||||
@ -999,6 +999,20 @@ QByteArrayView QHttpHeaders::valueAt(qsizetype i) const noexcept
|
||||
return d->headers.at(i).value;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the header name at index \a i. The index \a i must be valid
|
||||
(see \l size()).
|
||||
|
||||
Header names are case-insensitive, and the returned names are lower-cased.
|
||||
|
||||
\sa size(), valueAt()
|
||||
*/
|
||||
QLatin1StringView QHttpHeaders::nameAt(qsizetype i) const noexcept
|
||||
{
|
||||
d->verify(i);
|
||||
return QLatin1StringView{d->headers.at(i).name};
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the values of header \a name in a comma-combined string.
|
||||
Returns a \c null QByteArray if the header with \a name doesn't
|
||||
|
@ -236,6 +236,7 @@ public:
|
||||
Q_NETWORK_EXPORT QList<QByteArray> values(WellKnownHeader name) const;
|
||||
|
||||
Q_NETWORK_EXPORT QByteArrayView valueAt(qsizetype i) const noexcept;
|
||||
Q_NETWORK_EXPORT QLatin1StringView nameAt(qsizetype i) const noexcept;
|
||||
|
||||
Q_NETWORK_EXPORT QByteArray combinedValue(QAnyStringView name) const;
|
||||
Q_NETWORK_EXPORT QByteArray combinedValue(WellKnownHeader name) const;
|
||||
|
@ -195,6 +195,15 @@ void tst_QHttpHeaders::accessors()
|
||||
QCOMPARE(h1.valueAt(1), v2);
|
||||
QCOMPARE(h1.valueAt(2), v3);
|
||||
|
||||
// nameAt()
|
||||
h1.clear();
|
||||
h1.append(n1, v1);
|
||||
h1.append(n2, v2);
|
||||
h1.append(n3, v3);
|
||||
QCOMPARE(h1.nameAt(0), n1);
|
||||
QCOMPARE(h1.nameAt(1), n2);
|
||||
QCOMPARE(h1.nameAt(2), n3);
|
||||
|
||||
// removeAll()
|
||||
h1.clear();
|
||||
QVERIFY(h1.append(n1, v1));
|
||||
|
Loading…
x
Reference in New Issue
Block a user