diff --git a/src/network/access/qhttpheaders.cpp b/src/network/access/qhttpheaders.cpp index c6d76dee7e2..1ea2890f219 100644 --- a/src/network/access/qhttpheaders.cpp +++ b/src/network/access/qhttpheaders.cpp @@ -991,7 +991,7 @@ QList 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 diff --git a/src/network/access/qhttpheaders.h b/src/network/access/qhttpheaders.h index bb975b00ddd..a153d7911ef 100644 --- a/src/network/access/qhttpheaders.h +++ b/src/network/access/qhttpheaders.h @@ -236,6 +236,7 @@ public: Q_NETWORK_EXPORT QList 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; diff --git a/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp b/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp index 77f265c9e9d..5f6f9154208 100644 --- a/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp +++ b/tests/auto/network/access/qhttpheaders/tst_qhttpheaders.cpp @@ -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));