1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ 2 3 #ifndef HTTPHANDLER_H 4 #define HTTPHANDLER_H 5 6 #include "remote/i2-remote.hpp" 7 #include "remote/url.hpp" 8 #include "remote/httpserverconnection.hpp" 9 #include "remote/apiuser.hpp" 10 #include "base/registry.hpp" 11 #include "base/tlsstream.hpp" 12 #include <vector> 13 #include <boost/asio/spawn.hpp> 14 #include <boost/beast/http.hpp> 15 16 namespace icinga 17 { 18 19 /** 20 * HTTP handler. 21 * 22 * @ingroup remote 23 */ 24 class HttpHandler : public Object 25 { 26 public: 27 DECLARE_PTR_TYPEDEFS(HttpHandler); 28 29 virtual bool HandleRequest( 30 AsioTlsStream& stream, 31 const ApiUser::Ptr& user, 32 boost::beast::http::request<boost::beast::http::string_body>& request, 33 const Url::Ptr& url, 34 boost::beast::http::response<boost::beast::http::string_body>& response, 35 const Dictionary::Ptr& params, 36 boost::asio::yield_context& yc, 37 HttpServerConnection& server 38 ) = 0; 39 40 static void Register(const Url::Ptr& url, const HttpHandler::Ptr& handler); 41 static void ProcessRequest( 42 AsioTlsStream& stream, 43 const ApiUser::Ptr& user, 44 boost::beast::http::request<boost::beast::http::string_body>& request, 45 boost::beast::http::response<boost::beast::http::string_body>& response, 46 boost::asio::yield_context& yc, 47 HttpServerConnection& server 48 ); 49 50 private: 51 static Dictionary::Ptr m_UrlTree; 52 }; 53 54 /** 55 * Helper class for registering HTTP handlers. 56 * 57 * @ingroup remote 58 */ 59 class RegisterHttpHandler 60 { 61 public: 62 RegisterHttpHandler(const String& url, const HttpHandler& function); 63 }; 64 65 #define REGISTER_URLHANDLER(url, klass) \ 66 INITIALIZE_ONCE([]() { \ 67 Url::Ptr uurl = new Url(url); \ 68 HttpHandler::Ptr handler = new klass(); \ 69 HttpHandler::Register(uurl, handler); \ 70 }) 71 72 } 73 74 #endif /* HTTPHANDLER_H */ 75