2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-07-21 10:10:13 -04:00
|
|
|
|
|
|
|
|
#include "remote/httputility.hpp"
|
2019-02-14 11:27:17 -05:00
|
|
|
#include "remote/url.hpp"
|
2015-07-21 10:10:13 -04:00
|
|
|
#include "base/json.hpp"
|
2015-11-09 16:48:03 -05:00
|
|
|
#include "base/logger.hpp"
|
2018-12-21 05:52:37 -05:00
|
|
|
#include <map>
|
2019-02-14 11:27:17 -05:00
|
|
|
#include <string>
|
2018-12-21 05:52:37 -05:00
|
|
|
#include <vector>
|
2019-02-14 07:12:36 -05:00
|
|
|
#include <boost/beast/http.hpp>
|
2015-07-21 10:10:13 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2019-02-14 11:27:17 -05:00
|
|
|
Dictionary::Ptr HttpUtility::FetchRequestParameters(const Url::Ptr& url, const std::string& body)
|
2015-07-21 10:10:13 -04:00
|
|
|
{
|
|
|
|
|
Dictionary::Ptr result;
|
|
|
|
|
|
2019-02-14 11:27:17 -05:00
|
|
|
if (!body.empty()) {
|
2015-11-09 16:48:03 -05:00
|
|
|
Log(LogDebug, "HttpUtility")
|
2019-02-14 11:27:17 -05:00
|
|
|
<< "Request body: '" << body << '\'';
|
2018-10-09 06:55:53 -04:00
|
|
|
|
2015-07-21 10:10:13 -04:00
|
|
|
result = JsonDecode(body);
|
2015-11-09 16:48:03 -05:00
|
|
|
}
|
2015-07-21 10:10:13 -04:00
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
result = new Dictionary();
|
|
|
|
|
|
2018-12-21 05:52:37 -05:00
|
|
|
std::map<String, std::vector<String>> query;
|
2019-02-14 11:27:17 -05:00
|
|
|
for (const auto& kv : url->GetQuery()) {
|
2018-12-21 05:52:37 -05:00
|
|
|
query[kv.first].emplace_back(kv.second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& kv : query) {
|
2015-07-29 07:09:42 -04:00
|
|
|
result->Set(kv.first, Array::FromVector(kv.second));
|
2015-07-21 10:10:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-30 11:50:17 -04:00
|
|
|
Value HttpUtility::GetLastParameter(const Dictionary::Ptr& params, const String& key)
|
2015-07-29 07:39:58 -04:00
|
|
|
{
|
|
|
|
|
Value varr = params->Get(key);
|
|
|
|
|
|
|
|
|
|
if (!varr.IsObjectType<Array>())
|
|
|
|
|
return varr;
|
2015-07-21 10:10:13 -04:00
|
|
|
|
2015-07-29 07:39:58 -04:00
|
|
|
Array::Ptr arr = varr;
|
|
|
|
|
|
|
|
|
|
if (arr->GetLength() == 0)
|
2015-07-30 11:50:17 -04:00
|
|
|
return Empty;
|
2015-07-29 07:39:58 -04:00
|
|
|
else
|
|
|
|
|
return arr->Get(arr->GetLength() - 1);
|
|
|
|
|
}
|
2015-09-22 11:58:12 -04:00
|
|
|
|
2019-05-23 09:25:08 -04:00
|
|
|
void HttpUtility::SendJsonBody(boost::beast::http::response<boost::beast::http::string_body>& response, const Dictionary::Ptr& params, const Value& val)
|
2015-09-22 11:58:12 -04:00
|
|
|
{
|
2019-05-23 09:25:08 -04:00
|
|
|
namespace http = boost::beast::http;
|
2017-12-20 09:31:05 -05:00
|
|
|
|
2019-05-23 09:25:08 -04:00
|
|
|
response.set(http::field::content_type, "application/json");
|
|
|
|
|
response.body() = JsonEncode(val, params && GetLastParameter(params, "pretty"));
|
2020-12-22 08:32:56 -05:00
|
|
|
response.content_length(response.body().size());
|
2015-09-22 11:58:12 -04:00
|
|
|
}
|
|
|
|
|
|
2019-02-14 10:07:06 -05:00
|
|
|
void HttpUtility::SendJsonError(boost::beast::http::response<boost::beast::http::string_body>& response,
|
|
|
|
|
const Dictionary::Ptr& params, int code, const String& info, const String& diagnosticInformation)
|
|
|
|
|
{
|
|
|
|
|
Dictionary::Ptr result = new Dictionary({ { "error", code } });
|
|
|
|
|
|
|
|
|
|
if (!info.IsEmpty()) {
|
|
|
|
|
result->Set("status", info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (params && HttpUtility::GetLastParameter(params, "verbose") && !diagnosticInformation.IsEmpty()) {
|
|
|
|
|
result->Set("diagnostic_information", diagnosticInformation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.result(code);
|
|
|
|
|
|
|
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
|
|
|
|
}
|