diff --git a/lib/remote/httputility.cpp b/lib/remote/httputility.cpp index b53a8721b..4680cca64 100644 --- a/lib/remote/httputility.cpp +++ b/lib/remote/httputility.cpp @@ -52,6 +52,28 @@ Value HttpUtility::GetLastParameter(const Dictionary::Ptr& params, const String& return arr->Get(arr->GetLength() - 1); } +/** + * Stream a JSON-encoded body to the client. + * + * This function sets the Content-Type header to "application/json", starts the streaming of the response, + * and encodes the given value as JSON to the client. If pretty-print is requested, the JSON output will be + * formatted accordingly. It is assumed that the response status code and other necessary headers have already + * been set. + * + * @param response The HTTP response to send the body to. + * @param params The request parameters. + * @param val The value to encode as JSON and stream to the client. + * @param yc The yield context to use for asynchronous operations. + */ +void HttpUtility::SendJsonBody(HttpResponse& response, const Dictionary::Ptr& params, const Value& val, boost::asio::yield_context& yc) +{ + namespace http = boost::beast::http; + + response.set(http::field::content_type, "application/json"); + response.StartStreaming(); + response.GetJsonEncoder(params && GetLastParameter(params, "pretty")).Encode(val, &yc); +} + void HttpUtility::SendJsonBody(HttpResponse& response, const Dictionary::Ptr& params, const Value& val) { namespace http = boost::beast::http; diff --git a/lib/remote/httputility.hpp b/lib/remote/httputility.hpp index 6f6427713..17f7b1ce4 100644 --- a/lib/remote/httputility.hpp +++ b/lib/remote/httputility.hpp @@ -6,6 +6,7 @@ #include "remote/url.hpp" #include "base/dictionary.hpp" #include "remote/httpmessage.hpp" +#include #include namespace icinga @@ -23,6 +24,7 @@ public: static Dictionary::Ptr FetchRequestParameters(const Url::Ptr& url, const std::string& body); static Value GetLastParameter(const Dictionary::Ptr& params, const String& key); + static void SendJsonBody(HttpResponse& response, const Dictionary::Ptr& params, const Value& val, boost::asio::yield_context& yc); static void SendJsonBody(HttpResponse& response, const Dictionary::Ptr& params, const Value& val); static void SendJsonError(HttpResponse& response, const Dictionary::Ptr& params, const int code, const String& info = {}, const String& diagnosticInformation = {}); diff --git a/lib/remote/objectqueryhandler.cpp b/lib/remote/objectqueryhandler.cpp index 84520e7b4..acb904b6c 100644 --- a/lib/remote/objectqueryhandler.cpp +++ b/lib/remote/objectqueryhandler.cpp @@ -318,15 +318,11 @@ bool ObjectQueryHandler::HandleRequest( return new Dictionary{std::move(result1)}; }; - response.result(http::status::ok); - response.set(http::field::content_type, "application/json"); - response.StartStreaming(); - Dictionary::Ptr results = new Dictionary{{"results", new ValueGenerator{objs, generatorFunc}}}; results->Freeze(); - bool pretty = HttpUtility::GetLastParameter(params, "pretty"); - response.GetJsonEncoder(pretty).Encode(results, &yc); + response.result(http::status::ok); + HttpUtility::SendJsonBody(response, params, results, yc); return true; }