diff --git a/src/core/qhttpsocket.cpp b/src/core/qhttpsocket.cpp index 0bf5e7d..e8a9e65 100644 --- a/src/core/qhttpsocket.cpp +++ b/src/core/qhttpsocket.cpp @@ -30,16 +30,16 @@ #include "qhttpsocket.h" #include "qhttpsocket_p.h" -QHttpSocketPrivate::QHttpSocketPrivate(QHttpSocket *socket, QIODevice *baseDevice) +QHttpSocketPrivate::QHttpSocketPrivate(QHttpSocket *socket, QTcpSocket *baseSocket) : QObject(socket), q(socket), - device(baseDevice), + socket(baseSocket), statusCode("200 OK"), headersParsed(false), headersWritten(false) { - connect(device, SIGNAL(readyRead()), this, SLOT(onReadyRead())); - connect(device, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64))); + connect(socket, SIGNAL(readyRead()), this, SLOT(onReadyRead())); + connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64))); // Attempt to read data from the device the next time the event loop is entered QTimer::singleShot(0, this, SLOT(onReadyRead())); @@ -48,7 +48,7 @@ QHttpSocketPrivate::QHttpSocketPrivate(QHttpSocket *socket, QIODevice *baseDevic void QHttpSocketPrivate::onReadyRead() { // Add the new data to the internal buffer - buffer.append(device->readAll()); + buffer.append(socket->readAll()); // If the request headers have not yet been parsed, check for two CRLFs if(!headersParsed) { @@ -98,9 +98,9 @@ void QHttpSocketPrivate::onBytesWritten(qint64 bytes) } } -QHttpSocket::QHttpSocket(QIODevice *device, QObject *parent) +QHttpSocket::QHttpSocket(QTcpSocket *socket, QObject *parent) : QIODevice(parent), - d(new QHttpSocketPrivate(this, device)) + d(new QHttpSocketPrivate(this, socket)) { setOpenMode(QIODevice::ReadWrite); } @@ -172,11 +172,11 @@ void QHttpSocket::writeHeaders() // Append an extra CRLF header.append("\r\n"); - // Write the header - d->device->write(header); - d->headerLength = header.length(); d->headersWritten = true; + + // Write the header + d->socket->write(header); } qint64 QHttpSocket::readData(char *data, qint64 maxlen) @@ -198,11 +198,11 @@ qint64 QHttpSocket::readData(char *data, qint64 maxlen) qint64 QHttpSocket::writeData(const char *data, qint64 len) { - // If the response headers have not yet been written, they - // must immediately be written before the data can be + // If the response headers have not yet been written, they must + // immediately be written before the data can be if(!d->headersWritten) { writeHeaders(); } - return d->device->write(data, len); + return d->socket->write(data, len); } diff --git a/src/core/qhttpsocket.h b/src/core/qhttpsocket.h index 51ec382..3b4f383 100644 --- a/src/core/qhttpsocket.h +++ b/src/core/qhttpsocket.h @@ -25,8 +25,8 @@ #ifndef QHTTPENGINE_QHTTPSOCKET_H #define QHTTPENGINE_QHTTPSOCKET_H -#include #include +#include #include "../util/qibytearray.h" #include "config.h" @@ -37,7 +37,7 @@ class QHTTPENGINE_EXPORT QHttpSocketPrivate; * @brief Implementation of the HTTP protocol * * QHttpSocket provides a class derived from QIODevice that can be used to - * read data from and write data to an HTTP client through a QIODevice + * read data from and write data to an HTTP client through a QTcpSocket * provided in the constructor. * * Once the headersParsedChanged() signal is emitted, information about the @@ -60,12 +60,13 @@ class QHTTPENGINE_EXPORT QHttpSocket : public QIODevice public: /** - * @brief Create a new QHttpSocket from a QIODevice + * @brief Create a new QHttpSocket from a QTcpSocket * - * It is assumed that the device is already opened for reading and - * writing. + * This instance will assume ownership of the socket. That is, it will + * make itself the parent of the socket. It is assumed that the socket + * is already opened for reading and writing. */ - QHttpSocket(QIODevice *device, QObject *parent = 0); + QHttpSocket(QTcpSocket *socket, QObject *parent = 0); /** * @brief Retrieve the number of bytes available for reading diff --git a/src/core/qhttpsocket_p.h b/src/core/qhttpsocket_p.h index c42ef20..bfd1134 100644 --- a/src/core/qhttpsocket_p.h +++ b/src/core/qhttpsocket_p.h @@ -25,8 +25,8 @@ #ifndef QHTTPENGINE_QHTTPSOCKETPRIVATE_H #define QHTTPENGINE_QHTTPSOCKETPRIVATE_H -#include #include +#include #include "../util/qhttpparser.h" #include "qhttpsocket.h" @@ -37,9 +37,9 @@ class QHttpSocketPrivate : public QObject public: - QHttpSocketPrivate(QHttpSocket *socket, QIODevice *baseDevice); + QHttpSocketPrivate(QHttpSocket *socket, QTcpSocket *baseSocket); - QIODevice *const device; + QTcpSocket *const socket; QByteArray buffer; QByteArray method; diff --git a/tests/TestQFilesystemHandler.cpp b/tests/TestQFilesystemHandler.cpp index 1d83220..0c52509 100644 --- a/tests/TestQFilesystemHandler.cpp +++ b/tests/TestQFilesystemHandler.cpp @@ -29,6 +29,7 @@ #include #include +#include "common/qsocketpair.h" #include "core/qhttpsocket.h" #include "handler/qfilesystemhandler.h" @@ -85,8 +86,10 @@ void TestQFilesystemHandler::testRequests() QFilesystemHandler handler(QDir(dir.path()).absoluteFilePath("root")); - QBuffer buffer; - QHttpSocket socket(&buffer); + QSocketPair pair; + QTRY_VERIFY(pair.isConnected()); + + QHttpSocket socket(pair.server()); QCOMPARE(handler.process(&socket, path), process); }