ClickHouse/programs/server/HTTPHandlerFactory.cpp

183 lines
8.1 KiB
C++
Raw Normal View History

#include "HTTPHandlerFactory.h"
2020-04-04 08:57:16 +00:00
#include <re2/re2.h>
#include <re2/stringpiece.h>
#include <common/find_symbols.h>
#include <Poco/StringTokenizer.h>
#include "HTTPHandler.h"
2020-04-03 09:09:19 +00:00
#include "NotFoundHandler.h"
2020-04-04 08:57:16 +00:00
#include "StaticRequestHandler.h"
#include "ReplicasStatusHandler.h"
#include "InterserverIOHTTPHandler.h"
#include "PrometheusRequestHandler.h"
2020-04-04 08:57:16 +00:00
namespace DB
{
2020-04-03 09:09:19 +00:00
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
2020-04-04 08:57:16 +00:00
extern const int UNKNOWN_ELEMENT_IN_CONFIG;
2020-04-27 20:51:39 +00:00
extern const int INVALID_CONFIG_PARAMETER;
2020-04-03 09:09:19 +00:00
}
HTTPRequestHandlerFactoryMain::HTTPRequestHandlerFactoryMain(const std::string & name_)
: log(&Logger::get(name_)), name(name_)
{
}
2020-05-18 08:08:55 +00:00
Poco::Net::HTTPRequestHandler * HTTPRequestHandlerFactoryMain::createRequestHandler(const Poco::Net::HTTPServerRequest & request)
{
2020-05-23 19:43:13 +00:00
LOG_TRACE(log, "HTTP Request for " << name << ". Method: " << request.getMethod() << ", Address: " << request.clientAddress().toString() << ", User-Agent: " << (request.has("User-Agent") ? request.get("User-Agent") : "none") << (request.hasContentLength() ? (", Length: " + std::to_string(request.getContentLength())) : ("")) << ", Content Type: " << request.getContentType() << ", Transfer Encoding: " << request.getTransferEncoding());
2020-04-04 08:57:16 +00:00
for (auto & handler_factory : child_factories)
2019-11-26 16:34:37 +00:00
{
2020-05-18 08:08:55 +00:00
auto * handler = handler_factory->createRequestHandler(request);
2020-04-04 08:57:16 +00:00
if (handler != nullptr)
return handler;
}
2020-04-04 08:57:16 +00:00
if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET
|| request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD
|| request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST)
{
2020-04-04 08:57:16 +00:00
return new NotFoundHandler;
}
return nullptr;
}
2020-04-04 08:57:16 +00:00
HTTPRequestHandlerFactoryMain::~HTTPRequestHandlerFactoryMain()
2020-04-03 09:09:19 +00:00
{
while (!child_factories.empty())
2020-04-27 20:51:39 +00:00
{
delete child_factories.back();
child_factories.pop_back();
}
2020-04-04 08:57:16 +00:00
}
2020-04-03 09:09:19 +00:00
2020-04-04 08:57:16 +00:00
HTTPRequestHandlerFactoryMain::TThis * HTTPRequestHandlerFactoryMain::addHandler(Poco::Net::HTTPRequestHandlerFactory * child_factory)
{
child_factories.emplace_back(child_factory);
2020-04-04 08:57:16 +00:00
return this;
2020-04-03 09:09:19 +00:00
}
2020-04-04 08:57:16 +00:00
static inline auto createHandlersFactoryFromConfig(IServer & server, const std::string & name, const String & prefix)
2020-04-03 09:09:19 +00:00
{
2020-05-18 08:08:55 +00:00
auto main_handler_factory = std::make_unique<HTTPRequestHandlerFactoryMain>(name);
2020-04-04 08:57:16 +00:00
2020-05-18 08:08:55 +00:00
Poco::Util::AbstractConfiguration::Keys keys;
server.config().keys(prefix, keys);
2020-05-18 08:08:55 +00:00
for (const auto & key : keys)
{
2020-05-18 08:08:55 +00:00
if (!startsWith(key, "rule"))
throw Exception("Unknown element in config: " + prefix + "." + key + ", must be 'rule'", ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG);
const auto & handler_type = server.config().getString(prefix + "." + key + ".handler.type", "");
if (handler_type == "static")
main_handler_factory->addHandler(createStaticHandlerFactory(server, prefix + "." + key));
else if (handler_type == "dynamic_query_handler")
main_handler_factory->addHandler(createDynamicHandlerFactory(server, prefix + "." + key));
else if (handler_type == "predefined_query_handler")
main_handler_factory->addHandler(createPredefinedHandlerFactory(server, prefix + "." + key));
else if (handler_type.empty())
throw Exception("Handler type in config is not specified here: " +
prefix + "." + key + ".handler.type", ErrorCodes::INVALID_CONFIG_PARAMETER);
else
throw Exception("Unknown handler type '" + handler_type +"' in config here: " +
prefix + "." + key + ".handler.type",ErrorCodes::INVALID_CONFIG_PARAMETER);
2020-04-03 09:09:19 +00:00
}
2020-05-18 08:08:55 +00:00
return main_handler_factory.release();
2020-04-03 09:09:19 +00:00
}
2020-04-04 08:57:16 +00:00
static const auto ping_response_expression = "Ok.\n";
static const auto root_response_expression = "config://http_server_default_response";
2020-04-03 09:09:19 +00:00
2020-05-18 08:08:55 +00:00
static inline Poco::Net::HTTPRequestHandlerFactory * createHTTPHandlerFactory(
IServer & server, const std::string & name, AsynchronousMetrics & async_metrics)
2020-04-03 09:09:19 +00:00
{
2020-04-27 20:51:39 +00:00
if (server.config().has("http_handlers"))
return createHandlersFactoryFromConfig(server, name, "http_handlers");
2020-04-04 08:57:16 +00:00
else
2020-04-03 09:09:19 +00:00
{
2020-05-18 08:08:55 +00:00
auto factory = std::make_unique<HTTPRequestHandlerFactoryMain>(name);
auto root_handler = std::make_unique<HandlingRuleHTTPHandlerFactory<StaticRequestHandler>>(server, root_response_expression);
root_handler->attachStrictPath("/")->allowGetAndHeadRequest();
factory->addHandler(root_handler.release());
auto ping_handler = std::make_unique<HandlingRuleHTTPHandlerFactory<StaticRequestHandler>>(server, ping_response_expression);
ping_handler->attachStrictPath("/ping")->allowGetAndHeadRequest();
factory->addHandler(ping_handler.release());
auto replicas_status_handler = std::make_unique<HandlingRuleHTTPHandlerFactory<ReplicasStatusHandler>>(server);
replicas_status_handler->attachNonStrictPath("/replicas_status")->allowGetAndHeadRequest();
factory->addHandler(replicas_status_handler.release());
auto query_handler = std::make_unique<HandlingRuleHTTPHandlerFactory<DynamicQueryHandler>>(server, "query");
query_handler->allowPostAndGetParamsRequest();
factory->addHandler(query_handler.release());
2020-05-18 19:46:32 +00:00
/// We check that prometheus handler will be served on current (default) port.
/// Otherwise it will be created separately, see below.
if (server.config().has("prometheus") && server.config().getInt("prometheus.port", 0) == 0)
2020-05-18 08:08:55 +00:00
{
auto prometheus_handler = std::make_unique<HandlingRuleHTTPHandlerFactory<PrometheusRequestHandler>>(
server, PrometheusMetricsWriter(server.config(), "prometheus", async_metrics));
prometheus_handler->attachStrictPath(server.config().getString("prometheus.endpoint", "/metrics"))->allowGetAndHeadRequest();
factory->addHandler(prometheus_handler.release());
}
2020-05-18 08:08:55 +00:00
return factory.release();
2020-04-03 09:09:19 +00:00
}
}
2020-04-04 08:57:16 +00:00
static inline Poco::Net::HTTPRequestHandlerFactory * createInterserverHTTPHandlerFactory(IServer & server, const std::string & name)
{
2020-05-18 08:08:55 +00:00
auto factory = std::make_unique<HTTPRequestHandlerFactoryMain>(name);
auto root_handler = std::make_unique<HandlingRuleHTTPHandlerFactory<StaticRequestHandler>>(server, root_response_expression);
root_handler->attachStrictPath("/")->allowGetAndHeadRequest();
factory->addHandler(root_handler.release());
auto ping_handler = std::make_unique<HandlingRuleHTTPHandlerFactory<StaticRequestHandler>>(server, ping_response_expression);
ping_handler->attachStrictPath("/ping")->allowGetAndHeadRequest();
factory->addHandler(ping_handler.release());
auto replicas_status_handler = std::make_unique<HandlingRuleHTTPHandlerFactory<ReplicasStatusHandler>>(server);
replicas_status_handler->attachNonStrictPath("/replicas_status")->allowGetAndHeadRequest();
factory->addHandler(replicas_status_handler.release());
auto main_handler = std::make_unique<HandlingRuleHTTPHandlerFactory<InterserverIOHTTPHandler>>(server);
main_handler->allowPostAndGetParamsRequest();
factory->addHandler(main_handler.release());
return factory.release();
2020-04-04 08:57:16 +00:00
}
Poco::Net::HTTPRequestHandlerFactory * createHandlerFactory(IServer & server, AsynchronousMetrics & async_metrics, const std::string & name)
2020-04-04 08:57:16 +00:00
{
if (name == "HTTPHandler-factory" || name == "HTTPSHandler-factory")
return createHTTPHandlerFactory(server, name, async_metrics);
2020-04-04 08:57:16 +00:00
else if (name == "InterserverIOHTTPHandler-factory" || name == "InterserverIOHTTPSHandler-factory")
return createInterserverHTTPHandlerFactory(server, name);
else if (name == "PrometheusHandler-factory")
2020-05-18 08:08:55 +00:00
{
auto factory = std::make_unique<HTTPRequestHandlerFactoryMain>(name);
auto handler = std::make_unique<HandlingRuleHTTPHandlerFactory<PrometheusRequestHandler>>(
server, PrometheusMetricsWriter(server.config(), "prometheus", async_metrics));
handler->attachStrictPath(server.config().getString("prometheus.endpoint", "/metrics"))->allowGetAndHeadRequest();
factory->addHandler(handler.release());
return factory.release();
}
2020-04-04 08:57:16 +00:00
throw Exception("LOGICAL ERROR: Unknown HTTP handler factory name.", ErrorCodes::LOGICAL_ERROR);
2019-11-26 16:34:37 +00:00
}
2020-04-04 08:57:16 +00:00
2020-04-27 20:51:39 +00:00
}