mirror of https://github.com/Icinga/icinga2.git
Adjust /v1/events, too
This commit is contained in:
parent
1941c1da28
commit
fd239ba3fe
|
@ -13,11 +13,14 @@ using namespace icinga;
|
||||||
REGISTER_URLHANDLER("/v1/actions", ActionsHandler);
|
REGISTER_URLHANDLER("/v1/actions", ActionsHandler);
|
||||||
|
|
||||||
bool ActionsHandler::HandleRequest(
|
bool ActionsHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(ActionsHandler);
|
DECLARE_PTR_TYPEDEFS(ActionsHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,14 @@ using namespace icinga;
|
||||||
REGISTER_URLHANDLER("/v1/config/files", ConfigFilesHandler);
|
REGISTER_URLHANDLER("/v1/config/files", ConfigFilesHandler);
|
||||||
|
|
||||||
bool ConfigFilesHandler::HandleRequest(
|
bool ConfigFilesHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(ConfigFilesHandler);
|
DECLARE_PTR_TYPEDEFS(ConfigFilesHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,11 +11,14 @@ using namespace icinga;
|
||||||
REGISTER_URLHANDLER("/v1/config/packages", ConfigPackagesHandler);
|
REGISTER_URLHANDLER("/v1/config/packages", ConfigPackagesHandler);
|
||||||
|
|
||||||
bool ConfigPackagesHandler::HandleRequest(
|
bool ConfigPackagesHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(ConfigPackagesHandler);
|
DECLARE_PTR_TYPEDEFS(ConfigPackagesHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -12,11 +12,14 @@ using namespace icinga;
|
||||||
REGISTER_URLHANDLER("/v1/config/stages", ConfigStagesHandler);
|
REGISTER_URLHANDLER("/v1/config/stages", ConfigStagesHandler);
|
||||||
|
|
||||||
bool ConfigStagesHandler::HandleRequest(
|
bool ConfigStagesHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(ConfigStagesHandler);
|
DECLARE_PTR_TYPEDEFS(ConfigStagesHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -54,11 +54,14 @@ static void EnsureFrameCleanupTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ConsoleHandler::HandleRequest(
|
bool ConsoleHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -23,11 +23,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(ConsoleHandler);
|
DECLARE_PTR_TYPEDEFS(ConsoleHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
static std::vector<String> GetAutocompletionSuggestions(const String& word, ScriptFrame& frame);
|
static std::vector<String> GetAutocompletionSuggestions(const String& word, ScriptFrame& frame);
|
||||||
|
|
|
@ -15,11 +15,14 @@ using namespace icinga;
|
||||||
REGISTER_URLHANDLER("/v1/objects", CreateObjectHandler);
|
REGISTER_URLHANDLER("/v1/objects", CreateObjectHandler);
|
||||||
|
|
||||||
bool CreateObjectHandler::HandleRequest(
|
bool CreateObjectHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(CreateObjectHandler);
|
DECLARE_PTR_TYPEDEFS(CreateObjectHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,14 @@ using namespace icinga;
|
||||||
REGISTER_URLHANDLER("/v1/objects", DeleteObjectHandler);
|
REGISTER_URLHANDLER("/v1/objects", DeleteObjectHandler);
|
||||||
|
|
||||||
bool DeleteObjectHandler::HandleRequest(
|
bool DeleteObjectHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(DeleteObjectHandler);
|
DECLARE_PTR_TYPEDEFS(DeleteObjectHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,10 @@
|
||||||
#include "remote/filterutility.hpp"
|
#include "remote/filterutility.hpp"
|
||||||
#include "config/configcompiler.hpp"
|
#include "config/configcompiler.hpp"
|
||||||
#include "config/expression.hpp"
|
#include "config/expression.hpp"
|
||||||
|
#include "base/defer.hpp"
|
||||||
#include "base/objectlock.hpp"
|
#include "base/objectlock.hpp"
|
||||||
#include "base/json.hpp"
|
#include "base/json.hpp"
|
||||||
|
#include <boost/asio/buffer.hpp>
|
||||||
#include <boost/algorithm/string/replace.hpp>
|
#include <boost/algorithm/string/replace.hpp>
|
||||||
|
|
||||||
using namespace icinga;
|
using namespace icinga;
|
||||||
|
@ -14,13 +16,17 @@ using namespace icinga;
|
||||||
REGISTER_URLHANDLER("/v1/events", EventsHandler);
|
REGISTER_URLHANDLER("/v1/events", EventsHandler);
|
||||||
|
|
||||||
bool EventsHandler::HandleRequest(
|
bool EventsHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
namespace asio = boost::asio;
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
||||||
if (url->GetPath().size() != 2)
|
if (url->GetPath().size() != 2)
|
||||||
|
@ -75,18 +81,24 @@ bool EventsHandler::HandleRequest(
|
||||||
|
|
||||||
queue->AddClient(&request);
|
queue->AddClient(&request);
|
||||||
|
|
||||||
|
Defer removeClient ([&queue, &request, &queueName]() {
|
||||||
|
queue->RemoveClient(&request);
|
||||||
|
EventQueue::UnregisterIfUnused(queueName, queue);
|
||||||
|
});
|
||||||
|
|
||||||
|
hasStartedStreaming = true;
|
||||||
|
|
||||||
response.result(http::status::ok);
|
response.result(http::status::ok);
|
||||||
response.set(http::field::content_type, "application/json");
|
response.set(http::field::content_type, "application/json");
|
||||||
|
|
||||||
|
http::async_write(stream, response, yc);
|
||||||
|
stream.async_flush(yc);
|
||||||
|
|
||||||
|
asio::const_buffer newLine ("\n", 1);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
Dictionary::Ptr result = queue->WaitForEvent(&request);
|
Dictionary::Ptr result = queue->WaitForEvent(&request);
|
||||||
|
|
||||||
if (!response.IsPeerConnected()) {
|
|
||||||
queue->RemoveClient(&request);
|
|
||||||
EventQueue::UnregisterIfUnused(queueName, queue);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!result)
|
if (!result)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -94,14 +106,11 @@ bool EventsHandler::HandleRequest(
|
||||||
|
|
||||||
boost::algorithm::replace_all(body, "\n", "");
|
boost::algorithm::replace_all(body, "\n", "");
|
||||||
|
|
||||||
try {
|
asio::const_buffer payload (body.CStr(), body.GetLength());
|
||||||
response.WriteBody(body.CStr(), body.GetLength());
|
|
||||||
response.WriteBody("\n", 1);
|
stream.async_write_some(payload, yc);
|
||||||
} catch (const std::exception&) {
|
stream.async_write_some(newLine, yc);
|
||||||
queue->RemoveClient(&request);
|
stream.async_flush(yc);
|
||||||
EventQueue::UnregisterIfUnused(queueName, queue);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(EventsHandler);
|
DECLARE_PTR_TYPEDEFS(EventsHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -46,9 +46,12 @@ void HttpHandler::Register(const Url::Ptr& url, const HttpHandler::Ptr& handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpHandler::ProcessRequest(
|
void HttpHandler::ProcessRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
Dictionary::Ptr node = m_UrlTree;
|
Dictionary::Ptr node = m_UrlTree;
|
||||||
|
@ -96,7 +99,7 @@ void HttpHandler::ProcessRequest(
|
||||||
|
|
||||||
bool processed = false;
|
bool processed = false;
|
||||||
for (const HttpHandler::Ptr& handler : handlers) {
|
for (const HttpHandler::Ptr& handler : handlers) {
|
||||||
if (handler->HandleRequest(user, request, url, response, params)) {
|
if (handler->HandleRequest(stream, user, request, url, response, params, yc, hasStartedStreaming)) {
|
||||||
processed = true;
|
processed = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,9 @@
|
||||||
#include "remote/httpresponse.hpp"
|
#include "remote/httpresponse.hpp"
|
||||||
#include "remote/apiuser.hpp"
|
#include "remote/apiuser.hpp"
|
||||||
#include "base/registry.hpp"
|
#include "base/registry.hpp"
|
||||||
|
#include "base/tlsstream.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <boost/asio/spawn.hpp>
|
||||||
#include <boost/beast/http.hpp>
|
#include <boost/beast/http.hpp>
|
||||||
|
|
||||||
namespace icinga
|
namespace icinga
|
||||||
|
@ -25,18 +27,24 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(HttpHandler);
|
DECLARE_PTR_TYPEDEFS(HttpHandler);
|
||||||
|
|
||||||
virtual bool HandleRequest(
|
virtual bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) = 0;
|
) = 0;
|
||||||
|
|
||||||
static void Register(const Url::Ptr& url, const HttpHandler::Ptr& handler);
|
static void Register(const Url::Ptr& url, const HttpHandler::Ptr& handler);
|
||||||
static void ProcessRequest(
|
static void ProcessRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
);
|
);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -311,7 +311,7 @@ bool EnsureValidBody(
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
void ProcessRequest(
|
bool ProcessRequest(
|
||||||
AsioTlsStream& stream,
|
AsioTlsStream& stream,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
ApiUser::Ptr& authenticatedUser,
|
ApiUser::Ptr& authenticatedUser,
|
||||||
|
@ -321,11 +321,17 @@ void ProcessRequest(
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
||||||
|
bool hasStartedStreaming = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
CpuBoundWork handlingRequest (yc);
|
CpuBoundWork handlingRequest (yc);
|
||||||
|
|
||||||
HttpHandler::ProcessRequest(authenticatedUser, request, response);
|
HttpHandler::ProcessRequest(stream, authenticatedUser, request, response, yc, hasStartedStreaming);
|
||||||
} catch (const std::exception& ex) {
|
} catch (const std::exception& ex) {
|
||||||
|
if (hasStartedStreaming) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
http::response<http::string_body> response;
|
http::response<http::string_body> response;
|
||||||
|
|
||||||
HttpUtility::SendJsonError(response, nullptr, 500, "Unhandled exception" , DiagnosticInformation(ex));
|
HttpUtility::SendJsonError(response, nullptr, 500, "Unhandled exception" , DiagnosticInformation(ex));
|
||||||
|
@ -333,11 +339,17 @@ void ProcessRequest(
|
||||||
http::async_write(stream, response, yc);
|
http::async_write(stream, response, yc);
|
||||||
stream.async_flush(yc);
|
stream.async_flush(yc);
|
||||||
|
|
||||||
return;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasStartedStreaming) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
http::async_write(stream, response, yc);
|
http::async_write(stream, response, yc);
|
||||||
stream.async_flush(yc);
|
stream.async_flush(yc);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
|
void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
|
||||||
|
@ -419,7 +431,9 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcessRequest(*m_Stream, request, authenticatedUser, response, yc);
|
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, yc)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (request.version() != 11 || request[http::field::connection] == "close") {
|
if (request.version() != 11 || request[http::field::connection] == "close") {
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -9,11 +9,14 @@ using namespace icinga;
|
||||||
REGISTER_URLHANDLER("/", InfoHandler);
|
REGISTER_URLHANDLER("/", InfoHandler);
|
||||||
|
|
||||||
bool InfoHandler::HandleRequest(
|
bool InfoHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(InfoHandler);
|
DECLARE_PTR_TYPEDEFS(InfoHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,14 @@ using namespace icinga;
|
||||||
REGISTER_URLHANDLER("/v1/objects", ModifyObjectHandler);
|
REGISTER_URLHANDLER("/v1/objects", ModifyObjectHandler);
|
||||||
|
|
||||||
bool ModifyObjectHandler::HandleRequest(
|
bool ModifyObjectHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(ModifyObjectHandler);
|
DECLARE_PTR_TYPEDEFS(ModifyObjectHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -88,11 +88,14 @@ Dictionary::Ptr ObjectQueryHandler::SerializeObjectAttrs(const Object::Ptr& obje
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ObjectQueryHandler::HandleRequest(
|
bool ObjectQueryHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(ObjectQueryHandler);
|
DECLARE_PTR_TYPEDEFS(ObjectQueryHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -69,11 +69,14 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
bool StatusHandler::HandleRequest(
|
bool StatusHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(StatusHandler);
|
DECLARE_PTR_TYPEDEFS(StatusHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -76,11 +76,14 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
bool TemplateQueryHandler::HandleRequest(
|
bool TemplateQueryHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(TemplateQueryHandler);
|
DECLARE_PTR_TYPEDEFS(TemplateQueryHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -47,11 +47,14 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
bool TypeQueryHandler::HandleRequest(
|
bool TypeQueryHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(TypeQueryHandler);
|
DECLARE_PTR_TYPEDEFS(TypeQueryHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -57,11 +57,14 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
bool VariableQueryHandler::HandleRequest(
|
bool VariableQueryHandler::HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
namespace http = boost::beast::http;
|
namespace http = boost::beast::http;
|
||||||
|
|
|
@ -14,11 +14,14 @@ public:
|
||||||
DECLARE_PTR_TYPEDEFS(VariableQueryHandler);
|
DECLARE_PTR_TYPEDEFS(VariableQueryHandler);
|
||||||
|
|
||||||
bool HandleRequest(
|
bool HandleRequest(
|
||||||
|
AsioTlsStream& stream,
|
||||||
const ApiUser::Ptr& user,
|
const ApiUser::Ptr& user,
|
||||||
boost::beast::http::request<boost::beast::http::string_body>& request,
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
||||||
const Url::Ptr& url,
|
const Url::Ptr& url,
|
||||||
boost::beast::http::response<boost::beast::http::string_body>& response,
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
||||||
const Dictionary::Ptr& params
|
const Dictionary::Ptr& params,
|
||||||
|
boost::asio::yield_context& yc,
|
||||||
|
bool& hasStartedStreaming
|
||||||
) override;
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue