HTTPHandler.h 3.4 KB
Newer Older
1 2
#pragma once

V
Vadim Skipin 已提交
3 4 5 6
#include "IServer.h"

#include <Poco/Net/HTTPRequestHandler.h>

7
#include <Common/CurrentMetrics.h>
V
Vadim Skipin 已提交
8
#include <Common/HTMLForm.h>
9 10


11 12
namespace CurrentMetrics
{
13
    extern const Metric HTTPConnection;
14 15
}

P
proller 已提交
16 17
namespace Poco { class Logger; }

18 19 20
namespace DB
{

21
class WriteBufferFromHTTPServerResponse;
22

23

24
class HTTPHandler : public Poco::Net::HTTPRequestHandler
25 26
{
public:
Z
zhang2014 已提交
27
    explicit HTTPHandler(IServer & server_, const std::string & name);
28

29
    void handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response) override;
30

Z
zhang2014 已提交
31 32
    /// This method is called right before the query execution.
    virtual void customizeContext(Context & /* context */) {}
33

Z
zhang2014 已提交
34
    virtual bool customizeQueryParam(Context & context, const std::string & key, const std::string & value) = 0;
35

Z
zhang2014 已提交
36
    virtual std::string getQuery(Poco::Net::HTTPServerRequest & request, HTMLForm & params, Context & context) = 0;
37

Z
zhang2014 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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;
        }
60 61
    };

62
    IServer & server;
P
proller 已提交
63
    Poco::Logger * log;
64

65
    /// It is the name of the server that will be sent in an http-header X-ClickHouse-Server-Display-Name.
66
    String server_display_name;
67 68 69

    CurrentMetrics::Increment metric_increment{CurrentMetrics::HTTPConnection};

Z
zhang2014 已提交
70 71 72 73 74 75
    /// Also initializes 'used_output'.
    void processQuery(
        Poco::Net::HTTPServerRequest & request,
        HTMLForm & params,
        Poco::Net::HTTPServerResponse & response,
        Output & used_output);
76

Z
zhang2014 已提交
77 78 79 80 81 82
    void trySendExceptionToClient(
        const std::string & s,
        int exception_code,
        Poco::Net::HTTPServerRequest & request,
        Poco::Net::HTTPServerResponse & response,
        Output & used_output);
83

Z
zhang2014 已提交
84 85
    static void pushDelayedResults(Output & used_output);
};
86

Z
zhang2014 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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;
};

class PredefineQueryHandler : public HTTPHandler
{
private:
    NameSet receive_params;
    std::string predefine_query;
public:
    explicit PredefineQueryHandler(IServer & server, const NameSet & receive_params, const std::string & predefine_query_);

    std::string getQuery(Poco::Net::HTTPServerRequest & request, HTMLForm & params, Context & context) override;
108

Z
zhang2014 已提交
109
    bool customizeQueryParam(Context &context, const std::string &key, const std::string &value) override;
110 111 112
};

}