blob: 5164fcf0c2b910408d247555e599434c7b48a42d [file] [log] [blame]
[email protected]564b4912010-03-09 16:30:421// 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]d8eb84242010-09-25 02:25:066
[email protected]564b4912010-03-09 16:30:427#include "base/string_util.h"
[email protected]d8eb84242010-09-25 02:25:068#include "base/stringprintf.h"
[email protected]930cc742010-09-15 22:54:109#include "googleurl/src/gurl.h"
[email protected]6d81b482011-02-22 19:47:1910#include "net/base/net_util.h"
11#include "net/base/sys_addrinfo.h"
[email protected]564b4912010-03-09 16:30:4212
13namespace net {
14
[email protected]2fbaecf22010-07-22 22:20:3515HostPortPair::HostPortPair() : port_(0) {}
[email protected]2ff8b312010-04-26 22:20:5416HostPortPair::HostPortPair(const std::string& in_host, uint16 in_port)
[email protected]2fbaecf22010-07-22 22:20:3517 : host_(in_host), port_(in_port) {}
[email protected]2ff8b312010-04-26 22:20:5418
[email protected]930cc742010-09-15 22:54:1019// static
20HostPortPair HostPortPair::FromURL(const GURL& url) {
21 return HostPortPair(url.HostNoBrackets(), url.EffectiveIntPort());
22}
23
[email protected]6d81b482011-02-22 19:47:1924// static
25HostPortPair HostPortPair::FromAddrInfo(const struct addrinfo* ai) {
26 return HostPortPair(NetAddressToString(ai),
27 GetPortFromSockaddr(ai->ai_addr, ai->ai_addrlen));
28}
29
[email protected]564b4912010-03-09 16:30:4230std::string HostPortPair::ToString() const {
[email protected]d8eb84242010-09-25 02:25:0631 return base::StringPrintf("%s:%u", HostForURL().c_str(), port_);
[email protected]8c2b68152010-09-10 19:51:1532}
33
34std::string HostPortPair::HostForURL() const {
[email protected]2ff8b312010-04-26 22:20:5435 // Check to see if the host is an IPv6 address. If so, added brackets.
[email protected]2fbaecf22010-07-22 22:20:3536 if (host_.find(':') != std::string::npos) {
37 DCHECK_NE(host_[0], '[');
[email protected]d8eb84242010-09-25 02:25:0638 return base::StringPrintf("[%s]", host_.c_str());
[email protected]2fbaecf22010-07-22 22:20:3539 }
[email protected]8c2b68152010-09-10 19:51:1540
41 return host_;
[email protected]564b4912010-03-09 16:30:4242}
43
44} // namespace net