QNetworkAccessManager: Read all from socket on remote host close
authorMartin Petersson <Martin.Petersson@nokia.com>
Wed, 9 May 2012 11:53:46 +0000 (13:53 +0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 16 May 2012 02:24:32 +0000 (04:24 +0200)
When we get a remoteHostClosed we should try to read everything from
the socket before we close the channel.

Change-Id: Iaa87d79ea16d69735f6ba3e8b3b4a0f86fbd5f73
Reviewed-by: Shane Kearns <shane.kearns@accenture.com>
Reviewed-by: Prasanth Ullattil <prasanth.ullattil@nokia.com>
Reviewed-by: Richard J. Moore <rich@kde.org>
src/network/access/qhttpnetworkconnectionchannel.cpp

index 6e33836..006f533 100644 (file)
@@ -1111,6 +1111,32 @@ void QHttpNetworkConnectionChannel::_q_error(QAbstractSocket::SocketError socket
                 return;
             }
             // ok, we got a disconnect even though we did not expect it
+            // Try to read everything from the socket before we emit the error.
+            if (socket->bytesAvailable()) {
+                // Read everything from the socket into the reply buffer.
+                // we can ignore the readbuffersize as the data is already
+                // in memory and we will not recieve more data on the socket.
+                reply->setReadBufferSize(0);
+                _q_receiveReply();
+#ifndef QT_NO_SSL
+                if (ssl) {
+                    // QT_NO_OPENSSL. The QSslSocket can still have encrypted bytes in the plainsocket.
+                    // So we need to check this if the socket is a QSslSocket. When the socket is flushed
+                    // it will force a decrypt of the encrypted data in the plainsocket.
+                    QSslSocket *sslSocket = static_cast<QSslSocket*>(socket);
+                    qint64 beforeFlush = sslSocket->encryptedBytesAvailable();
+                    while (sslSocket->encryptedBytesAvailable()) {
+                        sslSocket->flush();
+                        _q_receiveReply();
+                        qint64 afterFlush = sslSocket->encryptedBytesAvailable();
+                        if (afterFlush == beforeFlush)
+                            break;
+                        beforeFlush = afterFlush;
+                    }
+                }
+#endif
+            }
+
             errorCode = QNetworkReply::RemoteHostClosedError;
         } else {
             errorCode = QNetworkReply::RemoteHostClosedError;