Support redirect improvement

This commit is contained in:
millb 2019-10-21 17:36:24 +03:00
parent e6d0c2a66c
commit 52d7e9a667
6 changed files with 13 additions and 14 deletions

View File

@ -14,14 +14,14 @@ namespace ErrorCodes
extern const int UNACCEPTABLE_URL;
}
void RemoteHostFilter::checkURL(const Poco::URI & uri)
void RemoteHostFilter::checkURL(const Poco::URI & uri) const
{
if (!checkString(uri.getHost()) &&
!checkString(uri.getHost() + ":" + toString(uri.getPort())))
throw Exception("URL \"" + uri.toString() + "\" is not allowed in config.xml", ErrorCodes::UNACCEPTABLE_URL);
}
void RemoteHostFilter::checkHostAndPort(const std::string & host, const std::string & port)
void RemoteHostFilter::checkHostAndPort(const std::string & host, const std::string & port) const
{
if (!checkString(host) &&
!checkString(host + ":" + port))
@ -44,7 +44,7 @@ void RemoteHostFilter::setValuesFromConfig(const Poco::Util::AbstractConfigurati
}
}
bool RemoteHostFilter::checkString(const std::string &host)
bool RemoteHostFilter::checkString(const std::string &host) const
{
if (!primary_hosts.empty() || !regexp_hosts.empty())
{

View File

@ -11,16 +11,18 @@ namespace DB
class RemoteHostFilter
{
public:
void checkURL(const Poco::URI &uri); /// If URL not allowed in config.xml throw UNACCEPTABLE_URL Exception
void checkURL(const Poco::URI &uri) const; /// If URL not allowed in config.xml throw UNACCEPTABLE_URL Exception
void setValuesFromConfig(const Poco::Util::AbstractConfiguration &config);
void checkHostAndPort(const std::string & host, const std::string & port);
void checkHostAndPort(const std::string & host, const std::string & port) const;
RemoteHostFilter() {}
private:
std::unordered_set<std::string> primary_hosts; /// Allowed primary (<host>) URL from config.xml
std::vector<std::string> regexp_hosts; /// Allowed regexp (<hots_regexp>) URL from config.xml
bool checkString(const std::string &host);
bool checkString(const std::string &host) const;
};
}

View File

@ -1562,7 +1562,7 @@ void Context::setRemoteHostFilter(const Poco::Util::AbstractConfiguration & conf
shared->remote_host_filter.setValuesFromConfig(config);
}
RemoteHostFilter & Context::getRemoteHostFilter() const
const RemoteHostFilter & Context::getRemoteHostFilter() const
{
return shared->remote_host_filter;
}

View File

@ -350,7 +350,7 @@ public:
/// Storage of allowed hosts from config.xml
void setRemoteHostFilter(const Poco::Util::AbstractConfiguration & config);
RemoteHostFilter & getRemoteHostFilter() const;
const RemoteHostFilter & getRemoteHostFilter() const;
/// The port that the server listens for executing SQL queries.
UInt16 getTCPPort() const;

View File

@ -5,7 +5,7 @@
#include <Interpreters/evaluateConstantExpression.h>
#include <Parsers/ASTLiteral.h>
#include <IO/ReadWriteBufferFromHTTP.h>
#include <IO/ReadWriteBufferFromHTTPWithHostFilter.h>
#include <IO/WriteBufferFromHTTP.h>
#include <Formats/FormatFactory.h>
@ -57,7 +57,7 @@ namespace
: name(name_)
{
context.getRemoteHostFilter().checkURL(uri);
read_buf = std::make_unique<ReadWriteBufferFromHTTP>(uri, method, callback, timeouts, context.getSettingsRef().max_http_get_redirects);
read_buf = std::make_unique<ReadWriteBufferFromHTTPWithHostFilter>(uri, method, callback, timeouts, context.getSettingsRef().max_http_get_redirects, context.getRemoteHostFilter());
reader = FormatFactory::instance().getInput(format, *read_buf, sample_block, context, max_block_size);
}
@ -88,7 +88,7 @@ namespace
private:
String name;
std::unique_ptr<ReadWriteBufferFromHTTP> read_buf;
std::unique_ptr<ReadWriteBufferFromHTTPWithHostFilter> read_buf;
BlockInputStreamPtr reader;
};

View File

@ -66,9 +66,6 @@ private:
size_t max_block_size) const;
virtual Block getHeaderBlock(const Names & column_names) const = 0;
/// Return true if host allowed
bool checkHost(const std::string & host);
};