diff --git a/src/handler/qfilesystemhandler.cpp b/src/handler/qfilesystemhandler.cpp index b133d94..a4684be 100644 --- a/src/handler/qfilesystemhandler.cpp +++ b/src/handler/qfilesystemhandler.cpp @@ -36,13 +36,13 @@ QFilesystemHandlerPrivate::QFilesystemHandlerPrivate(QFilesystemHandler *handler { } -QString QFilesystemHandlerPrivate::absolutePath(const QString &path) +bool QFilesystemHandlerPrivate::absolutePath(const QString &path, QString &absolutePath) { // Clean the path and make it absolute - QString absolutePath = QDir(root.absoluteFilePath(path)).canonicalPath(); + absolutePath = QDir(root.absoluteFilePath(path)).canonicalPath(); // Ensure that the absolute path is within the root - return absolutePath.startsWith(root.absolutePath()) ? absolutePath : QString(); + return absolutePath.startsWith(root.absolutePath()); } QByteArray QFilesystemHandlerPrivate::mimeType(const QString &path) @@ -68,8 +68,8 @@ QFilesystemHandler::QFilesystemHandler(const QString &root, QObject *parent) bool QFilesystemHandler::process(QHttpSocket *socket, const QString &path) { // Attempt to retrieve the absolute path - QString absolutePath = d->absolutePath(path); - if(absolutePath.isNull()) { + QString absolutePath; + if(!d->absolutePath(path, absolutePath)) { return false; } @@ -82,13 +82,14 @@ bool QFilesystemHandler::process(QHttpSocket *socket, const QString &path) // Create a QIODeviceCopier to copy the file contents to the socket QIODeviceCopier *copier = new QIODeviceCopier(file, socket); - connect(copier, SIGNAL(finished()), file, SLOT(deleteLater())); connect(copier, SIGNAL(finished()), copier, SLOT(deleteLater())); + connect(copier, SIGNAL(finished()), file, SLOT(deleteLater())); connect(copier, SIGNAL(finished()), socket, SLOT(deleteLater())); // Set the mimetype and contentlength socket->setHeader("Content-Type", d->mimeType(absolutePath)); socket->setHeader("Content-Length", QByteArray::number(file->size())); + socket->writeHeaders(); // Start the copy and indicate success copier->start(); diff --git a/src/handler/qfilesystemhandler_p.h b/src/handler/qfilesystemhandler_p.h index ccbb475..a99c723 100644 --- a/src/handler/qfilesystemhandler_p.h +++ b/src/handler/qfilesystemhandler_p.h @@ -38,7 +38,7 @@ public: QFilesystemHandlerPrivate(QFilesystemHandler *handler, const QString &root); - QString absolutePath(const QString &path); + bool absolutePath(const QString &path, QString &absolutePath); QByteArray mimeType(const QString &path); QDir root; diff --git a/tests/handler/TestQFilesystemHandler.cpp b/tests/handler/TestQFilesystemHandler.cpp index 8e81ffc..aed3fa7 100644 --- a/tests/handler/TestQFilesystemHandler.cpp +++ b/tests/handler/TestQFilesystemHandler.cpp @@ -22,18 +22,19 @@ * IN THE SOFTWARE. */ -#include #include #include #include +#include #include #include +#include "common/qsimplehttpclient.h" #include "common/qsocketpair.h" #include "core/qhttpsocket.h" #include "handler/qfilesystemhandler.h" -const QByteArray TestData = "test"; +const QByteArray Data = "test"; class TestQFilesystemHandler : public QObject { @@ -63,37 +64,44 @@ void TestQFilesystemHandler::initTestCase() void TestQFilesystemHandler::testRequests_data() { + QTest::addColumn("success"); QTest::addColumn("path"); - QTest::addColumn("process"); - - QTest::newRow("outside document root") - << "../outside" - << false; - - QTest::newRow("inside document root") - << "inside" - << true; QTest::newRow("nonexistent resource") - << "nonexistent" - << false; + << false + << "nonexistent"; + + QTest::newRow("outside document root") + << false + << "../outside"; + + QTest::newRow("inside document root") + << true + << "inside"; } void TestQFilesystemHandler::testRequests() { - /* + QFETCH(bool, success); QFETCH(QString, path); - QFETCH(bool, process); QFilesystemHandler handler(QDir(dir.path()).absoluteFilePath("root")); QSocketPair pair; QTRY_VERIFY(pair.isConnected()); - QHttpSocket socket(pair.server()); + QSimpleHttpClient client(pair.client()); + QHttpSocket *socket = new QHttpSocket(pair.server(), &pair); - QCOMPARE(handler.process(&socket, path), process); - */ + QCOMPARE(handler.process(socket, path), success); + + if(success) { + QSignalSpy destroyedSpy(socket, SIGNAL(destroyed())); + + QTRY_COMPARE(client.statusCode(), 200); + QTRY_COMPARE(client.data(), Data); + QTRY_COMPARE(destroyedSpy.count(), 1); + } } bool TestQFilesystemHandler::createFile(const QString &path) @@ -103,7 +111,7 @@ bool TestQFilesystemHandler::createFile(const QString &path) return false; } - file.write(TestData); + file.write(Data); return true; }