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