• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/40284947): 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 ProxyChain.
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                                            bool is_quic_allowed = false);
92 
93 // Converts a bracketed string of non-standard uris to a multi-proxy
94 // `net::ProxyChain`.
95 //
96 // The `uris` parameter may contain 1 or more non-standard URIs but not 0 which
97 // would result in an invalid `ProxyChain()`.
98 //
99 // If brackets are omitted from the `uris` string, it MUST be a single
100 // non-standard URI. Otherwise, an invalid `ProxyChain()` will be returned.
101 //
102 //
103 // The bracketed non-standard URIs strings have the format:
104 //   [x y z] where individual non-standard uris are space delimited and
105 //   encompassed within brackets.
106 //   ex. [https://foopy:17 https://hoopy:17]
107 //
108 // Each non-standard URI string follows the format described in the
109 // documentation for the `ProxyUriToProxyChain` function.
110 NET_EXPORT ProxyChain
111 MultiProxyUrisToProxyChain(std::string_view uris,
112                            ProxyServer::Scheme default_scheme,
113                            bool is_quic_allowed = false);
114 NET_EXPORT ProxyServer ProxyUriToProxyServer(std::string_view uri,
115                                              ProxyServer::Scheme default_scheme,
116                                              bool is_quic_allowed = false);
117 NET_EXPORT std::string ProxyServerToProxyUri(const ProxyServer& proxy_server);
118 NET_EXPORT ProxyServer
119 ProxySchemeHostAndPortToProxyServer(ProxyServer::Scheme scheme,
120                                     std::string_view host_and_port);
121 
122 // Parses the proxy scheme from the non-standard URI scheme string
123 // representation used in `ProxyUriToProxyServer()` and
124 // `ProxyServerToProxyUri()`. If no type could be matched, returns
125 // SCHEME_INVALID.
126 NET_EXPORT ProxyServer::Scheme GetSchemeFromUriScheme(
127     std::string_view scheme,
128     bool is_quic_allowed = false);
129 
130 }  // namespace net
131 
132 #endif  // NET_BASE_PROXY_STRING_UTIL_H_
133