This source file includes following definitions.
- port_
- FromURL
- FromIPEndPoint
- FromString
- ToString
- HostForURL
#include "net/base/host_port_pair.h"
#include "base/logging.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 "net/base/ip_endpoint.h"
#include "url/gurl.h"
namespace net {
HostPortPair::HostPortPair() : port_(0) {}
HostPortPair::HostPortPair(const std::string& in_host, uint16 in_port)
: host_(in_host), port_(in_port) {}
HostPortPair HostPortPair::FromURL(const GURL& url) {
return HostPortPair(url.HostNoBrackets(), url.EffectiveIntPort());
}
HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) {
return HostPortPair(ipe.ToStringWithoutPort(), ipe.port());
}
HostPortPair HostPortPair::FromString(const std::string& str) {
std::vector<std::string> key_port;
base::SplitString(str, ':', &key_port);
if (key_port.size() != 2)
return HostPortPair();
int port;
if (!base::StringToInt(key_port[1], &port))
return HostPortPair();
DCHECK_LT(port, 1 << 16);
HostPortPair host_port_pair;
host_port_pair.set_host(key_port[0]);
host_port_pair.set_port(port);
return host_port_pair;
}
std::string HostPortPair::ToString() const {
return base::StringPrintf("%s:%u", HostForURL().c_str(), port_);
}
std::string HostPortPair::HostForURL() const {
LOG_IF(DFATAL, host_.find('\0') != std::string::npos)
<< "Host has a null char: " << host_;
if (host_.find(':') != std::string::npos) {
DCHECK_NE(host_[0], '[');
return base::StringPrintf("[%s]", host_.c_str());
}
return host_;
}
}