| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/host_port_pair.h" | 5 #include "net/base/host_port_pair.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
| 13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 HostPortPair::HostPortPair() : port_(0) {} | 18 HostPortPair::HostPortPair() : port_(0) {} |
| 19 HostPortPair::HostPortPair(const std::string& in_host, uint16 in_port) | 19 HostPortPair::HostPortPair(const std::string& in_host, uint16 in_port) |
| 20 : host_(in_host), port_(in_port) {} | 20 : host_(in_host), port_(in_port) {} |
| 21 | 21 |
| 22 // static | 22 // static |
| 23 HostPortPair HostPortPair::FromURL(const GURL& url) { | 23 HostPortPair HostPortPair::FromURL(const GURL& url) { |
| 24 return HostPortPair(url.HostNoBrackets(), url.EffectiveIntPort()); | 24 return HostPortPair(url.HostNoBrackets(), |
| 25 static_cast<uint16>(url.EffectiveIntPort())); |
| 25 } | 26 } |
| 26 | 27 |
| 27 // static | 28 // static |
| 28 HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) { | 29 HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) { |
| 29 return HostPortPair(ipe.ToStringWithoutPort(), ipe.port()); | 30 return HostPortPair(ipe.ToStringWithoutPort(), ipe.port()); |
| 30 } | 31 } |
| 31 | 32 |
| 32 HostPortPair HostPortPair::FromString(const std::string& str) { | 33 HostPortPair HostPortPair::FromString(const std::string& str) { |
| 33 std::vector<std::string> key_port; | 34 std::vector<std::string> key_port; |
| 34 base::SplitString(str, ':', &key_port); | 35 base::SplitString(str, ':', &key_port); |
| 35 if (key_port.size() != 2) | 36 if (key_port.size() != 2) |
| 36 return HostPortPair(); | 37 return HostPortPair(); |
| 37 int port; | 38 int port; |
| 38 if (!base::StringToInt(key_port[1], &port)) | 39 if (!base::StringToInt(key_port[1], &port)) |
| 39 return HostPortPair(); | 40 return HostPortPair(); |
| 40 if (!IsPortValid(port)) | 41 if (!IsPortValid(port)) |
| 41 return HostPortPair(); | 42 return HostPortPair(); |
| 42 HostPortPair host_port_pair; | 43 HostPortPair host_port_pair; |
| 43 host_port_pair.set_host(key_port[0]); | 44 host_port_pair.set_host(key_port[0]); |
| 44 host_port_pair.set_port(port); | 45 host_port_pair.set_port(static_cast<uint16>(port)); |
| 45 return host_port_pair; | 46 return host_port_pair; |
| 46 } | 47 } |
| 47 | 48 |
| 48 std::string HostPortPair::ToString() const { | 49 std::string HostPortPair::ToString() const { |
| 49 return base::StringPrintf("%s:%u", HostForURL().c_str(), port_); | 50 return base::StringPrintf("%s:%u", HostForURL().c_str(), port_); |
| 50 } | 51 } |
| 51 | 52 |
| 52 std::string HostPortPair::HostForURL() const { | 53 std::string HostPortPair::HostForURL() const { |
| 53 // TODO(rtenneti): Add support for |host| to have '\0'. | 54 // TODO(rtenneti): Add support for |host| to have '\0'. |
| 54 if (host_.find('\0') != std::string::npos) { | 55 if (host_.find('\0') != std::string::npos) { |
| 55 std::string host_for_log(host_); | 56 std::string host_for_log(host_); |
| 56 size_t nullpos; | 57 size_t nullpos; |
| 57 while ((nullpos = host_for_log.find('\0')) != std::string::npos) { | 58 while ((nullpos = host_for_log.find('\0')) != std::string::npos) { |
| 58 host_for_log.replace(nullpos, 1, "%00"); | 59 host_for_log.replace(nullpos, 1, "%00"); |
| 59 } | 60 } |
| 60 LOG(DFATAL) << "Host has a null char: " << host_for_log; | 61 LOG(DFATAL) << "Host has a null char: " << host_for_log; |
| 61 } | 62 } |
| 62 // Check to see if the host is an IPv6 address. If so, added brackets. | 63 // Check to see if the host is an IPv6 address. If so, added brackets. |
| 63 if (host_.find(':') != std::string::npos) { | 64 if (host_.find(':') != std::string::npos) { |
| 64 DCHECK_NE(host_[0], '['); | 65 DCHECK_NE(host_[0], '['); |
| 65 return base::StringPrintf("[%s]", host_.c_str()); | 66 return base::StringPrintf("[%s]", host_.c_str()); |
| 66 } | 67 } |
| 67 | 68 |
| 68 return host_; | 69 return host_; |
| 69 } | 70 } |
| 70 | 71 |
| 71 } // namespace net | 72 } // namespace net |
| OLD | NEW |