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