1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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"
6 
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/trace_event/memory_usage_estimator.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/parse_number.h"
15 #include "net/base/port_util.h"
16 #include "url/gurl.h"
17 
18 namespace net {
19 
HostPortPair()20 HostPortPair::HostPortPair() : port_(0) {}
HostPortPair(const std::string & in_host,uint16_t in_port)21 HostPortPair::HostPortPair(const std::string& in_host, uint16_t in_port)
22     : host_(in_host), port_(in_port) {
23 }
24 
25 // static
FromURL(const GURL & url)26 HostPortPair HostPortPair::FromURL(const GURL& url) {
27   return HostPortPair(url.HostNoBrackets(),
28                       static_cast<uint16_t>(url.EffectiveIntPort()));
29 }
30 
31 // static
FromIPEndPoint(const IPEndPoint & ipe)32 HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) {
33   return HostPortPair(ipe.ToStringWithoutPort(), ipe.port());
34 }
35 
36 // static
FromString(const std::string & str)37 HostPortPair HostPortPair::FromString(const std::string& str) {
38   std::vector<base::StringPiece> key_port = base::SplitStringPiece(
39       str, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
40   if (key_port.size() != 2)
41     return HostPortPair();
42   int port;
43   if (!ParseInt32(key_port[1], ParseIntFormat::NON_NEGATIVE, &port))
44     return HostPortPair();
45   if (!IsPortValid(port))
46     return HostPortPair();
47   HostPortPair host_port_pair;
48   host_port_pair.set_host(key_port[0].as_string());
49   host_port_pair.set_port(static_cast<uint16_t>(port));
50   return host_port_pair;
51 }
52 
ToString() const53 std::string HostPortPair::ToString() const {
54   std::string ret(HostForURL());
55   ret += ':';
56   ret += base::NumberToString(port_);
57   return ret;
58 }
59 
HostForURL() const60 std::string HostPortPair::HostForURL() const {
61   // TODO(rtenneti): Add support for |host| to have '\0'.
62   if (host_.find('\0') != std::string::npos) {
63     std::string host_for_log(host_);
64     size_t nullpos;
65     while ((nullpos = host_for_log.find('\0')) != std::string::npos) {
66       host_for_log.replace(nullpos, 1, "%00");
67     }
68     LOG(DFATAL) << "Host has a null char: " << host_for_log;
69   }
70   // Check to see if the host is an IPv6 address.  If so, added brackets.
71   if (host_.find(':') != std::string::npos) {
72     DCHECK_NE(host_[0], '[');
73     return base::StringPrintf("[%s]", host_.c_str());
74   }
75 
76   return host_;
77 }
78 
EstimateMemoryUsage() const79 size_t HostPortPair::EstimateMemoryUsage() const {
80   return base::trace_event::EstimateMemoryUsage(host_);
81 }
82 
83 }  // namespace net
84