Introduce QHttpServerResponse::fromFile()

This is a shortcut for static file contents.

Change-Id: I54d97c60822e661c1405f27ea8d4598f0363b144
Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Tasuku Suzuki 2019-04-27 15:54:53 +09:00
parent c88c462ac7
commit fc7908e628
12 changed files with 91 additions and 1 deletions

View File

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>assets/qt-logo.png</file>
</qresource>
</RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -104,6 +104,10 @@ int main(int argc, char *argv[])
};
});
httpServer.route("/assets/<arg>", [] (const QUrl &url) {
return QHttpServerResponse::fromFile(QStringLiteral(":/assets/%1").arg(url.path()));
});
const auto port = httpServer.listen(QHostAddress::Any);
if (port == -1) {
qDebug() << QCoreApplication::translate(

View File

@ -9,3 +9,6 @@ SOURCES += \
target.path = $$[QT_INSTALL_EXAMPLES]/httpserver/simple
INSTALLS += target
RESOURCES += \
assets.qrc

View File

@ -31,6 +31,7 @@
#include <private/qhttpserverresponse_p.h>
#include <QtCore/qfile.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qmimedatabase.h>
@ -85,6 +86,17 @@ QHttpServerResponse::~QHttpServerResponse()
{
}
QHttpServerResponse QHttpServerResponse::fromFile(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly))
return QHttpServerResponse(StatusCode::NotFound);
const QByteArray data = file.readAll();
file.close();
const QByteArray mimeType = QMimeDatabase().mimeTypeForFileNameAndData(fileName, data).name().toLocal8Bit();
return QHttpServerResponse(mimeType, data);
}
QHttpServerResponse::QHttpServerResponse(QHttpServerResponsePrivate *d)
: d_ptr(d)
{

View File

@ -62,6 +62,7 @@ public:
const QByteArray &data,
const StatusCode status = StatusCode::Ok);
virtual ~QHttpServerResponse();
static QHttpServerResponse fromFile(const QString &fileName);
QByteArray data() const;

View File

@ -0,0 +1 @@
{ "key": "value" }

View File

@ -0,0 +1 @@
<html></html>

View File

@ -3,3 +3,5 @@ TARGET = tst_qhttpserver
SOURCES += tst_qhttpserver.cpp
QT = httpserver httpserver-private testlib
TESTDATA += data/

View File

@ -192,6 +192,10 @@ void tst_QHttpServer::initTestCase()
return request.body();
});
httpserver.route("/file/", [] (const QString &file) {
return QHttpServerResponse::fromFile(QFINDTESTDATA(QLatin1String("data/") + file));
});
urlBase = QStringLiteral("http://localhost:%1%2").arg(httpserver.listen());
}
@ -344,6 +348,18 @@ void tst_QHttpServer::routeGet_data()
<< 200
<< "text/plain"
<< "Get";
QTest::addRow("response from html file")
<< "/file/text.html"
<< 200
<< "text/html"
<< "<html></html>";
QTest::addRow("response from json file")
<< "/file/application.json"
<< 200
<< "application/json"
<< "{ \"key\": \"value\" }";
}
void tst_QHttpServer::routeGet()
@ -361,7 +377,7 @@ void tst_QHttpServer::routeGet()
QCOMPARE(reply->header(QNetworkRequest::ContentTypeHeader), type);
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), code);
QCOMPARE(reply->readAll(), body);
QCOMPARE(reply->readAll().trimmed(), body);
}
void tst_QHttpServer::routeKeepAlive()

View File

@ -0,0 +1 @@
{ "key": "value" }

View File

@ -41,6 +41,8 @@ class tst_QHttpServerResponse : public QObject
private slots:
void mimeTypeDetection_data();
void mimeTypeDetection();
void mimeTypeDetectionFromFile_data();
void mimeTypeDetectionFromFile();
};
void tst_QHttpServerResponse::mimeTypeDetection_data()
@ -86,6 +88,48 @@ void tst_QHttpServerResponse::mimeTypeDetection()
QCOMPARE(response.mimeType(), mimeType);
}
void tst_QHttpServerResponse::mimeTypeDetectionFromFile_data()
{
QTest::addColumn<QString>("content");
QTest::addColumn<QByteArray>("mimeType");
QTest::addRow("application/x-zerosize")
<< QFINDTESTDATA("data/empty")
<< QByteArrayLiteral("application/x-zerosize");
QTest::addRow("text/plain")
<< QFINDTESTDATA("data/text.plain")
<< QByteArrayLiteral("text/plain");
QTest::addRow("text/html")
<< QFINDTESTDATA("data/text.html")
<< QByteArrayLiteral("text/html");
QTest::addRow("image/png")
<< QFINDTESTDATA("data/image.png")
<< QByteArrayLiteral("image/png");
QTest::addRow("image/jpeg")
<< QFINDTESTDATA("data/image.jpeg")
<< QByteArrayLiteral("image/jpeg");
QTest::addRow("image/svg+xml")
<< QFINDTESTDATA("data/image.svg")
<< QByteArrayLiteral("image/svg+xml");
QTest::addRow("application/json")
<< QFINDTESTDATA("data/application.json")
<< QByteArrayLiteral("application/json");
}
void tst_QHttpServerResponse::mimeTypeDetectionFromFile()
{
QFETCH(QString, content);
QFETCH(QByteArray, mimeType);
QCOMPARE(QHttpServerResponse::fromFile(content).mimeType(), mimeType);
}
QT_END_NAMESPACE
QTEST_MAIN(tst_QHttpServerResponse)