From 6c0d08d16afb5eb82fa748dc48409553e1c85a77 Mon Sep 17 00:00:00 2001 From: Juha Vuolle Date: Fri, 19 Jan 2024 07:25:51 +0200 Subject: [PATCH] Add QHttpHeaders::valueAt() function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 valueAt() and (in a follow-up) nameAt() functions to enable efficient indexed iteration without the need to expose a value_type. Resulted from API-review Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Marc Mutz Reviewed-by: Ivan Solovev (cherry picked from commit 4bb12dab6a13c6deee713a77efa9ce996adc97a9) Change-Id: I2ee509d89e6f9080c17a4cef947f80f312d75180 --- src/network/access/qhttpheaders.cpp | 12 ++++++++++++ src/network/access/qhttpheaders.h | 2 ++ .../network/access/qhttpheaders/tst_qhttpheaders.cpp | 9 +++++++++ 3 files changed, 23 insertions(+) diff --git a/src/network/access/qhttpheaders.cpp b/src/network/access/qhttpheaders.cpp index fb4594663f3..c6d76dee7e2 100644 --- a/src/network/access/qhttpheaders.cpp +++ b/src/network/access/qhttpheaders.cpp @@ -987,6 +987,18 @@ QList QHttpHeaders::values(WellKnownHeader name) const return values(headerNames[qToUnderlying(name)]); } +/*! + Returns the header value at index \a i. The index \a i must be valid + (see \l size()). + + \sa size(), value(), values(), combinedValue() +*/ +QByteArrayView QHttpHeaders::valueAt(qsizetype i) const noexcept +{ + d->verify(i); + return d->headers.at(i).value; +} + /*! 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 diff --git a/src/network/access/qhttpheaders.h b/src/network/access/qhttpheaders.h index 661b9da0f4d..bb975b00ddd 100644 --- a/src/network/access/qhttpheaders.h +++ b/src/network/access/qhttpheaders.h @@ -235,6 +235,8 @@ public: Q_NETWORK_EXPORT QList values(QAnyStringView name) const; Q_NETWORK_EXPORT QList values(WellKnownHeader name) const; + Q_NETWORK_EXPORT QByteArrayView valueAt(qsizetype i) const noexcept; + Q_NETWORK_EXPORT QByteArray combinedValue(QAnyStringView name) const; Q_NETWORK_EXPORT QByteArray combinedValue(WellKnownHeader name) const; diff --git a/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp b/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp index 7a7e1da0a4b..77f265c9e9d 100644 --- a/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp +++ b/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp @@ -186,6 +186,15 @@ void tst_QHttpHeaders::accessors() QCOMPARE(h1.size(), 5); QCOMPARE(h1.names().size(), 2); + // valueAt() + h1.clear(); + h1.append(n1, v1); + h1.append(n2, v2); + h1.append(n3, v3); + QCOMPARE(h1.valueAt(0), v1); + QCOMPARE(h1.valueAt(1), v2); + QCOMPARE(h1.valueAt(2), v3); + // removeAll() h1.clear(); QVERIFY(h1.append(n1, v1));