QUIC - wait for disk cache to load server config if the server is among
[chromium-blink-merge.git] / net / proxy / proxy_config.h
blob303aa10b09aa7d1da82c4c77377be4d4c4ff082e
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_PROXY_PROXY_CONFIG_H_
6 #define NET_PROXY_PROXY_CONFIG_H_
8 #include <string>
10 #include "net/base/net_export.h"
11 #include "net/proxy/proxy_bypass_rules.h"
12 #include "net/proxy/proxy_config_source.h"
13 #include "net/proxy/proxy_list.h"
14 #include "net/proxy/proxy_server.h"
15 #include "url/gurl.h"
17 namespace base {
18 class Value;
21 namespace net {
23 class ProxyInfo;
25 // ProxyConfig describes a user's proxy settings.
27 // There are two categories of proxy settings:
28 // (1) Automatic (indicates the methods to obtain a PAC script)
29 // (2) Manual (simple set of proxy servers per scheme, and bypass patterns)
31 // When both automatic and manual settings are specified, the Automatic ones
32 // take precedence over the manual ones.
34 // For more details see:
35 // http://www.chromium.org/developers/design-documents/proxy-settings-fallback
36 class NET_EXPORT ProxyConfig {
37 public:
38 // ProxyRules describes the "manual" proxy settings.
39 // TODO(eroman): Turn this into a class.
40 // TODO(marq): Update the enum names; "TYPE_SINGLE_PROXY" really means
41 // the same set of proxies are used for all requests.
42 struct NET_EXPORT ProxyRules {
43 enum Type {
44 TYPE_NO_RULES,
45 TYPE_SINGLE_PROXY,
46 TYPE_PROXY_PER_SCHEME,
49 // Note that the default of TYPE_NO_RULES results in direct connections
50 // being made when using this ProxyConfig.
51 ProxyRules();
52 ~ProxyRules();
54 bool empty() const {
55 return type == TYPE_NO_RULES;
58 // Sets |result| with the proxies to use for |url| based on the current
59 // rules.
60 void Apply(const GURL& url, ProxyInfo* result) const;
62 // Parses the rules from a string, indicating which proxies to use.
64 // proxy-uri = [<proxy-scheme>"://"]<proxy-host>[":"<proxy-port>]
66 // proxy-uri-list = <proxy-uri>[","<proxy-uri-list>]
68 // url-scheme = "http" | "https" | "ftp" | "socks"
70 // scheme-proxies = [<url-scheme>"="]<proxy-uri-list>
72 // proxy-rules = scheme-proxies[";"<scheme-proxies>]
74 // Thus, the proxy-rules string should be a semicolon-separated list of
75 // ordered proxies that apply to a particular URL scheme. Unless specified,
76 // the proxy scheme for proxy-uris is assumed to be http.
78 // Some special cases:
79 // * If the scheme is omitted from the first proxy list, that list applies
80 // to all URL schemes and subsequent lists are ignored.
81 // * If a scheme is omitted from any proxy list after a list where a scheme
82 // has been provided, the list without a scheme is ignored.
83 // * If the url-scheme is set to 'socks', that sets a fallback list that
84 // to all otherwise unspecified url-schemes, however the default proxy-
85 // scheme for proxy urls in the 'socks' list is understood to be
86 // socks4:// if unspecified.
88 // For example:
89 // "http=foopy:80;ftp=foopy2" -- use HTTP proxy "foopy:80" for http://
90 // URLs, and HTTP proxy "foopy2:80" for
91 // ftp:// URLs.
92 // "foopy:80" -- use HTTP proxy "foopy:80" for all URLs.
93 // "foopy:80,bar,direct://" -- use HTTP proxy "foopy:80" for all URLs,
94 // failing over to "bar" if "foopy:80" is
95 // unavailable, and after that using no
96 // proxy.
97 // "socks4://foopy" -- use SOCKS v4 proxy "foopy:1080" for all
98 // URLs.
99 // "http=foop,socks5://bar.com -- use HTTP proxy "foopy" for http URLs,
100 // and fail over to the SOCKS5 proxy
101 // "bar.com" if "foop" is unavailable.
102 // "http=foopy,direct:// -- use HTTP proxy "foopy" for http URLs,
103 // and use no proxy if "foopy" is
104 // unavailable.
105 // "http=foopy;socks=foopy2 -- use HTTP proxy "foopy" for http URLs,
106 // and use socks4://foopy2 for all other
107 // URLs.
108 void ParseFromString(const std::string& proxy_rules);
110 // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp,
111 // &fallback_proxies}, or NULL if there is no proxy to use.
112 // Should only call this if the type is TYPE_PROXY_PER_SCHEME.
113 const ProxyList* MapUrlSchemeToProxyList(
114 const std::string& url_scheme) const;
116 // Returns true if |*this| describes the same configuration as |other|.
117 bool Equals(const ProxyRules& other) const;
119 // Exceptions for when not to use a proxy.
120 ProxyBypassRules bypass_rules;
122 // Reverse the meaning of |bypass_rules|.
123 bool reverse_bypass;
125 Type type;
127 // Set if |type| is TYPE_SINGLE_PROXY.
128 ProxyList single_proxies;
130 // Set if |type| is TYPE_PROXY_PER_SCHEME.
131 ProxyList proxies_for_http;
132 ProxyList proxies_for_https;
133 ProxyList proxies_for_ftp;
135 // Used when a fallback has been defined and the url to be proxied doesn't
136 // match any of the standard schemes.
137 ProxyList fallback_proxies;
139 private:
140 // Returns one of {&proxies_for_http, &proxies_for_https, &proxies_for_ftp}
141 // or NULL if it is a scheme that we don't have a mapping for. Should only
142 // call this if the type is TYPE_PROXY_PER_SCHEME. Intentionally returns
143 // NULL for "ws" and "wss" as those are handled specially by
144 // GetProxyListForWebSocketScheme().
145 ProxyList* MapUrlSchemeToProxyListNoFallback(const std::string& scheme);
147 // Returns the first of {&fallback_proxies, &proxies_for_https,
148 // &proxies_for_http} that is non-empty, or NULL.
149 const ProxyList* GetProxyListForWebSocketScheme() const;
152 typedef int ID;
154 // Indicates an invalid proxy config.
155 static const ID kInvalidConfigID = 0;
157 ProxyConfig();
158 ProxyConfig(const ProxyConfig& config);
159 ~ProxyConfig();
160 ProxyConfig& operator=(const ProxyConfig& config);
162 // Used to numerically identify this configuration.
163 ID id() const { return id_; }
164 void set_id(ID id) { id_ = id; }
165 bool is_valid() const { return id_ != kInvalidConfigID; }
167 // Returns true if the given config is equivalent to this config. The
168 // comparison ignores differences in |id()| and |source()|.
169 bool Equals(const ProxyConfig& other) const;
171 // Returns true if this config contains any "automatic" settings. See the
172 // class description for what that means.
173 bool HasAutomaticSettings() const;
175 void ClearAutomaticSettings();
177 // Creates a Value dump of this configuration.
178 scoped_ptr<base::DictionaryValue> ToValue() const;
180 ProxyRules& proxy_rules() {
181 return proxy_rules_;
184 const ProxyRules& proxy_rules() const {
185 return proxy_rules_;
188 void set_pac_url(const GURL& url) {
189 pac_url_ = url;
192 const GURL& pac_url() const {
193 return pac_url_;
196 void set_pac_mandatory(bool enable_pac_mandatory) {
197 pac_mandatory_ = enable_pac_mandatory;
200 bool pac_mandatory() const {
201 return pac_mandatory_;
204 bool has_pac_url() const {
205 return pac_url_.is_valid();
208 void set_auto_detect(bool enable_auto_detect) {
209 auto_detect_ = enable_auto_detect;
212 bool auto_detect() const {
213 return auto_detect_;
216 void set_source(ProxyConfigSource source) {
217 source_ = source;
220 ProxyConfigSource source() const {
221 return source_;
224 // Helpers to construct some common proxy configurations.
226 static ProxyConfig CreateDirect() {
227 return ProxyConfig();
230 static ProxyConfig CreateAutoDetect() {
231 ProxyConfig config;
232 config.set_auto_detect(true);
233 return config;
236 static ProxyConfig CreateFromCustomPacURL(const GURL& pac_url) {
237 ProxyConfig config;
238 config.set_pac_url(pac_url);
239 // By default fall back to direct connection in case PAC script fails.
240 config.set_pac_mandatory(false);
241 return config;
244 private:
245 // True if the proxy configuration should be auto-detected.
246 bool auto_detect_;
248 // If non-empty, indicates the URL of the proxy auto-config file to use.
249 GURL pac_url_;
251 // If true, blocks all traffic in case fetching the pac script from |pac_url_|
252 // fails. Only valid if |pac_url_| is non-empty.
253 bool pac_mandatory_;
255 // Manual proxy settings.
256 ProxyRules proxy_rules_;
258 // Source of proxy settings.
259 ProxyConfigSource source_;
261 ID id_;
264 } // namespace net
268 #endif // NET_PROXY_PROXY_CONFIG_H_