HttpServerConnection: encapsulate streaming start indicator

This commit is contained in:
Alexander A. Klimov 2019-04-02 17:37:29 +02:00
parent 09a2e04f4b
commit c284cf0b68
34 changed files with 48 additions and 40 deletions

View File

@ -20,7 +20,7 @@ bool ActionsHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -20,7 +20,7 @@ bool ConfigFilesHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -18,7 +18,7 @@ bool ConfigPackagesHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
private:

View File

@ -19,7 +19,7 @@ bool ConfigStagesHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
private:

View File

@ -61,7 +61,7 @@ bool ConsoleHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -30,7 +30,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
static std::vector<String> GetAutocompletionSuggestions(const String& word, ScriptFrame& frame);

View File

@ -22,7 +22,7 @@ bool CreateObjectHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -22,7 +22,7 @@ bool DeleteObjectHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -25,7 +25,7 @@ bool EventsHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace asio = boost::asio;
@ -88,7 +88,7 @@ bool EventsHandler::HandleRequest(
EventQueue::UnregisterIfUnused(queueName, queue);
});
hasStartedStreaming = true;
server.StartStreaming();
response.result(http::status::ok);
response.set(http::field::content_type, "application/json");

View File

@ -22,7 +22,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -51,7 +51,7 @@ void HttpHandler::ProcessRequest(
boost::beast::http::request<boost::beast::http::string_body>& request,
boost::beast::http::response<boost::beast::http::string_body>& response,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
Dictionary::Ptr node = m_UrlTree;
@ -99,7 +99,7 @@ void HttpHandler::ProcessRequest(
bool processed = false;
for (const HttpHandler::Ptr& handler : handlers) {
if (handler->HandleRequest(stream, user, request, url, response, params, yc, hasStartedStreaming)) {
if (handler->HandleRequest(stream, user, request, url, response, params, yc, server)) {
processed = true;
break;
}

View File

@ -6,6 +6,7 @@
#include "remote/i2-remote.hpp"
#include "remote/url.hpp"
#include "remote/httpresponse.hpp"
#include "remote/httpserverconnection.hpp"
#include "remote/apiuser.hpp"
#include "base/registry.hpp"
#include "base/tlsstream.hpp"
@ -34,7 +35,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) = 0;
static void Register(const Url::Ptr& url, const HttpHandler::Ptr& handler);
@ -44,7 +45,7 @@ public:
boost::beast::http::request<boost::beast::http::string_body>& request,
boost::beast::http::response<boost::beast::http::string_body>& response,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
);
private:

View File

@ -31,7 +31,7 @@ using namespace icinga;
auto const l_ServerHeader ("Icinga/" + Application::GetAppVersion());
HttpServerConnection::HttpServerConnection(const String& identity, bool authenticated, const std::shared_ptr<AsioTlsStream>& stream)
: m_Stream(stream), m_Seen(Utility::GetTime()), m_IoStrand(stream->get_io_service()), m_ShuttingDown(false)
: m_Stream(stream), m_Seen(Utility::GetTime()), m_IoStrand(stream->get_io_service()), m_ShuttingDown(false), m_HasStartedStreaming(false)
{
if (authenticated) {
m_ApiUser = ApiUser::GetByClientCN(identity);
@ -91,6 +91,11 @@ void HttpServerConnection::Disconnect()
});
}
void HttpServerConnection::StartStreaming()
{
m_HasStartedStreaming = true;
}
static inline
bool EnsureValidHeaders(
AsioTlsStream& stream,
@ -375,17 +380,17 @@ bool ProcessRequest(
boost::beast::http::request<boost::beast::http::string_body>& request,
ApiUser::Ptr& authenticatedUser,
boost::beast::http::response<boost::beast::http::string_body>& response,
HttpServerConnection& server,
bool& hasStartedStreaming,
boost::asio::yield_context& yc
)
{
namespace http = boost::beast::http;
bool hasStartedStreaming = false;
try {
CpuBoundWork handlingRequest (yc);
HttpHandler::ProcessRequest(stream, authenticatedUser, request, response, yc, hasStartedStreaming);
HttpHandler::ProcessRequest(stream, authenticatedUser, request, response, yc, server);
} catch (const std::exception& ex) {
if (hasStartedStreaming) {
return false;
@ -481,7 +486,7 @@ void HttpServerConnection::ProcessMessages(boost::asio::yield_context yc)
m_Seen = std::numeric_limits<decltype(m_Seen)>::max();
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, yc)) {
if (!ProcessRequest(*m_Stream, request, authenticatedUser, response, *this, m_HasStartedStreaming, yc)) {
break;
}

View File

@ -27,6 +27,7 @@ public:
void Start();
void Disconnect();
void StartStreaming();
private:
ApiUser::Ptr m_ApiUser;
@ -35,6 +36,7 @@ private:
String m_PeerAddress;
boost::asio::io_service::strand m_IoStrand;
bool m_ShuttingDown;
bool m_HasStartedStreaming;
void ProcessMessages(boost::asio::yield_context yc);
void CheckLiveness(boost::asio::yield_context yc);

View File

@ -16,7 +16,7 @@ bool InfoHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -20,7 +20,7 @@ bool ModifyObjectHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -95,7 +95,7 @@ bool ObjectQueryHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
private:

View File

@ -76,7 +76,7 @@ bool StatusHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -83,7 +83,7 @@ bool TemplateQueryHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -54,7 +54,7 @@ bool TypeQueryHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};

View File

@ -64,7 +64,7 @@ bool VariableQueryHandler::HandleRequest(
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
)
{
namespace http = boost::beast::http;

View File

@ -21,7 +21,7 @@ public:
boost::beast::http::response<boost::beast::http::string_body>& response,
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
bool& hasStartedStreaming
HttpServerConnection& server
) override;
};