From fd6dc2e9e7ce12d73e095a70b3259ea649f4a62d Mon Sep 17 00:00:00 2001 From: Juha Vuolle Date: Fri, 19 Jan 2024 07:50:04 +0200 Subject: [PATCH] Remove QHttpHeaders::names() We need a way for users to consume the complete contents of QHttpHeaders. Until now, the only way was 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 we added nameAt() and valueAt() functions in previous commits to enable efficient indexed iteration without the need to expose a value_type. Having added those, we can now remove names(), which had the wrong value_type (QByteArrays are by definition UTF-8 in Qt, while header names are L1), and is no longer needed to facilitate iteration. In QNetworkRequestFactory, temporarily use toMultiMap() because we need the combinedValue() of all headers here. The fix will be to make QNetworkRequest QHttpHeaders-aware, but that's a Qt 6.8 thing, even though we should still de-pessimize this code for Qt 6.7 with private API. Resulted from API-review. Pick-to: 6.7 Change-Id: I65086ef4c62e22554ae7325a846bebc08b44916f Reviewed-by: Ivan Solovev --- src/network/access/qhttpheaders.cpp | 15 ------------ src/network/access/qhttpheaders.h | 2 -- src/network/access/qnetworkrequestfactory.cpp | 3 ++- .../access/qhttpheaders/tst_qhttpheaders.cpp | 24 +++---------------- 4 files changed, 5 insertions(+), 39 deletions(-) diff --git a/src/network/access/qhttpheaders.cpp b/src/network/access/qhttpheaders.cpp index 1ea2890f21..7a640b509c 100644 --- a/src/network/access/qhttpheaders.cpp +++ b/src/network/access/qhttpheaders.cpp @@ -888,21 +888,6 @@ bool QHttpHeaders::contains(WellKnownHeader name) const return contains(headerNames[qToUnderlying(name)]); } -/*! - Returns a list of unique header names. - Header names are case-insensitive, and the returned - names are lower-cased. -*/ -QList QHttpHeaders::names() const -{ - QList names; - for (const Header &header: d->headers) { - if (!names.contains(header.name)) - names.append(header.name); - } - return names; -} - /*! Removes the header \a name. diff --git a/src/network/access/qhttpheaders.h b/src/network/access/qhttpheaders.h index a153d7911e..1edbd03b12 100644 --- a/src/network/access/qhttpheaders.h +++ b/src/network/access/qhttpheaders.h @@ -222,8 +222,6 @@ public: Q_NETWORK_EXPORT bool contains(QAnyStringView name) const; Q_NETWORK_EXPORT bool contains(WellKnownHeader name) const; - Q_NETWORK_EXPORT QList names() const; - Q_NETWORK_EXPORT void clear(); Q_NETWORK_EXPORT void removeAll(QAnyStringView name); Q_NETWORK_EXPORT void removeAll(WellKnownHeader name); diff --git a/src/network/access/qnetworkrequestfactory.cpp b/src/network/access/qnetworkrequestfactory.cpp index 8a1ade8f78..0941304404 100644 --- a/src/network/access/qnetworkrequestfactory.cpp +++ b/src/network/access/qnetworkrequestfactory.cpp @@ -9,6 +9,7 @@ #endif #include +#include QT_BEGIN_NAMESPACE @@ -497,7 +498,7 @@ QNetworkRequest QNetworkRequestFactoryPrivate::newRequest(const QUrl &url) const // may be multiple values per name. Note: this would not necessarily // produce right result for 'Set-Cookie' header if it has multiple values, // but since it is a purely server-side (response) header, not relevant here. - const auto headerNames = headers.names(); + const auto headerNames = headers.toMultiMap().uniqueKeys(); // ### fixme: port QNR to QHH for (const auto &name : headerNames) request.setRawHeader(name, headers.combinedValue(name)); diff --git a/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp b/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp index 5f6f915420..6a324c611d 100644 --- a/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp +++ b/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp @@ -166,26 +166,6 @@ void tst_QHttpHeaders::accessors() #undef EXISTS_N_TIMES #undef EXISTS_NOT - // names() - h1.clear(); - QVERIFY(h1.names().isEmpty()); - h1.append(n1, v1); - QCOMPARE(h1.names().size(), 1); - QCOMPARE(h1.size(), 1); - QVERIFY(h1.names().contains(n1)); - h1.append(n2, v2); - QCOMPARE(h1.names().size(), 2); - QCOMPARE(h1.size(), 2); - QVERIFY(h1.names().contains(n1)); - QVERIFY(h1.names().contains(n2)); - h1.append(n1, v1); - h1.append(n1, v1); - QCOMPARE(h1.size(), 4); - QCOMPARE(h1.names().size(), 2); - h1.append(N1, v1); // uppercase of n1 - QCOMPARE(h1.size(), 5); - QCOMPARE(h1.names().size(), 2); - // valueAt() h1.clear(); h1.append(n1, v1); @@ -353,7 +333,9 @@ void tst_QHttpHeaders::headerNameField() h1.append(u"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&'*+-.^_`|~"_s, v1); QCOMPARE(h1.size(), 4); - QCOMPARE(h1.names().size(), 1); + QCOMPARE(h1.nameAt(0), h1.nameAt(1)); + QCOMPARE(h1.nameAt(1), h1.nameAt(2)); + QCOMPARE(h1.nameAt(2), h1.nameAt(3)); h1.clear(); // Error cases