xref: /aosp_15_r20/external/cronet/net/base/proxy_string_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 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_STRING_UTIL_H_
6 #define NET_BASE_PROXY_STRING_UTIL_H_
7 
8 #include <string>
9 #include <string_view>
10 
11 #include "net/base/net_export.h"
12 #include "net/base/proxy_chain.h"
13 #include "net/base/proxy_server.h"
14 
15 namespace net {
16 
17 // Converts a PAC result element (commonly called a PAC string) to/from a
18 // ProxyServer / ProxyChain. Note that this only deals with a single proxy
19 // element separated out from the complete semicolon-delimited PAC result
20 // string.
21 //
22 // Note that PAC strings cannot currently specify multi-proxy chains.
23 //
24 // PAC result elements have the format:
25 // <scheme>" "<host>[":"<port>]
26 //
27 // Where <scheme> may be one of (case-insensitive):
28 // "DIRECT"
29 // "PROXY"
30 // "HTTPS"
31 // "SOCKS4"
32 // "SOCKS5"
33 // "SOCKS" (canonicalizes to "SOCKS4")
34 // "QUIC"
35 //
36 // If <port> is omitted, it will be assumed as the default port for the
37 // chosen scheme (via ProxyServer::GetDefaultPortForScheme()).
38 //
39 // Returns an invalid ProxyServer / ProxyChain if parsing fails.
40 //
41 // Examples:
42 //   "PROXY foopy:19"   {scheme=HTTP, host="foopy", port=19}
43 //   "DIRECT"           {scheme=DIRECT}
44 //   "SOCKS5 foopy"     {scheme=SOCKS5, host="foopy", port=1080}
45 //   "HTTPS foopy:123"  {scheme=HTTPS, host="foopy", port=123}
46 //   "QUIC foopy:123"   {scheme=QUIC, host="foopy", port=123}
47 //   "BLAH xxx:xx"      INVALID
48 NET_EXPORT ProxyChain
49 PacResultElementToProxyChain(std::string_view pac_result_element);
50 // TODO(crbug.com/1491092): Remove method once all calls are updated to use
51 // PacResultElementToProxyChain.
52 NET_EXPORT ProxyServer
53 PacResultElementToProxyServer(std::string_view pac_result_element);
54 NET_EXPORT std::string ProxyServerToPacResultElement(
55     const ProxyServer& proxy_server);
56 
57 // Converts a non-standard URI string to/from a ProxyServer.
58 //
59 // The non-standard URI strings have the format:
60 //   [<scheme>"://"]<server>[":"<port>]
61 //
62 // Where <scheme> may be one of:
63 // "http"
64 // "socks4"
65 // "socks5
66 // "socks" (equivalent to "socks5")
67 // "direct"
68 // "https"
69 // "quic"
70 //
71 // Both <scheme> and <port> are optional. If <scheme> is omitted, it will be
72 // assumed as |default_scheme|. If <port> is omitted, it will be assumed as
73 // the default port for the chosen scheme (via
74 // ProxyServer::GetDefaultPortForScheme()).
75 //
76 // If parsing fails the returned proxy will have scheme
77 // `ProxyServer::SCHEME_INVALID`.
78 //
79 // Examples (for `default_pac_scheme` = `kHttp` ):
80 //   "foopy"            {scheme=HTTP, host="foopy", port=80}
81 //   "socks://foopy"    {scheme=SOCKS5, host="foopy", port=1080}
82 //   "socks4://foopy"   {scheme=SOCKS4, host="foopy", port=1080}
83 //   "socks5://foopy"   {scheme=SOCKS5, host="foopy", port=1080}
84 //   "http://foopy:17"  {scheme=HTTP, host="foopy", port=17}
85 //   "https://foopy:17" {scheme=HTTPS, host="foopy", port=17}
86 //   "quic://foopy:17"  {scheme=QUIC, host="foopy", port=17}
87 //   "direct://"        {scheme=DIRECT}
88 //   "foopy:X"          INVALID -- bad port.
89 NET_EXPORT ProxyChain ProxyUriToProxyChain(std::string_view uri,
90                                            ProxyServer::Scheme default_scheme);
91 NET_EXPORT ProxyServer
92 ProxyUriToProxyServer(std::string_view uri, ProxyServer::Scheme default_scheme);
93 NET_EXPORT std::string ProxyServerToProxyUri(const ProxyServer& proxy_server);
94 NET_EXPORT ProxyServer
95 ProxySchemeHostAndPortToProxyServer(ProxyServer::Scheme scheme,
96                                     std::string_view host_and_port);
97 
98 // Parses the proxy scheme from the non-standard URI scheme string
99 // representation used in `ProxyUriToProxyServer()` and
100 // `ProxyServerToProxyUri()`. If no type could be matched, returns
101 // SCHEME_INVALID.
102 NET_EXPORT ProxyServer::Scheme GetSchemeFromUriScheme(std::string_view scheme);
103 
104 }  // namespace net
105 
106 #endif  // NET_BASE_PROXY_STRING_UTIL_H_
107