• 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     // A `Type` other than `EMPTY` does not guarantee the presence of a valid
40     // proxy chain.
41     enum class Type {
42       EMPTY,
43       PROXY_LIST,
44       PROXY_LIST_PER_SCHEME,
45     };
46 
47     // Note that the default of Type::EMPTY results in direct connections
48     // being made when using this ProxyConfig.
49     ProxyRules();
50     ProxyRules(const ProxyRules& other);
51     ~ProxyRules();
52 
emptyProxyRules53     bool empty() const { return type == Type::EMPTY; }
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     //  * In debug mode, allow_bracketed_proxy_chains can be set to true. In
85     //    this case, brackets can be used to format multi-proxy chains.
86     //
87     // For example:
88     //   "http=foopy:80;ftp=foopy2"  -- use HTTP proxy "foopy:80" for http://
89     //                                  URLs, and HTTP proxy "foopy2:80" for
90     //                                  ftp:// URLs.
91     //   "foopy:80"                  -- use HTTP proxy "foopy:80" for all URLs.
92     //   "foopy:80,bar,direct://"    -- use HTTP proxy "foopy:80" for all URLs,
93     //                                  failing over to "bar" if "foopy:80" is
94     //                                  unavailable, and after that using no
95     //                                  proxy.
96     //   "socks4://foopy"            -- use SOCKS v4 proxy "foopy:1080" for all
97     //                                  URLs.
98     //   "http=foop,socks5://bar.com -- use HTTP proxy "foopy" for http URLs,
99     //                                  and fail over to the SOCKS5 proxy
100     //                                  "bar.com" if "foop" is unavailable.
101     //   "http=foopy,direct://       -- use HTTP proxy "foopy" for http URLs,
102     //                                  and use no proxy if "foopy" is
103     //                                  unavailable.
104     //   "http=foopy;socks=foopy2   --  use HTTP proxy "foopy" for http URLs,
105     //                                  and use socks4://foopy2 for all other
106     //                                  URLs.
107     void ParseFromString(const std::string& proxy_rules,
108                          bool allow_bracketed_proxy_chains = false,
109                          bool is_quic_allowed = false);
110 
111     // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp,
112     // &fallback_proxies}, or NULL if there is no proxy to use.
113     // Should only call this if the type is Type::PROXY_LIST_PER_SCHEME.
114     const ProxyList* MapUrlSchemeToProxyList(
115         const std::string& url_scheme) const;
116 
117     // Returns true if |*this| describes the same configuration as |other|.
118     bool Equals(const ProxyRules& other) const;
119 
CreateForTestingProxyRules120     static ProxyRules CreateForTesting(const ProxyList& proxy_list) {
121       ProxyRules proxy_rules;
122       proxy_rules.type = Type::PROXY_LIST;
123       proxy_rules.single_proxies = proxy_list;
124       return proxy_rules;
125     }
126 
127     // Exceptions for when not to use a proxy.
128     ProxyBypassRules bypass_rules;
129 
130     // Reverse the meaning of |bypass_rules|.
131     bool reverse_bypass = 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
151     // returns 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(ProxyConfig&& config);
163   ProxyConfig& operator=(const ProxyConfig& config);
164   ProxyConfig& operator=(ProxyConfig&& config);
165   ~ProxyConfig();
166 
167   // Returns true if the given config is equivalent to this config.
168   bool Equals(const ProxyConfig& other) const;
169 
170   // Returns true if this config contains any "automatic" settings. See the
171   // class description for what that means.
172   bool HasAutomaticSettings() const;
173 
174   void ClearAutomaticSettings();
175 
176   // Creates a Value dump of this configuration.
177   base::Value ToValue() const;
178 
proxy_rules()179   ProxyRules& proxy_rules() { return proxy_rules_; }
180 
proxy_rules()181   const ProxyRules& proxy_rules() const { return proxy_rules_; }
182 
set_pac_url(const GURL & url)183   void set_pac_url(const GURL& url) { pac_url_ = url; }
184 
pac_url()185   const GURL& pac_url() const { return pac_url_; }
186 
set_pac_mandatory(bool enable_pac_mandatory)187   void set_pac_mandatory(bool enable_pac_mandatory) {
188     pac_mandatory_ = enable_pac_mandatory;
189   }
190 
pac_mandatory()191   bool pac_mandatory() const { return pac_mandatory_; }
192 
has_pac_url()193   bool has_pac_url() const { return pac_url_.is_valid(); }
194 
set_auto_detect(bool enable_auto_detect)195   void set_auto_detect(bool enable_auto_detect) {
196     auto_detect_ = enable_auto_detect;
197   }
198 
auto_detect()199   bool auto_detect() const { return auto_detect_; }
200 
set_from_system(bool from_system)201   void set_from_system(bool from_system) { from_system_ = from_system; }
202 
from_system()203   bool from_system() const { return from_system_; }
204 
205   // Helpers to construct some common proxy configurations.
206 
CreateDirect()207   static ProxyConfig CreateDirect() { return ProxyConfig(); }
208 
CreateAutoDetect()209   static ProxyConfig CreateAutoDetect() {
210     ProxyConfig config;
211     config.set_auto_detect(true);
212     return config;
213   }
214 
CreateFromCustomPacURL(const GURL & pac_url)215   static ProxyConfig CreateFromCustomPacURL(const GURL& pac_url) {
216     ProxyConfig config;
217     config.set_pac_url(pac_url);
218     // By default fall back to direct connection in case PAC script fails.
219     config.set_pac_mandatory(false);
220     return config;
221   }
222 
CreateForTesting(const ProxyList & proxy_list)223   static ProxyConfig CreateForTesting(const ProxyList& proxy_list) {
224     ProxyConfig config;
225     config.proxy_rules_ = ProxyRules::CreateForTesting(proxy_list);
226     return config;
227   }
228 
229  private:
230   // True if the proxy configuration should be auto-detected.
231   bool auto_detect_ = false;
232 
233   // True if the proxy configuration was created from system settings.
234   bool from_system_ = false;
235 
236   // If non-empty, indicates the URL of the proxy auto-config file to use.
237   GURL pac_url_;
238 
239   // If true, blocks all traffic in case fetching the PAC script from |pac_url_|
240   // fails. Only valid if |pac_url_| is non-empty.
241   bool pac_mandatory_ = false;
242 
243   // Manual proxy settings.
244   ProxyRules proxy_rules_;
245 };
246 
247 }  // namespace net
248 
249 #endif  // NET_PROXY_RESOLUTION_PROXY_CONFIG_H_
250