ClickHouse/src/Common/parseAddress.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.8 KiB
C++
Raw Normal View History

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);
port = closing_square_bracket + 1;
2017-12-28 04:28:05 +00:00
}
else
port = find_first_symbols<':'>(begin, end);
if (port != end)
{
if (*port != ':')
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Illegal port prefix passed to function parseAddress: {}", port);
++port;
UInt16 port_number;
ReadBufferFromMemory port_buf(port, end - port);
2021-09-06 15:59:46 +00:00
if (!tryReadText(port_number, port_buf) || !port_buf.eof())
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Illegal port passed to function parseAddress: {}", port);
}
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);
}
}