2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
#ifndef HTTPHANDLER_H
|
|
|
|
#define HTTPHANDLER_H
|
2014-04-12 04:21:09 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/i2-remote.hpp"
|
2019-02-14 17:27:17 +01:00
|
|
|
#include "remote/url.hpp"
|
2019-04-02 17:37:29 +02:00
|
|
|
#include "remote/httpserverconnection.hpp"
|
2015-07-09 15:27:14 +02:00
|
|
|
#include "remote/apiuser.hpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "base/registry.hpp"
|
2019-02-15 11:51:12 +01:00
|
|
|
#include "base/tlsstream.hpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include <vector>
|
2019-02-15 11:51:12 +01:00
|
|
|
#include <boost/asio/spawn.hpp>
|
2019-02-14 17:27:17 +01:00
|
|
|
#include <boost/beast/http.hpp>
|
2014-04-12 04:21:09 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2015-06-22 11:11:21 +02:00
|
|
|
* HTTP handler.
|
2014-04-12 04:21:09 +02:00
|
|
|
*
|
2014-05-03 20:02:22 +02:00
|
|
|
* @ingroup remote
|
2014-04-12 04:21:09 +02:00
|
|
|
*/
|
2017-12-31 07:22:16 +01:00
|
|
|
class HttpHandler : public Object
|
2014-04-12 04:21:09 +02:00
|
|
|
{
|
|
|
|
public:
|
2015-06-22 11:11:21 +02:00
|
|
|
DECLARE_PTR_TYPEDEFS(HttpHandler);
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2019-02-14 17:27:17 +01:00
|
|
|
virtual bool HandleRequest(
|
2019-02-15 11:51:12 +01:00
|
|
|
AsioTlsStream& stream,
|
2019-02-14 17:27:17 +01:00
|
|
|
const ApiUser::Ptr& user,
|
|
|
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
|
|
|
const Url::Ptr& url,
|
|
|
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
2019-02-15 11:51:12 +01:00
|
|
|
const Dictionary::Ptr& params,
|
|
|
|
boost::asio::yield_context& yc,
|
2019-04-02 17:37:29 +02:00
|
|
|
HttpServerConnection& server
|
2019-02-14 17:27:17 +01:00
|
|
|
) = 0;
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
static void Register(const Url::Ptr& url, const HttpHandler::Ptr& handler);
|
2019-02-14 17:27:17 +01:00
|
|
|
static void ProcessRequest(
|
2019-02-15 11:51:12 +01:00
|
|
|
AsioTlsStream& stream,
|
2019-02-14 17:27:17 +01:00
|
|
|
const ApiUser::Ptr& user,
|
|
|
|
boost::beast::http::request<boost::beast::http::string_body>& request,
|
2019-02-15 11:51:12 +01:00
|
|
|
boost::beast::http::response<boost::beast::http::string_body>& response,
|
|
|
|
boost::asio::yield_context& yc,
|
2019-04-02 17:37:29 +02:00
|
|
|
HttpServerConnection& server
|
2019-02-14 17:27:17 +01:00
|
|
|
);
|
2014-12-09 13:17:27 +01:00
|
|
|
|
2014-04-12 04:21:09 +02:00
|
|
|
private:
|
2015-06-22 11:11:21 +02:00
|
|
|
static Dictionary::Ptr m_UrlTree;
|
|
|
|
};
|
2015-02-26 12:41:47 +01:00
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
/**
|
|
|
|
* Helper class for registering HTTP handlers.
|
|
|
|
*
|
|
|
|
* @ingroup remote
|
|
|
|
*/
|
2017-12-31 07:22:16 +01:00
|
|
|
class RegisterHttpHandler
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
RegisterHttpHandler(const String& url, const HttpHandler& function);
|
2014-04-12 04:21:09 +02:00
|
|
|
};
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
#define REGISTER_URLHANDLER(url, klass) \
|
2016-08-27 09:35:08 +02:00
|
|
|
INITIALIZE_ONCE([]() { \
|
|
|
|
Url::Ptr uurl = new Url(url); \
|
|
|
|
HttpHandler::Ptr handler = new klass(); \
|
|
|
|
HttpHandler::Register(uurl, handler); \
|
|
|
|
})
|
2015-06-22 11:11:21 +02:00
|
|
|
|
2014-04-12 04:21:09 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
#endif /* HTTPHANDLER_H */
|