Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 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" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 6 | |
David Benjamin | 506122dc | 2023-11-07 01:32:18 | [diff] [blame] | 7 | #include <optional> |
| 8 | #include <string_view> |
| 9 | |
[email protected] | 257f24f | 2014-04-01 09:15:37 | [diff] [blame] | 10 | #include "base/logging.h" |
Eric Orth | 7df9e5e | 2021-08-18 18:56:54 | [diff] [blame] | 11 | #include "base/numerics/safe_conversions.h" |
[email protected] | 4b35521 | 2013-06-11 10:35:19 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 13 | #include "base/strings/string_split.h" |
[email protected] | 4b35521 | 2013-06-11 10:35:19 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
| 15 | #include "base/strings/stringprintf.h" |
xunjieli | 69720dd | 2017-01-30 15:36:29 | [diff] [blame] | 16 | #include "base/trace_event/memory_usage_estimator.h" |
Eric Orth | ba55dfd | 2022-10-24 17:54:09 | [diff] [blame] | 17 | #include "base/values.h" |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 18 | #include "net/base/ip_endpoint.h" |
Eric Orth | 7df9e5e | 2021-08-18 18:56:54 | [diff] [blame] | 19 | #include "net/base/url_util.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 20 | #include "url/gurl.h" |
Eric Orth | 2f01fb2 | 2021-06-17 00:52:27 | [diff] [blame] | 21 | #include "url/scheme_host_port.h" |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 22 | |
| 23 | namespace net { |
| 24 | |
Eric Orth | ba55dfd | 2022-10-24 17:54:09 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | // Value dictionary keys |
David Benjamin | 506122dc | 2023-11-07 01:32:18 | [diff] [blame] | 28 | constexpr std::string_view kValueHostKey = "host"; |
| 29 | constexpr std::string_view kValuePortKey = "port"; |
Eric Orth | ba55dfd | 2022-10-24 17:54:09 | [diff] [blame] | 30 | |
| 31 | } // namespace |
| 32 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 33 | HostPortPair::HostPortPair() : port_(0) {} |
David Benjamin | 506122dc | 2023-11-07 01:32:18 | [diff] [blame] | 34 | HostPortPair::HostPortPair(std::string_view in_host, uint16_t in_port) |
Jan Keitel | 298d029 | 2025-05-28 15:59:39 | [diff] [blame] | 35 | : port_(in_port), host_(in_host) {} |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 36 | |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 37 | // static |
| 38 | HostPortPair HostPortPair::FromURL(const GURL& url) { |
pkasting | cba1329 | 2014-11-20 03:35:21 | [diff] [blame] | 39 | return HostPortPair(url.HostNoBrackets(), |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 40 | static_cast<uint16_t>(url.EffectiveIntPort())); |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 43 | // static |
Eric Orth | 2f01fb2 | 2021-06-17 00:52:27 | [diff] [blame] | 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. |
David Benjamin | 506122dc | 2023-11-07 01:32:18 | [diff] [blame] | 50 | std::string_view host = scheme_host_port.host(); |
Eric Orth | 2f01fb2 | 2021-06-17 00:52:27 | [diff] [blame] | 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 |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 59 | HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) { |
| 60 | return HostPortPair(ipe.ToStringWithoutPort(), ipe.port()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 61 | } |
| 62 | |
Chris Palmer | 9cbf421 | 2017-07-19 01:54:55 | [diff] [blame] | 63 | // static |
David Benjamin | 506122dc | 2023-11-07 01:32:18 | [diff] [blame] | 64 | HostPortPair HostPortPair::FromString(std::string_view str) { |
Eric Orth | 7df9e5e | 2021-08-18 18:56:54 | [diff] [blame] | 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() != '[') { |
[email protected] | de95f92 | 2011-10-12 07:05:18 | [diff] [blame] | 74 | return HostPortPair(); |
Eric Orth | 7df9e5e | 2021-08-18 18:56:54 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | std::string host; |
[email protected] | de95f92 | 2011-10-12 07:05:18 | [diff] [blame] | 78 | int port; |
Eric Orth | 7df9e5e | 2021-08-18 18:56:54 | [diff] [blame] | 79 | if (!ParseHostAndPort(str, &host, &port)) |
[email protected] | de95f92 | 2011-10-12 07:05:18 | [diff] [blame] | 80 | return HostPortPair(); |
Eric Orth | 7df9e5e | 2021-08-18 18:56:54 | [diff] [blame] | 81 | |
| 82 | // Require a valid port. |
| 83 | if (port == -1) |
mmenke | 9f3e33d | 2014-11-17 21:43:39 | [diff] [blame] | 84 | return HostPortPair(); |
Eric Orth | 7df9e5e | 2021-08-18 18:56:54 | [diff] [blame] | 85 | DCHECK(base::IsValueInRangeForNumericType<uint16_t>(port)); |
| 86 | |
| 87 | return HostPortPair(host, port); |
[email protected] | de95f92 | 2011-10-12 07:05:18 | [diff] [blame] | 88 | } |
| 89 | |
Eric Orth | ba55dfd | 2022-10-24 17:54:09 | [diff] [blame] | 90 | // static |
David Benjamin | 506122dc | 2023-11-07 01:32:18 | [diff] [blame] | 91 | std::optional<HostPortPair> HostPortPair::FromValue(const base::Value& value) { |
Eric Orth | ba55dfd | 2022-10-24 17:54:09 | [diff] [blame] | 92 | const base::Value::Dict* dict = value.GetIfDict(); |
| 93 | if (!dict) |
David Benjamin | 506122dc | 2023-11-07 01:32:18 | [diff] [blame] | 94 | return std::nullopt; |
Eric Orth | ba55dfd | 2022-10-24 17:54:09 | [diff] [blame] | 95 | |
| 96 | const std::string* host = dict->FindString(kValueHostKey); |
David Benjamin | 506122dc | 2023-11-07 01:32:18 | [diff] [blame] | 97 | std::optional<int> port = dict->FindInt(kValuePortKey); |
Eric Orth | ba55dfd | 2022-10-24 17:54:09 | [diff] [blame] | 98 | |
| 99 | if (host == nullptr || !port.has_value() || |
| 100 | !base::IsValueInRangeForNumericType<uint16_t>(port.value())) { |
David Benjamin | 506122dc | 2023-11-07 01:32:18 | [diff] [blame] | 101 | return std::nullopt; |
Eric Orth | ba55dfd | 2022-10-24 17:54:09 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return HostPortPair(*host, base::checked_cast<uint16_t>(port.value())); |
| 105 | } |
| 106 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 107 | std::string HostPortPair::ToString() const { |
georgesak | c15df672 | 2014-12-02 23:52:12 | [diff] [blame] | 108 | std::string ret(HostForURL()); |
| 109 | ret += ':'; |
Raul Tambre | 8c1981dd | 2019-02-08 02:22:26 | [diff] [blame] | 110 | ret += base::NumberToString(port_); |
georgesak | c15df672 | 2014-12-02 23:52:12 | [diff] [blame] | 111 | return ret; |
[email protected] | 8c2b6815 | 2010-09-10 19:51:15 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | std::string HostPortPair::HostForURL() const { |
[email protected] | 257f24f | 2014-04-01 09:15:37 | [diff] [blame] | 115 | // TODO(rtenneti): Add support for |host| to have '\0'. |
[email protected] | 06d31b1f | 2014-04-15 23:24:21 | [diff] [blame] | 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 | } |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 124 | // Check to see if the host is an IPv6 address. If so, added brackets. |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 125 | if (host_.find(':') != std::string::npos) { |
| 126 | DCHECK_NE(host_[0], '['); |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 127 | return base::StringPrintf("[%s]", host_.c_str()); |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 128 | } |
[email protected] | 8c2b6815 | 2010-09-10 19:51:15 | [diff] [blame] | 129 | |
| 130 | return host_; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 131 | } |
| 132 | |
Eric Orth | ba55dfd | 2022-10-24 17:54:09 | [diff] [blame] | 133 | 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 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 141 | } // namespace net |