ClickHouse/programs/odbc-bridge/SchemaAllowedHandler.cpp

80 lines
2.2 KiB
C++
Raw Normal View History

#include "SchemaAllowedHandler.h"
#if USE_ODBC
2021-03-22 11:40:29 +00:00
#include <Server/HTTP/HTMLForm.h>
#include <Server/HTTP/WriteBufferFromHTTPServerResponse.h>
#include <IO/WriteHelpers.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
2022-04-27 15:05:45 +00:00
#include <Common/logger_useful.h>
2021-03-22 11:40:29 +00:00
#include "validateODBCConnectionString.h"
#include "ODBCPooledConnectionFactory.h"
2021-03-22 11:40:29 +00:00
#include <sql.h>
#include <sqlext.h>
namespace DB
{
namespace
{
2021-06-07 18:09:16 +00:00
bool isSchemaAllowed(nanodbc::ConnectionHolderPtr connection_holder)
{
2021-06-07 18:09:16 +00:00
uint32_t result = execute<uint32_t>(connection_holder,
[&](nanodbc::connection & connection) { return connection.get_info<uint32_t>(SQL_SCHEMA_USAGE); });
2021-03-22 11:40:29 +00:00
return result != 0;
}
}
void SchemaAllowedHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response)
{
2021-06-16 14:33:14 +00:00
HTMLForm params(getContext()->getSettingsRef(), request, request.getStream());
LOG_TRACE(log, "Request URI: {}", request.getURI());
auto process_error = [&response, this](const std::string & message)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
if (!response.sent())
*response.send() << message << std::endl;
LOG_WARNING(log, fmt::runtime(message));
};
if (!params.has("connection_string"))
{
process_error("No 'connection_string' in request URL");
return;
}
try
{
std::string connection_string = params.get("connection_string");
2021-04-06 18:59:34 +00:00
auto connection = ODBCPooledConnectionFactory::instance().get(
2021-04-06 18:59:34 +00:00
validateODBCConnectionString(connection_string),
getContext()->getSettingsRef().odbc_bridge_connection_pool_size);
2021-04-06 18:59:34 +00:00
2021-06-07 18:09:16 +00:00
bool result = isSchemaAllowed(std::move(connection));
WriteBufferFromHTTPServerResponse out(response, request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD, keep_alive_timeout);
try
{
writeBoolText(result, out);
out.finalize();
}
catch (...)
{
out.finalize();
}
}
catch (...)
{
process_error("Error getting schema usage from ODBC '" + getCurrentExceptionMessage(false) + "'");
tryLogCurrentException(log);
}
}
}
#endif