xref: /aosp_15_r20/external/cronet/net/base/host_port_pair.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/base/host_port_pair.h"
6 
7 #include <optional>
8 #include <string_view>
9 
10 #include "base/logging.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/trace_event/memory_usage_estimator.h"
17 #include "base/values.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/base/url_util.h"
20 #include "url/gurl.h"
21 #include "url/scheme_host_port.h"
22 
23 namespace net {
24 
25 namespace {
26 
27 // Value dictionary keys
28 constexpr std::string_view kValueHostKey = "host";
29 constexpr std::string_view kValuePortKey = "port";
30 
31 }  // namespace
32 
HostPortPair()33 HostPortPair::HostPortPair() : port_(0) {}
HostPortPair(std::string_view in_host,uint16_t in_port)34 HostPortPair::HostPortPair(std::string_view in_host, uint16_t in_port)
35     : host_(in_host), port_(in_port) {}
36 
37 // static
FromURL(const GURL & url)38 HostPortPair HostPortPair::FromURL(const GURL& url) {
39   return HostPortPair(url.HostNoBrackets(),
40                       static_cast<uint16_t>(url.EffectiveIntPort()));
41 }
42 
43 // static
FromSchemeHostPort(const url::SchemeHostPort & scheme_host_port)44 HostPortPair HostPortPair::FromSchemeHostPort(
45     const url::SchemeHostPort& scheme_host_port) {
46   DCHECK(scheme_host_port.IsValid());
47 
48   // HostPortPair assumes hostnames do not have surrounding brackets (as is
49   // commonly used for IPv6 literals), so strip them if present.
50   std::string_view host = scheme_host_port.host();
51   if (host.size() >= 2 && host.front() == '[' && host.back() == ']') {
52     host = host.substr(1, host.size() - 2);
53   }
54 
55   return HostPortPair(host, scheme_host_port.port());
56 }
57 
58 // static
FromIPEndPoint(const IPEndPoint & ipe)59 HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) {
60   return HostPortPair(ipe.ToStringWithoutPort(), ipe.port());
61 }
62 
63 // static
FromString(std::string_view str)64 HostPortPair HostPortPair::FromString(std::string_view str) {
65   // Input with more than one ':' is ambiguous unless it contains an IPv6
66   // literal (signified by starting with a '['). ParseHostAndPort() allows such
67   // input and always uses the last ':' as the host/port delimiter, but because
68   // HostPortPair often deals with IPv6 literals without brackets, disallow such
69   // input here to prevent a common error.
70   if (base::SplitStringPiece(str, ":", base::KEEP_WHITESPACE,
71                              base::SPLIT_WANT_ALL)
72               .size() > 2 &&
73       str.front() != '[') {
74     return HostPortPair();
75   }
76 
77   std::string host;
78   int port;
79   if (!ParseHostAndPort(str, &host, &port))
80     return HostPortPair();
81 
82   // Require a valid port.
83   if (port == -1)
84     return HostPortPair();
85   DCHECK(base::IsValueInRangeForNumericType<uint16_t>(port));
86 
87   return HostPortPair(host, port);
88 }
89 
90 // static
FromValue(const base::Value & value)91 std::optional<HostPortPair> HostPortPair::FromValue(const base::Value& value) {
92   const base::Value::Dict* dict = value.GetIfDict();
93   if (!dict)
94     return std::nullopt;
95 
96   const std::string* host = dict->FindString(kValueHostKey);
97   std::optional<int> port = dict->FindInt(kValuePortKey);
98 
99   if (host == nullptr || !port.has_value() ||
100       !base::IsValueInRangeForNumericType<uint16_t>(port.value())) {
101     return std::nullopt;
102   }
103 
104   return HostPortPair(*host, base::checked_cast<uint16_t>(port.value()));
105 }
106 
ToString() const107 std::string HostPortPair::ToString() const {
108   std::string ret(HostForURL());
109   ret += ':';
110   ret += base::NumberToString(port_);
111   return ret;
112 }
113 
HostForURL() const114 std::string HostPortPair::HostForURL() const {
115   // TODO(rtenneti): Add support for |host| to have '\0'.
116   if (host_.find('\0') != std::string::npos) {
117     std::string host_for_log(host_);
118     size_t nullpos;
119     while ((nullpos = host_for_log.find('\0')) != std::string::npos) {
120       host_for_log.replace(nullpos, 1, "%00");
121     }
122     LOG(DFATAL) << "Host has a null char: " << host_for_log;
123   }
124   // Check to see if the host is an IPv6 address.  If so, added brackets.
125   if (host_.find(':') != std::string::npos) {
126     DCHECK_NE(host_[0], '[');
127     return base::StringPrintf("[%s]", host_.c_str());
128   }
129 
130   return host_;
131 }
132 
ToValue() const133 base::Value HostPortPair::ToValue() const {
134   base::Value::Dict dict;
135   dict.Set(kValueHostKey, host_);
136   dict.Set(kValuePortKey, port_);
137 
138   return base::Value(std::move(dict));
139 }
140 
141 }  // namespace net
142