Merge pull request #27385 from BraulioVM/fix/27171

Do not fail if HTTP port is not set and user tries to send request to TCP port
This commit is contained in:
tavplubix 2021-08-09 15:30:58 +03:00 committed by GitHub
commit e7290fb134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 4 deletions

View File

@ -32,6 +32,7 @@
#include <DataTypes/DataTypeLowCardinality.h>
#include <Compression/CompressionFactory.h>
#include <common/logger_useful.h>
#include <fmt/format.h>
#include <Processors/Executors/PullingAsyncPipelineExecutor.h>
@ -46,6 +47,27 @@
namespace DB
{
namespace
{
std::string formatHTTPErrorResponse(const Poco::Util::AbstractConfiguration& config)
{
std::string result = fmt::format(
"HTTP/1.0 400 Bad Request\r\n\r\n"
"Port {} is for clickhouse-client program\r\n",
config.getString("tcp_port"));
if (config.has("http_port"))
{
result += fmt::format(
"You must use port {} for HTTP.\r\n",
config.getString("http_port"));
}
return result;
}
}
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
@ -922,10 +944,8 @@ void TCPHandler::receiveHello()
*/
if (packet_type == 'G' || packet_type == 'P')
{
writeString("HTTP/1.0 400 Bad Request\r\n\r\n"
"Port " + server.config().getString("tcp_port") + " is for clickhouse-client program.\r\n"
"You must use port " + server.config().getString("http_port") + " for HTTP.\r\n",
*out);
writeString(formatHTTPErrorResponse(server.config()),
*out);
throw Exception("Client has connected to wrong port", ErrorCodes::CLIENT_HAS_CONNECTED_TO_WRONG_PORT);
}

View File

@ -0,0 +1,3 @@
<yandex>
<http_port replace="replace">31337</http_port>
</yandex>

View File

@ -0,0 +1,3 @@
<yandex>
<http_port remove="remove"></http_port>
</yandex>

View File

@ -0,0 +1,42 @@
"""Test HTTP responses given by the TCP Handler."""
from pathlib import Path
import pytest
from helpers.cluster import ClickHouseCluster
import requests
cluster = ClickHouseCluster(__file__)
node_with_http = cluster.add_instance(
'node_with_http',
main_configs=["configs/config.d/http-port-31337.xml"]
)
HTTP_PORT = 31337
node_without_http = cluster.add_instance(
'node_without_http',
main_configs=["configs/config.d/no-http-port.xml"]
)
@pytest.fixture(scope="module")
def start_cluster():
try:
cluster.start()
yield cluster
finally:
cluster.shutdown()
def test_request_to_http_full_instance(start_cluster):
response = requests.get(
f'http://{node_with_http.ip_address}:9000'
)
assert response.status_code == 400
assert str(HTTP_PORT) in response.text
def test_request_to_http_less_instance(start_cluster):
response = requests.post(
f'http://{node_without_http.ip_address}:9000'
)
assert response.status_code == 400
assert str(HTTP_PORT) not in response.text
assert "8123" not in response.text