blob: d5e879169b44e62e578c8dac9f12b432c0341e94 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]564b4912010-03-09 16:30:422// 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]d8eb84242010-09-25 02:25:066
David Benjamin506122dc2023-11-07 01:32:187#include <optional>
8#include <string_view>
9
[email protected]257f24f2014-04-01 09:15:3710#include "base/logging.h"
Eric Orth7df9e5e2021-08-18 18:56:5411#include "base/numerics/safe_conversions.h"
[email protected]4b355212013-06-11 10:35:1912#include "base/strings/string_number_conversions.h"
[email protected]d778e0422013-03-06 18:10:2213#include "base/strings/string_split.h"
[email protected]4b355212013-06-11 10:35:1914#include "base/strings/string_util.h"
15#include "base/strings/stringprintf.h"
xunjieli69720dd2017-01-30 15:36:2916#include "base/trace_event/memory_usage_estimator.h"
Eric Orthba55dfd2022-10-24 17:54:0917#include "base/values.h"
[email protected]7054e78f2012-05-07 21:44:5618#include "net/base/ip_endpoint.h"
Eric Orth7df9e5e2021-08-18 18:56:5419#include "net/base/url_util.h"
[email protected]f89276a72013-07-12 06:41:5420#include "url/gurl.h"
Eric Orth2f01fb22021-06-17 00:52:2721#include "url/scheme_host_port.h"
[email protected]564b4912010-03-09 16:30:4222
23namespace net {
24
Eric Orthba55dfd2022-10-24 17:54:0925namespace {
26
27// Value dictionary keys
David Benjamin506122dc2023-11-07 01:32:1828constexpr std::string_view kValueHostKey = "host";
29constexpr std::string_view kValuePortKey = "port";
Eric Orthba55dfd2022-10-24 17:54:0930
31} // namespace
32
[email protected]2fbaecf22010-07-22 22:20:3533HostPortPair::HostPortPair() : port_(0) {}
Matt Menkec524062b2025-10-01 16:03:1234
David Benjamin506122dc2023-11-07 01:32:1835HostPortPair::HostPortPair(std::string_view in_host, uint16_t in_port)
Jan Keitel298d0292025-05-28 15:59:3936 : port_(in_port), host_(in_host) {}
[email protected]2ff8b312010-04-26 22:20:5437
Matt Menkec524062b2025-10-01 16:03:1238HostPortPair::HostPortPair(const char* in_host, uint16_t in_port)
39 : HostPortPair(std::string_view(in_host), in_port) {}
40
41HostPortPair::HostPortPair(std::string&& in_host, uint16_t in_port)
42 : port_(in_port), host_(std::move(in_host)) {}
43
[email protected]930cc742010-09-15 22:54:1044// static
45HostPortPair HostPortPair::FromURL(const GURL& url) {
Matt Menkec524062b2025-10-01 16:03:1246 return HostPortPair(url.HostNoBracketsPiece(),
wtc69f8ea82015-06-04 00:08:1347 static_cast<uint16_t>(url.EffectiveIntPort()));
[email protected]930cc742010-09-15 22:54:1048}
49
[email protected]6d81b482011-02-22 19:47:1950// static
Eric Orth2f01fb22021-06-17 00:52:2751HostPortPair HostPortPair::FromSchemeHostPort(
52 const url::SchemeHostPort& scheme_host_port) {
53 DCHECK(scheme_host_port.IsValid());
54
55 // HostPortPair assumes hostnames do not have surrounding brackets (as is
56 // commonly used for IPv6 literals), so strip them if present.
David Benjamin506122dc2023-11-07 01:32:1857 std::string_view host = scheme_host_port.host();
Eric Orth2f01fb22021-06-17 00:52:2758 if (host.size() >= 2 && host.front() == '[' && host.back() == ']') {
59 host = host.substr(1, host.size() - 2);
60 }
61
62 return HostPortPair(host, scheme_host_port.port());
63}
64
65// static
[email protected]7054e78f2012-05-07 21:44:5666HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) {
67 return HostPortPair(ipe.ToStringWithoutPort(), ipe.port());
[email protected]6d81b482011-02-22 19:47:1968}
69
Chris Palmer9cbf4212017-07-19 01:54:5570// static
David Benjamin506122dc2023-11-07 01:32:1871HostPortPair HostPortPair::FromString(std::string_view str) {
Eric Orth7df9e5e2021-08-18 18:56:5472 // Input with more than one ':' is ambiguous unless it contains an IPv6
73 // literal (signified by starting with a '['). ParseHostAndPort() allows such
74 // input and always uses the last ':' as the host/port delimiter, but because
75 // HostPortPair often deals with IPv6 literals without brackets, disallow such
76 // input here to prevent a common error.
77 if (base::SplitStringPiece(str, ":", base::KEEP_WHITESPACE,
78 base::SPLIT_WANT_ALL)
79 .size() > 2 &&
80 str.front() != '[') {
[email protected]de95f922011-10-12 07:05:1881 return HostPortPair();
Eric Orth7df9e5e2021-08-18 18:56:5482 }
83
84 std::string host;
[email protected]de95f922011-10-12 07:05:1885 int port;
Eric Orth7df9e5e2021-08-18 18:56:5486 if (!ParseHostAndPort(str, &host, &port))
[email protected]de95f922011-10-12 07:05:1887 return HostPortPair();
Eric Orth7df9e5e2021-08-18 18:56:5488
89 // Require a valid port.
90 if (port == -1)
mmenke9f3e33d2014-11-17 21:43:3991 return HostPortPair();
Eric Orth7df9e5e2021-08-18 18:56:5492 DCHECK(base::IsValueInRangeForNumericType<uint16_t>(port));
93
94 return HostPortPair(host, port);
[email protected]de95f922011-10-12 07:05:1895}
96
Eric Orthba55dfd2022-10-24 17:54:0997// static
David Benjamin506122dc2023-11-07 01:32:1898std::optional<HostPortPair> HostPortPair::FromValue(const base::Value& value) {
Eric Orthba55dfd2022-10-24 17:54:0999 const base::Value::Dict* dict = value.GetIfDict();
100 if (!dict)
David Benjamin506122dc2023-11-07 01:32:18101 return std::nullopt;
Eric Orthba55dfd2022-10-24 17:54:09102
103 const std::string* host = dict->FindString(kValueHostKey);
David Benjamin506122dc2023-11-07 01:32:18104 std::optional<int> port = dict->FindInt(kValuePortKey);
Eric Orthba55dfd2022-10-24 17:54:09105
106 if (host == nullptr || !port.has_value() ||
107 !base::IsValueInRangeForNumericType<uint16_t>(port.value())) {
David Benjamin506122dc2023-11-07 01:32:18108 return std::nullopt;
Eric Orthba55dfd2022-10-24 17:54:09109 }
110
111 return HostPortPair(*host, base::checked_cast<uint16_t>(port.value()));
112}
113
[email protected]564b4912010-03-09 16:30:42114std::string HostPortPair::ToString() const {
georgesakc15df6722014-12-02 23:52:12115 std::string ret(HostForURL());
116 ret += ':';
Raul Tambre8c1981dd2019-02-08 02:22:26117 ret += base::NumberToString(port_);
georgesakc15df6722014-12-02 23:52:12118 return ret;
[email protected]8c2b68152010-09-10 19:51:15119}
120
121std::string HostPortPair::HostForURL() const {
[email protected]257f24f2014-04-01 09:15:37122 // TODO(rtenneti): Add support for |host| to have '\0'.
[email protected]06d31b1f2014-04-15 23:24:21123 if (host_.find('\0') != std::string::npos) {
124 std::string host_for_log(host_);
125 size_t nullpos;
126 while ((nullpos = host_for_log.find('\0')) != std::string::npos) {
127 host_for_log.replace(nullpos, 1, "%00");
128 }
129 LOG(DFATAL) << "Host has a null char: " << host_for_log;
130 }
[email protected]2ff8b312010-04-26 22:20:54131 // Check to see if the host is an IPv6 address. If so, added brackets.
[email protected]2fbaecf22010-07-22 22:20:35132 if (host_.find(':') != std::string::npos) {
133 DCHECK_NE(host_[0], '[');
[email protected]d8eb84242010-09-25 02:25:06134 return base::StringPrintf("[%s]", host_.c_str());
[email protected]2fbaecf22010-07-22 22:20:35135 }
[email protected]8c2b68152010-09-10 19:51:15136
137 return host_;
[email protected]564b4912010-03-09 16:30:42138}
139
Eric Orthba55dfd2022-10-24 17:54:09140base::Value HostPortPair::ToValue() const {
141 base::Value::Dict dict;
142 dict.Set(kValueHostKey, host_);
143 dict.Set(kValuePortKey, port_);
144
145 return base::Value(std::move(dict));
146}
147
[email protected]564b4912010-03-09 16:30:42148} // namespace net