ClickHouse/src/Server/HTTPHandler.h

164 lines
5.6 KiB
C++
Raw Normal View History

2012-03-09 03:06:09 +00:00
#pragma once
#include <Core/Names.h>
#include <Server/HTTP/HTMLForm.h>
#include <Server/HTTP/HTTPRequestHandler.h>
#include <Server/HTTP/WriteBufferFromHTTPServerResponse.h>
#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
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 Session;
class Credentials;
class IServer;
2021-08-01 14:12:34 +00:00
struct Settings;
class WriteBufferFromHTTPServerResponse;
using CompiledRegexPtr = std::shared_ptr<const re2::RE2>;
class HTTPHandler : public HTTPRequestHandler
2012-03-09 03:06:09 +00:00
{
public:
HTTPHandler(IServer & server_, const std::string & name);
virtual ~HTTPHandler() override;
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 */) {}
2021-05-31 14:49:02 +00:00
virtual bool customizeQueryParam(ContextMutablePtr context, const std::string & key, const std::string & value) = 0;
2021-05-31 14:49:02 +00:00
virtual std::string getQuery(HTTPServerRequest & request, HTMLForm & params, ContextMutablePtr 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;
}
inline void finalize() const
{
if (out_maybe_delayed_and_compressed)
out_maybe_delayed_and_compressed->finalize();
if (out_maybe_compressed)
out_maybe_compressed->finalize();
if (out)
out->finalize();
}
inline bool isFinalized() const
{
return out_maybe_delayed_and_compressed->isFinalized();
}
};
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};
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.
// 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.
// 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.
// 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(
HTTPServerRequest & request,
2020-04-04 08:57:16 +00:00
HTMLForm & params,
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,
HTTPServerRequest & request,
HTTPServerResponse & response,
2020-04-04 08:57:16 +00:00
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");
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_);
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;
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
};
}