Skip to content

Commit 6717de8

Browse files
Martin PeterssonPasi Pentikäinen
authored andcommitted
QNam: try to read the last CRLF when chunked encoding is used.
When chunked encoding is used we should try to read the last CRLF after the last zero-lenght chunk, with chunk size coded as 0. Task-number: QTBUG-19480 Task-number: QTBUG-20924 Change-Id: I39c85f45c329246d6c53c29ba1511039b2503e13 Reviewed-by: Shane Kearns <[email protected]> (cherry picked from commit 2cfc2a2) Reviewed-by: Jaakko Helanti <[email protected]> Reviewed-by: Pasi Pentikäinen <[email protected]>
1 parent c81d3af commit 6717de8

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/network/access/qhttpnetworkreply.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ QHttpNetworkReplyPrivate::QHttpNetworkReplyPrivate(const QUrl &newUrl)
252252
chunkedTransferEncoding(false),
253253
connectionCloseEnabled(true),
254254
forceConnectionCloseEnabled(false),
255+
lastChunkRead(false),
255256
currentChunkSize(0), currentChunkRead(0), connection(0), initInflate(false),
256257
autoDecompress(false), responseData(), requestIsPrepared(false)
257258
,pipeliningUsed(false), downstreamLimited(false)
@@ -272,6 +273,7 @@ void QHttpNetworkReplyPrivate::clearHttpLayerInformation()
272273
totalProgress = 0;
273274
currentChunkSize = 0;
274275
currentChunkRead = 0;
276+
lastChunkRead = false;
275277
connectionCloseEnabled = true;
276278
#ifndef QT_NO_COMPRESS
277279
if (initInflate)
@@ -770,7 +772,7 @@ qint64 QHttpNetworkReplyPrivate::readReplyBodyChunked(QAbstractSocket *socket, Q
770772
{
771773
qint64 bytes = 0;
772774
while (socket->bytesAvailable()) {
773-
if (currentChunkRead >= currentChunkSize) {
775+
if (!lastChunkRead && currentChunkRead >= currentChunkSize) {
774776
// For the first chunk and when we're done with a chunk
775777
currentChunkSize = 0;
776778
currentChunkRead = 0;
@@ -793,8 +795,23 @@ qint64 QHttpNetworkReplyPrivate::readReplyBodyChunked(QAbstractSocket *socket, Q
793795
break;
794796
}
795797
// if the chunk size is 0, end of the stream
796-
if (currentChunkSize == 0) {
797-
state = AllDoneState;
798+
if (currentChunkSize == 0 || lastChunkRead) {
799+
lastChunkRead = true;
800+
// try to read the "\r\n" after the chunk
801+
char crlf[2];
802+
qint64 haveRead = socket->read(crlf, 2);
803+
if (haveRead > 0)
804+
bytes += haveRead;
805+
806+
if ((haveRead == 2 && crlf[0] == '\r' && crlf[1] == '\n') || (haveRead == 1 && crlf[0] == '\n'))
807+
state = AllDoneState;
808+
else if (haveRead == 1 && crlf[0] == '\r')
809+
break; // Still waiting for the last \n
810+
else if (haveRead > 0) {
811+
// If we read something else then CRLF, we need to close the channel.
812+
forceConnectionCloseEnabled = true;
813+
state = AllDoneState;
814+
}
798815
break;
799816
}
800817

src/network/access/qhttpnetworkreply_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class QHttpNetworkReplyPrivate : public QObjectPrivate, public QHttpNetworkHeade
233233
bool chunkedTransferEncoding;
234234
bool connectionCloseEnabled;
235235
bool forceConnectionCloseEnabled;
236+
bool lastChunkRead;
236237
qint64 currentChunkSize;
237238
qint64 currentChunkRead;
238239
QPointer<QHttpNetworkConnection> connection;

0 commit comments

Comments
 (0)