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>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
2017-08-09 14:33:07 +00:00
|
|
|
#include <Common/HTMLForm.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
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Metric HTTPConnection;
|
2016-10-24 04:06:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 19:04:46 +00:00
|
|
|
namespace Poco { class Logger; }
|
|
|
|
|
2012-03-09 03:06:09 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-01-22 15:03:55 +00:00
|
|
|
class WriteBufferFromHTTPServerResponse;
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2020-04-28 07:19:37 +00:00
|
|
|
typedef std::shared_ptr<const re2::RE2> CompiledRegexPtr;
|
2014-04-08 13:43:20 +00:00
|
|
|
|
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);
|
2017-02-06 08:23:02 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
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 */) {}
|
2019-11-04 18:36:28 +00:00
|
|
|
|
2020-04-04 08:57:16 +00:00
|
|
|
virtual bool customizeQueryParam(Context & context, const std::string & key, const std::string & value) = 0;
|
2019-11-04 18:36:28 +00:00
|
|
|
|
2020-04-04 08:57:16 +00:00
|
|
|
virtual std::string getQuery(Poco::Net::HTTPServerRequest & request, HTMLForm & params, Context & context) = 0;
|
2019-11-04 18:36:28 +00:00
|
|
|
|
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-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
2017-08-09 11:57:09 +00:00
|
|
|
IServer & server;
|
2017-09-01 19:04:46 +00:00
|
|
|
Poco::Logger * log;
|
2018-03-08 07:36:58 +00:00
|
|
|
|
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.
|
2018-03-08 07:36:58 +00:00
|
|
|
String server_display_name;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
CurrentMetrics::Increment metric_increment{CurrentMetrics::HTTPConnection};
|
|
|
|
|
2020-04-04 08:57:16 +00:00
|
|
|
/// Also initializes 'used_output'.
|
|
|
|
void processQuery(
|
|
|
|
Poco::Net::HTTPServerRequest & request,
|
|
|
|
HTMLForm & params,
|
|
|
|
Poco::Net::HTTPServerResponse & response,
|
|
|
|
Output & used_output);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
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);
|
2019-11-07 10:24:30 +00:00
|
|
|
|
2020-04-04 08:57:16 +00:00
|
|
|
static void pushDelayedResults(Output & used_output);
|
|
|
|
};
|
2019-11-06 07:02:10 +00:00
|
|
|
|
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_);
|
2020-04-21 11:30:45 +00:00
|
|
|
|
|
|
|
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;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-21 11:30:45 +00:00
|
|
|
bool customizeQueryParam(Context & context, const std::string & key, const std::string & value) override;
|
2012-03-09 03:06:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|