• 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 NET_EXPORT ProxyServer
51 PacResultElementToProxyServer(std::string_view pac_result_element);
52 NET_EXPORT std::string ProxyServerToPacResultElement(
53     const ProxyServer& proxy_server);
54 
55 // Converts a non-standard URI string to/from a ProxyServer.
56 //
57 // The non-standard URI strings have the format:
58 //   [<scheme>"://"]<server>[":"<port>]
59 //
60 // Where <scheme> may be one of:
61 // "http"
62 // "socks4"
63 // "socks5
64 // "socks" (equivalent to "socks5")
65 // "direct"
66 // "https"
67 // "quic"
68 //
69 // Both <scheme> and <port> are optional. If <scheme> is omitted, it will be
70 // assumed as |default_scheme|. If <port> is omitted, it will be assumed as
71 // the default port for the chosen scheme (via
72 // ProxyServer::GetDefaultPortForScheme()).
73 //
74 // If parsing fails the returned proxy will have scheme
75 // `ProxyServer::SCHEME_INVALID`.
76 //
77 // Examples (for `default_pac_scheme` = `kHttp` ):
78 //   "foopy"            {scheme=HTTP, host="foopy", port=80}
79 //   "socks://foopy"    {scheme=SOCKS5, host="foopy", port=1080}
80 //   "socks4://foopy"   {scheme=SOCKS4, host="foopy", port=1080}
81 //   "socks5://foopy"   {scheme=SOCKS5, host="foopy", port=1080}
82 //   "http://foopy:17"  {scheme=HTTP, host="foopy", port=17}
83 //   "https://foopy:17" {scheme=HTTPS, host="foopy", port=17}
84 //   "quic://foopy:17"  {scheme=QUIC, host="foopy", port=17}
85 //   "direct://"        {scheme=DIRECT}
86 //   "foopy:X"          INVALID -- bad port.
87 NET_EXPORT ProxyChain ProxyUriToProxyChain(std::string_view uri,
88                                            ProxyServer::Scheme default_scheme);
89 NET_EXPORT ProxyServer
90 ProxyUriToProxyServer(std::string_view uri, ProxyServer::Scheme default_scheme);
91 NET_EXPORT std::string ProxyServerToProxyUri(const ProxyServer& proxy_server);
92 
93 // Parses the proxy scheme from the non-standard URI scheme string
94 // representation used in `ProxyUriToProxyServer()` and
95 // `ProxyServerToProxyUri()`. If no type could be matched, returns
96 // SCHEME_INVALID.
97 NET_EXPORT ProxyServer::Scheme GetSchemeFromUriScheme(std::string_view scheme);
98 
99 }  // namespace net
100 
101 #endif  // NET_BASE_PROXY_STRING_UTIL_H_
102