encapsulate HTTP response creation, add common headers
This commit is contained in:
parent
e81c06be61
commit
db57f48846
3 changed files with 54 additions and 27 deletions
|
@ -48,16 +48,11 @@ void ApiServer::answerClient(QTcpSocket* socket, const HttpRequest& request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
reply_message = QSL("HTTP/1.0 200 OK \r\n"
|
reply_message = generateHttpAnswer(200,
|
||||||
"Access-Control-Allow-Origin: *\r\n"
|
{{QSL("Access-Control-Allow-Origin"), QSL("*")},
|
||||||
"Access-Control-Allow-Headers: *\r\n"
|
{QSL("Access-Control-Allow-Headers"), QSL("*")},
|
||||||
"Content-Type: application/json; charset=\"utf-8\"\r\n"
|
{QSL("Content-Type"), QSL("application/json; charset=\"utf-8\"")}},
|
||||||
"Content-Length: %1"
|
json_data);
|
||||||
"\r\n\r\n")
|
|
||||||
.arg(QString::number(json_data.size()))
|
|
||||||
.toLocal8Bit();
|
|
||||||
|
|
||||||
reply_message += json_data;
|
|
||||||
|
|
||||||
#if !defined(NDEBUG)
|
#if !defined(NDEBUG)
|
||||||
IOFactory::writeFile("a.out", json_data);
|
IOFactory::writeFile("a.out", json_data);
|
||||||
|
@ -69,12 +64,10 @@ void ApiServer::answerClient(QTcpSocket* socket, const HttpRequest& request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray ApiServer::processCorsPreflight() const {
|
QByteArray ApiServer::processCorsPreflight() const {
|
||||||
QString answer = QSL("HTTP/1.0 204 No Content\r\n"
|
return generateHttpAnswer(204,
|
||||||
"Access-Control-Allow-Origin: *\r\n"
|
{{QSL("Access-Control-Allow-Origin"), QSL("*")},
|
||||||
"Access-Control-Allow-Headers: *\r\n"
|
{QSL("Access-Control-Allow-Headers"), QSL("*")},
|
||||||
"Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE\r\n\r\n");
|
{QSL("Access-Control-Allow-Methods"), QSL("POST, GET, OPTIONS, DELETE")}});
|
||||||
|
|
||||||
return answer.toLocal8Bit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray ApiServer::processHtmlPage() const {
|
QByteArray ApiServer::processHtmlPage() const {
|
||||||
|
@ -88,18 +81,14 @@ QByteArray ApiServer::processHtmlPage() const {
|
||||||
page = IOFactory::readFile(WEB_UI_FOLDER + QL1C('/') + WEB_UI_FILE);
|
page = IOFactory::readFile(WEB_UI_FOLDER + QL1C('/') + WEB_UI_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString answer = QSL("HTTP/1.0 200 OK\r\n"
|
QByteArray data = generateHttpAnswer(200,
|
||||||
"Access-Control-Allow-Origin: *\r\n"
|
{{QSL("Access-Control-Allow-Origin"), QSL("*")},
|
||||||
"Access-Control-Allow-Headers: *\r\n"
|
{QSL("Access-Control-Allow-Headers"), QSL("*")},
|
||||||
"Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE\r\n"
|
{QSL("Access-Control-Allow-Methods"), QSL("POST, GET, OPTIONS, DELETE")},
|
||||||
"Content-Type: text/html; charset=\"utf-8\"\r\n"
|
{QSL("Content-Type"), QSL("text/html; charset=\"utf-8\"")}},
|
||||||
"Content-Length: %1\r\n"
|
page);
|
||||||
"\r\n")
|
|
||||||
.arg(QString::number(page.size()));
|
|
||||||
|
|
||||||
QByteArray data = answer.toLocal8Bit();
|
return data;
|
||||||
|
|
||||||
return data + page;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiResponse ApiServer::processRequest(const ApiRequest& req) const {
|
ApiResponse ApiServer::processRequest(const ApiRequest& req) const {
|
||||||
|
|
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
#include "definitions/definitions.h"
|
#include "definitions/definitions.h"
|
||||||
|
|
||||||
|
#include <QDateTime>
|
||||||
|
|
||||||
HttpServer::HttpServer(QObject* parent) : QObject(parent), m_listenAddress(QHostAddress()), m_listenPort(0) {
|
HttpServer::HttpServer(QObject* parent) : QObject(parent), m_listenAddress(QHostAddress()), m_listenPort(0) {
|
||||||
connect(&m_httpServer, &QTcpServer::newConnection, this, &HttpServer::clientConnected);
|
connect(&m_httpServer, &QTcpServer::newConnection, this, &HttpServer::clientConnected);
|
||||||
|
|
||||||
|
@ -64,6 +66,35 @@ void HttpServer::setListenAddressPort(const QString& full_uri, bool start_handle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QByteArray HttpServer::generateHttpAnswer(int http_code,
|
||||||
|
const QList<HttpHeader>& headers,
|
||||||
|
const QByteArray& body) const {
|
||||||
|
QList<HttpHeader> my_headers = headers;
|
||||||
|
QByteArray answer = QSL("HTTP/1.0 %1 \r\n").arg(http_code).toLocal8Bit();
|
||||||
|
int body_length = body.size();
|
||||||
|
|
||||||
|
// Append body length.
|
||||||
|
if (body_length > 0) {
|
||||||
|
my_headers.append({QSL("Content-Length"), QString::number(body_length)});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append server ID and other common headers.
|
||||||
|
my_headers.append({QSL("Date"), QDateTime::currentDateTimeUtc().toString(Qt::DateFormat::RFC2822Date)});
|
||||||
|
my_headers.append({QSL("Server"), QSL(APP_LONG_NAME)});
|
||||||
|
|
||||||
|
for (const HttpHeader& header : my_headers) {
|
||||||
|
answer.append(QSL("%1: %2\r\n").arg(header.m_name, header.m_value).toLocal8Bit());
|
||||||
|
}
|
||||||
|
|
||||||
|
answer.append(QSL("\r\n").toLocal8Bit());
|
||||||
|
|
||||||
|
if (body_length > 0) {
|
||||||
|
answer.append(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
void HttpServer::clientConnected() {
|
void HttpServer::clientConnected() {
|
||||||
QTcpSocket* socket = m_httpServer.nextPendingConnection();
|
QTcpSocket* socket = m_httpServer.nextPendingConnection();
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,13 @@ class HttpServer : public QObject {
|
||||||
void setListenAddressPort(const QString& full_uri, bool start_handler);
|
void setListenAddressPort(const QString& full_uri, bool start_handler);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
struct HttpHeader {
|
||||||
|
QString m_name;
|
||||||
|
QString m_value;
|
||||||
|
};
|
||||||
|
|
||||||
|
QByteArray generateHttpAnswer(int http_code, const QList<HttpHeader>& headers, const QByteArray& body = {}) const;
|
||||||
|
|
||||||
struct HttpRequest {
|
struct HttpRequest {
|
||||||
bool readMethod(QTcpSocket* socket);
|
bool readMethod(QTcpSocket* socket);
|
||||||
bool readUrl(QTcpSocket* socket);
|
bool readUrl(QTcpSocket* socket);
|
||||||
|
|
Loading…
Add table
Reference in a new issue