• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NET_BASE_HOST_PORT_PAIR_H_
6 #define NET_BASE_HOST_PORT_PAIR_H_
7 #pragma once
8 
9 #include <string>
10 #include "base/basictypes.h"
11 
12 struct addrinfo;
13 class GURL;
14 
15 namespace net {
16 
17 class HostPortPair {
18  public:
19   HostPortPair();
20   // If |in_host| represents an IPv6 address, it should not bracket the address.
21   HostPortPair(const std::string& in_host, uint16 in_port);
22 
23   // Creates a HostPortPair for the origin of |url|.
24   static HostPortPair FromURL(const GURL& url);
25 
26   // Creates a HostPortPair from an addrinfo struct.
27   static HostPortPair FromAddrInfo(const struct addrinfo* ai);
28 
29   // TODO(willchan): Define a functor instead.
30   // Comparator function so this can be placed in a std::map.
31   bool operator<(const HostPortPair& other) const {
32     if (port_ != other.port_)
33       return port_ < other.port_;
34     return host_ < other.host_;
35   }
36 
37   // Equality test of contents. (Probably another violation of style guide).
Equals(const HostPortPair & other)38   bool Equals(const HostPortPair& other) const {
39     return host_ == other.host_ && port_ == other.port_;
40   }
41 
host()42   const std::string& host() const {
43     return host_;
44   }
45 
port()46   uint16 port() const {
47     return port_;
48   }
49 
set_host(const std::string & in_host)50   void set_host(const std::string& in_host) {
51     host_ = in_host;
52   }
53 
set_port(uint16 in_port)54   void set_port(uint16 in_port) {
55     port_ = in_port;
56   }
57 
58   // ToString() will convert the HostPortPair to "host:port".  If |host_| is an
59   // IPv6 literal, it will add brackets around |host_|.
60   std::string ToString() const;
61 
62   // Returns |host_|, adding IPv6 brackets if needed.
63   std::string HostForURL() const;
64 
65  private:
66   // If |host_| represents an IPv6 address, this string will not contain
67   // brackets around the address.
68   std::string host_;
69   uint16 port_;
70 };
71 
72 }  // namespace net
73 
74 #endif  // NET_BASE_HOST_PORT_PAIR_H_
75