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" |
(...skipping 12 matching lines...) Expand all Loading... |
23 HostPortPair HostPortPair::FromURL(const GURL& url) { | 23 HostPortPair HostPortPair::FromURL(const GURL& url) { |
24 return HostPortPair(url.HostNoBrackets(), | 24 return HostPortPair(url.HostNoBrackets(), |
25 static_cast<uint16>(url.EffectiveIntPort())); | 25 static_cast<uint16>(url.EffectiveIntPort())); |
26 } | 26 } |
27 | 27 |
28 // static | 28 // static |
29 HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) { | 29 HostPortPair HostPortPair::FromIPEndPoint(const IPEndPoint& ipe) { |
30 return HostPortPair(ipe.ToStringWithoutPort(), ipe.port()); | 30 return HostPortPair(ipe.ToStringWithoutPort(), ipe.port()); |
31 } | 31 } |
32 | 32 |
| 33 // static |
33 HostPortPair HostPortPair::FromString(const std::string& str) { | 34 HostPortPair HostPortPair::FromString(const std::string& str) { |
34 std::vector<std::string> key_port; | 35 std::vector<std::string> key_port; |
35 base::SplitString(str, ':', &key_port); | 36 base::SplitString(str, ':', &key_port); |
36 if (key_port.size() != 2) | 37 if (key_port.size() != 2) |
37 return HostPortPair(); | 38 return HostPortPair(); |
38 int port; | 39 int port; |
39 if (!base::StringToInt(key_port[1], &port)) | 40 if (!base::StringToInt(key_port[1], &port)) |
40 return HostPortPair(); | 41 return HostPortPair(); |
41 if (!IsPortValid(port)) | 42 if (!IsPortValid(port)) |
42 return HostPortPair(); | 43 return HostPortPair(); |
43 HostPortPair host_port_pair; | 44 HostPortPair host_port_pair; |
44 host_port_pair.set_host(key_port[0]); | 45 host_port_pair.set_host(key_port[0]); |
45 host_port_pair.set_port(static_cast<uint16>(port)); | 46 host_port_pair.set_port(static_cast<uint16>(port)); |
46 return host_port_pair; | 47 return host_port_pair; |
47 } | 48 } |
48 | 49 |
| 50 // static |
| 51 HostPortPair HostPortPair::FromOrigin(const url::Origin& origin) { |
| 52 return HostPortPair(origin.host(), origin.port()); |
| 53 } |
| 54 |
49 std::string HostPortPair::ToString() const { | 55 std::string HostPortPair::ToString() const { |
50 std::string ret(HostForURL()); | 56 std::string ret(HostForURL()); |
51 ret += ':'; | 57 ret += ':'; |
52 ret += base::IntToString(port_); | 58 ret += base::IntToString(port_); |
53 return ret; | 59 return ret; |
54 } | 60 } |
55 | 61 |
56 std::string HostPortPair::HostForURL() const { | 62 std::string HostPortPair::HostForURL() const { |
57 // TODO(rtenneti): Add support for |host| to have '\0'. | 63 // TODO(rtenneti): Add support for |host| to have '\0'. |
58 if (host_.find('\0') != std::string::npos) { | 64 if (host_.find('\0') != std::string::npos) { |
59 std::string host_for_log(host_); | 65 std::string host_for_log(host_); |
60 size_t nullpos; | 66 size_t nullpos; |
61 while ((nullpos = host_for_log.find('\0')) != std::string::npos) { | 67 while ((nullpos = host_for_log.find('\0')) != std::string::npos) { |
62 host_for_log.replace(nullpos, 1, "%00"); | 68 host_for_log.replace(nullpos, 1, "%00"); |
63 } | 69 } |
64 LOG(DFATAL) << "Host has a null char: " << host_for_log; | 70 LOG(DFATAL) << "Host has a null char: " << host_for_log; |
65 } | 71 } |
66 // Check to see if the host is an IPv6 address. If so, added brackets. | 72 // Check to see if the host is an IPv6 address. If so, added brackets. |
67 if (host_.find(':') != std::string::npos) { | 73 if (host_.find(':') != std::string::npos) { |
68 DCHECK_NE(host_[0], '['); | 74 DCHECK_NE(host_[0], '['); |
69 return base::StringPrintf("[%s]", host_.c_str()); | 75 return base::StringPrintf("[%s]", host_.c_str()); |
70 } | 76 } |
71 | 77 |
72 return host_; | 78 return host_; |
73 } | 79 } |
74 | 80 |
75 } // namespace net | 81 } // namespace net |
OLD | NEW |