[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 1 | // Copyright (c) 2010 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" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 6 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 7 | #include "base/string_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 8 | #include "base/stringprintf.h" |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 9 | #include "googleurl/src/gurl.h" |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 10 | #include "net/base/net_util.h" |
| 11 | #include "net/base/sys_addrinfo.h" |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 12 | |
| 13 | namespace net { |
| 14 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 15 | HostPortPair::HostPortPair() : port_(0) {} |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 16 | HostPortPair::HostPortPair(const std::string& in_host, uint16 in_port) |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 17 | : host_(in_host), port_(in_port) {} |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 18 | |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 19 | // static |
| 20 | HostPortPair HostPortPair::FromURL(const GURL& url) { |
| 21 | return HostPortPair(url.HostNoBrackets(), url.EffectiveIntPort()); |
| 22 | } |
| 23 | |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 24 | // static |
| 25 | HostPortPair HostPortPair::FromAddrInfo(const struct addrinfo* ai) { |
| 26 | return HostPortPair(NetAddressToString(ai), |
| 27 | GetPortFromSockaddr(ai->ai_addr, ai->ai_addrlen)); |
| 28 | } |
| 29 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 30 | std::string HostPortPair::ToString() const { |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 31 | return base::StringPrintf("%s:%u", HostForURL().c_str(), port_); |
[email protected] | 8c2b6815 | 2010-09-10 19:51:15 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | std::string HostPortPair::HostForURL() const { |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 35 | // Check to see if the host is an IPv6 address. If so, added brackets. |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 36 | if (host_.find(':') != std::string::npos) { |
| 37 | DCHECK_NE(host_[0], '['); |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 38 | return base::StringPrintf("[%s]", host_.c_str()); |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 39 | } |
[email protected] | 8c2b6815 | 2010-09-10 19:51:15 | [diff] [blame] | 40 | |
| 41 | return host_; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | } // namespace net |