#include "net/base/host_port_pair.h"
#include <optional>
#include <string_view>
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "base/values.h"
#include "net/base/ip_endpoint.h"
#include "net/base/url_util.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
namespace net {
namespace {
constexpr std::string_view kValueHostKey = "host";
constexpr std::string_view kValuePortKey = "port";
}
HostPortPair::HostPortPair() : port_(0) {}
HostPortPair::HostPortPair(std::string_view in_host, uint16_t in_port)
: port_(in_port), host_(in_host) {}
HostPortPair::HostPortPair(const char* in_host, uint16_t in_port)
: HostPortPair(std::string_view(in_host), in_port) {}
HostPortPair::HostPortPair(std::string&& in_host, uint16_t in_port)
: port_(in_port), host_(std::move(in_host)) {}
HostPortPair HostPortPair::FromURL(const GURL& url) {
return HostPortPair(url.HostNoBracketsPiece(),
static_cast<uint16_t>(url.EffectiveIntPort()));
}
HostPortPair HostPortPair::FromSchemeHostPort(
const url::SchemeHostPort& scheme_host_port) {
DCHECK(scheme_host_port.IsValid());
std::string_view host = scheme_host_port.host();
if (host.size() >= 2 && host.front() == '[' && host.back() == ']') {
host = host.substr(1, host.size() - 2);
}
return HostPortPair(host, scheme_host_port.port());
}
HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) {
return HostPortPair(ipe.ToStringWithoutPort(), ipe.port());
}
HostPortPair HostPortPair::FromString(std::string_view str) {
if (base::SplitStringPiece(str, ":", base::KEEP_WHITESPACE,
base::SPLIT_WANT_ALL)
.size() > 2 &&
str.front() != '[') {
return HostPortPair();
}
std::string host;
int port;
if (!ParseHostAndPort(str, &host, &port))
return HostPortPair();
if (port == -1)
return HostPortPair();
DCHECK(base::IsValueInRangeForNumericType<uint16_t>(port));
return HostPortPair(host, port);
}
std::optional<HostPortPair> HostPortPair::FromValue(const base::Value& value) {
const base::Value::Dict* dict = value.GetIfDict();
if (!dict)
return std::nullopt;
const std::string* host = dict->FindString(kValueHostKey);
std::optional<int> port = dict->FindInt(kValuePortKey);
if (host == nullptr || !port.has_value() ||
!base::IsValueInRangeForNumericType<uint16_t>(port.value())) {
return std::nullopt;
}
return HostPortPair(*host, base::checked_cast<uint16_t>(port.value()));
}
std::string HostPortPair::ToString() const {
std::string ret(HostForURL());
ret += ':';
ret += base::NumberToString(port_);
return ret;
}
std::string HostPortPair::HostForURL() const {
if (host_.find('\0') != std::string::npos) {
std::string host_for_log(host_);
size_t nullpos;
while ((nullpos = host_for_log.find('\0')) != std::string::npos) {
host_for_log.replace(nullpos, 1, "%00");
}
LOG(DFATAL) << "Host has a null char: " << host_for_log;
}
if (host_.find(':') != std::string::npos) {
DCHECK_NE(host_[0], '[');
return base::StringPrintf("[%s]", host_.c_str());
}
return host_;
}
base::Value HostPortPair::ToValue() const {
base::Value::Dict dict;
dict.Set(kValueHostKey, host_);
dict.Set(kValuePortKey, port_);
return base::Value(std::move(dict));
}
}