ClickHouse/src/Server/HTTPHandler.h

125 lines
4.0 KiB
C++
Raw Normal View History

2012-03-09 03:06:09 +00:00
#pragma once
2017-08-09 14:33:07 +00:00
#include "IServer.h"
#include <Poco/Net/HTTPRequestHandler.h>
#include <Common/CurrentThread.h>
#include <Common/CurrentMetrics.h>
2017-08-09 14:33:07 +00:00
#include <Common/HTMLForm.h>
#include <Core/Names.h>
2012-03-09 03:06:09 +00:00
2020-04-27 22:19:24 +00:00
#include <re2/re2.h>
2012-03-09 03:06:09 +00:00
namespace CurrentMetrics
{
extern const Metric HTTPConnection;
}
2017-09-01 19:04:46 +00:00
namespace Poco { class Logger; }
2012-03-09 03:06:09 +00:00
namespace DB
{
class WriteBufferFromHTTPServerResponse;
using CompiledRegexPtr = std::shared_ptr<const re2::RE2>;
2012-03-09 15:46:52 +00:00
class HTTPHandler : public Poco::Net::HTTPRequestHandler
2012-03-09 03:06:09 +00:00
{
public:
2020-04-04 08:57:16 +00:00
explicit HTTPHandler(IServer & server_, const std::string & name);
void handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response) override;
2012-03-09 03:06:09 +00:00
2020-04-04 08:57:16 +00:00
/// This method is called right before the query execution.
2020-04-27 20:51:39 +00:00
virtual void customizeContext(Poco::Net::HTTPServerRequest & /*request*/, Context & /* context */) {}
2020-04-04 08:57:16 +00:00
virtual bool customizeQueryParam(Context & context, const std::string & key, const std::string & value) = 0;
2020-04-04 08:57:16 +00:00
virtual std::string getQuery(Poco::Net::HTTPServerRequest & request, HTMLForm & params, Context & context) = 0;
2020-04-04 08:57:16 +00:00
private:
struct Output
{
/* Raw data
*
* CascadeWriteBuffer out_maybe_delayed_and_compressed (optional)
* (forwards data if an overflow is occur or explicitly via pushDelayedResults)
* CompressedWriteBuffer out_maybe_compressed (optional)
*
* WriteBufferFromHTTPServerResponse out
*/
std::shared_ptr<WriteBufferFromHTTPServerResponse> out;
/// Points to 'out' or to CompressedWriteBuffer(*out), depending on settings.
std::shared_ptr<WriteBuffer> out_maybe_compressed;
/// Points to 'out' or to CompressedWriteBuffer(*out) or to CascadeWriteBuffer.
std::shared_ptr<WriteBuffer> out_maybe_delayed_and_compressed;
inline bool hasDelayed() const
{
return out_maybe_delayed_and_compressed != out_maybe_compressed;
}
};
2017-08-09 11:57:09 +00:00
IServer & server;
2017-09-01 19:04:46 +00:00
Poco::Logger * log;
2018-05-07 02:01:11 +00:00
/// It is the name of the server that will be sent in an http-header X-ClickHouse-Server-Display-Name.
String server_display_name;
CurrentMetrics::Increment metric_increment{CurrentMetrics::HTTPConnection};
2020-04-04 08:57:16 +00:00
/// Also initializes 'used_output'.
void processQuery(
Context & context,
2020-04-04 08:57:16 +00:00
Poco::Net::HTTPServerRequest & request,
HTMLForm & params,
Poco::Net::HTTPServerResponse & response,
Output & used_output,
std::optional<CurrentThread::QueryScope> & query_scope);
2020-04-04 08:57:16 +00:00
void trySendExceptionToClient(
const std::string & s,
int exception_code,
Poco::Net::HTTPServerRequest & request,
Poco::Net::HTTPServerResponse & response,
Output & used_output);
2020-04-04 08:57:16 +00:00
static void pushDelayedResults(Output & used_output);
};
2020-04-04 08:57:16 +00:00
class DynamicQueryHandler : public HTTPHandler
{
private:
std::string param_name;
public:
explicit DynamicQueryHandler(IServer & server_, const std::string & param_name_ = "query");
std::string getQuery(Poco::Net::HTTPServerRequest & request, HTMLForm & params, Context & context) override;
bool customizeQueryParam(Context &context, const std::string &key, const std::string &value) override;
};
2020-04-27 20:51:39 +00:00
class PredefinedQueryHandler : public HTTPHandler
2020-04-04 08:57:16 +00:00
{
private:
NameSet receive_params;
2020-04-27 20:51:39 +00:00
std::string predefined_query;
2020-04-27 22:19:24 +00:00
CompiledRegexPtr url_regex;
std::unordered_map<String, CompiledRegexPtr> header_name_with_capture_regex;
2020-04-04 08:57:16 +00:00
public:
2020-05-09 23:34:24 +00:00
PredefinedQueryHandler(
IServer & server_, const NameSet & receive_params_, const std::string & predefined_query_
2020-04-27 22:19:24 +00:00
, const CompiledRegexPtr & url_regex_, const std::unordered_map<String, CompiledRegexPtr> & header_name_with_regex_);
virtual void customizeContext(Poco::Net::HTTPServerRequest & request, Context & context) override;
2020-04-04 08:57:16 +00:00
std::string getQuery(Poco::Net::HTTPServerRequest & request, HTMLForm & params, Context & context) override;
bool customizeQueryParam(Context & context, const std::string & key, const std::string & value) override;
2012-03-09 03:06:09 +00:00
};
}