mirror of
https://github.com/QuasarApp/qthttpserver.git
synced 2025-04-26 18:34:31 +00:00
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:
parent
c88c462ac7
commit
fc7908e628
5
examples/httpserver/simple/assets.qrc
Normal file
5
examples/httpserver/simple/assets.qrc
Normal file
@ -0,0 +1,5 @@
|
||||
<RCC>
|
||||
<qresource prefix="/">
|
||||
<file>assets/qt-logo.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
BIN
examples/httpserver/simple/assets/qt-logo.png
Normal file
BIN
examples/httpserver/simple/assets/qt-logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.0 KiB |
@ -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(
|
||||
|
@ -9,3 +9,6 @@ SOURCES += \
|
||||
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/httpserver/simple
|
||||
INSTALLS += target
|
||||
|
||||
RESOURCES += \
|
||||
assets.qrc
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -62,6 +62,7 @@ public:
|
||||
const QByteArray &data,
|
||||
const StatusCode status = StatusCode::Ok);
|
||||
virtual ~QHttpServerResponse();
|
||||
static QHttpServerResponse fromFile(const QString &fileName);
|
||||
|
||||
QByteArray data() const;
|
||||
|
||||
|
1
tests/auto/qhttpserver/data/application.json
Normal file
1
tests/auto/qhttpserver/data/application.json
Normal file
@ -0,0 +1 @@
|
||||
{ "key": "value" }
|
1
tests/auto/qhttpserver/data/text.html
Normal file
1
tests/auto/qhttpserver/data/text.html
Normal file
@ -0,0 +1 @@
|
||||
<html></html>
|
@ -3,3 +3,5 @@ TARGET = tst_qhttpserver
|
||||
SOURCES += tst_qhttpserver.cpp
|
||||
|
||||
QT = httpserver httpserver-private testlib
|
||||
|
||||
TESTDATA += data/
|
||||
|
@ -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()
|
||||
|
1
tests/auto/qhttpserverresponse/data/application.json
Normal file
1
tests/auto/qhttpserverresponse/data/application.json
Normal file
@ -0,0 +1 @@
|
||||
{ "key": "value" }
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user