blob: 1103287fc305ef12c6a5086898759847f5810dfd [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) {}
David Benjamin506122dc2023-11-07 01:32:1834HostPortPair::HostPortPair(std::string_view in_host, uint16_t in_port)
Jan Keitel298d0292025-05-28 15:59:3935 : port_(in_port), host_(in_host) {}
[email protected]2ff8b312010-04-26 22:20:5436
[email protected]930cc742010-09-15 22:54:1037// static
38HostPortPair HostPortPair::FromURL(const GURL& url) {
pkastingcba13292014-11-20 03:35:2139 return HostPortPair(url.HostNoBrackets(),
wtc69f8ea82015-06-04 00:08:1340 static_cast<uint16_t>(url.EffectiveIntPort()));
[email protected]930cc742010-09-15 22:54:1041}
42
[email protected]6d81b482011-02-22 19:47:1943// static
Eric Orth2f01fb22021-06-17 00:52:2744HostPortPair 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 Benjamin506122dc2023-11-07 01:32:1850 std::string_view host = scheme_host_port.host();
Eric Orth2f01fb22021-06-17 00:52:2751 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]7054e78f2012-05-07 21:44:5659HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) {
60 return HostPortPair(ipe.ToStringWithoutPort(), ipe.port());
[email protected]6d81b482011-02-22 19:47:1961}
62
Chris Palmer9cbf4212017-07-19 01:54:5563// static
David Benjamin506122dc2023-11-07 01:32:1864HostPortPair HostPortPair::FromString(std::string_view str) {
Eric Orth7df9e5e2021-08-18 18:56:5465 // 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]de95f922011-10-12 07:05:1874 return HostPortPair();
Eric Orth7df9e5e2021-08-18 18:56:5475 }
76
77 std::string host;
[email protected]de95f922011-10-12 07:05:1878 int port;
Eric Orth7df9e5e2021-08-18 18:56:5479 if (!ParseHostAndPort(str, &host, &port))
[email protected]de95f922011-10-12 07:05:1880 return HostPortPair();
Eric Orth7df9e5e2021-08-18 18:56:5481
82 // Require a valid port.
83 if (port == -1)
mmenke9f3e33d2014-11-17 21:43:3984 return HostPortPair();
Eric Orth7df9e5e2021-08-18 18:56:5485 DCHECK(base::IsValueInRangeForNumericType<uint16_t>(port));
86
87 return HostPortPair(host, port);
[email protected]de95f922011-10-12 07:05:1888}
89
Eric Orthba55dfd2022-10-24 17:54:0990// static
David Benjamin506122dc2023-11-07 01:32:1891std::optional<HostPortPair> HostPortPair::FromValue(const base::Value& value) {
Eric Orthba55dfd2022-10-24 17:54:0992 const base::Value::Dict* dict = value.GetIfDict();
93 if (!dict)
David Benjamin506122dc2023-11-07 01:32:1894 return std::nullopt;
Eric Orthba55dfd2022-10-24 17:54:0995
96 const std::string* host = dict->FindString(kValueHostKey);
David Benjamin506122dc2023-11-07 01:32:1897 std::optional<int> port = dict->FindInt(kValuePortKey);
Eric Orthba55dfd2022-10-24 17:54:0998
99 if (host == nullptr || !port.has_value() ||
100 !base::IsValueInRangeForNumericType<uint16_t>(port.value())) {
David Benjamin506122dc2023-11-07 01:32:18101 return std::nullopt;
Eric Orthba55dfd2022-10-24 17:54:09102 }
103
104 return HostPortPair(*host, base::checked_cast<uint16_t>(port.value()));
105}
106
[email protected]564b4912010-03-09 16:30:42107std::string HostPortPair::ToString() const {
georgesakc15df6722014-12-02 23:52:12108 std::string ret(HostForURL());
109 ret += ':';
Raul Tambre8c1981dd2019-02-08 02:22:26110 ret += base::NumberToString(port_);
georgesakc15df6722014-12-02 23:52:12111 return ret;
[email protected]8c2b68152010-09-10 19:51:15112}
113
114std::string HostPortPair::HostForURL() const {
[email protected]257f24f2014-04-01 09:15:37115 // TODO(rtenneti): Add support for |host| to have '\0'.
[email protected]06d31b1f2014-04-15 23:24:21116 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]2ff8b312010-04-26 22:20:54124 // Check to see if the host is an IPv6 address. If so, added brackets.
[email protected]2fbaecf22010-07-22 22:20:35125 if (host_.find(':') != std::string::npos) {
126 DCHECK_NE(host_[0], '[');
[email protected]d8eb84242010-09-25 02:25:06127 return base::StringPrintf("[%s]", host_.c_str());
[email protected]2fbaecf22010-07-22 22:20:35128 }
[email protected]8c2b68152010-09-10 19:51:15129
130 return host_;
[email protected]564b4912010-03-09 16:30:42131}
132
Eric Orthba55dfd2022-10-24 17:54:09133base::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]564b4912010-03-09 16:30:42141} // namespace net