1 | #include <Common/parseAddress.h> |
2 | #include <Common/Exception.h> |
3 | #include <IO/ReadHelpers.h> |
4 | #include <common/find_symbols.h> |
5 | |
6 | |
7 | namespace DB |
8 | { |
9 | |
10 | namespace ErrorCodes |
11 | { |
12 | extern const int BAD_ARGUMENTS; |
13 | } |
14 | |
15 | std::pair<std::string, UInt16> parseAddress(const std::string & str, UInt16 default_port) |
16 | { |
17 | if (str.empty()) |
18 | throw Exception("Empty address passed to function parseAddress" , ErrorCodes::BAD_ARGUMENTS); |
19 | |
20 | const char * begin = str.data(); |
21 | const char * end = begin + str.size(); |
22 | const char * port = end; |
23 | |
24 | if (begin[0] == '[') |
25 | { |
26 | const char * closing_square_bracket = find_first_symbols<']'>(begin + 1, end); |
27 | if (closing_square_bracket >= end) |
28 | throw Exception("Illegal address passed to function parseAddress: " |
29 | "the address begins with opening square bracket, but no closing square bracket found" , ErrorCodes::BAD_ARGUMENTS); |
30 | |
31 | port = find_first_symbols<':'>(closing_square_bracket + 1, end); |
32 | } |
33 | else |
34 | port = find_first_symbols<':'>(begin, end); |
35 | |
36 | if (port != end) |
37 | { |
38 | UInt16 port_number = parse<UInt16>(port + 1); |
39 | return { std::string(begin, port), port_number }; |
40 | } |
41 | else if (default_port) |
42 | { |
43 | return { str, default_port }; |
44 | } |
45 | else |
46 | throw Exception("The address passed to function parseAddress doesn't contain port number " |
47 | "and no 'default_port' was passed" , ErrorCodes::BAD_ARGUMENTS); |
48 | } |
49 | |
50 | } |
51 | |