|
|
|
|
@@ -4,6 +4,7 @@
|
|
|
|
|
|
|
|
|
|
#ifdef HAS_QTHTTPSERVER
|
|
|
|
|
#include <QTcpServer>
|
|
|
|
|
#include <QSslServer>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QEventLoop>
|
|
|
|
|
#include <QUuid>
|
|
|
|
|
@@ -77,21 +78,32 @@ bool HttpServer::checkPin(const QHttpServerRequest& request, const QHostAddress&
|
|
|
|
|
|
|
|
|
|
bool HttpServer::start(quint16 port, bool https)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(https)
|
|
|
|
|
|
|
|
|
|
if (m_tcpServer && m_tcpServer->isListening()) {
|
|
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_port = port;
|
|
|
|
|
|
|
|
|
|
m_tcpServer = new QTcpServer(this);
|
|
|
|
|
if (!m_tcpServer->listen(QHostAddress::Any, port)) {
|
|
|
|
|
delete m_tcpServer;
|
|
|
|
|
m_tcpServer = nullptr;
|
|
|
|
|
return false;
|
|
|
|
|
if (https && !m_sslConfig.isNull()) {
|
|
|
|
|
auto* sslServer = new QSslServer(this);
|
|
|
|
|
sslServer->setSslConfiguration(m_sslConfig);
|
|
|
|
|
if (!sslServer->listen(QHostAddress::Any, port)) {
|
|
|
|
|
delete sslServer;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_tcpServer = sslServer;
|
|
|
|
|
m_server->bind(m_tcpServer);
|
|
|
|
|
qDebug() << "[HttpServer] Listening on HTTPS port:" << port;
|
|
|
|
|
} else {
|
|
|
|
|
m_tcpServer = new QTcpServer(this);
|
|
|
|
|
if (!m_tcpServer->listen(QHostAddress::Any, port)) {
|
|
|
|
|
delete m_tcpServer;
|
|
|
|
|
m_tcpServer = nullptr;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_server->bind(m_tcpServer);
|
|
|
|
|
qDebug() << "[HttpServer] Listening on HTTP port:" << port;
|
|
|
|
|
}
|
|
|
|
|
m_server->bind(m_tcpServer);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
@@ -165,30 +177,33 @@ void HttpServer::respondToPrepareUpload(const QString& sessionId, bool accepted,
|
|
|
|
|
|
|
|
|
|
void HttpServer::setupRoutes()
|
|
|
|
|
{
|
|
|
|
|
m_server->route(ApiRoute::INFO, QHttpServerRequest::Method::Get,
|
|
|
|
|
[this](const QHttpServerRequest&) {
|
|
|
|
|
return handleInfoRequest();
|
|
|
|
|
});
|
|
|
|
|
auto infoHandler = [this](const QHttpServerRequest&) {
|
|
|
|
|
return handleInfoRequest();
|
|
|
|
|
};
|
|
|
|
|
auto registerHandler = [this](const QHttpServerRequest& request) {
|
|
|
|
|
return handleRegisterRequest(request, request.remoteAddress());
|
|
|
|
|
};
|
|
|
|
|
auto prepareUploadHandler = [this](const QHttpServerRequest& request) {
|
|
|
|
|
return handlePrepareUploadRequest(request);
|
|
|
|
|
};
|
|
|
|
|
auto uploadHandler = [this](const QHttpServerRequest& request) {
|
|
|
|
|
return handleUploadRequest(request);
|
|
|
|
|
};
|
|
|
|
|
auto cancelHandler = [this](const QHttpServerRequest& request) {
|
|
|
|
|
return handleCancelRequest(request);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
m_server->route(ApiRoute::REGISTER, QHttpServerRequest::Method::Post,
|
|
|
|
|
[this](const QHttpServerRequest& request) {
|
|
|
|
|
return handleRegisterRequest(request, request.remoteAddress());
|
|
|
|
|
});
|
|
|
|
|
m_server->route(ApiRoute::INFO, QHttpServerRequest::Method::Get, infoHandler);
|
|
|
|
|
m_server->route(ApiRoute::REGISTER, QHttpServerRequest::Method::Post, registerHandler);
|
|
|
|
|
m_server->route(ApiRoute::PREPARE_UPLOAD, QHttpServerRequest::Method::Post, prepareUploadHandler);
|
|
|
|
|
m_server->route(ApiRoute::UPLOAD, QHttpServerRequest::Method::Post, uploadHandler);
|
|
|
|
|
m_server->route(ApiRoute::CANCEL, QHttpServerRequest::Method::Post, cancelHandler);
|
|
|
|
|
|
|
|
|
|
m_server->route(ApiRoute::PREPARE_UPLOAD, QHttpServerRequest::Method::Post,
|
|
|
|
|
[this](const QHttpServerRequest& request) {
|
|
|
|
|
return handlePrepareUploadRequest(request);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_server->route(ApiRoute::UPLOAD, QHttpServerRequest::Method::Post,
|
|
|
|
|
[this](const QHttpServerRequest& request) {
|
|
|
|
|
return handleUploadRequest(request);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_server->route(ApiRoute::CANCEL, QHttpServerRequest::Method::Post,
|
|
|
|
|
[this](const QHttpServerRequest& request) {
|
|
|
|
|
return handleCancelRequest(request);
|
|
|
|
|
});
|
|
|
|
|
m_server->route(ApiRoute::V1_INFO, QHttpServerRequest::Method::Get, infoHandler);
|
|
|
|
|
m_server->route(ApiRoute::V1_REGISTER, QHttpServerRequest::Method::Post, registerHandler);
|
|
|
|
|
m_server->route(ApiRoute::V1_PREPARE_UPLOAD, QHttpServerRequest::Method::Post, prepareUploadHandler);
|
|
|
|
|
m_server->route(ApiRoute::V1_UPLOAD, QHttpServerRequest::Method::Post, uploadHandler);
|
|
|
|
|
m_server->route(ApiRoute::V1_CANCEL, QHttpServerRequest::Method::Post, cancelHandler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QHttpServerResponse HttpServer::handleInfoRequest()
|
|
|
|
|
|