proxy_server.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2011 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. #ifndef NET_BASE_PROXY_SERVER_H_
  5. #define NET_BASE_PROXY_SERVER_H_
  6. #include <stdint.h>
  7. #include <ostream>
  8. #include <string>
  9. #include <tuple>
  10. #include "base/strings/string_piece.h"
  11. #include "net/base/host_port_pair.h"
  12. #include "net/base/net_export.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace net {
  15. // ProxyServer encodes the {type, host, port} of a proxy server.
  16. // ProxyServer is immutable.
  17. class NET_EXPORT ProxyServer {
  18. public:
  19. // The type of proxy. These are defined as bit flags so they can be ORed
  20. // together to pass as the |scheme_bit_field| argument to
  21. // ProxyList::RemoveProxiesWithoutScheme().
  22. enum Scheme {
  23. SCHEME_INVALID = 1 << 0,
  24. SCHEME_DIRECT = 1 << 1,
  25. SCHEME_HTTP = 1 << 2,
  26. SCHEME_SOCKS4 = 1 << 3,
  27. SCHEME_SOCKS5 = 1 << 4,
  28. SCHEME_HTTPS = 1 << 5,
  29. // A QUIC proxy is an HTTP proxy in which QUIC is used as the transport,
  30. // instead of TCP.
  31. SCHEME_QUIC = 1 << 6,
  32. };
  33. // Default copy-constructor and assignment operator are OK!
  34. // Constructs an invalid ProxyServer.
  35. ProxyServer() = default;
  36. ProxyServer(Scheme scheme, const HostPortPair& host_port_pair);
  37. // Creates a ProxyServer, validating and canonicalizing input. Port is
  38. // optional and, if not provided, will be replaced with the default port for
  39. // the given scheme. Accepts IPv6 literal `host`s with surrounding brackets
  40. // (URL format) or without (HostPortPair format). On invalid input, result
  41. // will be a `SCHEME_INVALID` ProxyServer.
  42. //
  43. // Must not be called with `SCHEME_INVALID` or `SCHEME_DIRECT`. Use
  44. // `ProxyServer()` or `Direct()` respectively to create an invalid or direct
  45. // ProxyServer.
  46. static ProxyServer FromSchemeHostAndPort(Scheme scheme,
  47. base::StringPiece host,
  48. base::StringPiece port_str);
  49. static ProxyServer FromSchemeHostAndPort(Scheme scheme,
  50. base::StringPiece host,
  51. absl::optional<uint16_t> port);
  52. // In URL format (with brackets around IPv6 literals). Must not call for
  53. // ProxyServers without a host (invalid or direct).
  54. std::string GetHost() const;
  55. // Must not call for ProxyServers without a host (invalid or direct).
  56. uint16_t GetPort() const;
  57. bool is_valid() const { return scheme_ != SCHEME_INVALID; }
  58. // Gets the proxy's scheme (i.e. SOCKS4, SOCKS5, HTTP)
  59. Scheme scheme() const { return scheme_; }
  60. // Returns true if this ProxyServer is actually just a DIRECT connection.
  61. bool is_direct() const { return scheme_ == SCHEME_DIRECT; }
  62. // Returns true if this ProxyServer is an HTTP proxy.
  63. bool is_http() const { return scheme_ == SCHEME_HTTP; }
  64. // Returns true if this ProxyServer is an HTTPS proxy. Note this
  65. // does not include proxies matched by |is_quic()|.
  66. //
  67. // Generally one should test the more general concept of
  68. // |is_secure_http_like()| to account for |is_quic()|.
  69. bool is_https() const { return scheme_ == SCHEME_HTTPS; }
  70. // Returns true if this ProxyServer is a SOCKS proxy.
  71. bool is_socks() const {
  72. return scheme_ == SCHEME_SOCKS4 || scheme_ == SCHEME_SOCKS5;
  73. }
  74. // Returns true if this ProxyServer is a QUIC proxy.
  75. bool is_quic() const { return scheme_ == SCHEME_QUIC; }
  76. // Returns true if the ProxyServer's scheme is HTTP compatible (uses HTTP
  77. // headers, has a CONNECT method for establishing tunnels).
  78. bool is_http_like() const { return is_http() || is_https() || is_quic(); }
  79. // Returns true if the proxy server has HTTP semantics, AND
  80. // the channel between the client and proxy server is secure.
  81. bool is_secure_http_like() const { return is_https() || is_quic(); }
  82. const HostPortPair& host_port_pair() const;
  83. // Returns a ProxyServer representing DIRECT connections.
  84. static ProxyServer Direct() {
  85. return ProxyServer(SCHEME_DIRECT, HostPortPair());
  86. }
  87. // Returns the default port number for a proxy server with the specified
  88. // scheme. Returns -1 if unknown.
  89. static int GetDefaultPortForScheme(Scheme scheme);
  90. bool operator==(const ProxyServer& other) const {
  91. return scheme_ == other.scheme_ &&
  92. host_port_pair_.Equals(other.host_port_pair_);
  93. }
  94. bool operator!=(const ProxyServer& other) const { return !(*this == other); }
  95. // Comparator function so this can be placed in a std::map.
  96. bool operator<(const ProxyServer& other) const {
  97. return std::tie(scheme_, host_port_pair_) <
  98. std::tie(other.scheme_, other.host_port_pair_);
  99. }
  100. private:
  101. Scheme scheme_ = SCHEME_INVALID;
  102. HostPortPair host_port_pair_;
  103. };
  104. NET_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
  105. const ProxyServer& proxy_server);
  106. typedef std::pair<HostPortPair, ProxyServer> HostPortProxyPair;
  107. } // namespace net
  108. #endif // NET_BASE_PROXY_SERVER_H_