From: Alexander A. Klimov Date: Thu, 14 Feb 2019 12:12:36 +0000 (+0100) Subject: Add HttpUtility::SendJsonBody() overload for Boost/Beast X-Git-Tag: v2.11.0-rc1~174^2~56 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fc22cbaf096d6031abe9fab2fee62c30b521a9e9;p=icinga2 Add HttpUtility::SendJsonBody() overload for Boost/Beast --- diff --git a/lib/remote/httputility.cpp b/lib/remote/httputility.cpp index 043f3cf9e..f7fef7713 100644 --- a/lib/remote/httputility.cpp +++ b/lib/remote/httputility.cpp @@ -5,6 +5,7 @@ #include "base/logger.hpp" #include #include +#include using namespace icinga; @@ -55,6 +56,15 @@ void HttpUtility::SendJsonBody(HttpResponse& response, const Dictionary::Ptr& pa response.WriteBody(body.CStr(), body.GetLength()); } +void HttpUtility::SendJsonBody(boost::beast::http::response& response, const Dictionary::Ptr& params, const Value& val) +{ + namespace http = boost::beast::http; + + response.set(http::field::content_type, "application/json"); + response.body() = JsonEncode(val, params && GetLastParameter(params, "pretty")); + response.set(http::field::content_length, response.body().size()); +} + Value HttpUtility::GetLastParameter(const Dictionary::Ptr& params, const String& key) { Value varr = params->Get(key); diff --git a/lib/remote/httputility.hpp b/lib/remote/httputility.hpp index 0a0418161..e758b2663 100644 --- a/lib/remote/httputility.hpp +++ b/lib/remote/httputility.hpp @@ -6,6 +6,7 @@ #include "remote/httprequest.hpp" #include "remote/httpresponse.hpp" #include "base/dictionary.hpp" +#include namespace icinga { @@ -21,6 +22,7 @@ class HttpUtility public: static Dictionary::Ptr FetchRequestParameters(HttpRequest& request); static void SendJsonBody(HttpResponse& response, const Dictionary::Ptr& params, const Value& val); + static void SendJsonBody(boost::beast::http::response& response, const Dictionary::Ptr& params, const Value& val); static Value GetLastParameter(const Dictionary::Ptr& params, const String& key); static void SendJsonError(HttpResponse& response, const Dictionary::Ptr& params, const int code, const String& verbose = String(), const String& diagnosticInformation = String());