2012-03-09 03:06:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-05-27 17:52:52 +00:00
|
|
|
#include <Core/Names.h>
|
2021-02-19 12:51:26 +00:00
|
|
|
#include <Server/HTTP/HTMLForm.h>
|
|
|
|
#include <Server/HTTP/HTTPRequestHandler.h>
|
2021-09-07 18:57:38 +00:00
|
|
|
#include <Server/HTTP/WriteBufferFromHTTPServerResponse.h>
|
2021-02-19 12:51:26 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
|
|
|
#include <Common/CurrentThread.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
|
|
|
|
{
|
|
|
|
|
2021-03-05 14:57:16 +00:00
|
|
|
class Session;
|
2021-03-11 20:41:10 +00:00
|
|
|
class Credentials;
|
2021-02-19 12:51:26 +00:00
|
|
|
class IServer;
|
2021-08-01 14:12:34 +00:00
|
|
|
struct Settings;
|
2017-01-22 15:03:55 +00:00
|
|
|
class WriteBufferFromHTTPServerResponse;
|
2017-02-09 10:10:13 +00:00
|
|
|
|
2020-05-27 17:52:52 +00:00
|
|
|
using CompiledRegexPtr = std::shared_ptr<const re2::RE2>;
|
2014-04-08 13:43:20 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
class HTTPHandler : public HTTPRequestHandler
|
2012-03-09 03:06:09 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPHandler(IServer & server_, const std::string & name);
|
2021-03-11 20:41:10 +00:00
|
|
|
virtual ~HTTPHandler() override;
|
2017-02-06 08:23:02 +00:00
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
void handleRequest(HTTPServerRequest & request, 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.
|
2021-05-31 14:49:02 +00:00
|
|
|
virtual void customizeContext(HTTPServerRequest & /* request */, ContextMutablePtr /* context */) {}
|
2019-11-04 18:36:28 +00:00
|
|
|
|
2021-05-31 14:49:02 +00:00
|
|
|
virtual bool customizeQueryParam(ContextMutablePtr context, const std::string & key, const std::string & value) = 0;
|
2019-11-04 18:36:28 +00:00
|
|
|
|
2021-05-31 14:49:02 +00:00
|
|
|
virtual std::string getQuery(HTTPServerRequest & request, HTMLForm & params, ContextMutablePtr 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;
|
|
|
|
|
2021-11-24 17:21:21 +00:00
|
|
|
bool finalized = false;
|
|
|
|
|
2020-04-04 08:57:16 +00:00
|
|
|
inline bool hasDelayed() const
|
|
|
|
{
|
|
|
|
return out_maybe_delayed_and_compressed != out_maybe_compressed;
|
|
|
|
}
|
2021-09-07 18:57:38 +00:00
|
|
|
|
2021-11-24 17:21:21 +00:00
|
|
|
inline void finalize()
|
2021-09-07 18:57:38 +00:00
|
|
|
{
|
2021-11-24 17:21:21 +00:00
|
|
|
finalized = true;
|
2021-09-07 18:57:38 +00:00
|
|
|
if (out_maybe_delayed_and_compressed)
|
|
|
|
out_maybe_delayed_and_compressed->finalize();
|
|
|
|
if (out_maybe_compressed)
|
|
|
|
out_maybe_compressed->finalize();
|
|
|
|
if (out)
|
|
|
|
out->finalize();
|
|
|
|
}
|
2021-11-24 15:30:33 +00:00
|
|
|
|
|
|
|
inline bool isFinalized() const
|
|
|
|
{
|
2021-11-24 17:21:21 +00:00
|
|
|
return finalized;
|
2021-11-24 15:30:33 +00:00
|
|
|
}
|
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};
|
|
|
|
|
2021-08-01 14:12:34 +00:00
|
|
|
/// Reference to the immutable settings in the global context.
|
|
|
|
/// Those settings are used only to extract a http request's parameters.
|
|
|
|
/// See settings http_max_fields, http_max_field_name_size, http_max_field_value_size in HTMLForm.
|
|
|
|
const Settings & default_settings;
|
|
|
|
|
|
|
|
// session is reset at the end of each request/response.
|
|
|
|
std::unique_ptr<Session> session;
|
|
|
|
|
|
|
|
// The request_credential instance may outlive a single request/response loop.
|
2021-03-11 20:41:10 +00:00
|
|
|
// This happens only when the authentication mechanism requires more than a single request/response exchange (e.g., SPNEGO).
|
|
|
|
std::unique_ptr<Credentials> request_credentials;
|
|
|
|
|
|
|
|
// Returns true when the user successfully authenticated,
|
2021-08-01 14:12:34 +00:00
|
|
|
// the session instance will be configured accordingly, and the request_credentials instance will be dropped.
|
2021-03-11 20:41:10 +00:00
|
|
|
// Returns false when the user is not authenticated yet, and the 'Negotiate' response is sent,
|
2021-08-01 14:12:34 +00:00
|
|
|
// the session and request_credentials instances are preserved.
|
2021-03-11 20:41:10 +00:00
|
|
|
// Throws an exception if authentication failed.
|
|
|
|
bool authenticateUser(
|
|
|
|
HTTPServerRequest & request,
|
|
|
|
HTMLForm & params,
|
|
|
|
HTTPServerResponse & response);
|
|
|
|
|
2020-04-04 08:57:16 +00:00
|
|
|
/// Also initializes 'used_output'.
|
|
|
|
void processQuery(
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPServerRequest & request,
|
2020-04-04 08:57:16 +00:00
|
|
|
HTMLForm & params,
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPServerResponse & response,
|
2020-10-16 19:22:14 +00:00
|
|
|
Output & used_output,
|
|
|
|
std::optional<CurrentThread::QueryScope> & query_scope);
|
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,
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPServerRequest & request,
|
|
|
|
HTTPServerResponse & response,
|
2020-04-04 08:57:16 +00:00
|
|
|
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");
|
|
|
|
|
2021-05-31 14:49:02 +00:00
|
|
|
std::string getQuery(HTTPServerRequest & request, HTMLForm & params, ContextMutablePtr context) override;
|
2020-04-04 08:57:16 +00:00
|
|
|
|
2021-05-31 14:49:02 +00:00
|
|
|
bool customizeQueryParam(ContextMutablePtr context, const std::string &key, const std::string &value) override;
|
2020-04-04 08:57:16 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2021-05-31 14:49:02 +00:00
|
|
|
virtual void customizeContext(HTTPServerRequest & request, ContextMutablePtr context) override;
|
2020-04-04 08:57:16 +00:00
|
|
|
|
2021-05-31 14:49:02 +00:00
|
|
|
std::string getQuery(HTTPServerRequest & request, HTMLForm & params, ContextMutablePtr context) override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-05-31 14:49:02 +00:00
|
|
|
bool customizeQueryParam(ContextMutablePtr context, const std::string & key, const std::string & value) override;
|
2012-03-09 03:06:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|