Added handler

This commit is contained in:
Sergey Lokhmatikov 2023-04-06 16:38:24 +00:00 committed by Konstantin Bogdanov
parent bdb5346a8c
commit 6749d8bc65
No known key found for this signature in database
7 changed files with 140 additions and 1 deletions

15
outputfile Normal file
View File

@ -0,0 +1,15 @@
There is no handle /files
Use / or /ping for health checks.
Or /replicas_status for more sophisticated health checks.
Send queries from your program with POST method or GET /?query=...
Use clickhouse-client:
For interactive data analysis:
clickhouse-client
For batch query processing:
clickhouse-client --query='SELECT 1' > result
clickhouse-client < query > result

View File

@ -2488,6 +2488,23 @@ void Server::createServers(
{
const char * port_name;
/// File System
port_name = "file_system.port";
createServer(config, listen_host, port_name, listen_try, start_servers, servers, [&](UInt16 port) -> ProtocolServerAdapter
{
Poco::Net::ServerSocket socket;
auto address = socketBindListen(config, socket, listen_host, port);
socket.setReceiveTimeout(settings.http_receive_timeout);
socket.setSendTimeout(settings.http_send_timeout);
return ProtocolServerAdapter(
listen_host,
port_name,
"http://" + address.toString(),
std::make_unique<HTTPServer>(
httpContext(), createHandlerFactory(*this, config, async_metrics, "FilesHTTPHandler-factory"), server_pool, socket, http_params));
});
if (server_type.shouldStart(ServerType::Type::HTTP))
{
/// HTTP

View File

@ -141,6 +141,15 @@
-->
<http_port>8123</http_port>
<!-- Port that allows to download files from directory.
It is essentially used for completing HTTP-01 challenge and fetching
certificates from Let's Encrypt. (Should be equal to 80)
-->
<file_system>
<port>8124</port>
<base_directory>/etc/clickhouse-server/export</base_directory>
</file_system>
<!-- Port for interaction by native protocol with:
- clickhouse-client and other native ClickHouse tools (clickhouse-benchmark);
- clickhouse-server with other clickhouse-servers for distributed query processing;
@ -1607,6 +1616,20 @@
</rule>
</http_handlers>
-->
<!--
<http_handlers>
<rule>
<url>/files</url>
<handler>
<type>static</type>
<content_type>application/octet-stream</content_type>
<response_content>file:///../../../../../../home/lohmat/ClickHouse/README.md</response_content>
</handler>
</rule>
</http_handlers>
-->
<send_crash_reports>
<!-- Changing <enabled> to true allows sending crash reports to -->

View File

@ -49,7 +49,7 @@ public:
/// Handle configuration reload
void tryLoad(const Poco::Util::AbstractConfiguration & config);
/// Reload certificates
/// Callback for Let's Enrypt integration
void reloadCertificates();
/// A callback for OpenSSL

View File

@ -0,0 +1,40 @@
#include "FileRequestHandler.h"
#include "IServer.h"
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Util/LayeredConfiguration.h>
#include <IO/HTTPCommon.h>
#include <Common/getResource.h>
#include <re2/re2.h>
namespace DB
{
FileRequestHandler::FileRequestHandler(IServer & server_, const std::string & base_directory_path_)
: server(server_), base_directory_path(base_directory_path_)
{
}
void FileRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response)
{
auto keep_alive_timeout = server.config().getUInt("keep_alive_timeout", 10);
response.setContentType("text/html; charset=UTF-8");
if (request.getVersion() == HTTPServerRequest::HTTP_1_1)
response.setChunkedTransferEncoding(true);
setResponseDefaultHeaders(response, keep_alive_timeout);
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_OK);
*response.send() << getResource(base_directory_path + request.getURI());
}
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <Server/HTTP/HTTPRequestHandler.h>
namespace DB
{
class IServer;
/// Response with file to user.
class FileRequestHandler : public HTTPRequestHandler
{
private:
IServer & server;
const std::string & base_directory_path;
public:
FileRequestHandler(IServer & server_, const std::string & base_directory_path_);
void handleRequest(HTTPServerRequest & request, HTTPServerResponse & response) override;
};
}

View File

@ -10,6 +10,7 @@
#include "HTTPHandler.h"
#include "StaticRequestHandler.h"
#include "ReplicasStatusHandler.h"
#include "FileRequestHandler.h"
#include "InterserverIOHTTPHandler.h"
#include "WebUIRequestHandler.h"
@ -66,6 +67,11 @@ static void addDefaultHandlersFactory(
IServer & server,
const Poco::Util::AbstractConfiguration & config,
AsynchronousMetrics & async_metrics);
static void addFileSystemHandlerFactory(
HTTPRequestHandlerFactoryMain & factory,
IServer & server,
const Poco::Util::AbstractConfiguration & config
);
static auto createPingHandlerFactory(IServer & server)
{
@ -192,6 +198,13 @@ static inline HTTPRequestHandlerFactoryPtr createInterserverHTTPHandlerFactory(I
return factory;
}
static inline HTTPRequestHandlerFactoryPtr createFileSystemHTTPHandlerFactory(IServer & server, const Poco::Util::AbstractConfiguration & config, const std::string & name)
{
auto factory = std::make_shared<HTTPRequestHandlerFactoryMain>(name);
addFileSystemHandlerFactory(*factory, server, config);
return factory;
}
HTTPRequestHandlerFactoryPtr createHandlerFactory(IServer & server, const Poco::Util::AbstractConfiguration & config, AsynchronousMetrics & async_metrics, const std::string & name)
{
@ -255,6 +268,14 @@ void addCommonDefaultHandlersFactory(HTTPRequestHandlerFactoryMain & factory, IS
factory.addHandler(js_handler);
}
void addFileSystemHandlerFactory(HTTPRequestHandlerFactoryMain & factory, IServer & server, const Poco::Util::AbstractConfiguration & config)
{
auto files_handler = std::make_shared<HandlingRuleHTTPHandlerFactory<FileRequestHandler>>(server, config.getString("file_system.base_directory", "/"));
files_handler->attachNonStrictPath("/");
files_handler->allowGetAndHeadRequest();
factory.addHandler(files_handler);
}
void addDefaultHandlersFactory(
HTTPRequestHandlerFactoryMain & factory,
IServer & server,