Ensure readChannelFinished() is only emitted once (fixes #21).

This commit is contained in:
Nathan Osman
2017-10-25 19:31:54 -07:00
parent 9652e8f508
commit c2b21cdca4
3 changed files with 12 additions and 3 deletions

View File

@@ -68,7 +68,7 @@ SocketPrivate::SocketPrivate(Socket *httpSocket, QTcpSocket *tcpSocket)
connect(socket, &QTcpSocket::readyRead, this, &SocketPrivate::onReadyRead); connect(socket, &QTcpSocket::readyRead, this, &SocketPrivate::onReadyRead);
connect(socket, &QTcpSocket::bytesWritten, this, &SocketPrivate::onBytesWritten); connect(socket, &QTcpSocket::bytesWritten, this, &SocketPrivate::onBytesWritten);
connect(socket, &QTcpSocket::readChannelFinished, q, &Socket::readChannelFinished); connect(socket, &QTcpSocket::readChannelFinished, this, &SocketPrivate::onReadChannelFinished);
connect(socket, &QTcpSocket::disconnected, q, &Socket::disconnected); connect(socket, &QTcpSocket::disconnected, q, &Socket::disconnected);
// Process anything already received by the socket // Process anything already received by the socket
@@ -137,6 +137,13 @@ void SocketPrivate::onBytesWritten(qint64 bytes)
} }
} }
void SocketPrivate::onReadChannelFinished()
{
if (requestDataTotal == -1) {
emit q->readChannelFinished();
}
}
bool SocketPrivate::readHeaders() bool SocketPrivate::readHeaders()
{ {
// Check for the double CRLF that signals the end of the headers and // Check for the double CRLF that signals the end of the headers and

View File

@@ -73,6 +73,7 @@ private Q_SLOTS:
void onReadyRead(); void onReadyRead();
void onBytesWritten(qint64 bytes); void onBytesWritten(qint64 bytes);
void onReadChannelFinished();
private: private:

View File

@@ -132,9 +132,9 @@ void TestSocket::testSignals()
QSignalSpy headersParsedSpy(server, SIGNAL(headersParsed())); QSignalSpy headersParsedSpy(server, SIGNAL(headersParsed()));
QSignalSpy readyReadSpy(server, SIGNAL(readyRead())); QSignalSpy readyReadSpy(server, SIGNAL(readyRead()));
QSignalSpy readChannelFinishedSpy(server, SIGNAL(readChannelFinished()));
QSignalSpy bytesWrittenSpy(server, SIGNAL(bytesWritten(qint64))); QSignalSpy bytesWrittenSpy(server, SIGNAL(bytesWritten(qint64)));
QSignalSpy aboutToCloseSpy(server, SIGNAL(aboutToClose())); QSignalSpy aboutToCloseSpy(server, SIGNAL(aboutToClose()));
QSignalSpy readChannelFinishedSpy(server, SIGNAL(readChannelFinished()));
client.sendHeaders(Method, Path, headers); client.sendHeaders(Method, Path, headers);
@@ -145,7 +145,6 @@ void TestSocket::testSignals()
QTRY_COMPARE(server->bytesAvailable(), Data.length()); QTRY_COMPARE(server->bytesAvailable(), Data.length());
QVERIFY(readyReadSpy.count() > 0); QVERIFY(readyReadSpy.count() > 0);
QCOMPARE(readChannelFinishedSpy.count(), 1);
server->writeHeaders(); server->writeHeaders();
server->write(Data); server->write(Data);
@@ -162,6 +161,8 @@ void TestSocket::testSignals()
QTRY_COMPARE(aboutToCloseSpy.count(), 0); QTRY_COMPARE(aboutToCloseSpy.count(), 0);
server->close(); server->close();
QTRY_COMPARE(aboutToCloseSpy.count(), 1); QTRY_COMPARE(aboutToCloseSpy.count(), 1);
QCOMPARE(readChannelFinishedSpy.count(), 1);
} }
void TestSocket::testJson() void TestSocket::testJson()