| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/gprpp/host_port.h" |
| 22 |
|
| 23 |
#include <stddef.h> |
| 24 |
|
| 25 |
#include "absl/strings/str_format.h" |
| 26 |
#include "absl/strings/string_view.h" |
| 27 |
|
| 28 |
#include <grpc/support/log.h> |
| 29 |
|
| 30 |
namespace grpc_core { |
| 31 |
|
| 32 |
std::string JoinHostPort(absl::string_view host, int port) { |
| 33 |
if (!host.empty() && host[0] != '[' && host.rfind(':') != host.npos) { |
| 34 |
// IPv6 literals must be enclosed in brackets. |
| 35 |
return absl::StrFormat("[%s]:%d", host, port); |
| 36 |
} |
| 37 |
// Ordinary non-bracketed host:port. |
| 38 |
return absl::StrFormat("%s:%d", host, port); |
| 39 |
} |
| 40 |
|
| 41 |
namespace { |
| 42 |
bool DoSplitHostPort(absl::string_view name, absl::string_view* host, |
| 43 |
absl::string_view* port, bool* has_port) { |
| 44 |
*has_port = false; |
| 45 |
if (!name.empty() && name[0] == '[') { |
| 46 |
/* Parse a bracketed host, typically an IPv6 literal. */ |
| 47 |
const size_t rbracket = name.find(']', 1); |
| 48 |
if (rbracket == absl::string_view::npos) { |
| 49 |
/* Unmatched [ */ |
| 50 |
return false; |
| 51 |
} |
| 52 |
if (rbracket == name.size() - 1) { |
| 53 |
/* ]<end> */ |
| 54 |
*port = absl::string_view(); |
| 55 |
} else if (name[rbracket + 1] == ':') { |
| 56 |
/* ]:<port?> */ |
| 57 |
*port = name.substr(rbracket + 2, name.size() - rbracket - 2); |
| 58 |
*has_port = true; |
| 59 |
} else { |
| 60 |
/* ]<invalid> */ |
| 61 |
return false; |
| 62 |
} |
| 63 |
*host = name.substr(1, rbracket - 1); |
| 64 |
if (host->find(':') == absl::string_view::npos) { |
| 65 |
/* Require all bracketed hosts to contain a colon, because a hostname or |
| 66 |
IPv4 address should never use brackets. */ |
| 67 |
*host = absl::string_view(); |
| 68 |
return false; |
| 69 |
} |
| 70 |
} else { |
| 71 |
size_t colon = name.find(':'); |
| 72 |
if (colon != absl::string_view::npos && |
| 73 |
name.find(':', colon + 1) == absl::string_view::npos) { |
| 74 |
/* Exactly 1 colon. Split into host:port. */ |
| 75 |
*host = name.substr(0, colon); |
| 76 |
*port = name.substr(colon + 1, name.size() - colon - 1); |
| 77 |
*has_port = true; |
| 78 |
} else { |
| 79 |
/* 0 or 2+ colons. Bare hostname or IPv6 litearal. */ |
| 80 |
*host = name; |
| 81 |
*port = absl::string_view(); |
| 82 |
} |
| 83 |
} |
| 84 |
return true; |
| 85 |
} |
| 86 |
} // namespace |
| 87 |
|
| 88 |
bool SplitHostPort(absl::string_view name, absl::string_view* host, |
| 89 |
absl::string_view* port) { |
| 90 |
bool unused; |
| 91 |
return DoSplitHostPort(name, host, port, &unused); |
| 92 |
} |
| 93 |
|
| 94 |
bool SplitHostPort(absl::string_view name, std::string* host, |
| 95 |
std::string* port) { |
| 96 |
GPR_DEBUG_ASSERT(host != nullptr && host->empty()); |
| 97 |
GPR_DEBUG_ASSERT(port != nullptr && port->empty()); |
| 98 |
absl::string_view host_view; |
| 99 |
absl::string_view port_view; |
| 100 |
bool has_port; |
| 101 |
const bool ret = DoSplitHostPort(name, &host_view, &port_view, &has_port); |
| 102 |
if (ret) { |
| 103 |
// We always set the host, but port is set only when DoSplitHostPort find a |
| 104 |
// port in the string, to remain backward compatible with the old |
| 105 |
// gpr_split_host_port API. |
| 106 |
*host = std::string(host_view); |
| 107 |
if (has_port) { |
| 108 |
*port = std::string(port_view); |
| 109 |
} |
| 110 |
} |
| 111 |
return ret; |
| 112 |
} |
| 113 |
|
| 114 |
} // namespace grpc_core |