• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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_PROXY_RESOLUTION_PROXY_CONFIG_H_
6 #define NET_PROXY_RESOLUTION_PROXY_CONFIG_H_
7 
8 #include <string>
9 
10 #include "net/base/net_export.h"
11 #include "net/base/proxy_server.h"
12 #include "net/proxy_resolution/proxy_bypass_rules.h"
13 #include "net/proxy_resolution/proxy_list.h"
14 #include "url/gurl.h"
15 
16 namespace base {
17 class Value;
18 }
19 
20 namespace net {
21 
22 class ProxyInfo;
23 
24 // ProxyConfig describes a user's proxy settings.
25 //
26 // There are two categories of proxy settings:
27 //   (1) Automatic (indicates the methods to obtain a PAC script)
28 //   (2) Manual (simple set of proxy servers per scheme, and bypass patterns)
29 //
30 // When both automatic and manual settings are specified, the Automatic ones
31 // take precedence over the manual ones.
32 //
33 // For more details see:
34 // http://www.chromium.org/developers/design-documents/network-stack/proxy-settings-fallback
35 class NET_EXPORT ProxyConfig {
36  public:
37   // ProxyRules describes the "manual" proxy settings.
38   struct NET_EXPORT ProxyRules {
39     enum class Type {
40       EMPTY,
41       PROXY_LIST,
42       PROXY_LIST_PER_SCHEME,
43     };
44 
45     // Note that the default of Type::EMPTY results in direct connections
46     // being made when using this ProxyConfig.
47     ProxyRules();
48     ProxyRules(const ProxyRules& other);
49     ~ProxyRules();
50 
emptyProxyRules51     bool empty() const {
52       return type == Type::EMPTY;
53     }
54 
55     // Sets |result| with the proxies to use for |url| based on the current
56     // rules.
57     void Apply(const GURL& url, ProxyInfo* result) const;
58 
59     // Parses the rules from a string, indicating which proxies to use.
60     //
61     //   proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>]
62     //
63     //   proxy-uri-list = <proxy-uri>[","<proxy-uri-list>]
64     //
65     //   url-scheme = "http" | "https" | "ftp" | "socks"
66     //
67     //   scheme-proxies = [<url-scheme>"="]<proxy-uri-list>
68     //
69     //   proxy-rules = scheme-proxies[";"<scheme-proxies>]
70     //
71     // Thus, the proxy-rules string should be a semicolon-separated list of
72     // ordered proxies that apply to a particular URL scheme. Unless specified,
73     // the proxy scheme for proxy-uris is assumed to be http.
74     //
75     // Some special cases:
76     //  * If the scheme is omitted from the first proxy list, that list applies
77     //    to all URL schemes and subsequent lists are ignored.
78     //  * If a scheme is omitted from any proxy list after a list where a scheme
79     //    has been provided, the list without a scheme is ignored.
80     //  * If the url-scheme is set to 'socks', that sets a fallback list that
81     //    to all otherwise unspecified url-schemes, however the default proxy-
82     //    scheme for proxy urls in the 'socks' list is understood to be
83     //    socks4:// if unspecified.
84     //
85     // For example:
86     //   "http=foopy:80;ftp=foopy2"  -- use HTTP proxy "foopy:80" for http://
87     //                                  URLs, and HTTP proxy "foopy2:80" for
88     //                                  ftp:// URLs.
89     //   "foopy:80"                  -- use HTTP proxy "foopy:80" for all URLs.
90     //   "foopy:80,bar,direct://"    -- use HTTP proxy "foopy:80" for all URLs,
91     //                                  failing over to "bar" if "foopy:80" is
92     //                                  unavailable, and after that using no
93     //                                  proxy.
94     //   "socks4://foopy"            -- use SOCKS v4 proxy "foopy:1080" for all
95     //                                  URLs.
96     //   "http=foop,socks5://bar.com -- use HTTP proxy "foopy" for http URLs,
97     //                                  and fail over to the SOCKS5 proxy
98     //                                  "bar.com" if "foop" is unavailable.
99     //   "http=foopy,direct://       -- use HTTP proxy "foopy" for http URLs,
100     //                                  and use no proxy if "foopy" is
101     //                                  unavailable.
102     //   "http=foopy;socks=foopy2   --  use HTTP proxy "foopy" for http URLs,
103     //                                  and use socks4://foopy2 for all other
104     //                                  URLs.
105     void ParseFromString(const std::string& proxy_rules);
106 
107     // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp,
108     // &fallback_proxies}, or NULL if there is no proxy to use.
109     // Should only call this if the type is Type::PROXY_LIST_PER_SCHEME.
110     const ProxyList* MapUrlSchemeToProxyList(
111         const std::string& url_scheme) const;
112 
113     // Returns true if |*this| describes the same configuration as |other|.
114     bool Equals(const ProxyRules& other) const;
115 
CreateForTestingProxyRules116     static ProxyRules CreateForTesting(const ProxyList& proxy_list) {
117       ProxyRules proxy_rules;
118       proxy_rules.type = Type::PROXY_LIST;
119       proxy_rules.single_proxies = proxy_list;
120       return proxy_rules;
121     }
122 
123     // Exceptions for when not to use a proxy.
124     ProxyBypassRules bypass_rules;
125 
126     // Reverse the meaning of |bypass_rules|.
127     bool reverse_bypass = false;
128 
129     // Only proxy resources in NetworkServiceProxyAllowList if they are used in
130     // a 3P context. 1P resources will bypass the proxy.
131     bool restrict_to_network_service_proxy_allow_list = false;
132 
133     Type type = Type::EMPTY;
134 
135     // Set if |type| is Type::PROXY_LIST.
136     ProxyList single_proxies;
137 
138     // Set if |type| is Type::PROXY_LIST_PER_SCHEME.
139     ProxyList proxies_for_http;
140     ProxyList proxies_for_https;
141     ProxyList proxies_for_ftp;
142 
143     // Used when a fallback has been defined and the url to be proxied doesn't
144     // match any of the standard schemes.
145     ProxyList fallback_proxies;
146 
147    private:
148     // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp}
149     // or NULL if it is a scheme that we don't have a mapping for. Should only
150     // call this if the type is Type::PROXY_LIST_PER_SCHEME. Intentionally returns
151     // NULL for "ws" and "wss" as those are handled specially by
152     // GetProxyListForWebSocketScheme().
153     ProxyList* MapUrlSchemeToProxyListNoFallback(const std::string& scheme);
154 
155     // Returns the first of {&fallback_proxies, &proxies_for_https,
156     // &proxies_for_http} that is non-empty, or NULL.
157     const ProxyList* GetProxyListForWebSocketScheme() const;
158   };
159 
160   ProxyConfig();
161   ProxyConfig(const ProxyConfig& config);
162   ~ProxyConfig();
163   ProxyConfig& operator=(const ProxyConfig& config);
164 
165   // Returns true if the given config is equivalent to this config.
166   bool Equals(const ProxyConfig& other) const;
167 
168   // Returns true if this config contains any "automatic" settings. See the
169   // class description for what that means.
170   bool HasAutomaticSettings() const;
171 
172   void ClearAutomaticSettings();
173 
174   // Creates a Value dump of this configuration.
175   base::Value ToValue() const;
176 
proxy_rules()177   ProxyRules& proxy_rules() {
178     return proxy_rules_;
179   }
180 
proxy_rules()181   const ProxyRules& proxy_rules() const {
182     return proxy_rules_;
183   }
184 
set_pac_url(const GURL & url)185   void set_pac_url(const GURL& url) {
186     pac_url_ = url;
187   }
188 
pac_url()189   const GURL& pac_url() const {
190     return pac_url_;
191   }
192 
set_pac_mandatory(bool enable_pac_mandatory)193   void set_pac_mandatory(bool enable_pac_mandatory) {
194     pac_mandatory_ = enable_pac_mandatory;
195   }
196 
pac_mandatory()197   bool pac_mandatory() const {
198     return pac_mandatory_;
199   }
200 
has_pac_url()201   bool has_pac_url() const {
202     return pac_url_.is_valid();
203   }
204 
set_auto_detect(bool enable_auto_detect)205   void set_auto_detect(bool enable_auto_detect) {
206     auto_detect_ = enable_auto_detect;
207   }
208 
auto_detect()209   bool auto_detect() const {
210     return auto_detect_;
211   }
212 
set_from_system(bool from_system)213   void set_from_system(bool from_system) { from_system_ = from_system; }
214 
from_system()215   bool from_system() const { return from_system_; }
216 
217   // Helpers to construct some common proxy configurations.
218 
CreateDirect()219   static ProxyConfig CreateDirect() {
220     return ProxyConfig();
221   }
222 
CreateAutoDetect()223   static ProxyConfig CreateAutoDetect() {
224     ProxyConfig config;
225     config.set_auto_detect(true);
226     return config;
227   }
228 
CreateFromCustomPacURL(const GURL & pac_url)229   static ProxyConfig CreateFromCustomPacURL(const GURL& pac_url) {
230     ProxyConfig config;
231     config.set_pac_url(pac_url);
232     // By default fall back to direct connection in case PAC script fails.
233     config.set_pac_mandatory(false);
234     return config;
235   }
236 
CreateForTesting(const ProxyList & proxy_list)237   static ProxyConfig CreateForTesting(const ProxyList& proxy_list) {
238     ProxyConfig config;
239     config.proxy_rules_ = ProxyRules::CreateForTesting(proxy_list);
240     return config;
241   }
242 
243  private:
244   // True if the proxy configuration should be auto-detected.
245   bool auto_detect_ = false;
246 
247   // True if the proxy configuration was created from system settings.
248   bool from_system_ = false;
249 
250   // If non-empty, indicates the URL of the proxy auto-config file to use.
251   GURL pac_url_;
252 
253   // If true, blocks all traffic in case fetching the PAC script from |pac_url_|
254   // fails. Only valid if |pac_url_| is non-empty.
255   bool pac_mandatory_ = false;
256 
257   // Manual proxy settings.
258   ProxyRules proxy_rules_;
259 };
260 
261 }  // namespace net
262 
263 
264 
265 #endif  // NET_PROXY_RESOLUTION_PROXY_CONFIG_H_
266