Move JSON output functionality to socket.
This commit is contained in:
@@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
#include "qhttpengine_global.h"
|
#include "qhttpengine_global.h"
|
||||||
|
|
||||||
|
class QJsonDocument;
|
||||||
class QTcpSocket;
|
class QTcpSocket;
|
||||||
class QHTTPENGINE_EXPORT QHttpSocketPrivate;
|
class QHTTPENGINE_EXPORT QHttpSocketPrivate;
|
||||||
|
|
||||||
@@ -289,6 +290,11 @@ public:
|
|||||||
*/
|
*/
|
||||||
void writeError(int statusCode, const QByteArray &statusReason = QByteArray());
|
void writeError(int statusCode, const QByteArray &statusReason = QByteArray());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Write the specified JSON document to the socket and close it
|
||||||
|
*/
|
||||||
|
void writeJson(const QJsonDocument &document, int statusCode = OK);
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -157,11 +157,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
static bool readJson(QHttpSocket *socket, QJsonDocument &document);
|
static bool readJson(QHttpSocket *socket, QJsonDocument &document);
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Write the response to the socket as a JSON document
|
|
||||||
*/
|
|
||||||
static void writeJson(QHttpSocket *socket, const QJsonDocument &document);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
#include <QJsonDocument>
|
||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
|
|
||||||
#include <QHttpEngine/QHttpParser>
|
#include <QHttpEngine/QHttpParser>
|
||||||
@@ -325,6 +326,16 @@ void QHttpSocket::writeError(int statusCode, const QByteArray &statusReason)
|
|||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QHttpSocket::writeJson(const QJsonDocument &document, int statusCode)
|
||||||
|
{
|
||||||
|
QByteArray data = document.toJson();
|
||||||
|
setStatusCode(statusCode);
|
||||||
|
setHeader("Content-Length", QByteArray::number(data.length()));
|
||||||
|
setHeader("Content-Type", "application/json");
|
||||||
|
write(data);
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
qint64 QHttpSocket::readData(char *data, qint64 maxlen)
|
qint64 QHttpSocket::readData(char *data, qint64 maxlen)
|
||||||
{
|
{
|
||||||
// Ensure the connection is in the correct state for reading data
|
// Ensure the connection is in the correct state for reading data
|
||||||
|
|||||||
@@ -90,15 +90,6 @@ bool QObjectHandler::readJson(QHttpSocket *socket, QJsonDocument &document)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QObjectHandler::writeJson(QHttpSocket *socket, const QJsonDocument &document)
|
|
||||||
{
|
|
||||||
QByteArray data = document.toJson();
|
|
||||||
socket->setHeader("Content-Length", QByteArray::number(data.length()));
|
|
||||||
socket->setHeader("Content-Type", "application/json");
|
|
||||||
socket->write(data);
|
|
||||||
socket->close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void QObjectHandler::process(QHttpSocket *socket, const QString &path)
|
void QObjectHandler::process(QHttpSocket *socket, const QString &path)
|
||||||
{
|
{
|
||||||
// Ensure the method has been registered
|
// Ensure the method has been registered
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public Q_SLOTS:
|
|||||||
void echo(QHttpSocket *socket) {
|
void echo(QHttpSocket *socket) {
|
||||||
QJsonDocument document;
|
QJsonDocument document;
|
||||||
if (QObjectHandler::readJson(socket, document)) {
|
if (QObjectHandler::readJson(socket, document)) {
|
||||||
QObjectHandler::writeJson(socket, document);
|
socket->writeJson(document);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user