2022-06-22 12:54:22 +00:00
|
|
|
// Copyright (C) 2018 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2018-10-30 15:23:23 +00:00
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include <QtHttpServer>
|
|
|
|
|
|
|
|
static inline QString host(const QHttpServerRequest &request)
|
|
|
|
{
|
2022-07-29 12:51:39 +00:00
|
|
|
return QString::fromLatin1(request.value("Host"));
|
2018-10-30 15:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
|
|
|
QHttpServer httpServer;
|
|
|
|
httpServer.route("/", []() {
|
|
|
|
return "Hello world";
|
|
|
|
});
|
|
|
|
|
|
|
|
httpServer.route("/query", [] (const QHttpServerRequest &request) {
|
|
|
|
return QString("%1/query/").arg(host(request));
|
|
|
|
});
|
|
|
|
|
|
|
|
httpServer.route("/query/", [] (qint32 id, const QHttpServerRequest &request) {
|
|
|
|
return QString("%1/query/%2").arg(host(request)).arg(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpServer.route("/query/<arg>/log", [] (qint32 id, const QHttpServerRequest &request) {
|
|
|
|
return QString("%1/query/%2/log").arg(host(request)).arg(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpServer.route("/query/<arg>/log/", [] (qint32 id, float threshold,
|
|
|
|
const QHttpServerRequest &request) {
|
|
|
|
return QString("%1/query/%2/log/%3").arg(host(request)).arg(id).arg(threshold);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpServer.route("/user/", [] (const qint32 id) {
|
|
|
|
return QString("User %1").arg(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpServer.route("/user/<arg>/detail", [] (const qint32 id) {
|
|
|
|
return QString("User %1 detail").arg(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpServer.route("/user/<arg>/detail/", [] (const qint32 id, const qint32 year) {
|
|
|
|
return QString("User %1 detail year - %2").arg(id).arg(year);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpServer.route("/json/", [] {
|
|
|
|
return QJsonObject{
|
|
|
|
{
|
|
|
|
{"key1", "1"},
|
|
|
|
{"key2", "2"},
|
|
|
|
{"key3", "3"}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-04-27 06:54:53 +00:00
|
|
|
httpServer.route("/assets/<arg>", [] (const QUrl &url) {
|
|
|
|
return QHttpServerResponse::fromFile(QStringLiteral(":/assets/%1").arg(url.path()));
|
|
|
|
});
|
|
|
|
|
2019-06-14 05:43:06 +00:00
|
|
|
httpServer.route("/remote_address", [](const QHttpServerRequest &request) {
|
|
|
|
return request.remoteAddress().toString();
|
|
|
|
});
|
|
|
|
|
2022-03-29 12:59:50 +00:00
|
|
|
// Basic authentication example (RFC 7617)
|
|
|
|
httpServer.route("/auth", [](const QHttpServerRequest &request) {
|
|
|
|
auto auth = request.value("authorization").simplified();
|
|
|
|
|
|
|
|
if (auth.size() > 6 && auth.first(6).toLower() == "basic ") {
|
|
|
|
auto token = auth.sliced(6);
|
|
|
|
auto userPass = QByteArray::fromBase64(token);
|
|
|
|
|
|
|
|
if (auto colon = userPass.indexOf(':'); colon > 0) {
|
|
|
|
auto userId = userPass.first(colon);
|
|
|
|
auto password = userPass.sliced(colon + 1);
|
|
|
|
|
|
|
|
if (userId == "Aladdin" && password == "open sesame")
|
|
|
|
return QHttpServerResponse("text/plain", "Success\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QHttpServerResponse response("text/plain", "Authentication required\n",
|
|
|
|
QHttpServerResponse::StatusCode::Unauthorized);
|
|
|
|
response.setHeader("WWW-Authenticate", R"(Basic realm="Simple example", charset="UTF-8")");
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
|
2018-10-30 15:23:23 +00:00
|
|
|
const auto port = httpServer.listen(QHostAddress::Any);
|
2019-11-11 20:40:27 +00:00
|
|
|
if (!port) {
|
2018-10-30 15:23:23 +00:00
|
|
|
qDebug() << QCoreApplication::translate(
|
2019-11-11 20:40:27 +00:00
|
|
|
"QHttpServerExample", "Server failed to listen on a port.");
|
2018-10-30 15:23:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << QCoreApplication::translate(
|
|
|
|
"QHttpServerExample", "Running on http://127.0.0.1:%1/ (Press CTRL+C to quit)").arg(port);
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|