15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_BASE_HOST_PORT_PAIR_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_BASE_HOST_PORT_PAIR_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GURL;
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IPEndPoint;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT HostPortPair {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HostPortPair();
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |in_host| represents an IPv6 address, it should not bracket the address.
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HostPortPair(const std::string& in_host, uint16 in_port);
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a HostPortPair for the origin of |url|.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static HostPortPair FromURL(const GURL& url);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a HostPortPair from an IPEndPoint.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static HostPortPair FromIPEndPoint(const IPEndPoint& ipe);
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a HostPortPair from a string formatted in same manner as
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ToString().
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static HostPortPair FromString(const std::string& str);
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(willchan): Define a functor instead.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Comparator function so this can be placed in a std::map.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator<(const HostPortPair& other) const {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (port_ != other.port_)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return port_ < other.port_;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return host_ < other.host_;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Equality test of contents. (Probably another violation of style guide).
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Equals(const HostPortPair& other) const {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return host_ == other.host_ && port_ == other.port_;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsEmpty() const {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return host_.empty() && port_ == 0;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& host() const {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return host_;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint16 port() const {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return port_;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_host(const std::string& in_host) {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    host_ = in_host;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_port(uint16 in_port) {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    port_ = in_port;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ToString() will convert the HostPortPair to "host:port".  If |host_| is an
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // IPv6 literal, it will add brackets around |host_|.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string ToString() const;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns |host_|, adding IPv6 brackets if needed.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string HostForURL() const;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |host_| represents an IPv6 address, this string will not contain
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // brackets around the address.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string host_;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint16 port_;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_BASE_HOST_PORT_PAIR_H_
84