host_port.cc
Go to the documentation of this file.
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 
20 
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 
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> */
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;
82  }
83  }
84  return true;
85 }
86 } // namespace
87 
90  bool unused;
91  return DoSplitHostPort(name, host, port, &unused);
92 }
93 
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
log.h
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:103
absl::string_view::find
size_type find(string_view s, size_type pos=0) const noexcept
Definition: abseil-cpp/absl/strings/string_view.cc:81
grpc_core
Definition: call_metric_recorder.h:31
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::SplitHostPort
bool SplitHostPort(absl::string_view name, absl::string_view *host, absl::string_view *port)
Definition: host_port.cc:88
setup.name
name
Definition: setup.py:542
grpc_core::JoinHostPort
std::string JoinHostPort(absl::string_view host, int port)
Definition: host_port.cc:32
host_port.h
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
absl::string_view::rfind
size_type rfind(string_view s, size_type pos=npos) const noexcept
Definition: abseil-cpp/absl/strings/string_view.cc:101
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
string_view
absl::string_view string_view
Definition: attr.cc:22
absl::string_view::empty
constexpr bool empty() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:292
absl::string_view::npos
static constexpr size_type npos
Definition: abseil-cpp/absl/strings/string_view.h:182
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:01