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