2015-06-22 11:11:21 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2015-06-22 11:11:21 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "remote/httphandler.hpp"
|
2015-09-22 17:58:12 +02:00
|
|
|
#include "remote/httputility.hpp"
|
2015-06-22 11:11:21 +02:00
|
|
|
#include "base/singleton.hpp"
|
2016-05-10 15:16:35 +02:00
|
|
|
#include "base/exception.hpp"
|
2015-09-22 17:58:12 +02:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
Dictionary::Ptr HttpHandler::m_UrlTree;
|
|
|
|
|
|
|
|
void HttpHandler::Register(const Url::Ptr& url, const HttpHandler::Ptr& handler)
|
|
|
|
{
|
|
|
|
if (!m_UrlTree)
|
|
|
|
m_UrlTree = new Dictionary();
|
|
|
|
|
|
|
|
Dictionary::Ptr node = m_UrlTree;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& elem : url->GetPath()) {
|
2015-06-22 11:11:21 +02:00
|
|
|
Dictionary::Ptr children = node->Get("children");
|
|
|
|
|
|
|
|
if (!children) {
|
|
|
|
children = new Dictionary();
|
|
|
|
node->Set("children", children);
|
|
|
|
}
|
|
|
|
|
2015-07-22 12:23:56 +02:00
|
|
|
Dictionary::Ptr sub_node = children->Get(elem);
|
|
|
|
if (!sub_node) {
|
|
|
|
sub_node = new Dictionary();
|
|
|
|
children->Set(elem, sub_node);
|
|
|
|
}
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
node = sub_node;
|
|
|
|
}
|
|
|
|
|
2015-07-28 13:57:59 +02:00
|
|
|
Array::Ptr handlers = node->Get("handlers");
|
2015-06-22 11:11:21 +02:00
|
|
|
|
2015-07-28 13:57:59 +02:00
|
|
|
if (!handlers) {
|
|
|
|
handlers = new Array();
|
|
|
|
node->Set("handlers", handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers->Add(handler);
|
2015-06-22 11:11:21 +02:00
|
|
|
}
|
|
|
|
|
2015-07-09 15:27:14 +02:00
|
|
|
void HttpHandler::ProcessRequest(const ApiUser::Ptr& user, HttpRequest& request, HttpResponse& response)
|
2015-06-22 11:11:21 +02:00
|
|
|
{
|
|
|
|
Dictionary::Ptr node = m_UrlTree;
|
2015-07-28 13:57:59 +02:00
|
|
|
std::vector<HttpHandler::Ptr> handlers;
|
2015-08-11 09:31:28 +02:00
|
|
|
const std::vector<String>& path = request.RequestUrl->GetPath();
|
2015-06-22 11:11:21 +02:00
|
|
|
|
2016-08-24 19:59:13 +02:00
|
|
|
for (std::vector<String>::size_type i = 0; i <= path.size(); i++) {
|
2015-07-28 13:57:59 +02:00
|
|
|
Array::Ptr current_handlers = node->Get("handlers");
|
|
|
|
|
|
|
|
if (current_handlers) {
|
|
|
|
ObjectLock olock(current_handlers);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const HttpHandler::Ptr current_handler : current_handlers) {
|
2015-07-28 13:57:59 +02:00
|
|
|
handlers.push_back(current_handler);
|
|
|
|
}
|
|
|
|
}
|
2015-06-22 11:11:21 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr children = node->Get("children");
|
|
|
|
|
|
|
|
if (!children) {
|
|
|
|
node.reset();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-11 09:31:28 +02:00
|
|
|
if (i == path.size())
|
|
|
|
break;
|
|
|
|
|
|
|
|
node = children->Get(path[i]);
|
2015-06-22 11:11:21 +02:00
|
|
|
|
2015-07-28 13:57:59 +02:00
|
|
|
if (!node)
|
2015-06-22 11:11:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-28 13:57:59 +02:00
|
|
|
std::reverse(handlers.begin(), handlers.end());
|
2015-06-22 11:11:21 +02:00
|
|
|
|
2016-05-10 15:16:35 +02:00
|
|
|
Dictionary::Ptr params;
|
|
|
|
|
|
|
|
try {
|
|
|
|
params = HttpUtility::FetchRequestParameters(request);
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
HttpUtility::SendJsonError(response, 400, "Invalid request body: " + DiagnosticInformation(ex, false));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-28 13:57:59 +02:00
|
|
|
bool processed = false;
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const HttpHandler::Ptr& handler : handlers) {
|
2016-05-10 15:16:35 +02:00
|
|
|
if (handler->HandleRequest(user, request, response, params)) {
|
2015-07-28 13:57:59 +02:00
|
|
|
processed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-10 15:16:35 +02:00
|
|
|
|
2015-07-28 13:57:59 +02:00
|
|
|
if (!processed) {
|
2015-09-22 17:58:12 +02:00
|
|
|
String path = boost::algorithm::join(request.RequestUrl->GetPath(), "/");
|
2015-09-28 16:08:14 +02:00
|
|
|
HttpUtility::SendJsonError(response, 404, "The requested path '" + path +
|
2015-11-09 10:40:31 +01:00
|
|
|
"' could not be found or the request method is not valid for this path.");
|
2015-06-22 11:11:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-09-22 17:58:12 +02:00
|
|
|
|