2017-12-28 04:28:05 +00:00
|
|
|
#include <Common/parseAddress.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/find_symbols.h>
|
2017-12-28 04:28:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string, UInt16> parseAddress(const std::string & str, UInt16 default_port)
|
|
|
|
{
|
|
|
|
if (str.empty())
|
|
|
|
throw Exception("Empty address passed to function parseAddress", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
const char * begin = str.data();
|
|
|
|
const char * end = begin + str.size();
|
2020-03-18 18:26:40 +00:00
|
|
|
const char * port = end; // NOLINT
|
2017-12-28 04:28:05 +00:00
|
|
|
|
|
|
|
if (begin[0] == '[')
|
|
|
|
{
|
|
|
|
const char * closing_square_bracket = find_first_symbols<']'>(begin + 1, end);
|
|
|
|
if (closing_square_bracket >= end)
|
|
|
|
throw Exception("Illegal address passed to function parseAddress: "
|
|
|
|
"the address begins with opening square bracket, but no closing square bracket found", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
2021-05-20 18:02:45 +00:00
|
|
|
port = closing_square_bracket + 1;
|
2017-12-28 04:28:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
port = find_first_symbols<':'>(begin, end);
|
|
|
|
|
|
|
|
if (port != end)
|
|
|
|
{
|
2021-05-20 18:02:45 +00:00
|
|
|
if (*port != ':')
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS,
|
|
|
|
"Illegal port prefix passed to function parseAddress: {}", port);
|
|
|
|
|
2021-05-20 18:02:45 +00:00
|
|
|
++port;
|
|
|
|
|
2021-05-20 18:02:45 +00:00
|
|
|
UInt16 port_number;
|
2021-05-20 18:02:45 +00:00
|
|
|
ReadBufferFromMemory port_buf(port, end - port);
|
2021-09-06 15:59:46 +00:00
|
|
|
if (!tryReadText(port_number, port_buf) || !port_buf.eof())
|
2021-05-20 18:02:45 +00:00
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS,
|
2021-05-20 18:02:45 +00:00
|
|
|
"Illegal port passed to function parseAddress: {}", port);
|
2021-05-20 18:02:45 +00:00
|
|
|
}
|
2021-05-20 18:02:45 +00:00
|
|
|
return { std::string(begin, port - 1), port_number };
|
2017-12-28 04:28:05 +00:00
|
|
|
}
|
|
|
|
else if (default_port)
|
|
|
|
{
|
|
|
|
return { str, default_port };
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("The address passed to function parseAddress doesn't contain port number "
|
|
|
|
"and no 'default_port' was passed", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|