2017-08-09 14:33:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-04-21 11:30:45 +00:00
|
|
|
#include <Interpreters/AsynchronousMetrics.h>
|
2021-02-19 12:51:26 +00:00
|
|
|
#include <Server/HTTP/HTMLForm.h>
|
|
|
|
#include <Server/HTTP/HTTPRequestHandlerFactory.h>
|
|
|
|
#include <Server/HTTPHandlerRequestFilter.h>
|
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2021-02-19 12:51:26 +00:00
|
|
|
|
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
2017-08-09 14:33:07 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
namespace ErrorCodes
|
2017-08-09 14:33:07 +00:00
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
extern const int UNKNOWN_ELEMENT_IN_CONFIG;
|
|
|
|
}
|
2020-04-04 08:57:16 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
class IServer;
|
2019-11-24 15:43:53 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
/// Handle request using child handlers
|
|
|
|
class HTTPRequestHandlerFactoryMain : public HTTPRequestHandlerFactory
|
|
|
|
{
|
2020-04-04 08:57:16 +00:00
|
|
|
public:
|
2021-02-19 12:51:26 +00:00
|
|
|
explicit HTTPRequestHandlerFactoryMain(const std::string & name_);
|
2020-04-04 08:57:16 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
void addHandler(HTTPRequestHandlerFactoryPtr child_factory) { child_factories.emplace_back(child_factory); }
|
2019-11-24 15:43:53 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
std::unique_ptr<HTTPRequestHandler> createRequestHandler(const HTTPServerRequest & request) override;
|
2020-04-04 08:57:16 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
private:
|
|
|
|
Poco::Logger * log;
|
|
|
|
std::string name;
|
2020-04-04 08:57:16 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
std::vector<HTTPRequestHandlerFactoryPtr> child_factories;
|
2020-04-04 08:57:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TEndpoint>
|
2021-02-19 12:51:26 +00:00
|
|
|
class HandlingRuleHTTPHandlerFactory : public HTTPRequestHandlerFactory
|
2019-11-24 15:43:53 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-02-19 12:51:26 +00:00
|
|
|
using Filter = std::function<bool(const HTTPServerRequest &)>;
|
2017-08-09 14:33:07 +00:00
|
|
|
|
2020-04-04 08:57:16 +00:00
|
|
|
template <typename... TArgs>
|
2021-02-19 12:51:26 +00:00
|
|
|
explicit HandlingRuleHTTPHandlerFactory(TArgs &&... args)
|
2020-04-04 08:57:16 +00:00
|
|
|
{
|
|
|
|
creator = [args = std::tuple<TArgs...>(std::forward<TArgs>(args) ...)]()
|
|
|
|
{
|
|
|
|
return std::apply([&](auto && ... endpoint_args)
|
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
return std::make_unique<TEndpoint>(std::forward<decltype(endpoint_args)>(endpoint_args)...);
|
2020-04-04 08:57:16 +00:00
|
|
|
}, std::move(args));
|
|
|
|
};
|
|
|
|
}
|
2019-11-24 15:43:53 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
void addFilter(Filter cur_filter)
|
2020-04-04 08:57:16 +00:00
|
|
|
{
|
|
|
|
Filter prev_filter = filter;
|
|
|
|
filter = [prev_filter, cur_filter](const auto & request)
|
|
|
|
{
|
|
|
|
return prev_filter ? prev_filter(request) && cur_filter(request) : cur_filter(request);
|
|
|
|
};
|
2021-02-19 12:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void addFiltersFromConfig(Poco::Util::AbstractConfiguration & config, const std::string & prefix)
|
|
|
|
{
|
|
|
|
Poco::Util::AbstractConfiguration::Keys filters_type;
|
|
|
|
config.keys(prefix, filters_type);
|
2019-11-24 15:43:53 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
for (const auto & filter_type : filters_type)
|
|
|
|
{
|
|
|
|
if (filter_type == "handler")
|
|
|
|
continue;
|
|
|
|
else if (filter_type == "url")
|
|
|
|
addFilter(urlFilter(config, prefix + ".url"));
|
|
|
|
else if (filter_type == "headers")
|
|
|
|
addFilter(headersFilter(config, prefix + ".headers"));
|
|
|
|
else if (filter_type == "methods")
|
|
|
|
addFilter(methodsFilter(config, prefix + ".methods"));
|
|
|
|
else
|
|
|
|
throw Exception("Unknown element in config: " + prefix + "." + filter_type, ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG);
|
|
|
|
}
|
2020-04-04 08:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
void attachStrictPath(const String & strict_path)
|
2020-04-04 08:57:16 +00:00
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
addFilter([strict_path](const auto & request) { return request.getURI() == strict_path; });
|
2020-04-04 08:57:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
void attachNonStrictPath(const String & non_strict_path)
|
2020-04-04 08:57:16 +00:00
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
addFilter([non_strict_path](const auto & request) { return startsWith(request.getURI(), non_strict_path); });
|
2020-04-04 08:57:16 +00:00
|
|
|
}
|
2019-11-24 15:43:53 +00:00
|
|
|
|
2020-04-04 08:57:16 +00:00
|
|
|
/// Handle GET or HEAD endpoint on specified path
|
2021-02-19 12:51:26 +00:00
|
|
|
void allowGetAndHeadRequest()
|
2020-04-04 08:57:16 +00:00
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
addFilter([](const auto & request)
|
2020-04-04 08:57:16 +00:00
|
|
|
{
|
|
|
|
return request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET
|
|
|
|
|| request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-06 13:09:34 +00:00
|
|
|
/// Handle Post request or (Get or Head) with params or OPTIONS requests
|
2021-09-22 10:34:48 +00:00
|
|
|
void allowPostAndGetParamsAndOptionsRequest()
|
2020-04-04 08:57:16 +00:00
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
addFilter([](const auto & request)
|
2020-04-04 08:57:16 +00:00
|
|
|
{
|
2021-10-06 11:43:05 +00:00
|
|
|
return (request.getURI().find('?') != std::string::npos
|
|
|
|
&& (request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET
|
|
|
|
|| request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD))
|
2021-09-22 10:34:48 +00:00
|
|
|
|| request.getMethod() == Poco::Net::HTTPRequest::HTTP_OPTIONS
|
2020-04-04 08:57:16 +00:00
|
|
|
|| request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
std::unique_ptr<HTTPRequestHandler> createRequestHandler(const HTTPServerRequest & request) override
|
2020-04-04 08:57:16 +00:00
|
|
|
{
|
|
|
|
return filter(request) ? creator() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Filter filter;
|
2021-02-19 12:51:26 +00:00
|
|
|
std::function<std::unique_ptr<HTTPRequestHandler> ()> creator;
|
2019-11-24 15:43:53 +00:00
|
|
|
};
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPRequestHandlerFactoryPtr createStaticHandlerFactory(IServer & server, const std::string & config_prefix);
|
2020-04-04 08:57:16 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPRequestHandlerFactoryPtr createDynamicHandlerFactory(IServer & server, const std::string & config_prefix);
|
2020-04-04 08:57:16 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPRequestHandlerFactoryPtr createPredefinedHandlerFactory(IServer & server, const std::string & config_prefix);
|
2020-06-12 16:15:02 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPRequestHandlerFactoryPtr createReplicasStatusHandlerFactory(IServer & server, const std::string & config_prefix);
|
2020-06-12 16:15:02 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPRequestHandlerFactoryPtr
|
|
|
|
createPrometheusHandlerFactory(IServer & server, AsynchronousMetrics & async_metrics, const std::string & config_prefix);
|
2020-04-21 11:30:45 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPRequestHandlerFactoryPtr createHandlerFactory(IServer & server, AsynchronousMetrics & async_metrics, const std::string & name);
|
2017-08-09 14:33:07 +00:00
|
|
|
}
|