From f059e9bc668a8d20a7e0a220926c8afc53f4e1de Mon Sep 17 00:00:00 2001 From: Nathan Osman Date: Fri, 14 Oct 2016 14:14:06 -0700 Subject: [PATCH] Move JSON output functionality to socket. --- include/QHttpEngine/qhttpsocket.h | 6 ++++++ include/QHttpEngine/qobjecthandler.h | 5 ----- src/qhttpsocket.cpp | 11 +++++++++++ src/qobjecthandler.cpp | 9 --------- tests/TestQObjectHandler.cpp | 2 +- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/include/QHttpEngine/qhttpsocket.h b/include/QHttpEngine/qhttpsocket.h index 25e7fd5..f18de34 100644 --- a/include/QHttpEngine/qhttpsocket.h +++ b/include/QHttpEngine/qhttpsocket.h @@ -30,6 +30,7 @@ #include "qhttpengine_global.h" +class QJsonDocument; class QTcpSocket; class QHTTPENGINE_EXPORT QHttpSocketPrivate; @@ -289,6 +290,11 @@ public: */ 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: /** diff --git a/include/QHttpEngine/qobjecthandler.h b/include/QHttpEngine/qobjecthandler.h index 6f35a97..9c09a4c 100644 --- a/include/QHttpEngine/qobjecthandler.h +++ b/include/QHttpEngine/qobjecthandler.h @@ -157,11 +157,6 @@ public: */ 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: /** diff --git a/src/qhttpsocket.cpp b/src/qhttpsocket.cpp index 39f97b1..970678d 100644 --- a/src/qhttpsocket.cpp +++ b/src/qhttpsocket.cpp @@ -22,6 +22,7 @@ #include +#include #include #include @@ -325,6 +326,16 @@ void QHttpSocket::writeError(int statusCode, const QByteArray &statusReason) 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) { // Ensure the connection is in the correct state for reading data diff --git a/src/qobjecthandler.cpp b/src/qobjecthandler.cpp index 8565aed..f847590 100644 --- a/src/qobjecthandler.cpp +++ b/src/qobjecthandler.cpp @@ -90,15 +90,6 @@ bool QObjectHandler::readJson(QHttpSocket *socket, QJsonDocument &document) 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) { // Ensure the method has been registered diff --git a/tests/TestQObjectHandler.cpp b/tests/TestQObjectHandler.cpp index b1fcca1..c76aa54 100644 --- a/tests/TestQObjectHandler.cpp +++ b/tests/TestQObjectHandler.cpp @@ -45,7 +45,7 @@ public Q_SLOTS: void echo(QHttpSocket *socket) { QJsonDocument document; if (QObjectHandler::readJson(socket, document)) { - QObjectHandler::writeJson(socket, document); + socket->writeJson(document); } } };