optimize code

This commit is contained in:
凌涛 2023-11-21 19:47:39 +08:00
parent c5f16725ec
commit 5e581a9fa4
5 changed files with 17 additions and 19 deletions

View File

@ -83,23 +83,20 @@ public:
auto result_column = ColumnString::create();
const String default_value;
String forbidden_header_names = getContext()->getClientHTTPHeaderForbiddenHeaders();
std::vector<String> forbidden_header_list;
boost::split(forbidden_header_list, forbidden_header_names, [](char c) { return c == ','; });
String header_list;
const std::unordered_set<String> & forbidden_header_list = getContext()->getClientHTTPHeaderForbiddenHeaders();
for (size_t row = 0; row < input_rows_count; ++row)
{
auto header_name = arg_string->getDataAt(row).toString();
if (!headers.has(header_name))
if (!headers.has(header_name))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "{} is not in HTTP request headers.", header_name);
else
{
auto it = std::find(forbidden_header_list.begin(), forbidden_header_list.end(), header_name);
auto it = forbidden_header_list.find(header_name);
if (it != forbidden_header_list.end())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "The header {} is in headers_forbidden_to_return_list, you can config it in config file.", header_name);
const String & value = headers[header_name];
result_column->insertData(value.data(), value.size());
}

View File

@ -317,7 +317,7 @@ struct ContextSharedPart : boost::noncopyable
std::optional<MergeTreeSettings> merge_tree_settings TSA_GUARDED_BY(mutex); /// Settings of MergeTree* engines.
std::optional<MergeTreeSettings> replicated_merge_tree_settings TSA_GUARDED_BY(mutex); /// Settings of ReplicatedMergeTree* engines.
std::atomic_size_t max_table_size_to_drop = 50000000000lu; /// Protects MergeTree tables from accidental DROP (50GB by default)
String get_client_http_header_forbidden_headers;
std::unordered_set<String> get_client_http_header_forbidden_headers;
bool allow_get_client_http_header;
std::atomic_size_t max_partition_size_to_drop = 50000000000lu; /// Protects MergeTree partitions from accidental DROP (50GB by default)
/// No lock required for format_schema_path modified only during initialization
@ -3903,7 +3903,9 @@ void Context::checkTableCanBeDropped(const String & database, const String & tab
void Context::setClientHTTPHeaderForbiddenHeaders(const String & forbidden_headers)
{
shared->get_client_http_header_forbidden_headers = forbidden_headers;
std::unordered_set<String> forbidden_header_list;
boost::split(forbidden_header_list, forbidden_headers, [](char c) { return c == ','; });
shared->get_client_http_header_forbidden_headers = forbidden_header_list;
}
void Context::setAllowGetHTTPHeaderFunction(bool allow_get_http_header_function)
@ -3911,7 +3913,7 @@ void Context::setAllowGetHTTPHeaderFunction(bool allow_get_http_header_function)
shared->allow_get_client_http_header= allow_get_http_header_function;
}
String Context::getClientHTTPHeaderForbiddenHeaders() const
const std::unordered_set<String> & Context::getClientHTTPHeaderForbiddenHeaders() const
{
return shared->get_client_http_header_forbidden_headers;
}

View File

@ -1072,8 +1072,8 @@ public:
size_t getMaxTableSizeToDrop() const;
void setClientHTTPHeaderForbiddenHeaders(const String & forbidden_headers);
/// Return the forbiddent headers that users cant get via getClientHTTPHeader function
String getClientHTTPHeaderForbiddenHeaders() const;
void setAllowGetHTTPHeaderFunction(const bool allow_get_http_header_function);
const std::unordered_set<String> & getClientHTTPHeaderForbiddenHeaders() const;
void setAllowGetHTTPHeaderFunction(bool allow_get_http_header_function);
bool allowGetHTTPHeaderFunction() const;
void checkTableCanBeDropped(const String & database, const String & table, const size_t & table_size) const;

View File

@ -1,9 +1,8 @@
value
value1 value2
value1 value1 value2
BAD_ARGUMENTS
BAD_ARGUMENTS
BAD_ARGUMENTS
NOT-FOUND-KEY is not in HTTP request headers
FORBIDDEN-KEY1 is in headers_forbidden_to_return_list
1 row1_value1 row1_value2 row1_value3 row1_value4 row1_value5 row1_value6 row1_value7
2 row2_value1 row2_value2 row2_value3 row2_value4 row2_value5 row2_value6 row2_value7
3

View File

@ -14,11 +14,11 @@ echo "SELECT getClientHTTPHeader('test-' || 'key' || '-1'), getClientHTTPHeader(
#Code: 36. DB::Exception: NOT-FOUND-KEY is not in HTTP request headers
echo "SELECT getClientHTTPHeader('NOT-FOUND-KEY')"| curl -s -H 'X-Clickhouse-User: default' \
-H 'X-ClickHouse-Key: ' -H 'key1: value1' -H 'key2: value2' 'http://localhost:8123/' -d @- | grep -o -e BAD_ARGUMENTS
-H 'X-ClickHouse-Key: ' -H 'key1: value1' -H 'key2: value2' 'http://localhost:8123/' -d @- | grep -o -e "NOT-FOUND-KEY is not in HTTP request headers"
#Code: 36. DB::Exception: The header FORBIDDEN-KEY is in headers_forbidden_to_return_list, you can config it in config file.
echo "SELECT getClientHTTPHeader('FORBIDDEN-KEY')" | curl -s -H 'X-ClickHouse-User: default' -H 'X-ClickHouse-Key: ' -H 'FORBIDDEN-KEY1: forbbiden1' 'http://localhost:8123/' -d @- | grep -o -e BAD_ARGUMENTS
echo "SELECT getClientHTTPHeader('FORBIDDEN-KEY')" | curl -s -H 'X-ClickHouse-User: default' -H 'X-ClickHouse-Key: ' -H 'FORBIDDEN-KEY2: forbbiden2' 'http://localhost:8123/' -d @- | grep -o -e BAD_ARGUMENTS
#Code: 36. DB::Exception: The header FORBIDDEN-KEY is in headers_forbidden_to_return, you can config it in config file.
echo "SELECT getClientHTTPHeader('FORBIDDEN-KEY1')" | curl -s -H 'X-ClickHouse-User: default' -H 'X-ClickHouse-Key: ' \
-H 'FORBIDDEN-KEY1: forbbiden1' 'http://localhost:8123/' -d @- | grep -o -e "FORBIDDEN-KEY1 is in headers_forbidden_to_return_list"
db_name=${CLICKHOUSE_DATABASE}