#pragma once #include #include "config.h" #include #include #include "IServer.h" #if USE_SSL # include #endif namespace CurrentMetrics { extern const Metric PostgreSQLConnection; } namespace DB { class ReadBufferFromPocoSocket; class Session; class TCPServer; /** PostgreSQL wire protocol implementation. * For more info see https://www.postgresql.org/docs/current/protocol.html */ class PostgreSQLHandler : public Poco::Net::TCPServerConnection { public: PostgreSQLHandler( const Poco::Net::StreamSocket & socket_, IServer & server_, TCPServer & tcp_server_, bool ssl_enabled_, Int32 connection_id_, std::vector> & auth_methods_, const ProfileEvents::Event & read_event_ = ProfileEvents::end(), const ProfileEvents::Event & write_event_ = ProfileEvents::end()); void run() final; private: LoggerPtr log = getLogger("PostgreSQLHandler"); IServer & server; TCPServer & tcp_server; std::unique_ptr session; bool ssl_enabled = false; Int32 connection_id = 0; Int32 secret_key = 0; std::shared_ptr in; std::shared_ptr out; std::shared_ptr message_transport; ProfileEvents::Event read_event; ProfileEvents::Event write_event; #if USE_SSL std::shared_ptr ss; #endif PostgreSQLProtocol::PGAuthentication::AuthenticationManager authentication_manager; CurrentMetrics::Increment metric_increment{CurrentMetrics::PostgreSQLConnection}; void changeIO(Poco::Net::StreamSocket & socket); bool startup(); void establishSecureConnection(Int32 & payload_size, Int32 & info); void makeSecureConnectionSSL(); void sendParameterStatusData(PostgreSQLProtocol::Messaging::StartupMessage & start_up_message); void cancelRequest(); std::unique_ptr receiveStartupMessage(int payload_size); void processQuery(); static bool isEmptyQuery(const String & query); }; }