| 1 | #pragma once | 
|---|---|
| 2 | |
| 3 | #include "IServer.h" | 
| 4 | |
| 5 | #include <Poco/Net/HTTPRequestHandler.h> | 
| 6 | |
| 7 | #include <Common/CurrentMetrics.h> | 
| 8 | #include <Common/HTMLForm.h> | 
| 9 | |
| 10 | |
| 11 | namespace CurrentMetrics | 
| 12 | { | 
| 13 | extern const Metric HTTPConnection; | 
| 14 | } | 
| 15 | |
| 16 | namespace Poco { class Logger; } | 
| 17 | |
| 18 | namespace DB | 
| 19 | { | 
| 20 | |
| 21 | class WriteBufferFromHTTPServerResponse; | 
| 22 | |
| 23 | |
| 24 | class HTTPHandler : public Poco::Net::HTTPRequestHandler | 
| 25 | { | 
| 26 | public: | 
| 27 | explicit HTTPHandler(IServer & server_); | 
| 28 | |
| 29 | void handleRequest(Poco::Net::HTTPServerRequest & request, Poco::Net::HTTPServerResponse & response) override; | 
| 30 | |
| 31 | /// This method is called right before the query execution. | 
| 32 | virtual void customizeContext(DB::Context& /* context */) {} | 
| 33 | |
| 34 | private: | 
| 35 | struct Output | 
| 36 | { | 
| 37 | /* Raw data | 
| 38 | * ↓ | 
| 39 | * CascadeWriteBuffer out_maybe_delayed_and_compressed (optional) | 
| 40 | * ↓ (forwards data if an overflow is occur or explicitly via pushDelayedResults) | 
| 41 | * CompressedWriteBuffer out_maybe_compressed (optional) | 
| 42 | * ↓ | 
| 43 | * WriteBufferFromHTTPServerResponse out | 
| 44 | */ | 
| 45 | |
| 46 | std::shared_ptr<WriteBufferFromHTTPServerResponse> out; | 
| 47 | /// Points to 'out' or to CompressedWriteBuffer(*out), depending on settings. | 
| 48 | std::shared_ptr<WriteBuffer> out_maybe_compressed; | 
| 49 | /// Points to 'out' or to CompressedWriteBuffer(*out) or to CascadeWriteBuffer. | 
| 50 | std::shared_ptr<WriteBuffer> out_maybe_delayed_and_compressed; | 
| 51 | |
| 52 | inline bool hasDelayed() const | 
| 53 | { | 
| 54 | return out_maybe_delayed_and_compressed != out_maybe_compressed; | 
| 55 | } | 
| 56 | }; | 
| 57 | |
| 58 | IServer & server; | 
| 59 | Poco::Logger * log; | 
| 60 | |
| 61 | /// It is the name of the server that will be sent in an http-header X-ClickHouse-Server-Display-Name. | 
| 62 | String server_display_name; | 
| 63 | |
| 64 | CurrentMetrics::Increment metric_increment{CurrentMetrics::HTTPConnection}; | 
| 65 | |
| 66 | /// Also initializes 'used_output'. | 
| 67 | void processQuery( | 
| 68 | Poco::Net::HTTPServerRequest & request, | 
| 69 | HTMLForm & params, | 
| 70 | Poco::Net::HTTPServerResponse & response, | 
| 71 | Output & used_output); | 
| 72 | |
| 73 | void trySendExceptionToClient( | 
| 74 | const std::string & s, | 
| 75 | int exception_code, | 
| 76 | Poco::Net::HTTPServerRequest & request, | 
| 77 | Poco::Net::HTTPServerResponse & response, | 
| 78 | Output & used_output); | 
| 79 | |
| 80 | void pushDelayedResults(Output & used_output); | 
| 81 | }; | 
| 82 | |
| 83 | } | 
| 84 | 
