summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Goetz <Markus.Goetz@nokia.com>2009-10-22 09:31:27 (GMT)
committerMarkus Goetz <Markus.Goetz@nokia.com>2009-10-22 15:12:58 (GMT)
commit760f221e7f1550ecc8198fb0c01c65ee13ded7f4 (patch)
tree667dff08f0213f5a1285eef34070fa3a76a40224
parent0e4d5715992f9d7d7e1c598527907797e0b98427 (diff)
downloadQt-760f221e7f1550ecc8198fb0c01c65ee13ded7f4.zip
Qt-760f221e7f1550ecc8198fb0c01c65ee13ded7f4.tar.gz
Qt-760f221e7f1550ecc8198fb0c01c65ee13ded7f4.tar.bz2
QSslSocket: Trigger a SSL transmission when reading from the socket.
In certain cases a SSL transfer stalled when a readBufferSize was set. This change triggers a SSL transmission when there is data on the socket waiting to be decrypted. Task-number: QTBUG-3860 Reviewed-by: Thiago
-rw-r--r--src/network/ssl/qsslsocket.cpp15
-rw-r--r--src/network/ssl/qsslsocket.h1
-rw-r--r--src/network/ssl/qsslsocket_p.h1
-rw-r--r--tests/auto/qsslsocket/tst_qsslsocket.cpp61
4 files changed, 78 insertions, 0 deletions
diff --git a/src/network/ssl/qsslsocket.cpp b/src/network/ssl/qsslsocket.cpp
index ad766c1..2c88130 100644
--- a/src/network/ssl/qsslsocket.cpp
+++ b/src/network/ssl/qsslsocket.cpp
@@ -1740,6 +1740,11 @@ qint64 QSslSocket::readData(char *data, qint64 maxlen)
#ifdef QSSLSOCKET_DEBUG
qDebug() << "QSslSocket::readData(" << (void *)data << ',' << maxlen << ") ==" << readBytes;
#endif
+
+ // possibly trigger another transmit() to decrypt more data from the socket
+ if (d->readBuffer.isEmpty() && d->plainSocket->bytesAvailable())
+ QMetaObject::invokeMethod(this, "_q_flushReadBuffer", Qt::QueuedConnection);
+
return readBytes;
}
@@ -2134,6 +2139,16 @@ void QSslSocketPrivate::_q_flushWriteBuffer()
q->flush();
}
+/*!
+ \internal
+*/
+void QSslSocketPrivate::_q_flushReadBuffer()
+{
+ // trigger a read from the plainSocket into SSL
+ if (mode != QSslSocket::UnencryptedMode)
+ transmit();
+}
+
QT_END_NAMESPACE
// For private slots
diff --git a/src/network/ssl/qsslsocket.h b/src/network/ssl/qsslsocket.h
index adb206c..82cda35 100644
--- a/src/network/ssl/qsslsocket.h
+++ b/src/network/ssl/qsslsocket.h
@@ -207,6 +207,7 @@ private:
Q_PRIVATE_SLOT(d_func(), void _q_readyReadSlot())
Q_PRIVATE_SLOT(d_func(), void _q_bytesWrittenSlot(qint64))
Q_PRIVATE_SLOT(d_func(), void _q_flushWriteBuffer())
+ Q_PRIVATE_SLOT(d_func(), void _q_flushReadBuffer())
friend class QSslSocketBackendPrivate;
};
diff --git a/src/network/ssl/qsslsocket_p.h b/src/network/ssl/qsslsocket_p.h
index 24d4ebe..ee21956 100644
--- a/src/network/ssl/qsslsocket_p.h
+++ b/src/network/ssl/qsslsocket_p.h
@@ -120,6 +120,7 @@ public:
void _q_readyReadSlot();
void _q_bytesWrittenSlot(qint64);
void _q_flushWriteBuffer();
+ void _q_flushReadBuffer();
// Platform specific functions
virtual void startClientEncryption() = 0;
diff --git a/tests/auto/qsslsocket/tst_qsslsocket.cpp b/tests/auto/qsslsocket/tst_qsslsocket.cpp
index d576201..2bd1684 100644
--- a/tests/auto/qsslsocket/tst_qsslsocket.cpp
+++ b/tests/auto/qsslsocket/tst_qsslsocket.cpp
@@ -170,6 +170,7 @@ private slots:
void setEmptyKey();
void spontaneousWrite();
void setReadBufferSize();
+ void setReadBufferSize_task_250027();
void waitForMinusOne();
void verifyMode();
void verifyDepth();
@@ -1241,6 +1242,66 @@ void tst_QSslSocket::setReadBufferSize()
QVERIFY(receiver->bytesAvailable() > oldBytesAvailable);
}
+class SetReadBufferSize_task_250027_handler : public QObject {
+ Q_OBJECT
+public slots:
+ void readyReadSlot() {
+ QTestEventLoop::instance().exitLoop();
+ }
+ void waitSomeMore(QSslSocket *socket) {
+ QTime t;
+ t.start();
+ while (!socket->encryptedBytesAvailable()) {
+ QCoreApplication::processEvents(QEventLoop::AllEvents | QEventLoop::WaitForMoreEvents, 250);
+ if (t.elapsed() > 1000 || socket->state() != QAbstractSocket::ConnectedState)
+ return;
+ }
+ }
+};
+
+void tst_QSslSocket::setReadBufferSize_task_250027()
+{
+ // do not execute this when a proxy is set.
+ QFETCH_GLOBAL(bool, setProxy);
+ if (setProxy)
+ return;
+
+ QSslSocketPtr socket = newSocket();
+ socket->setReadBufferSize(1000); // limit to 1 kb/sec
+ socket->ignoreSslErrors();
+ socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443);
+ socket->ignoreSslErrors();
+ QVERIFY(socket->waitForConnected(10*1000));
+ QVERIFY(socket->waitForEncrypted(10*1000));
+
+ // exit the event loop as soon as we receive a readyRead()
+ SetReadBufferSize_task_250027_handler setReadBufferSize_task_250027_handler;
+ connect(socket, SIGNAL(readyRead()), &setReadBufferSize_task_250027_handler, SLOT(readyReadSlot()));
+
+ // provoke a response by sending a request
+ socket->write("GET /gif/fluke.gif HTTP/1.0\n"); // this file is 27 KB
+ socket->write("Host: ");
+ socket->write(QtNetworkSettings::serverName().toLocal8Bit().constData());
+ socket->write("\n");
+ socket->write("Connection: close\n");
+ socket->write("\n");
+ socket->flush();
+
+ QTestEventLoop::instance().enterLoop(10);
+ setReadBufferSize_task_250027_handler.waitSomeMore(socket);
+ QByteArray firstRead = socket->readAll();
+ // First read should be some data, but not the whole file
+ QVERIFY(firstRead.size() > 0 && firstRead.size() < 20*1024);
+
+ QTestEventLoop::instance().enterLoop(10);
+ setReadBufferSize_task_250027_handler.waitSomeMore(socket);
+ QByteArray secondRead = socket->readAll();
+ // second read should be some more data
+ QVERIFY(secondRead.size() > 0);
+
+ socket->close();
+}
+
class SslServer3 : public QTcpServer
{
Q_OBJECT