From 46b84b0b69e35414106ce4eccf3ef89f78c72f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Wed, 19 Jun 2024 12:59:42 +0200 Subject: [PATCH] QHttp2Connection: add sendDATA(QByteArray) Inside QtNetwork itself we are using byte-devices, but outside users need to send byte-arrays. The solution then is to wrap it in a QBuffer, but the QBuffer then needs to be allocated and deleted for each transmission. Change-Id: I1e5870b11450f0bd2b68fedb01d1653de115ffda Reviewed-by: Marc Mutz Reviewed-by: Mate Barany --- src/network/access/qhttp2connection.cpp | 19 +++++++++++++++++++ src/network/access/qhttp2connection_p.h | 1 + .../qhttp2connection/tst_qhttp2connection.cpp | 10 ++-------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/network/access/qhttp2connection.cpp b/src/network/access/qhttp2connection.cpp index 2d92684863c..c1c946cfdcf 100644 --- a/src/network/access/qhttp2connection.cpp +++ b/src/network/access/qhttp2connection.cpp @@ -234,6 +234,25 @@ bool QHttp2Stream::sendRST_STREAM(quint32 errorCode) return frameWriter.write(*connection->getSocket()); } +/*! + Sends a DATA frame with the bytes obtained from \a payload. + + This function will send as many DATA frames as needed to send all the data + from \a payload. If \a endStream is \c true, the END_STREAM flag will be + set. +*/ +void QHttp2Stream::sendDATA(const QByteArray &payload, bool endStream) +{ + Q_ASSERT(!m_uploadByteDevice); + if (m_state != State::Open && m_state != State::HalfClosedRemote) + return; + + auto *byteDevice = QNonContiguousByteDeviceFactory::create(payload); + connect(this, &QHttp2Stream::uploadFinished, byteDevice, &QObject::deleteLater); + byteDevice->setParent(this); + sendDATA(byteDevice, endStream); +} + /*! Sends a DATA frame with the bytes obtained from \a device. diff --git a/src/network/access/qhttp2connection_p.h b/src/network/access/qhttp2connection_p.h index ca2cae58e04..0ee19bac07e 100644 --- a/src/network/access/qhttp2connection_p.h +++ b/src/network/access/qhttp2connection_p.h @@ -132,6 +132,7 @@ public Q_SLOTS: bool sendRST_STREAM(quint32 errorCode); bool sendHEADERS(const HPack::HttpHeader &headers, bool endStream, quint8 priority = DefaultPriority); + void sendDATA(const QByteArray &payload, bool endStream); void sendDATA(QIODevice *device, bool endStream); void sendDATA(QNonContiguousByteDevice *device, bool endStream); void sendWINDOW_UPDATE(quint32 delta); diff --git a/tests/auto/network/access/qhttp2connection/tst_qhttp2connection.cpp b/tests/auto/network/access/qhttp2connection/tst_qhttp2connection.cpp index b9d5219ae98..c4095d0a404 100644 --- a/tests/auto/network/access/qhttp2connection/tst_qhttp2connection.cpp +++ b/tests/auto/network/access/qhttp2connection/tst_qhttp2connection.cpp @@ -352,11 +352,8 @@ void tst_QHttp2Connection::WINDOW_UPDATE() // we can check the headers now immediately QCOMPARE(serverStream->receivedHeaders(), expectedRequestHeaders); - QBuffer *buffer = new QBuffer(clientStream); QByteArray uploadedData = "Hello World"_ba.repeated(1000); - buffer->setData(uploadedData); - buffer->open(QIODevice::ReadWrite); - clientStream->sendDATA(buffer, true); + clientStream->sendDATA(uploadedData, true); bool streamEnd = false; QByteArray serverReceivedData; @@ -374,10 +371,7 @@ void tst_QHttp2Connection::WINDOW_UPDATE() const HPack::HttpHeader ExpectedResponseHeaders{ { ":status", "200" } }; serverStream->sendHEADERS(ExpectedResponseHeaders, false); - QBuffer *serverBuffer = new QBuffer(serverStream); - serverBuffer->setData(uploadedData); - serverBuffer->open(QIODevice::ReadWrite); - serverStream->sendDATA(serverBuffer, true); + serverStream->sendDATA(uploadedData, true); QVERIFY(clientHeaderReceivedSpy.wait()); const HPack::HttpHeader